【发布时间】:2016-12-06 10:39:05
【问题描述】:
我有一个表达式 require 应该在运行时解决,但是对于这个简单的示例,我无法理解 webpack 配置:
import something from 'module';
import pkg from './package.json';
let a;
if (pkg.main) {
a = require(pkg.main);
}
生成的构建应该包含 module,但在运行时还需要 ./package.json 和 pkg.main 作为 commonjs 模块 - 换句话说,将它们从构建中排除。
到目前为止我的webpack.config.js:
var webpack = require('webpack');
module.exports = {
entry: './src/main.js',
output: {
filename: '[name].js',
path: './build'
},
target: 'node-webkit',
plugins: [
new webpack.ExternalsPlugin('commonjs', './package.json')
],
module: {
noParse: /\.min\.js/,
exprContextRegExp: /$^/,
exprContextCritical: false,
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
}
]
}
};
现在发生的是 pkg.main 的 require 导致 webpackMissingModule 异常,如果我删除 exprContextRegExp,则 require 将使用上下文。
感谢您的帮助
【问题讨论】:
标签: javascript node.js webpack require commonjs