我的webpack.config.js文件中包含以下内容:
const webpack = require('webpack');
const path = require('path');
const config = {
context: path.resolve(__dirname, 'src'),
entry: './app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.css$/,
exclude: path.resolve(__dirname, 'node_modules'),
use: [ 'style-loader', 'css-loader' ]
},
{
test: /\.js$/,
exclude: path.resolve(__dirname, 'node_modules'),
use: [{
loader: 'babel-loader',
options: {
presets: [
['es2015']
]
}
}]
}
]
}
};
module.exports = config;
当我运行webpack
时,我收到以下错误:
ERROR in (webpack)/~/buffer/index.js
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/home/echessa/node/node-v6.9.1-linux-x64/lib/node_modules/webpack/node_modules/buffer"
at /home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (native)
at OptionManager.resolvePresets (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (/home/echessa/Documents/DEV/demo-project/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
at transpile (/home/echessa/Documents/DEV/demo-project/node_modules/babel-loader/lib/index.js:48:20)
at Object.module.exports (/home/echessa/Documents/DEV/demo-project/node_modules/babel-loader/lib/index.js:163:20)
@ ../~/css-loader/lib/css-base.js 1:0-117
@ ../~/css-loader!./main.css
@ ./main.css
@ ./app.js
我正在使用Webpack版本2.3.2。
以下是我使用的开发依赖项:
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^7.0.0-beta.1",
"babel-preset-es2015": "^6.24.0",
"css-loader": "^0.27.3",
"style-loader": "^0.16.1"
}
对于babel-loader
,我使用测试版,因为建议将7.x版用于Webpack 2.以下内容来自Github repo:
webpack 1.x | babel-loader< = 6.x
webpack 2.x | babel-loader> = 7.x(推荐)(^ 6.2.10也可以,但有弃用警告)
版本7.0.0还没有出来,所以我正在使用测试版。我尝试过使用6.2.10,但我仍然遇到同样的错误。
当我只使用其中一条规则时,webpack构建成功。例如。当我只有js
规则(并删除CSS文件)时,它可以工作。
module: {
rules: [
{
test: /\.js$/,
exclude: path.resolve(__dirname, 'node_modules'),
use: [{
loader: 'babel-loader',
options: {
presets: [
['es2015']
]
}
}]
}
]
}
或者当我只有CSS规则(并删除任何ES6代码)时,它可以工作。
module: {
rules: [
{
test: /\.css$/,
exclude: path.resolve(__dirname, 'node_modules'),
use: [ 'style-loader', 'css-loader' ]
}
]
}
什么可能?这可能是babel-loader
的问题,还是我的配置文件结构不正确?
答案 0 :(得分:0)
我找到了一个有效的解决方案。如果我将以下内容从webpack.config.js
文件移到.babelrc
文件,那么webpack运行正常。
{
"presets": ["es2015"]
}
我不确定为什么配置文件中的预设不起作用。我在网上看过的任何Webpack 2指南都在配置文件中。无论如何,我认为我应该在这里留下这个解决方案以防其他人被卡住。