【发布时间】:2016-06-30 17:44:39
【问题描述】:
我正在编写一个像这样导出函数的库:
function getFreshModule(absolutePath) {
if (someCond) {
delete require.cache[absolutePath]
}
return require(absolutePath)
}
使用 webpack 编译这个 (babel) 以及 libraryTarget: 'commonjs' 和 target: 'node'。
对于我的生活,我无法弄清楚如何让 webpack 忽略这一点。
我认为externals 不适合这个,我还检查了 ContextReplacementPlugin,以及摆弄这些选项(来自文档的 sn-p)。
{
module: {
// Disable handling of requires with a single expression
exprContextRegExp: /$^/,
exprContextCritical: false
}
}
但是,这似乎并没有像它所说的那样 - 因为我的 require 语句仍然被 __webpack_require_ 函数替换。我也尝试过module.noParse,但这也不起作用,因为它不是通过 babel 转换的。
库的其余部分正常,我只是希望它专门忽略require的这种用法。
【问题讨论】:
-
这很疯狂,有同样的问题。找不到任何方法。
-
晚会,但在尝试解决同样的问题时,我发现了
__non_webpack_require__,这是对 webpack 运行时原始 require 函数的引用。