一些遗留模块依赖的 this 指向的是 window 对象。例如
// index.js
this.alert('hello webpack')
当模块运行在 CommonJS 上下文中,这将会变成一个问题,也就是说此时的 this 指向的是 module.exports。在这种情况下,我们可以通过使用 import-loader 覆盖 this 指向
module.exports = {
module: {
rules: [
{
test: require.resolve('./src/index.js'),
use: 'imports-loader?wrapper=window'
}
]
}
}