sprockets-commoner 0.6.0 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b5d2ada3e40387a555b4b1be5c0284eeb8b82078
4
- data.tar.gz: 00145faf36173d45a06d3fd4e8bd840b31a80c72
2
+ SHA256:
3
+ metadata.gz: 5ed9326282bf87b5df475f96c527717170a5c7fc4a5913892f5f9b59d95e32b2
4
+ data.tar.gz: 1ee006c7d7fa8667b49fe8dd59dec1bcdb65ecf8a18db87e9348188dd826fa69
5
5
  SHA512:
6
- metadata.gz: 609da3bec1c0a44a191105feafbe84e29bea5d299651c391b58b2fa8b76c867afccb1e44e4c8b7fe41d596305a6d955515a72a545d544cf313e2ae17fdec4f36
7
- data.tar.gz: a46ce177fc4f3d19aa70c1bb872526da7251dc59866df310b2ccacd7064395f938c4cb1990c3a39407d66a926c96a0b752276ee8863918c5b13119300f22186a
6
+ metadata.gz: 0153d804065935185b1bb8eaf5e986e9aeb87d765da59a41818926dfe18fd563bfa8f292ddf8eba43803fc78d731de8480b0c6fe6f4bf2734eb0027c6e6eef77
7
+ data.tar.gz: 3e71a85683eaa949ce70306d8cc26b89d10d97a1f46d37b1a3ce5dcbf82bd8cf04e8c5b77d4386fbec54ea6f87921cda041e125b493f8095d314d396b3ba998c
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
  /tmp/
10
10
  node_modules
11
11
  npm-debug.log
12
+ yarn.lock
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.6.5
4
+
5
+ * Add file to follow `Bundler.require` convention.
6
+
7
+ ## v0.6.4
8
+
9
+ * Add support for ExecJS.
10
+
11
+ ## v0.6.2
12
+
13
+ * Add support for Sprockets 4
14
+
15
+ ## v0.6.1
16
+
17
+ * Ensure a file's cache is busted if it depends on an environment variable.
18
+
3
19
  ## v0.6.0
4
20
 
5
21
  * Adds support for resolving files in other bundles to global variables.
data/Gemfile CHANGED
@@ -1,3 +1,13 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ group :development, :test do
4
+ gem 'sprockets', github: 'rails/sprockets'
5
+ end
6
+
7
+ group :development do
8
+ gem 'pry'
9
+ gem 'pry-rescue'
10
+ gem 'pry-stack_explorer'
11
+ end
12
+
3
13
  gemspec
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Sprockets::Commoner
2
2
 
3
+ ### ⚠️ This gem is unmaintained. I advise you to use [Webpacker](https://github.com/rails/webpacker) in any new project ⚠️
4
+
3
5
  `Sprockets::Commoner` is a gem that enables JavaScript package and Babel transformation in Sprockets. It is meant as a replacement for [Browserify](http://browserify.org/) or [Webpack](https://webpack.github.io/) in Rails.
4
6
 
5
7
  ## Features
@@ -17,7 +19,7 @@
17
19
  ### Requirements
18
20
 
19
21
  1. Ruby v2+.
20
- 2. Rails/Any other application that uses Sprockets.
22
+ 2. Rails 4+/Any other application that uses Sprockets 3+.
21
23
  2. NPM v3+. We only support version 3 because commoner doesn't do any sort of deduplication of dependencies, so you could end up with a huge bundle if you don't want out. We only test against version 3, so you will definitely run into issues when running version 2.
22
24
  3. We recommend and support version 4+ of Node.js.
23
25
 
@@ -37,7 +39,7 @@ console.log(_.map([1, 2, 3], function(n) { return n * 3; }));
37
39
  ### Enabling Babel transforms
38
40
 
39
41
  1. Install any Babel plugins or presets you want to use. We'll use the default ES2015 preset; `npm install babel-preset-es2015`.
40
- 1. Add a `.babelrc` with you required configuration. We just need to do `echo '{presets: ["es2015"]}' > .babelrc`.
42
+ 1. Add a `.babelrc` with you required configuration. We just need to do `echo '{"presets": ["es2015"]}' > .babelrc`.
41
43
  1. Use any feature you want! For example, let's use `import` and arrow functions in our `application.js`:
42
44
 
43
45
  ```javascript
@@ -57,11 +59,11 @@ By default, commoner will process any file under the application root directory.
57
59
  Rails.application.config.assets.configure do |env|
58
60
  Sprockets::Commoner::Processor.configure(env,
59
61
  # include, exclude, and babel_exclude patterns can be path prefixes or regexes.
60
- # Explicitely list paths to include. The default is `[env.root]`
62
+ # Explicitly list paths to include. The default is `[env.root]`
61
63
  include: ['app/assets/javascripts/subdirectory'],
62
64
  # List files to ignore and not process require calls or apply any Babel transforms to. Default is ['vendor/bundle'].
63
65
  exclude: ['vendor/bundle', /ignored/],
64
- # Anything listed in babel_exclude has its require calls resolved, but no transforms listed in .babelrcs applied.
66
+ # Anything listed in babel_exclude has its require calls resolved, but no transforms listed in .babelrc applied.
65
67
  # Default is [/node_modules/]
66
68
  babel_exclude: [/node_modules/]
67
69
  )
data/bin/setup CHANGED
@@ -4,12 +4,15 @@ set -euo pipefail
4
4
  gem install bundler --version '1.11.2'
5
5
  bundle check || bundle install -j 4
6
6
 
7
- for path in "./test/fixtures" "./test/fixtures/vendor-stub" "./js/babel-plugin-sprockets-commoner-internal" "./test/fixtures/root-node-modules" "./test/demo"; do
7
+ for path in "./test/fixtures" "./test/fixtures/vendor-stub" "./test/fixtures/root-node-modules" "./test/demo"; do
8
8
  pushd "$path" >/dev/null
9
- npm install
9
+ yarn install
10
10
  popd >/dev/null
11
11
  done
12
+ pushd "./js/babel-plugin-sprockets-commoner-internal"
13
+ npm install
14
+ popd>/dev/null
12
15
 
13
16
  pushd "./test/demo"
14
17
  bundle check || bundle install -j 4
15
- popd
18
+ popd>/dev/null
data/circle.yml CHANGED
@@ -6,7 +6,12 @@ machine:
6
6
 
7
7
  dependencies:
8
8
  override:
9
+ - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
10
+ - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
11
+ - sudo apt-get update && sudo apt-get install yarn
9
12
  - bin/setup
13
+ cache_directories:
14
+ - ~/.cache/yarn
10
15
 
11
16
  test:
12
17
  override:
@@ -2,23 +2,4 @@
2
2
 
3
3
  ## Usage
4
4
 
5
- `babel-plugin-commoner` should be included in your `.babelrc` by adding `commoner`. You can't install it through npm, but this directory is automatically added to the path by the Sprockets plugin.
6
-
7
- ## Options
8
-
9
- * `globals`: A mapping of module name to global variable name, which will ovveride any import of that package with a global variable reference.
10
-
11
- ## Example
12
-
13
- ```json
14
- {
15
- presets: ["es2015"],
16
- plugins: [
17
- ["commoner", {
18
- globals: {
19
- "jquery": "$"
20
- }
21
- }]
22
- ]
23
- }
24
- ```
5
+ `babel-plugin-commoner` is an internal plugin to Commoner, and not something you should add to your Babel configuration.
@@ -158,12 +158,8 @@ module.exports = function (context) {
158
158
  if (name === false) {
159
159
  path.get('init').replaceWith(t.objectExpression([]));
160
160
  } else {
161
- if (path.scope.hasBinding(name)) {
162
- path.scope.rename(name);
163
- }
164
161
  path.scope.rename(path.node.id.name, name);
165
162
  path.remove();
166
- path.scope.removeBinding(name);
167
163
  }
168
164
  },
169
165
  CallExpression: function CallExpression(path, state) {
@@ -203,13 +199,17 @@ module.exports = function (context) {
203
199
  if (file.metadata.targetsToProcess == null) {
204
200
  file.metadata.targetsToProcess = [];
205
201
  }
202
+ if (file.metadata.includedEnvironmentVariables == null) {
203
+ file.metadata.includedEnvironmentVariables = [];
204
+ }
206
205
  },
207
206
 
208
207
  visitor: {
209
- MemberExpression: function MemberExpression(path) {
208
+ MemberExpression: function MemberExpression(path, state) {
210
209
  if (path.get("object").matchesPattern("process.env")) {
211
210
  var key = path.toComputedKey();
212
211
  if (t.isStringLiteral(key)) {
212
+ state.file.metadata.includedEnvironmentVariables.push(key.value);
213
213
  path.replaceWith(t.valueToNode(process.env[key.value]));
214
214
  }
215
215
  }
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sprockets/commoner'
@@ -5,6 +5,9 @@ require 'sprockets/commoner/bundle'
5
5
 
6
6
  module Sprockets
7
7
  module Commoner
8
+ def self.sprockets4?
9
+ @@sprockets4 ||= Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new('4.0.0.beta')
10
+ end
8
11
  end
9
12
 
10
13
  register_postprocessor 'application/javascript', ::Sprockets::Commoner::Processor
@@ -13,6 +16,10 @@ module Sprockets
13
16
  register_bundle_metadata_reducer 'application/javascript', :commoner_required, Set.new, :+
14
17
  register_bundle_metadata_reducer 'application/javascript', :commoner_used_helpers, Set.new, :+
15
18
  register_bundle_processor 'application/javascript', ::Sprockets::Commoner::Bundle
19
+ register_dependency_resolver 'commoner-environment-variable' do |env, str|
20
+ _, variable = str.split(':', 2)
21
+ ENV[variable]
22
+ end
16
23
  end
17
24
 
18
25
  require 'sprockets/commoner/railtie' if defined?(Rails)
@@ -29,17 +29,16 @@ function(helpers, globalIdentifiers) {
29
29
  JS
30
30
 
31
31
  PRELUDE = <<-JS.freeze
32
- !function() {
32
+ !function(global) {
33
33
  var __commoner_initialize_module__ = function(f) {
34
34
  var module = {exports: {}};
35
35
  f.call(module.exports, module, module.exports);
36
36
  return module.exports;
37
37
  };
38
- var global = window;
39
38
  JS
40
39
 
41
40
  OUTRO = <<-JS.freeze
42
- }();
41
+ }(typeof global != 'undefined' ? global : typeof window != 'undefined' ? window : this);
43
42
  JS
44
43
  def initialize(root)
45
44
  super(root, 'NODE_PATH' => JS_PACKAGE_PATH)
@@ -63,7 +62,11 @@ JS
63
62
  assets_missing = input[:metadata][:commoner_required] - assets_in_bundle
64
63
 
65
64
  global_identifiers = assets_missing.map do |filename|
66
- uri, _ = env.resolve(filename, type: input[:content_type], pipeline: :self, compat: false)
65
+ uri, _ = if Commoner.sprockets4?
66
+ env.resolve(filename, accept: input[:content_type], pipeline: :self)
67
+ else
68
+ env.resolve(filename, accept: input[:content_type], pipeline: :self, compat: false)
69
+ end
67
70
  asset = env.load(uri)
68
71
  # Retrieve the global variable the file is exposed through
69
72
  global = asset.metadata[:commoner_global_identifier]
@@ -74,9 +77,26 @@ JS
74
77
  used_helpers = input[:metadata][:commoner_used_helpers].to_a
75
78
  header_code = generate_header(used_helpers, global_identifiers)
76
79
  {
77
- data: "#{PRELUDE}#{header_code}\n#{input[:data]}#{OUTRO}"
80
+ data: "#{PRELUDE}#{header_code}\n#{input[:data]}#{OUTRO}",
81
+ map: shift_map(input[:metadata][:map], PRELUDE.lines.count + header_code.lines.count),
78
82
  }
79
83
  end
84
+
85
+ private
86
+
87
+ def shift_map(map, offset)
88
+ return unless map && Commoner.sprockets4?
89
+ index_map = Sprockets::SourceMapUtils.make_index_map(map)
90
+ index_map.merge({
91
+ "sections" => index_map["sections"].map { |section|
92
+ section.merge({
93
+ "offset" => section["offset"].merge({
94
+ "line" => section["offset"]["line"] + offset
95
+ })
96
+ })
97
+ }
98
+ })
99
+ end
80
100
  end
81
101
  end
82
102
  end
@@ -7,7 +7,7 @@ module Sprockets
7
7
 
8
8
  ExcludedFileError = Class.new(::StandardError)
9
9
 
10
- VERSION = '2'.freeze
10
+ VERSION = '3'.freeze
11
11
  BABELRC_FILE = '.babelrc'.freeze
12
12
  PACKAGE_JSON = 'package.json'.freeze
13
13
  JS_PACKAGE_PATH = File.expand_path('../../../js', __dir__)
@@ -100,10 +100,17 @@ module Sprockets
100
100
  @required.insert(insertion_index, asset)
101
101
  end
102
102
 
103
+ result['metadata']['includedEnvironmentVariables'].each do |env|
104
+ @dependencies << "commoner-environment-variable:#{env}"
105
+ end
106
+
107
+ map = process_map(input[:metadata][:map], result['map'], input)
108
+
103
109
  {
104
110
  data: result['code'],
105
111
  dependencies: @dependencies,
106
112
  required: Set.new(@required),
113
+ map: map,
107
114
 
108
115
  commoner_global_identifier: result['metadata']['globalIdentifier'],
109
116
  commoner_required: commoner_required,
@@ -113,6 +120,13 @@ module Sprockets
113
120
  end
114
121
 
115
122
  private
123
+ def process_map(oldmap, map, input)
124
+ if Commoner.sprockets4?
125
+ formatted_map = Sprockets::SourceMapUtils.format_source_map(map, input)
126
+ Sprockets::SourceMapUtils.combine_source_maps(oldmap, formatted_map)
127
+ end
128
+ end
129
+
116
130
  def compute_cache_key
117
131
  package_file = File.join(@root, 'node_modules', 'babel-core', 'package.json')
118
132
  raise Schmooze::DependencyError, 'Cannot determine babel version as babel-core has not been installed' unless File.exist?(package_file)
@@ -193,7 +207,6 @@ module Sprockets
193
207
  end
194
208
 
195
209
  def options(input)
196
- # TODO(bouk): Fix sourcemaps. Sourcemaps are only available in Sprockets v4
197
210
  {
198
211
  'ast' => false,
199
212
  'babelrc' => !match_any?(self.babel_exclude, input[:filename]),
@@ -201,6 +214,7 @@ module Sprockets
201
214
  'filenameRelative' => PathUtils.split_subpath(input[:load_path], input[:filename]),
202
215
  'moduleRoot' => nil,
203
216
  'sourceRoot' => @env.root,
217
+ 'sourceMaps' => Commoner.sprockets4?,
204
218
  }
205
219
  end
206
220
 
@@ -3,7 +3,8 @@ module Sprockets
3
3
  class Railtie < ::Rails::Railtie
4
4
  initializer 'sprockets-commoner' do
5
5
  # We need to disable debugging because otherwise Rails will include each file individually, while we need everything to be bundled up together into a single file.
6
- config.assets.debug = false
6
+ # ^ Not applicable to sprockets 4+
7
+ config.assets.debug = false unless Commoner.sprockets4?
7
8
  config.assets.paths << 'node_modules'
8
9
  end
9
10
  end
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Commoner
3
- VERSION = '0.6.0'
3
+ VERSION = '0.6.5'
4
4
  end
5
5
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.bindir = "exe"
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
- spec.add_dependency "sprockets", ">= 3", "< 4"
29
+ spec.add_dependency "sprockets", ">= 3", "< 5"
30
30
  spec.add_dependency "schmooze", "~> 0.1.6"
31
31
 
32
32
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-commoner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bouke van der Bijl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-10 00:00:00.000000000 Z
11
+ date: 2020-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '3'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '4'
22
+ version: '5'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '3'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '4'
32
+ version: '5'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: schmooze
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -154,6 +154,7 @@ files:
154
154
  - js/babel-plugin-sprockets-commoner-internal/node_modules/resolve/package.json
155
155
  - js/babel-plugin-sprockets-commoner-internal/package.json
156
156
  - js/babel-plugin-sprockets-commoner-internal/path-to-identifier.js
157
+ - lib/sprockets-commoner.rb
157
158
  - lib/sprockets/commoner.rb
158
159
  - lib/sprockets/commoner/bundle.rb
159
160
  - lib/sprockets/commoner/json_processor.rb
@@ -181,8 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
182
  - !ruby/object:Gem::Version
182
183
  version: '0'
183
184
  requirements: []
184
- rubyforge_project:
185
- rubygems_version: 2.5.1
185
+ rubygems_version: 3.1.2
186
186
  signing_key:
187
187
  specification_version: 4
188
188
  summary: Use Babel in Sprockets to compile modules for the browser