我的 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
,我使用的是 beta 版本,因为建议将 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
还是我的配置文件结构不正确?