目标:对于某些匹配模式P的库,按原样webpack发出/编译require语句.
例:
假设我有一个mylib我想要通过的as-is,所以需要在运行时工作.
和这样的代码.
var b = require("./some.stuff.that.webpack.should.inline");
a = require('mylib/should/stay/a/Require');
我想要看起来像这样的输出
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
a = __webpack_require__(1);
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = require('mylib/should/stay/a/Require');
Run Code Online (Sandbox Code Playgroud)
我知道插件可以做到这一点,但我无法拦截正确的事件/理解插件文档
到目前为止尝试过:1 external....这假定定义在别的地方2. IgnorePlugin给出了webpackMissingModule......与我想要的相反.
在您的第一次尝试中,您可能会参考https://webpack.js.org/configuration/externals/,因此您很可能已经非常接近找到解决方案了。
老实说,使用externals可能有点不直观,因为它没有完整记录,并且需要将加载机制指定为字符串的一部分(而不是正确的 js 对象)。
要指示 webpack 保持原样require,请在配置文件中使用类似的内容(使用v4.25.5进行测试):
const IGNORED = ['dep1', 'dep2']
module.exports = {
// ...
// other options
// ...
externals: IGNORED.reduce((acc, p) => (acc[p] = `commonjs ${p}`, acc), {})
};
Run Code Online (Sandbox Code Playgroud)
如果您需要更多的灵活性,请使用以下function方法:
externals: (_, req, cb) => {
if (IGNORED.indexOf(req) >= 0) {
return cb(null, `commonjs ${req}`)
}
cb()
}
Run Code Online (Sandbox Code Playgroud)
如果省略commonjs全局范围将用于解决依赖关系。
| 归档时间: |
|
| 查看次数: |
211 次 |
| 最近记录: |