webpack browserify --ignore 等效吗?

01A*_*key 3 javascript node.js npm browserify webpack

我有一些代码在浏览器中不起作用,除非我“忽略”两个包,我可以用 browserify: 做到这一点browserify files.js -i fs-extra -i request --standalone files > files.browserify.js,结果代码可以正常工作,但是如果我尝试用 webpack 来做,代码会抱怨模块被丢失的。

...
  plugins: [
      new webpack.IgnorePlugin(/fs-extra$/),
      new webpack.IgnorePlugin(/request$/),
      new webpack.IgnorePlugin(/fs$/)
  ],
...
Run Code Online (Sandbox Code Playgroud)
test.webpack.js:7655 Uncaught Error: Cannot find module "request"
    at webpackMissingModule (test.webpack.js:7655)
    at Object.exports.byteLength (test.webpack.js:7655)
    at __webpack_require__ (test.webpack.js:20)
    at Object.<anonymous> (test.webpack.js:17012)
    at __webpack_require__ (test.webpack.js:20)
    at test.webpack.js:66
    at test.webpack.js:69
Run Code Online (Sandbox Code Playgroud)

我怀疑可能的WebPack不会创建一个“空存根”像browserify作用:--ignore, -i Replace a file with an empty stub. Files can be globs.

我能做些什么来解决这个问题?

资源

Ste*_*nev 5

你要找的是null-loader它返回一个空模块:

module: {
    loaders: [
        {
            test: /^(fs-extra|fs|request)$/,
            loader: "null"
        },
        ...
]
Run Code Online (Sandbox Code Playgroud)

安装:

$ npm i -D null-loader
Run Code Online (Sandbox Code Playgroud)

  • 在 Webpack 2 中:将 `loaders` 替换为 `rules`,并用 `null-loader` 代替 `null`。 (2认同)