我在从 node_modules
导入模块时遇到问题。由于某种原因,Babel 不会转译从 node_modules
导入的模块,而是转译从 src
导入的模块。
这是一个示例回购协议: https ://github.com/NikitaKA/babeltest
主程序
// result code contains const and let, but it shouldn't. :(
索引.js
import qs from 'query-string; // not transpiled
import lib from './lib' // transpiled
const query = qs.parse(window.location.search);
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
},
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader"
}
}
]
}
};
.babelrc
{
"presets": [
["@babel/preset-env", {
"modules": false,
"targets": {
"chrome": 39
}
}],
["@babel/preset-stage-1", {
"modules": false,
"decoratorsLegacy": true,
"pipelineProposal": "minimal"
}]
],
"plugins": [
"transform-es2015-constants",
"@babel/plugin-transform-block-scoping",
"@babel/plugin-transform-runtime"
]
}
原文由 NikitaK 发布,翻译遵循 CC BY-SA 4.0 许可协议
这种情况下的解决方案是再次转译模块,这可以通过修改 webpack 配置中的
exclude
属性来完成:模块
es6-module
和another-es6-module
将不再被 webpack 忽略,并将与源代码的其余部分一起转译。请参阅 有关 webpack 项目的 GitHub 问题。
使用
webpack@3.12.0
、babel-core@6.26.3
和babel-core@6.26.3
测试