Three days have passed when I try to understand webpack to perform a simple task (which, of course, for three days, which I could do manually), but for the sake of exploring the web package and the possibility of scale up ...
I came with you with a desperate question, probably related to what this person was trying to achieve How to combine and minimize files using webpack , but his solution does not work for me.
The problem is pretty simple. I have three classes:
./SRC/class1.js
export default class One { constructor() { this.isHorrible = true } whatIsHorrible() { return (this) } }
./SRC/class2.js
class Two { iMSoFat() { this.fatness = 88 return (this.fatness) } } export { Two }
./SRC/class3.js
class Three { constructor() { this.isHorrible = true } whatIsHorrible() { return (this) } } export { Three }
What I would like to do on the index.html page:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test ton cul</title> <script src="./lib/ebjs.js" charset="utf-8"></script> </head> <body> <script type="text/javascript"> console.log(One, Two, Three); </script> </body> </html>
I would also be satisfied
new window.One() //.. or EVEN new window.ebjs.One()
So, I tried several configs, I will spare you with the detailed details and disappointments that I experienced ... Lack of sleep and food.
I tried for a long time to make an entry point into the array ... but then I read somewhere in the document "If you pass an array: all modules are loaded at startup, and the last is exported." And that explained a lot ... I always had only one of the class ... whatever that means ... why do this? For me, this makes absolutely no sense ... anyway ... But even then the class I received did not have the form library.class () or window.class () or class ().
So, after some time I can load everything into index.js and export it!
At first I tried importing ES6 for the style, because why not. But import {One} from './src/class1'
doesn't work anyway, just create a bundle on undefined export. Like window.library.One = undefined.
So, I muttered again before settling this index.js:
index.js
const class1 = require('./src/class1') const class2 = require('./src/class2') const class3 = require('./src/class3') export { class1, class2, class3 }
My webpack configuration has changed quite a bit, but here is what I am using now:
webpackrc.config.babel.js
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)/, }, ], }, }
Many details and trial / error were undertaken with pain .. and I wrote a journal of my experiments. Maybe I will share it with the world before pulling the trigger in a couple of hours.
In any case, this works, but not as expected, and definitely not in terms of production quality. To access the class I have to use "new libraryName.One.One ()". If I wanted to bundle this for npm, that would not make sense to users. This still does not work as expected.
Here is a screenshot of the object on the page:
I hope someone comes to my aid. My life can really depend on this :)
Thanks!
EDIT AND END
So the cbll answer really worked ... I could use the es6 import statement and the classes were correctly exported to the kit. In html, I could use
libraryName.class()
I created a small repo if someone was always in the same predicament as me:
https://github.com/albertbuchard/example-webpack-es6-class-bundle
Thanks again cbll and Michael Jungo!