Skip to content Skip to sidebar Skip to footer

How To Export Function With Webpack

I intend to bundle all my .js using webpack. I tried with a very simple example as following. Function to bundle in a test.js file : function test() { console.log('hello'); } We

Solution 1:

why?

Because you haven't exported anything from your entry point and, by default, webpack generates output in umd format without polluting global scope.

You first have to export your function:

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

Then specify "library" and "libraryTarget" in your webpack config. Docs. For example:

output: {
  filename: 'test.js',
  path: __dirname + '/public/javascript/dist',
  library: 'test',
  libraryTarget: 'window',
  libraryExport: 'default'
},

this will generate code that adds window.test = _entry_return_.default .


Post a Comment for "How To Export Function With Webpack"