javascript node.js npm webpack webpack-2

Use webpack to bundle several ES6 classes into one file for import in a script tag

It has been three days now that I am trying to understand webpack to perform a simple task (that of course, in the course of three days I could have done by hand) but for the sake of learning webpack and be able to scale up…

I come with you with a desperate question, that is related probably to what this person was trying to achieve How do I concatenate and minify files using webpack but his solution did not work for me.

The problem is quite simple I have three classes:


export default class One {
constructor() {
this.isHorrible = true
whatIsHorrible() {
return (this)


class Two {
iMSoFat() {
this.fatness = 88
return (this.fatness)
export { Two }


class Three {
constructor() {
this.isHorrible = true
whatIsHorrible() {
return (this)
export { Three }

What i would like to do is in an index.html page :

<!DOCTYPE html>
<meta charset="utf-8">
<title>Test ton cul</title>
<script src="" charset="utf-8"></script>
<script type="text/javascript">
console.log(One, Two, Three);

I would also be satisfied with

new window.One() 
//.. or EVEN
new window.ebjs.One()

So I tried several config, I will spare you the excrutiating details and frustrations I went through… Lack of sleep and food.

I tried for a long long time to make the array entry point work… but then I read somewhere in the doc “If you pass an array: All modules are loaded upon startup. The last one is exported.”
And that explained a lot… I always got only one of the class somehow…. whatever that means… why would it behave like that ? It make absolutely no sense to me… anyways…
But even then, the class I got was not of the form library.class() or window.class() or class().

So after a while I though, lets load everything in an index.js and export it !

I first tried ES6 import for the style, because why not. But import {One} from './src/class1' does not work somehow, just produce a bunch on undefined export.. Like window.library.One = undefined.

So I fiddled round for a while again before settling for this index.js:


const class1 = require('./src/class1')
const class2 = require('./src/class2')
const class3 = require('./src/class3')
export { class1, class2, class3 }

My webpack config has changed quite a bit but here is what I am using now :


const libraryName="ebjs"
const outputFile = `${libraryName}.js`
export default {
entry: './index.js',
target: 'web',
// entry: './index.js',
devtool: 'source-map',
output: {
path: `${__dirname}/lib`,
filename: outputFile,
library: libraryName,
libraryTarget: 'umd',
umdNamedDefine: true,
module: {
loaders: [
test: /(\.jsx|\.js)$/,
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/,

A lot of details and trial/errors have been painfully undertaken.. and I did write a log of my experimentations.. maybe I will share it with the world before pulling the trigger in a couple of hours.

Anyways, it kindof works, but not as expected and definitly not at production quality. To access the class I have to use “new libraryName.One.One()”. If I wanted to bundle this for npm this would make no sense for the users. It still does not work as expect.

Here is a screenshot of the object on the page:
First world problems

I hope someone will come to my aid. My life truly might depend on it 🙂

Thanks !


So cbll answer did work… I could use the es6 import statement and the classes were correctly exported into the bundle.
In the html I could use


I created a small repo if someone is ever in the same predicament as I was:

Thank you again cbll and Michael Jungo !

In each classN.js, export each class by export default One either at the end of the file, of at the beginning(As you do in your “One” example, but not the others). In your index.js, import each by such: import One from './classN.js', of course assuming they are in the same folder(if not, add proper pathing). It will then be included in your main index.js, and bundled with Webpack.

In your case, this means your index.js would start out something like this:

import One from ('./src/class1.js')
import Two from ('./src/class2.js')
import Three from ('./src/class3.js')

This is assuming you export all your classes like One in your example, so:

export default class One {
// code goes here

export default class Two {
// code goes here

export default class Three {
// code goes here