Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack umd library return Object.default

I'm writing a lib with webpack with these settings:

output: {
    path: path.join('build'),
    filename: 'my_lib.js',
    library: 'MyLib',
    libraryTarget: 'umd'
  },

MyLib:

export default function() {
  console.log('MyLib');
}

The problem is, when I try to load the build/my_lib.js in a browser, the only way to access MyLib is through MyLib.default...

Any idea?

like image 871
gtournie Avatar asked Jan 12 '16 06:01

gtournie


Video Answer


2 Answers

You should set libraryExport to default;

https://webpack.js.org/configuration/output/#outputlibraryexport

like image 194
Jack Pu Avatar answered Oct 13 '22 06:10

Jack Pu


the key is to use libraryExport: "default" like this:

  module.exports = {
    entry: ...,
    output: {
      path: __dirname + "/dist/",
      filename: "Template.js",
      library: "Template",
      libraryTarget: "umd",
      libraryExport: "default",
      globalObject: "this",
    },
like image 22
Dorian Avatar answered Oct 13 '22 05:10

Dorian