【问题标题】:Exclude files from bundle (Webpack)从包中排除文件(Webpack)
【发布时间】:2018-12-22 05:33:12
【问题描述】:

我的项目结构如下:

-dist
  └ a.js (this is a bundled file)
-src
  └ b.js

文件 a.js

function foo (name) {
  console.log(name);
}

module.exports = foo;

文件 b.js

const log = require('../dist/a.js');

log('stackoverflow');

我想捆绑 b.js 文件,但我不想在此捆绑包中包含 a.js 文件。所以,当 b.js 被捆绑并且输出写入 dist 文件夹时,b.js(捆绑文件)仍然需要 a.js

如何使用 webpack 做到这一点?我认为我应该在 webpack 配置中使用“externals”选项,但我找不到非 node_modules 库的示例。

【问题讨论】:

    标签: node.js webpack externals


    【解决方案1】:

    在具有相对路径的代码中包含外部文件不适合您吗?例如,不要做任何其他事情,在你的代码中只做

    const log = require('../dist/a.js'); // relative path to a.js
    

    如果这不起作用?尝试在webpack.config.js 中设置externals 为相对路径

    module.exports = {
      externals: {
        log: '../dist/a.js'
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-22
      • 2017-07-10
      • 2020-05-05
      • 1970-01-01
      • 2016-01-14
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多