您的位置:首页 > Web前端 > Webpack

[Webpack 2] Import a non-ES6 module with Webpack

2016-06-25 01:22 706 查看
When you have a dependency that does not export itself properly, you can use the
exports-loader
to force it to export the pieces of the file that you need.

Install:

npm i -D exports-loader


Add exports-loader to the module you want:

module: {
loaders: [
...
{
test: require.resolve('./src/js/non_node_modules/left-pad'),
loaders: [
'exports?leftPad',
],
}
],


There is no problem, the module still exists on 'window' object, we want it be es6 module which not exists on 'window' object.

Install:

npm i -D imports-loader


Add imports-loader the the module:

module: {
loaders: [
...
{
test: require.resolve('./src/js/non_node_modules/left-pad'),
loaders: [
'imports?window=>{}',
'exports?leftPad',
],
}
],
},


Here it sets 'windows' object to empty object to clean the left-pad module.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: