Skip to content

Commit

Permalink
Update to babel 7
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Macklin <richard.github@nrm.com>
  • Loading branch information
Mathew Baltes and rmacklin committed Feb 26, 2019
1 parent 9b77345 commit 6a2b4a8
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 148 deletions.
43 changes: 17 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Sprockets::BumbleD provides this with a Sprockets transformer that acts on
[ES2015 -> UMD modules transform] plugin, preserving any globals that you've
[registered](#registering-globals).

[ES2015 -> UMD modules transform]: https://github.com/babel/babel/tree/v6.12.0/packages/babel-plugin-transform-es2015-modules-umd
[ES2015 -> UMD modules transform]: https://github.com/babel/babel/tree/v7.3.4/packages/babel-plugin-transform-modules-umd

## Setup

Expand All @@ -35,23 +35,16 @@ Sprockets::BumbleD provides this with a Sprockets transformer that acts on
1. Add `gem 'sprockets-bumble_d'` to your `Gemfile` (or add a gemspec
dependency to an [inline engine](#inline-rails-engines) in your app) and
run `bundle install`.
2. Run `npm install --save babel-core babel-plugin-external-helpers babel-plugin-transform-es2015-modules-umd babel-preset-es2015`
2. Run `npm install --save @babel/core @babel/plugin-external-helpers @babel/plugin-transform-modules-umd @babel/preset-env`
to install the modules for the default babel config. If you want to
[customize the babel options](#customizing-your-babel-options), install any
additional plugins and presets you want.
- **Note:** Sprockets::BumbleD requires babel-core version 6.22.0 or higher.
This is because it can be used to transpile assets provided by a gem (e.g.
a rails plugin), and those assets would exist outside your application
subtree (they'd exist where your gems are globally installed), so we rely
on the `resolvePlugin` and `resolvePreset` methods introduced in
[babel PR #4729].
3. Generate the [external helpers] and `//= require` them in at the beginning
of your application manifest or pull them in with a separate script tag. This
step is of course unnecessary if you won't be using the external-helpers
plugin, but it's highly recommended that you do (to avoid inlining them
everywhere, which unnecessarily bloats the bundle sent to the browser).

[babel PR #4729]: https://github.com/babel/babel/pull/4729
[external helpers]: https://babeljs.io/docs/plugins/external-helpers

### Basic configuration
Expand All @@ -74,28 +67,28 @@ as its node `require` statements will resolve from that directory.

### Customizing your babel options

By default you get [babel-preset-es2015], [babel-plugin-external-helpers], and
[babel-plugin-transform-es2015-modules-umd]. If you want to customize this with
By default you get [@babel/preset-env], [@babel/plugin-external-helpers], and
[@babel/plugin-transform-modules-umd]. If you want to customize this with
different plugins and presets, specify them in the
`configure_sprockets_bumble_d` block with the `babel_options` setting. Note
that (because it's central to the purpose of this gem)
babel-plugin-transform-es2015-modules-umd is _always_ included for you (unless
@babel/plugin-transform-modules-umd is _always_ included for you (unless
you [set `transform_to_umd` to `false`](#do-i-have-to-transpile-to-umd-modules))
and configured to use the [registered globals](#registering-globals), so this
plugin does not need to be specified when you override the default plugins.

[babel-preset-es2015]: https://babeljs.io/docs/plugins/preset-es2015
[babel-plugin-external-helpers]: https://babeljs.io/docs/plugins/external-helpers/
[babel-plugin-transform-es2015-modules-umd]: https://babeljs.io/docs/plugins/transform-es2015-modules-umd
[@babel/preset-env]: https://babeljs.io/docs/en/babel-preset-env
[@babel/plugin-external-helpers]: https://babeljs.io/docs/en/babel-plugin-external-helpers
[@babel/plugin-transform-modules-umd]: https://babeljs.io/docs/en/babel-plugin-transform-modules-umd

For example:
```ruby
configure_sprockets_bumble_d do |config|
config.root_dir = File.expand_path('..', __dir__)
config.babel_config_version = 2
config.babel_options = {
presets: ['es2015', 'react'],
plugins: ['external-helpers', 'custom-plugin']
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-external-helpers', 'custom-plugin']
}
end
```
Expand Down Expand Up @@ -124,13 +117,11 @@ gem. This is what the `root_dir` config is for. It's also why the

### Registering globals

As of version 6.12.0, babel-plugin-transform-es2015-modules-umd includes an
`exactGlobals` option that lets you specify exactly how to transpile any import
statements into the global reference it should resolve to. It also lets you
specify what global should be exported by an ES6 module in the resultant UMD
output. (A complete description is available in [babel PR #3534]). If you're
using an older version of the plugin, upgrade to at least 6.12.0 to get the
full value of this gem and registering globals.
@babel/plugin-transform-modules-umd includes an `exactGlobals` option that lets
you specify exactly how to transpile any import statements into the global
reference it should resolve to. It also lets you specify what global should be
exported by an ES6 module in the resultant UMD output. (A complete description
is available in [babel PR #3534]).

[babel PR #3534]: (https://github.com/babel/babel/pull/3534)

Expand Down Expand Up @@ -223,8 +214,8 @@ configure_sprockets_bumble_d do |config|
config.babel_config_version = 1
config.transform_to_umd = false
config.babel_options = {
presets: ['es2015'],
plugins: ['external-helpers', 'transform-es2015-modules-amd']
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-external-helpers', '@babel/plugin-transform-modules-amd']
}
end
```
Expand Down
4 changes: 2 additions & 2 deletions lib/sprockets/bumble_d/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize
@globals_map = {}.freeze
@transform_to_umd = true
@babel_options = {
presets: ['es2015'],
plugins: ['external-helpers']
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-external-helpers']
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/bumble_d/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Railtie < ::Rails::Railtie

if bumble_d_config.transform_to_umd
babel_plugins += [
['transform-es2015-modules-umd', {
['@babel/plugin-transform-modules-umd', {
exactGlobals: true,
globals: bumble_d_config.globals_map
}]
Expand Down
5 changes: 2 additions & 3 deletions lib/sprockets/bumble_d/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Sprockets
module BumbleD
class Transformer
class BabelBridge < Schmooze::Base
dependencies babel: 'babel-core'
dependencies babel: '@babel/core'

method :resolvePlugin, 'babel.resolvePlugin'
method :resolvePreset, 'babel.resolvePreset'
Expand Down Expand Up @@ -53,15 +53,14 @@ def call(input)
options = {
moduleIds: true,
sourceRoot: input[:load_path],
moduleRoot: nil,
filename: input[:filename],
filenameRelative: filename_relative,
ast: false
}.merge(options_with_resolved_plugins_and_presets)

if options[:moduleIds] && options[:moduleRoot]
options[:moduleId] ||= File.join(options[:moduleRoot], input[:name])
elsif options[:moduleIds]
elsif options[:moduleIds] && input[:name] && !input[:name].empty?
options[:moduleId] ||= input[:name]
end

Expand Down
8 changes: 4 additions & 4 deletions test/sprockets/bumble_d/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ def test_configure
config = Sprockets::BumbleD::Config.new

default_babel_options = {
presets: ['es2015'],
plugins: ['external-helpers']
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-external-helpers']
}

assert_nil config.root_dir
assert_equal true, config.transform_to_umd
assert_equal default_babel_options, config.babel_options

custom_babel_options = {
presets: ['es2015', 'react'],
plugins: ['external-helpers', 'custom-plugin']
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-external-helpers', 'custom-plugin']
}

config.configure do |c|
Expand Down
8 changes: 4 additions & 4 deletions test/sprockets/bumble_d/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"private": true,
"dependencies": {
"babel-core": "^6.22.1",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-es2015-modules-umd": "^6.22.0",
"babel-preset-es2015": "^6.22.0"
"@babel/core": "^7.3.4",
"@babel/plugin-external-helpers": "^7.2.0",
"@babel/plugin-transform-modules-umd": "^7.2.0",
"@babel/preset-env": "^7.3.4"
}
}
24 changes: 12 additions & 12 deletions test/sprockets/bumble_d/resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ class ResolverTest < Minitest::Test
def test_resolve_plugins
node_modules_path = `cd #{File.expand_path(__dir__)} && npm root`[0...-1]
resolver = new_resolver
resolved_external_helpers_plugin = File.join(node_modules_path, 'babel-plugin-external-helpers/lib/index.js')
resolved_umd_plugin = File.join(node_modules_path, 'babel-plugin-transform-es2015-modules-umd/lib/index.js')
resolved_external_helpers_plugin = File.join(node_modules_path, '@babel/plugin-external-helpers/lib/index.js')
resolved_umd_plugin = File.join(node_modules_path, '@babel/plugin-transform-modules-umd/lib/index.js')

assert_equal [], resolver.resolve_plugins([])

assert_equal [resolved_external_helpers_plugin], resolver.resolve_plugins(['external-helpers'])
assert_equal [resolved_external_helpers_plugin], resolver.resolve_plugins(['@babel/plugin-external-helpers'])

assert_equal [resolved_external_helpers_plugin, resolved_umd_plugin],
resolver.resolve_plugins(['external-helpers', 'transform-es2015-modules-umd'])
resolver.resolve_plugins(['@babel/plugin-external-helpers', '@babel/plugin-transform-modules-umd'])

assert_equal [[resolved_umd_plugin, { exactGlobals: true }]],
resolver.resolve_plugins([['transform-es2015-modules-umd', { exactGlobals: true }]])
resolver.resolve_plugins([['@babel/plugin-transform-modules-umd', { exactGlobals: true }]])

plugins = ['external-helpers', ['transform-es2015-modules-umd', { exactGlobals: true }]]
plugins = ['@babel/plugin-external-helpers', ['@babel/plugin-transform-modules-umd', { exactGlobals: true }]]
assert_equal [resolved_external_helpers_plugin, [resolved_umd_plugin, { exactGlobals: true }]],
resolver.resolve_plugins(plugins)
end

def test_resolve_presets
node_modules_path = `cd #{File.expand_path(__dir__)} && npm root`[0...-1]
resolver = new_resolver
resolved_es2015_preset = File.join(node_modules_path, 'babel-preset-es2015/lib/index.js')
resolved_env_preset = File.join(node_modules_path, '@babel/preset-env/lib/index.js')

assert_equal [], resolver.resolve_presets([])

assert_equal [resolved_es2015_preset], resolver.resolve_presets(['es2015'])
assert_equal [resolved_env_preset], resolver.resolve_presets(['@babel/preset-env'])

assert_equal [[resolved_es2015_preset, { modules: false }]],
resolver.resolve_presets([['es2015', { modules: false }]])
assert_equal [[resolved_env_preset, { modules: false }]],
resolver.resolve_presets([['@babel/preset-env', { modules: false }]])

assert_equal [resolved_es2015_preset, [resolved_es2015_preset, { modules: false }]],
resolver.resolve_presets(['es2015', ['es2015', { modules: false }]])
assert_equal [resolved_env_preset, [resolved_env_preset, { modules: false }]],
resolver.resolve_presets(['@babel/preset-env', ['@babel/preset-env', { modules: false }]])
end

private
Expand Down
Loading

0 comments on commit 6a2b4a8

Please sign in to comment.