I have a web application that I compile using webpack. One of the modules that my code uses is called table.js . Until recently, it was another module and was compiled into my bundle.js file with everything else.
Now I need to run table.js in Web Worker, so I need to pull it and its dependencies into a separate file, which can be downloaded both offline and my other modules.
At first I thought to include table.js in my webpack.config.js entry .
var config = { ... entry: { app: [ './src/main.js', './src/classes/table.js' ], vendors: [], }, ... }
This did not work. Then I decided to split it up, as with my suppliers.
var config = { addExternal: function (name, globalVar) { this.externals[name] = globalVar; this.entry.vendors.push(name); }, addVendor: function (name, path) { this.resolve.alias[name] = path; this.entry.vendors.push(name); }, addPlugin: function (plugin) { this.plugins.push(plugin); }, entry: { app: [ './src/main.js' ], vendors: [], table: [ __dirname + '/src/classes/table.js' ] }, plugins: [], externals: { }, output: { path: __dirname + '/public/dist/', filename: 'bundle.js', publicPath: '/dist/', sourceMapFile: '[file].map' }, resolve: { alias: { 'table': './src/classes/table.js' }, extensions: [ '', '.js', '.jsx' ] }, ... } ... config.addPlugin(new CommonsChunkPlugin('vendors', 'vendors.js')); config.addPlugin(new CommonsChunkPlugin('table', 'table.js'));
This seems to pull the table and its dependencies into a piece of bundle.js , 1.bundle.js . Unfortunately, calling import Table from 'table' raises this error:
ERROR in CommonsChunkPlugin: While running in normal mode it not allowed to use a non-entry chunk (table)
I also have a circular relationship between TableStore and Table . TableStore should remain in bundle.js because it should not be loaded into Web Worker. Previously, when I needed to throw things in a separate piece, I did:
if (someThingNeedsRequiring) { require.ensure([], () => { require('something'); } }
With cyclic dependency, this does not seem to work.
let _inWebWorker = self instanceof Window, TableStore = null; if (!_inWebWorker) { require.ensure([], function() { TableStore = require('../stores/table-store'); } ); } import Table from 'table';
Can someone properly install me with my webpack.config.js and how to use my import in my module files?