Skip to content

ES5 transpiling #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2020
Merged

ES5 transpiling #369

merged 3 commits into from
Feb 10, 2020

Conversation

fiedl
Copy link
Collaborator

@fiedl fiedl commented Feb 9, 2020

Issue #238: make webpacker create es5 code instead of es6 code

Changes

  • Configure babel to include all necessary js files
  • Prepare polyfills for later use, but do not include them, yet

Notes

Babel es5 transpiling

In the builder app, we use webpacker v4, which, by default, is configured to transpile js code using babel in production.

However I've experienced huge issues with that, because, no matter what I did, babel did not transpile everything, but left loads of arrow functions (=>) un-transpiled, which do not work in ie11. Inspired by rails/webpacker#1239 (comment), I've discovered that our configuration did not include all necessary js paths in the scope that babel did run on.

Apparently, the paths that are considered by babel, are not taken from the config/webpacker.yml, but need to be added by hand:

// builder/config/webpack/production.js

const environment = require('./environment')
const babelLoader = environment.loaders.get('babel')

// Remove the `exclude`, which, by default, removes node_modules from babel transpiling:
delete babelLoader.exclude

// Add the other paths we need to run babel on:
const path = require('path')
babelLoader.include.push(path.resolve(__dirname, '../../../node_modules'))
babelLoader.include.push(path.resolve(__dirname, '../../../app/concepts/matestack/ui/core'))

// To inspect the paths:
console.log(babelLoader.include)

Then run rake webpack from the matestack-ui-core root directory.

Polyfills

Scope: Asset pipeline vs. webpack(er)

Users of the webpack(er) workflow transpile the js code on their own and make their own decisions regarding babel transpiling and the inclusion of polyfills.

For users of the asset pipeline, we provide ready-to-use assets including minified, transpiled js code of matestack-ui-core using the builder app. For these files, we need to consider the inclusion of polyfills here.

Filesize

With polyfills:

$ du -h vendor/assets/javascripts/dist/*.js
1,1M	vendor/assets/javascripts/dist/matestack-ui-core.js
256K	vendor/assets/javascripts/dist/matestack-ui-core.min.js

Without poylfills:

$ du -h vendor/assets/javascripts/dist/*.js
460K	vendor/assets/javascripts/dist/matestack-ui-core.js
136K	vendor/assets/javascripts/dist/matestack-ui-core.min.js

The babel online converter http://babeljs.io does not include polyfills. This is why the online-converted js file size is significantly smaller than the local one when including polyfills locally.

Do we need polyfills?

I've tested matestack with ie11 (in virtualbox): Currently, matestack-ui-core runs well without polyfillls.

Thus, due to filesize considerations, I'm deactivating polyfills for now in the builder app..

How to include polyfills

If we do need polyfills later, they can be activated by adding them to packs/matestack-ui-core.js:

// app/javascripts/packs/matestack-ui-core.js

// Polyfills:
import 'core-js/stable'
import 'regenerator-runtime/runtime'

import '../matestack-ui-core/index'

Then, run webpack using rake webpack from the matestack-ui-core root directory.

Development vs. production

By default, webpacker is configured to use babel es5 transpiling only in production. However, above filesize experiment (where the min version corresponds to production, the non-min version to development) shows that polyfills are also included in development.

Thus, when testing internet explorer, you need to require the production asset manually in your main application:

// app/assets/javascripts/application.js

// Production:
//= require 'dist/matestack-ui-core.min'

// Auto (default):
//= require 'matestack-ui-core'

@jonasjabari jonasjabari merged commit 0b65c13 into develop Feb 10, 2020
@jonasjabari jonasjabari added this to the 0.7.4 milestone Feb 10, 2020
@pascalwengerter pascalwengerter deleted the sf/es5-javascript branch February 17, 2020 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants