sprockets-bumble_d 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e344b1743d35a046b99bbc04c8e28345d7d41214
4
+ data.tar.gz: 307b8f0365ae245ab881b5cff1f75d9163beaf9b
5
+ SHA512:
6
+ metadata.gz: 6a751efa6deee7bb1b77e4acbf1acb7aee4d4063c91fd5746f184fe4ef58d51fe8329dedea49ff185997d62f355eb1bcce2e1551c2ca3616e4e235712f25e4d9
7
+ data.tar.gz: a539c07de181232403dffc71c06f9b8c887e0b97a7c38d94757b40678e0ebed5f91201231ce2628dda3b78d646e23857d566d9850ed5dd93a754ad4c6b85d0c3
@@ -0,0 +1,12 @@
1
+ require 'rails/railtie'
2
+ require 'schmooze'
3
+ require 'sprockets'
4
+ require 'sprockets/bumble_d/dsl'
5
+ require 'sprockets/bumble_d/errors'
6
+ require 'sprockets/bumble_d/railtie'
7
+ require 'sprockets/bumble_d/version'
8
+
9
+ module Sprockets
10
+ module BumbleD
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ require 'sprockets/bumble_d/errors'
2
+
3
+ module Sprockets
4
+ module BumbleD
5
+ class Config
6
+ attr_accessor :babel_options, :babel_config_version, :root_dir, :transform_to_umd
7
+ attr_reader :globals_map
8
+
9
+ def initialize
10
+ @globals_map = {}.freeze
11
+ @transform_to_umd = true
12
+ @babel_options = {
13
+ presets: ['es2015'],
14
+ plugins: ['external-helpers']
15
+ }
16
+ end
17
+
18
+ def configure
19
+ yield self
20
+ end
21
+
22
+ def register_globals(globals_map)
23
+ duplicate_keys = @globals_map.keys & globals_map.keys
24
+ unless duplicate_keys.empty?
25
+ error_message = "Duplicate keys registered: #{duplicate_keys}"
26
+ raise ConflictingGlobalRegistrationError, error_message
27
+ end
28
+
29
+ @globals_map = @globals_map.merge(globals_map).freeze
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module Sprockets
2
+ module BumbleD
3
+ module DSL
4
+ def configure_sprockets_bumble_d(&block)
5
+ initializer 'configure_sprockets_bumble_d',
6
+ before: 'sprockets-bumble_d.configure_transformer' do |app|
7
+ app.config.sprockets_bumble_d.configure(&block)
8
+ end
9
+ end
10
+
11
+ def register_umd_globals(module_name, globals_map)
12
+ initializer "#{module_name}.register_umd_globals",
13
+ before: 'sprockets-bumble_d.configure_transformer' do |app|
14
+ app.config.sprockets_bumble_d.register_globals(globals_map)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module Sprockets
2
+ module BumbleD
3
+ class Error < StandardError; end
4
+
5
+ class ConflictingGlobalRegistrationError < Error; end
6
+ class RootDirectoryDoesNotExistError < Error; end
7
+ end
8
+ end
@@ -0,0 +1,46 @@
1
+ require 'sprockets/bumble_d/config'
2
+ require 'sprockets/bumble_d/transformer'
3
+
4
+ module Sprockets
5
+ module BumbleD
6
+ class Railtie < ::Rails::Railtie
7
+ config.before_configuration do
8
+ config.sprockets_bumble_d = Config.new
9
+ end
10
+
11
+ initializer 'sprockets-bumble_d.configure_transformer' do |app|
12
+ bumble_d_config = app.config.sprockets_bumble_d
13
+ root_dir = bumble_d_config.root_dir
14
+ babel_config_version = bumble_d_config.babel_config_version
15
+ babel_plugins = bumble_d_config.babel_options[:plugins]
16
+
17
+ if bumble_d_config.transform_to_umd
18
+ babel_plugins += [
19
+ ['transform-es2015-modules-umd', {
20
+ exactGlobals: true,
21
+ globals: bumble_d_config.globals_map
22
+ }]
23
+ ]
24
+ end
25
+
26
+ options = bumble_d_config.babel_options.merge(
27
+ plugins: babel_plugins,
28
+ root_dir: root_dir,
29
+ babel_config_version: babel_config_version
30
+ )
31
+ es6_transformer = Transformer.new(options)
32
+
33
+ # Using the deprecated register_engine rather than register_mime_type
34
+ # and register_transformer because otherwise .es6 files that aren't
35
+ # in the precompile list will get unnecessarily compiled. See
36
+ # https://github.com/rails/sprockets/issues/384
37
+ Sprockets.register_engine(
38
+ '.es6',
39
+ es6_transformer,
40
+ mime_type: 'application/javascript',
41
+ silence_deprecation: true
42
+ )
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ module Sprockets
2
+ module BumbleD
3
+ class Resolver
4
+ def initialize(babel)
5
+ @babel = babel
6
+ end
7
+
8
+ def resolve_plugins(plugins)
9
+ resolve_them(plugins, type: 'Plugin')
10
+ end
11
+
12
+ def resolve_presets(presets)
13
+ resolve_them(presets, type: 'Preset')
14
+ end
15
+
16
+ private
17
+
18
+ def resolve_them(plugins_or_presets, type:)
19
+ plugins_or_presets.map do |item|
20
+ if item.is_a?(Array)
21
+ [@babel.send("resolve#{type}", item[0]), item[1]]
22
+ else
23
+ @babel.send("resolve#{type}", item)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,89 @@
1
+ require 'schmooze/base'
2
+ require 'sprockets/path_utils'
3
+ require 'sprockets/bumble_d/errors'
4
+ require 'sprockets/bumble_d/resolver'
5
+ require 'sprockets/bumble_d/version'
6
+
7
+ module Sprockets
8
+ module BumbleD
9
+ class Transformer
10
+ class BabelBridge < Schmooze::Base
11
+ dependencies babel: 'babel-core'
12
+
13
+ method :resolvePlugin, 'babel.resolvePlugin'
14
+ method :resolvePreset, 'babel.resolvePreset'
15
+ method :transform, 'babel.transform'
16
+ end
17
+
18
+ attr_reader :cache_key
19
+
20
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
21
+ def initialize(options)
22
+ @options = options.dup
23
+ @root_dir = @options.delete(:root_dir)
24
+ babel_config_version = @options.delete(:babel_config_version)
25
+
26
+ unless @root_dir && File.directory?(@root_dir)
27
+ error_message =
28
+ 'You must provide the `root_dir` directory from which ' \
29
+ 'node modules are to be resolved'
30
+ raise RootDirectoryDoesNotExistError, error_message
31
+ end
32
+
33
+ resolver = Resolver.new(babel)
34
+ plugins = @options[:plugins]
35
+ presets = @options[:presets]
36
+ @options[:plugins] = resolver.resolve_plugins(plugins) if plugins.is_a?(Array)
37
+ @options[:presets] = resolver.resolve_presets(presets) if presets.is_a?(Array)
38
+
39
+ @cache_key = [
40
+ self.class.name,
41
+ VERSION,
42
+ babel_config_version,
43
+ @options
44
+ ].freeze
45
+ end
46
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
47
+
48
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
49
+ def call(input)
50
+ data = input[:data]
51
+
52
+ result = input[:cache].fetch(cache_key_from_input(input)) do
53
+ filename_relative = Sprockets::PathUtils.split_subpath(
54
+ input[:load_path],
55
+ input[:filename]
56
+ )
57
+
58
+ options = {
59
+ moduleIds: true,
60
+ sourceRoot: input[:load_path],
61
+ moduleRoot: nil,
62
+ filename: input[:filename],
63
+ filenameRelative: filename_relative,
64
+ ast: false
65
+ }.merge(@options)
66
+
67
+ if options[:moduleIds] && options[:moduleRoot]
68
+ options[:moduleId] ||= File.join(options[:moduleRoot], input[:name])
69
+ elsif options[:moduleIds]
70
+ options[:moduleId] ||= input[:name]
71
+ end
72
+
73
+ babel.transform(data, options)
74
+ end
75
+
76
+ { data: result['code'] }
77
+ end
78
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
79
+
80
+ def cache_key_from_input(input)
81
+ @cache_key + [input[:filename]] + [input[:data]]
82
+ end
83
+
84
+ def babel
85
+ @babel ||= BabelBridge.new(@root_dir)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,5 @@
1
+ module Sprockets
2
+ module BumbleD
3
+ VERSION = '1.0.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-bumble_d
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Macklin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 4.2.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: schmooze
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 0.1.6
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.6
47
+ - !ruby/object:Gem::Dependency
48
+ name: sprockets
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.5'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.5'
61
+ description: |2
62
+ Babel + UMD = BumbleD
63
+ Facilitate incremental migration of a large Sprockets-powered javascript
64
+ codebase to ES6 modules by transforming them to UMD modules that preserve
65
+ your existing global references.
66
+ email:
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/sprockets/bumble_d.rb
72
+ - lib/sprockets/bumble_d/config.rb
73
+ - lib/sprockets/bumble_d/dsl.rb
74
+ - lib/sprockets/bumble_d/errors.rb
75
+ - lib/sprockets/bumble_d/railtie.rb
76
+ - lib/sprockets/bumble_d/resolver.rb
77
+ - lib/sprockets/bumble_d/transformer.rb
78
+ - lib/sprockets/bumble_d/version.rb
79
+ homepage: https://github.com/rmacklin/sprockets-bumble_d
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.10
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Let Sprockets use Babel to transpile ES6 modules to UMD
103
+ test_files: []