Javascript Babel不会从';节点模块';
从Javascript Babel不会从';节点模块';,javascript,webpack,babeljs,Javascript,Webpack,Babeljs,从node\u modules传输导入的模块时遇到问题。出于某种原因,Babel不会传输从节点导入的模块,而是传输从src导入的模块 以下是回购的一个例子: main.js // result code contains const and let, but it shouldn't. :( import qs from 'query-string; // not transpiled import lib from './lib' // transpiled const query = q
node\u modules
传输导入的模块时遇到问题。出于某种原因,Babel不会传输从节点导入的模块,而是传输从src
导入的模块
以下是回购的一个例子:
main.js
// result code contains const and let, but it shouldn't. :(
import qs from 'query-string; // not transpiled
import lib from './lib' // transpiled
const query = qs.parse(window.location.search);
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"
}
}
]
}
};
index.js
// result code contains const and let, but it shouldn't. :(
import qs from 'query-string; // not transpiled
import lib from './lib' // transpiled
const query = qs.parse(window.location.search);
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"
}
}
]
}
};
webpack.config.js
// result code contains const and let, but it shouldn't. :(
import qs from 'query-string; // not transpiled
import lib from './lib' // transpiled
const query = qs.parse(window.location.search);
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"
]
}
在我的评论的基础上进一步阐述:
您确实不想传输所有的节点模块
——这需要很长时间,而且那里的大部分代码都应该已经是ES5(除非它们实际上是ES6模块,在这种情况下,ES6入口点在包.json
清单中宣布为“module”
)
查询-string@6.x
不是,它在:
此模块面向Node.js 6或更高版本以及最新版本的Chrome、Firefox和Safari。如果您希望支持较旧的浏览器,请使用版本5:npm安装查询-string@5
这种情况下的解决方案是再次传输模块,这可以通过修改网页配置中的exclude
属性来完成:
{
test: /\.js$/,
exclude: /node_modules\/(?!(es6-module|another-es6-module)\/).*/,
},
模块es6模块
和另一个-es6模块
将不再被网页忽略,并将与其他源代码一起传输
看
使用webpack@3.12.0,巴别塔-core@6.26.3
和巴别塔-core@6.26.3
事情通常应该是这样的。是否有特殊原因要传输位于节点单元模块中的已准备发行版的模块?顺便说一句,示例repo缺少webpack cli
/webpack命令
,因此start
命令不起作用OOTB.oops,似乎我已经全局安装了webpack cli。@AKX节点_模块中的lib使用const
,我想将其编译为var
,以支持lower broswerI明白你的意思。你说得对,我不应该传输分布式模块,特别是如果有支持旧浏览器的旧版本(我没有检查,我的错)。但我觉得奇怪的是,您无法将现代库编译到ES5,如果没有旧版本呢?在我的情况下,有必要在旧浏览器下编译项目,这是幸运的,因为它在项目中只有一个外部库,并且它有ES5模拟:)。无论如何,有没有办法从node\u modules
?从:{test:/\.js$/,exclude:/node\u modules\/(?!(my-module |另一个)\/)./,
据我所知,OP希望传输节点模块(或至少是特定的非一致性模块)。您提到模块应该已经是ES5
(关键字应该
)。因此,有时模块没有正确打包,我们可能需要手动传输它们。。。你的回答没有回答那个问题