Tuesday, 6 June 2017

Separating my library to addons

I'm pretty new in this world so bear with me.

I have my big library that i'm trying to convert it to es6 modules, babel & webpack to bundle it later on to several files.

My library folders look like this

--src
  -- core
     -- folder1
     -- folder2
  -- addons
     -- addon 1
        -- addon 1 files and folders
     -- addon 2
        -- addon 2 files and folders
  -- libs
     -- internal
        -- internal lib1 
        -- internal lib2 
     -- external
        -- external lib1 
        -- external lib2 

My library code is separate to 2 - the core code and the addons and i'm using some libraries (libs folder)

My wish is that:

  • All internal libs will be bundeled into one libs file
  • External libs are excluded from bundle
  • All core files will be bundleds into one core file
  • All my addons will be bundleds into each addon file
  • Note: addons are using core files and are not to bundle in them

final result: libs.js core.js addon1.js addon2.js

My clients will have to use the first 2 js files. and Addon will be included as they wish.

So far I've managed to separate my internal lib using CommonsChunkPlugin

new webpack.optimize.CommonsChunkPlugin({
    name: "libs",
    minChunks: function(module){
      return module.context && module.context.indexOf("\\libs\\internal\\") !== -1;
    },
  });

External libs are ignored using null-loader

{
   test: /libs\\external/,
   loader: 'null-loader'
},

my entry point js looks like this

import a from

"./core/a";
import b from "./core/b";
import addon1C from "./addons/addon1/c";

const myLib = {
   core: {a,b},
   addons: {addon1C}
}

export {myLib}

and now the problem starts with the core and addons.

If i'm skipping the addons part, in my client side including 2 scripts (libs, core)

and simply myLib.core.a is working.

The question do i prevent the bundling of the core code in the addons.

I've tried to do it with another chunk plugin but it causes some errors in the modules because the addon is using the core code and the entry point is using the addons to export it, so it doesn't find the module.

Tried 2 entry points so i will have the first entry point without the addons and export the addons separately, but now the core code is bundled in it.

So, How do I do that ? Or if there is a better way to do any of what I've done so far. i'm happy to learn.



via Raziza O

No comments:

Post a Comment