krasimir / webpack-library-starter

Webpack based boilerplate for producing libraries (Input: ES6, Output: universal library)
MIT License
1.36k stars 290 forks source link

Can't get babel-polyfill working with this #11

Closed nelsyeung closed 7 years ago

nelsyeung commented 7 years ago

I'm trying to get babel-polyfill working with for a library but I'm getting the following error:

TypeError: Cannot read property '_babelPolyfill' of undefined
at Object.<anonymous> (/Users/foo/webpack-library-starter/lib/library.js:73:6)
...

Full replication details:

git clone https://github.com/krasimir/webpack-library-starter.git
cd webpack-library-starter
npm i
npm i --save babel-polyfill

Add import 'babel-polyfill'; to the beginning of "src/index.js".

Add 'babel-polyfill' to "webpack.config.js":

  entry: ['babel-polyfill', __dirname + '/src/index.js'],

Then build and test:

npm run dev
npm run build
npm test
krasimir commented 7 years ago

Hm ... why you need to add babel-polyfill to the webpack.config.js file. By importing it you are already including it in the bundle.

nelsyeung commented 7 years ago

@krasimir Because of this documentation: https://babeljs.io/docs/usage/polyfill/ I've tried removing the import 'babel-polyfill'; from the beginning of "src/index.js" and it's still not working. Also tried using just the import 'babel-polyfill' instead of the entry inside webpack.config.js.

krasimir commented 7 years ago

I see ... what if you not importing it. I mean using only the addition to webpack.config.js file.

nelsyeung commented 7 years ago

I've tried every single combinations: 1) Have both the import and webpack entry: not working 2) Have only the import in index.js: not working 3) Have only the webpack entry: not working 4) Without the import and webpack entry: working (lol basically just your package so of course its working)

krasimir commented 7 years ago

Thanks. I'll investigate.

nelsyeung commented 7 years ago

Sorry about this, I shouldn't even be using babel-polyfill for a library. The reason why this doesn't work is because webpack build for the browsers but Mocha is running tests in node, so it wouldn't have the window object to store _babelPolyfill, therefore undefined. I will close this issue now.