简体   繁体   中英

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

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

module.exports = foo;

File 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. So, when b.js be bundled and the output is written in dist folder, the b.js (bundled file) still require the a.js

How can i do this with webpack? i think that i should use 'externals' option in webpack config, but i can't find an example for non node_modules libs.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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