Javascript 在网页包配置文件中使用babel插件
在他们的网站上,这个网页展示了这样的插件用法Javascript 在网页包配置文件中使用babel插件,javascript,webpack,babeljs,Javascript,Webpack,Babeljs,在他们的网站上,这个网页展示了这样的插件用法 plugins: [ new webpack.optimize.UglifyJsPlugin(), new HtmlWebpackPlugin({template: './src/index.html'}) ] 我想使用babel插件转换异步到生成器,所以我在babelrc文件中添加了它,但我不知道这是不够的,我应该添加它也网页文件?如果是,怎么办 我不能确定是否需要在webpack配置文件中编写插件,因为现在我遇到了运行时
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new HtmlWebpackPlugin({template: './src/index.html'})
]
我想使用babel插件转换异步到生成器,所以我在babelrc文件中添加了它,但我不知道这是不够的,我应该添加它也网页文件?如果是,怎么办
我不能确定是否需要在webpack配置文件中编写插件,因为现在我遇到了运行时错误,并且不确定它是否可以只在babelrc文件中编写
我当前的网页包配置文件
var path = require('path')
module.exports = {
entry: path.resolve(__dirname, 'partner/index.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'partner_bundle.js'
},
target: 'web',
module: {
rules: [
{
test: /\.js$/, // Check for all js files
loader: 'babel-loader',
query: {
presets: [
'babel-preset-env',
'babel-preset-stage-0'
].map(require.resolve)
},
exclude: /node_modules\/(?!other-module)/
}
]
},
stats: {
colors: true
},
devtool: 'source-map',
resolve: { symlinks: false }
}
babelrc文件
{
"presets": [
"env",
"stage-2"
],
"plugins": [
"transform-async-to-generator",
"transform-async-generator-functions",
[
"transform-runtime",
{
"helpers": false,
"polyfill": false,
"regenerator": true
}
]
]
}
在这里,您可以在我的网页配置中看到我是如何包含babel插件的:
test: /(\.jsx|\.js)$/, // JSX and JS files should be present.
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'babel-loader',
options: {
// Babel must be required like this to be able to use npm-link to link to shared code, see:
// https://stackoverflow.com/questions/34574403/how-to-set-resolve-for-babel-loader-presets/
presets: [
[node_modules + '/@babel/preset-env', {
// Ref: 1) http://2ality.com/2017/02/babel-preset-env.html
// 2) http://caniuse.com/usage-table
// In case it supports the browserlist in package.json, remove this here, see:
// https://github.com/babel/babel-preset-env/issues/149
"targets": {"browsers": ["> 4%", "safari 10", "ie 11", "iOS 9"]},
"modules": false,
"useBuiltIns": 'entry',
// "debug": true
}],
[node_modules + '/@babel/preset-react'],
],
plugins: [
node_modules + '/@babel/plugin-proposal-class-properties',
node_modules + '/@babel/plugin-proposal-object-rest-spread'].map(require.resolve)
}
}]
您可能需要将
模块
条目添加到您的网页配置中,以使用巴别塔加载程序
。您的.babelrc
文件定义了巴贝尔应该做什么。在您的网页配置中包括babel加载程序
,实际上会在您的代码上运行babel。我在我的帖子中添加了webpack配置文件,已经在使用babel loader了,我只是要求在webpack配置文件中添加babel polugins,或者我应该怎么做?babel loader
将自动使用.babelrc
文件,所以你不必在webpack.config.js
中添加相同的配置。实际上,您还可以将当前在网页配置中的巴别塔预设也放入.babelrc
。