Skip to content
This repository has been archived by the owner on Apr 26, 2018. It is now read-only.

Commit

Permalink
Enable ES2015's (un-transpiled) module imports while supporting Webpa…
Browse files Browse the repository at this point in the history
…ck's tree shaking.

This is currently *not* supported by Webpack (see webpack/webpack#1979 (comment) and webpack/webpack#2933 for rationale).

We workaround this by exposing our babel config to the wild (i.e. js/nagato/babel-options.js). This way we can give full controls for both bundling (i.e. 'tree shaking' in Webpack) and module building (i.e. 'transpiling') to the library users (I hope).

We just can't enforce our users to use some huge-sized arbitrary bundle.

Users with ES2015+ (or whatever) environment shall `require()` this config file inside their 'webpack.config.js', and use it as a hash object for the `babel-loader`'s `option: ` option. This could be achieved by looking into the `module: ` option in `package.json`; which (I believe) is the default behavior for Webpack when you use the native `import Something from 'other-external-library'` syntax.

If this is not desirable, use the fully-transpiled .js file inside our distributed npm package. This could be achieved by referring to the old-school `"main"` value inside the package.json.

Disclaimer:
By using this method we abandon Webpack-specific aggresive transpilation features for our entire library. This means we can't use Webpack-specific custom `import`s (i.e. importing non-JavaScript files like images (.png, .jpg, etc.) inside our library (.js)).

Additional notes:
This issue described in the disclaimer section can be workarounded by specifying your library-specific `npm run build` action inside the `prepare` section of package.json.
  • Loading branch information
saki7 committed Oct 29, 2017
1 parent 91e0c2e commit 335a1a5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
9 changes: 2 additions & 7 deletions .babelrc → js/nagato/babel-options.js
@@ -1,4 +1,4 @@
{
module.exports = {
"presets": [
["env", {
"modules": false,
Expand All @@ -12,11 +12,6 @@
"transform-function-bind",
"transform-runtime",
"transform-class-properties"
],
"env": {
"production": {
"presets": []
}
}
]
}

1 change: 1 addition & 0 deletions webpack.common.js
Expand Up @@ -24,6 +24,7 @@ module.exports = {
},
{
loader: 'babel-loader',
options: require('./js/nagato/babel-options'),
},
],
exclude: /node_modules/,
Expand Down

0 comments on commit 335a1a5

Please sign in to comment.