Mark
Mark

Reputation: 368

Force webpack to include unreferenced functions in bundle

I have some functions that I intend to use in an unrelated commercial product developed by Oracle. I will simply include the javascript file and then reference the functions in the vendor's code where needed. However, I'd like to do some transpiling, add some pollyfills, and minify the code before including it. I'd like to automate this with webpack but it seems that since my entrypoint only contains functions that are not being used in the entry point they are not included in the bundle. I end up with a bundle that only contains webpack stub code and babel-pollyfill code. None of the functions from my entry point. I've searched through the docs looking for some option to tell it to include everything but no luck. Is there a way that I can force webpack to include unreferenced code in the bundle?

Upvotes: 0

Views: 2468

Answers (1)

Franci
Franci

Reputation: 2245

  1. if you export the functions, webpack will includes them.
  2. webpack wouldn't expose all the functions to global variable, in that case, the global variable will be polluted.

you need to use the webpack output's library feature.

for example, you can export the function in entry js file:

export function foo() {
    console.log(bar)
}

and in the webpack.config.js file, you can specify the output library:

module.exports = {
    //....,
    output:{
        library:'myLib'
    }
}

Webpack default export you library as variable, therefore you can use your function as myLib.foo().

you can check more here.

Upvotes: 1

Related Questions