Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'move' babel-runtime into a webpack vendors DLL?

I'm creating two webpack bundles: vendors.dll.js and client.js.

Vendors is created with webpack.DllPlugin. It includes all front-end modules in node_modules/. This is working great.

Client includes my app code. It uses the webpack.DllReferencePlugin to delegate to the DLL for vendors. This is working great.

Client runs all javascript through babel-loader. I'm using the babel-plugin-transform-runtime plugin, which causes a whole mess of core-js stuff to be compiled into my client bundle. I'd prefer to move that stuff into the DLL, since it won't change as frequently as the app.

AFAIK you cannot just include babel-runtime in the DLL (I tried this). From what I can tell the core-js stuff is loaded more directly, and babel-runtime doesn't even have a main from what I can tell.

Babel: 6.x Webpack: 1.x

Happy to provide actual config if its needed to solve the problem.

like image 932
Brian Link Avatar asked May 06 '16 20:05

Brian Link


People also ask

How does Babel work with Webpack?

If Babel is a translator for JS, you can think of Webpack as a mega-multi-translator that works with all kinds of languages (or assets). For example, Webpack often runs Babel as one of its jobs. Another example, Webpack can collect all your inline CSS styles in your Javascript files and bundle them into one.

Is Babel runtime a dev dependency?

The short answer is yes: @babel/runtime (and @babel/plugin-transform-runtime ) is intended to be treated as a dependency.

Does Webpack come with Babel?

webpack-dev-server is a simple server that we can use during development. This installs the core Babel package and two of Babel's presets. Babel works by using 'plugins' to it's core package to transform and compile the code you give it.


1 Answers

You could scan the required directories and include all files like this.

const readDir = dir => {
  const result = [];
  fs.readdirSync(dir).map(file => {
    if (file.match(/\.js$/)) result.push(`${dir.replace('./node_modules/', '')}/${file}`);
    else if (fs.lstatSync(dir + '/' + file).isDirectory()) result.push(...readDir(dir + '/' + file));
  });
  return result
}
const babelRuntimeHelpers = readDir('./node_modules/babel-runtime/helpers');
const babelRuntimeCoreJs = readDir('./node_modules/babel-runtime/core-js');

And then add those to your vendor array:

vendor: [
    ...babelRuntimeHelpers,
    ...babelRuntimeCoreJs,
    ...rest
]
like image 166
Gabriel Bull Avatar answered Sep 27 '22 19:09

Gabriel Bull