Webpack需要expression式外部

我有一个expression式要求,应该在运行时解决,但我不能得到我的头围绕这个简单的例子的webpackconfiguration:

import something from 'module'; import pkg from './package.json'; let a; if (pkg.main) { a = require(pkg.main); } 

生成的版本应该包含module但在运行时也需要./package.jsonpkg.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结果需要webpackMissingModuleexception,如果我删除了exprContextRegExp ,require将使用上下文。

谢谢你的帮助

对于任何人想知道:你可以用这个插件来解决它:

 function() { this.parser.plugin('call require', function(expr) { if (expr.arguments.length !== 1) { return; } const param = this.evaluateExpression(expr.arguments[0]); if (!param.isString() && !param.isConditional()) { return true; } }); } 

任何无法通过webpack解决的问题都将保持原样。