如何使webpack跳过一个要求

时间:2022-04-13 20:26:33

How can I make webpack skip occurences of

如何使webpack跳过发生

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.

在我的源文件吗?我想要将我的源文件打包,但要在文件中保留require('shelljs/global'),而不是捆绑shelljs/global。

Thank you in advance

提前谢谢你

Thomas

托马斯。

4 个解决方案

#1


15  

You can use Ignore Plugin.

你可以使用Ignore Plugin。

Add plugin in webpack.config.js:

在webpack.config.js添加插件:

plugins: [
  new webpack.IgnorePlugin(/shelljs\/global/),
],

#2


13  

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

如果将路径存储在变量中,则IgnorePlugin无效。尽管你仍然可以:

const myCustomModule = eval('require')(myCustomPath)

#3


7  

for new comers, on webpack 2 the way to do this is like so:

对于新来者,webpack 2的方法是这样的:

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

请阅读webpack配置指南上更多支持的格式和一些很好的例子

#4


2  

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

如果require在全局命名空间中,这就是为什么要Webpack忽略它的原因,只需执行windows .require()

#1


15  

You can use Ignore Plugin.

你可以使用Ignore Plugin。

Add plugin in webpack.config.js:

在webpack.config.js添加插件:

plugins: [
  new webpack.IgnorePlugin(/shelljs\/global/),
],

#2


13  

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

如果将路径存储在变量中,则IgnorePlugin无效。尽管你仍然可以:

const myCustomModule = eval('require')(myCustomPath)

#3


7  

for new comers, on webpack 2 the way to do this is like so:

对于新来者,webpack 2的方法是这样的:

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

请阅读webpack配置指南上更多支持的格式和一些很好的例子

#4


2  

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

如果require在全局命名空间中,这就是为什么要Webpack忽略它的原因,只需执行windows .require()