简体   繁体   English

从包(Webpack)中排除文件

[英]Exclude files from bundle (Webpack)

I have the following project structure: 我有以下项目结构:

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

File a.js 档案a.js

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

module.exports = foo;

File b.js 文件b.js

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

log('stackoverflow');

I want bundle the b.js file but i don't want include in this bundle the a.js file. 我想捆绑b.js文件,但我不想在捆绑中包含a.js文件。 So, when b.js be bundled and the output is written in dist folder, the b.js (bundled file) still require the a.js 因此,当将b.js捆绑在一起并将输出写入dist文件夹时,b.js(捆绑的文件)仍然需要a.js

How can i do this with webpack? 我该如何使用webpack? i think that i should use 'externals' option in webpack config, but i can't find an example for non node_modules libs. 我认为我应该在webpack配置中使用'externals'选项,但是我找不到非node_modules库的示例。

Does including external file in code with relative path not work for you? 在具有相对路径的代码中包含外部文件对您不起作用吗? For example, don't do anything else, in your code just do 例如,不要执行其他任何操作,而是在代码中执行

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

If that doesn't work? 如果那不起作用? Try in webpack.config.js set externals as relative path 尝试在webpack.config.js中将externals设置为相对路径

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM