Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make webpack skip a require

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.

like image 253
Thomas Andersen Avatar asked Jan 16 '16 15:01

Thomas Andersen


People also ask

Can I use require with Webpack?

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.

Does Webpack require node?

js. Webpack runs on Node. js, a JavaScript runtime that can be used in computers and servers outside a browser environment.

Can Webpack work without Babel?

No unless all dependencies of your project supports ES6.


6 Answers

If you store the path in a variable then IgnorePlugin will not work. Though you still could do:

const myCustomModule = eval('require')(myCustomPath)
like image 115
James Akwuh Avatar answered Oct 22 '22 10:10

James Akwuh


You can use Ignore Plugin (webpack 1) / Ignore plugin (webpack 2).

Add plugin in webpack.config.js:

plugins: [
  new webpack.IgnorePlugin(/shelljs\/global/),
],
like image 45
Dmitry Yaremenko Avatar answered Oct 22 '22 10:10

Dmitry Yaremenko


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

like image 26
MrBar Avatar answered Oct 22 '22 10:10

MrBar


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.

like image 30
redfox05 Avatar answered Oct 22 '22 10:10

redfox05


If require is in the global namespace and this is why you want Webpack to ignore it, just do window.require()

like image 29
DanV Avatar answered Oct 22 '22 09:10

DanV


Here a trick

const require = module[`require`].bind(module);

Note the use of a template string

like image 39
Skywalker13 Avatar answered Oct 22 '22 11:10

Skywalker13