How can I make webpack skip occurences of
require('shelljs/global');
in my source files? I want to make a bundle of my source files but keep the require('shelljs/global')
in the files and not bundle shelljs/global
.
Each time you use the AMD-style require([]) or require. ensure() webpack will see what you passed in there and go and create a bundle with every possible match. That works great if you pass in a single file, but when you use a variable, it might end up bundling your entire view folder.
js. Webpack runs on Node. js, a JavaScript runtime that can be used in computers and servers outside a browser environment.
No unless all dependencies of your project supports ES6.
If you store the path in a variable then IgnorePlugin will not work. Though you still could do:
const myCustomModule = eval('require')(myCustomPath)
You can use Ignore Plugin (webpack 1) / Ignore plugin (webpack 2).
Add plugin in webpack.config.js
:
plugins: [
new webpack.IgnorePlugin(/shelljs\/global/),
],
for new comers, on webpack 2+ the way to do this is like so:
module.exports = {
entry: __dirname + '/src/app',
output: {
path: __dirname + '/dist',
libraryTarget: 'umd'
},
externals: {
'shelljs/globals': 'commonjs shelljs/global'
}
};
the bundle will contain a verbatim require:
require('shelljs/global');
read on more supported formats on webpack's config guide and some good examples here
This should be a last resort option, but if you are certain that your JS file is always parsed by Webpack, and nothing else:
You could replace your require()
calls with __non_webpack_require__()
Webpack will parse and convert any occurrence of this and output a normal require()
call. This will only work if you are executing in NodeJS OR in a browser if you have a global require library available to trigger.
If webpack does not parse the file, and the script is run by something else, then your code will try to call the non-converted __non_webpack_require__
which will fail. You could potentially write some code that checks earlier in your script if __non_webpack_require__
exists as a function and if not, have it pass the call on to require.
However, this should be temporary, and only to just avoid the build errors, until you can replace it with something like Webpack's own dynamic imports.
If require
is in the global namespace and this is why you want Webpack to ignore it, just do window.require()
Here a trick
const require = module[`require`].bind(module);
Note the use of a template string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With