babel-plugin-remove-webpack
Removes webpack-specific functions from JavaScript code.
require.ensure
is replaced with self-executing anonymous functions.
require.ensure(['a', 'b', 'c'], function (require) {
const a = require('a');
const b = require('b');
const c = require('c');
});
(function () {
const a = require('a');
const b = require('b');
const c = require('c');
})();
require.include
is removed entirely.
require.include('a');
Motivation
require.ensure
and require.include
are great for code splitting;
however, they can cause issues when writing universal JavaScript. The typical
solution is to use synchronous shims. In order for webpack code splitting
to work properly these shims have to be defined in each file where they are
used.
This plugin makes it possible to universally run code which uses
webpack-specific functions without having to manually polyfill those
functions.
Usage Notes
This plugin should not be used as a part of a build with webpack, otherwise
code splitting will stop working. The intended usage is with the
babel-register
package or some other build with babel that specifically
targets node. Usage as such will remove require.ensure
and
require.include
calls as shown above so you can run your client code on the
server without shims.
License
MIT