我已经在ES6中创建了节点API Webpack编译器。没有es6,一切都会很好。但是如果我使用es6,我会得到:
[13:29:41] TypeError: Cannot read property 'concat' of undefined
at new NormalModuleFactory (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/NormalModuleFactory.js:115:51)
at Compiler.createNormalModuleFactory (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:586:31)
at Compiler.newCompilationParams (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:603:30)
at Compiler.compile (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:611:23)
at readRecords.err (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:284:11)
at Compiler.readRecords (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:479:11)
at hooks.run.callAsync.err (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:281:10)
at AsyncSeriesHook.eval [as callAsync] (eval at create (/private/var/www/MyProject/legacy/shop/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (/private/var/www/MyProject/legacy/shop/node_modules/tapable/lib/Hook.js:154:20)
at hooks.beforeRun.callAsync.err (/private/var/www/MyProject/legacy/shop/node_modules/webpack/lib/Compiler.js:278:19)
Process finished with exit code 1
这是我的代码:
import {Compiler} from 'webpack';
const config = require("./config/webpack.config");
class WebpackCompiler {
constructor(context) {
this.config = config;
this.compilerOptions = {};
this.compiler = new Compiler(context);
}
start() {
this.compiler.options = Object.assign({}, this.config, this.compilerOptions);
this.compiler.run().then((stats) => this._handleStats(stats)).catch((err) => this._handleErrors(err));
}
setOutput(output) {
this.compilerOptions.output = output;
}
setEntries(entires) {
this.compilerOptions.entry = entires;
}
_handleStats(stats) {
const statsErrors = stats.toJson();
if (stats.hasErrors()) {
console.error(statsErrors.errors.toString({
chunks: false,
colors: true
}));
}
if (stats.hasWarnings()) {
console.warn(statsErrors.warnings.toString({
chunks: false,
colors: true
}));
}
}
_handleErrors(err) {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
}
}
export const CompilerFactory = {
create: function (context) {
return new WebpackCompiler(context);
}
}
之后,是这样的:
var compiler = CompilerFactory.create(WEBPACK_CONTEXT);
compiler.setEntries({bundle: "./js/main.js"});
compiler.setOutput("./js/compiled");
compiler.start();
简单的Webpack配置:
const path = require('path');
const sourcePath = path.resolve("MY_PATH_TO_ROOT");
const mode = process.env.NODE_ENV === 'production' || "development";
module.exports = {
mode: mode,
node: {
__dirname: true
},
watchOptions: {
poll: true
},
devtool: "eval",
entry: {},
output: {
filename: '[name].js'
},
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
sourceMaps: true
}
}
]
}
]
},
externals: {
"jquery": "jQuery"
},
resolve: {
modules: [
path.resolve(sourcePath, 'node_modules')
]
}
};
问题在调用方法Run()
之后。在NormalModuleFactory.js中,问题在以下行:
this.ruleSet = new RuleSet(options.defaultRules.concat(options.rules));
正如我所说,webpack文档中没有太多有关使用ES6的信息。大家都知道哪里出问题了吗? 谢谢您的宝贵时间。