Accessing webpack bundled libraries in the browser

后端 未结 3 887
余生分开走
余生分开走 2021-02-05 07:59

I\'m having trouble accessing a webpack bundled library from the browser.

Example: I have a class Foo

// foo.js

\"use strict\";

export de         


        
3条回答
  •  鱼传尺愫
    2021-02-05 08:37

    To make this code re-usable, you need to tell webpack you are authoring a Library.

    From the webpack documentation:

    To make your library available for reuse, add library property in webpack configuration.

    To create your library, make the following change:

    module.exports = {
      entry: './src.js',
      output: {
        path: '.',
        filename: 'bundle.js',
        library: 'fooLibrary', //add this line to enable re-use
      },
    ...
    

    In order to use the library, you can then reference it in your other scripts:

    
    

提交回复
热议问题