sprockets-commoner 0.1.4 → 0.2.0

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
2
  SHA1:
3
- metadata.gz: 72d705ccec2dcfa3c419987dffd054c3c9718905
4
- data.tar.gz: 71803b7576ae34379d0ea53b8182edca664076ec
3
+ metadata.gz: 8a5ceb584e1eacf7990a65e0dfb94534f9d2fdd2
4
+ data.tar.gz: 4ba4cc82f4df684af207003516778fa80ee9fd81
5
5
  SHA512:
6
- metadata.gz: 73d3e0c02678fc1a72a3aa5b33319dcc35874c38d8aa4d2eb9fcd62be1aeab6a3e5a8d2a58f55363b46f3c906e0bb3cee68479616289d729d1b8b06e8e83e2f7
7
- data.tar.gz: 0d14803ec4e7d8bc7bc4d843fe4aacb12071f337c503ee8f8ddffa6fcb01e3c602ae19b1c23da9d91350d5b080ea17ae3782560ff4d9dde31c1fd818c49fa7a8
6
+ metadata.gz: 424bfe23119eef889e07d452835e7bae3ca03f96017e4be8614792aaedf6b2172a93abdb577263953641fba60a95d0641fdddae9a1eef67bf29ee77a1871723d
7
+ data.tar.gz: f094b1ba53d8da39d3fd06272960a5af01d8e366dc67abb6e740b8466b6990b184537b7a27d72b79e9ac096357a6687ad3d7ec99f901d6d42f88c4b381f58866
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## v0.2.0
4
+
5
+ Add support for requiring JSON files.
6
+
7
+ ## v0.1.0
8
+
9
+ Initial version.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Sprockets::Commoner
2
2
 
3
- `Sprockets::Commoner` is a gem that enables JavaScript package and Babel transformation in Sprockets.
3
+ `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
4
 
5
5
  ## Features
6
6
 
@@ -28,7 +28,7 @@ To get started, let's begin with the simplest possible set up: just enabling res
28
28
  1. Add `sprockets-commoner` to Gemfile, run `bundle install`, and restart your Rails server.
29
29
  1. Add package.json with `babel-core` version 6 and any packages you want. For the example, we'll use the excellent [lodash](https://lodash.com) library. `npm install -S babel-core@6 lodash`
30
30
  1. `require` your client-side JavaScript packages from `application.js`!
31
- ```
31
+ ```javascript
32
32
  var _ = require('lodash');
33
33
 
34
34
  console.log(_.map([1, 2, 3], function(n) { return n * 3; }));
@@ -40,7 +40,7 @@ console.log(_.map([1, 2, 3], function(n) { return n * 3; }));
40
40
  1. Add a `.babelrc` with you required configuration. We just need to do `echo '{presets: ["es2015"]}' > .babelrc`.
41
41
  1. Use any feature you want! For example, let's use `import` and arrow functions in our `application.js`:
42
42
 
43
- ```
43
+ ```javascript
44
44
  import {map} from 'lodash';
45
45
 
46
46
  console.log(map([1, 2, 3], (n) => n * 3));
@@ -52,7 +52,7 @@ console.log(map([1, 2, 3], (n) => n * 3));
52
52
 
53
53
  By default, commoner will process any file under the application root directory. If you want more fine-tuned control over which files to process, you can specify which paths to include or exclude. To do so, you will need to re-register the Sprockets processor. For example:
54
54
 
55
- ```
55
+ ```ruby
56
56
  # In config/initializers/sprockets_commoner.rb
57
57
  Rails.application.config.assets.configure do |env|
58
58
  env.unregister_postprocessor('application/javascript', Sprockets::Commoner::Processor)
@@ -83,12 +83,12 @@ Commoner is designed from the start to facilitate a transition from CoffeeScript
83
83
  Any JavaScript file can `require` a CoffeeScript file, which will cause that CoffeeScript file to be scanned for a global variable reference and the `require` call to be replaced with a reference.
84
84
  If we have the following two files:
85
85
 
86
- ```
86
+ ```coffeescript
87
87
  # file.coffee
88
88
  class window.ImportantClass
89
89
  ```
90
90
 
91
- ```
91
+ ```javascript
92
92
  // main.js
93
93
  var klass = require('./file');
94
94
 
@@ -101,15 +101,15 @@ Then the second file will just be compiled down to `new window.ImportantClass()`
101
101
 
102
102
  We have added a custom directive that makes it very easy to expose an ES2015 module to the global namespace so it can be used by CoffeeScript files or any other code. For example:
103
103
 
104
- ```
105
- 'expose window.MyClass`;
104
+ ```javascript
105
+ 'expose window.MyClass';
106
106
 
107
107
  export default class MyClass {}
108
108
  ```
109
109
 
110
110
  `expose` will use the default export if available, otherwise the whole module namespace will be assigned to the global variable. For example:
111
111
 
112
- ```
112
+ ```javascript
113
113
  // constants.js
114
114
  'expose window.Constants';
115
115
 
@@ -138,7 +138,7 @@ After the regular Babel plugins are done doing their thing, `babel-plugin-common
138
138
  * It finds any `require` calls and rewires them to variable references (as detailed in [`require` support](#require-support))
139
139
  * It wraps the module in a function and supplies it with `module` and `exports`. The end value of `module.exports` gets assigned to the module identifier, which is referenced by other files (as specified in '`require` support') Example:
140
140
 
141
- ```
141
+ ```javascript
142
142
  var __commoner_module__node_modules$package$index_js = __commoner_initialize_module__(function (module, exports) {
143
143
  exports.default = 123;
144
144
  });
@@ -151,14 +151,14 @@ After all the files have been transformed, there is a bundle step which combines
151
151
 
152
152
  For example, if we have the following two files:
153
153
 
154
- ```
154
+ ```javascript
155
155
  // module.js
156
156
  export default function a() {
157
157
  return 1;
158
158
  };
159
159
  ```
160
160
 
161
- ```
161
+ ```javascript
162
162
  // application.js
163
163
  import a from './module';
164
164
 
@@ -167,7 +167,7 @@ a();
167
167
 
168
168
  We will end up with the following (browser-runnable) file:
169
169
 
170
- ```
170
+ ```javascript
171
171
  !function() {
172
172
  var __commoner_initialize_module__ = function(f) {
173
173
  var module = {exports: {}};
@@ -202,4 +202,3 @@ var __commoner_module__app$assets$javascripts$application_js = __commoner_initia
202
202
  ```
203
203
 
204
204
  This file is meant to be compressed, and does incredibly well when processed by UglifyJS.
205
-
@@ -25,9 +25,7 @@ if (typeof Object.assign != 'function') {
25
25
 
26
26
  var fs = require('fs');
27
27
  var dirname = require('path').dirname;
28
- var join = require('path').join;
29
28
  var resolve = require('browser-resolve').sync;
30
- var emptyModule = join(__dirname, 'node_modules', 'browser-resolve', 'empty.js');
31
29
 
32
30
  module.exports = function (context) {
33
31
  var t = context.types;
@@ -65,7 +63,7 @@ module.exports = function (context) {
65
63
  }
66
64
 
67
65
  if (identifiers.length === 0) {
68
- return false;
66
+ throw new Error('No identifiers found in ' + path);
69
67
  } else if (identifiers.length > 1) {
70
68
  throw new Error('Multiple identifiers found in ' + path);
71
69
  }
@@ -116,16 +114,12 @@ module.exports = function (context) {
116
114
  return '__commoner_module__' + escapedPath;
117
115
  }
118
116
 
119
- function resolveTarget(file, path, ensureTargetIsProcessed) {
117
+ function resolveTarget(file, path) {
120
118
  var name = void 0;
121
119
  if (opts.globals != null && (name = opts.globals[path]) != null) {
122
120
  return name;
123
121
  } else {
124
122
  var resolvedPath = resolve(path, opts);
125
- if (resolvedPath === emptyModule) {
126
- return false;
127
- }
128
-
129
123
  file.metadata.required.push(resolvedPath);
130
124
 
131
125
  // Check if the path is under sourceRoot
@@ -135,13 +129,10 @@ module.exports = function (context) {
135
129
  }
136
130
 
137
131
  if (/\.coffee$/.test(resolvedPath)) {
138
- // If it's a coffee script file, look for global variable assignments.
132
+ // If it's a coffee script file, look for global variable assignments
139
133
  return findDeclarationInCoffeeFile(resolvedPath);
140
134
  } else {
141
- if (ensureTargetIsProcessed) {
142
- file.metadata.targetsToProcess.push(resolvedPath);
143
- }
144
- // Otherwise we just look for the module by referencing its Special Identifier™.
135
+ // Otherwise we just look for the module by referencing its Special Identifier™
145
136
  return pathToIdentifier(resolvedPath);
146
137
  }
147
138
  }
@@ -163,14 +154,10 @@ module.exports = function (context) {
163
154
  return;
164
155
  }
165
156
 
166
- var name = resolveTarget(state.file, target, true);
167
- if (name === false) {
168
- path.get('init').replaceWith(t.objectExpression([]));
169
- } else {
170
- path.scope.rename(name);
171
- path.scope.rename(path.node.id.name, name);
172
- path.remove();
173
- }
157
+ var name = resolveTarget(state.file, target);
158
+ path.scope.rename(name);
159
+ path.scope.rename(path.node.id.name, name);
160
+ path.remove();
174
161
  },
175
162
  CallExpression: function CallExpression(path, state) {
176
163
  if (!isRequire(path)) {
@@ -182,21 +169,16 @@ module.exports = function (context) {
182
169
  return;
183
170
  }
184
171
 
172
+ var replacement = resolveTarget(state.file, target);
185
173
  switch (path.parent.type) {
186
- case "ExpressionStatement":
187
- // We just need to know there's a dependency, we can remove the `require` call.
188
- resolveTarget(state.file, target, false);
189
- path.remove();
190
- break;
191
- default:
192
- // Otherwise we just look for the module by referencing its Special Identifier™.
193
- var replacement = resolveTarget(state.file, target, true);
194
- if (replacement === false) {
195
- path.replaceWith(t.objectExpression([]));
196
- } else {
174
+ case "ExpressionStatement":
175
+ // We just need to know there's a dependency, we can remove it then
176
+ path.remove();
177
+ break;
178
+ default:
179
+ // Otherwise we just look for the module by referencing its Special Identifier™
197
180
  path.replaceWith(t.identifier(replacement));
198
- }
199
- break;
181
+ break;
200
182
  }
201
183
  }
202
184
  };
@@ -206,9 +188,6 @@ module.exports = function (context) {
206
188
  if (file.metadata.required == null) {
207
189
  file.metadata.required = [];
208
190
  }
209
- if (file.metadata.targetsToProcess == null) {
210
- file.metadata.targetsToProcess = [];
211
- }
212
191
  },
213
192
 
214
193
  visitor: {
@@ -0,0 +1,9 @@
1
+ module Sprockets
2
+ module Commoner
3
+ class JSONProcessor
4
+ def self.call(input)
5
+ { data: "module.exports = #{input[:data]};" }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -4,19 +4,14 @@ require 'open3'
4
4
  module Sprockets
5
5
  module Commoner
6
6
  class Processor < Schmooze::Base
7
-
8
- ExcludedFileError = Class.new(::StandardError)
9
-
10
- VERSION = '1'.freeze
11
7
  BABELRC_FILE = '.babelrc'.freeze
12
8
  PACKAGE_JSON = 'package.json'.freeze
13
9
  JS_PACKAGE_PATH = File.expand_path('../../../js', __dir__)
14
- ALLOWED_EXTENSIONS = /\.js(?:\.erb)?\z/
15
- COFFEE_EXTENSION = /\.coffee(:?\.erb)?\z/
10
+ ALLOWED_EXTENSIONS = /\.js(?:on)?(?:\.erb)?\z/
16
11
 
17
12
  dependencies babel: 'babel-core', commoner: 'babel-plugin-sprockets-commoner-internal'
18
13
 
19
- method :version, 'function() { return babel.version; }'
14
+ method :version, 'function() { return [process.version, babel.version]; }'
20
15
  method :transform, %q{function(code, opts, commonerOpts) {
21
16
  try {
22
17
  var file = new babel.File(opts);
@@ -49,25 +44,20 @@ module Sprockets
49
44
  end
50
45
 
51
46
  attr_reader :include, :exclude, :babel_exclude
52
- def initialize(root, include: [root], exclude: ['vendor/bundle'], babel_exclude: [/node_modules/])
47
+ def initialize(root, include: [root], exclude: [], babel_exclude: [/node_modules/])
53
48
  @include = include.map {|path| expand_to_root(path, root) }
54
49
  @exclude = exclude.map {|path| expand_to_root(path, root) }
55
50
  @babel_exclude = babel_exclude.map {|path| expand_to_root(path, root) }
56
51
  super(root, 'NODE_PATH' => JS_PACKAGE_PATH)
57
52
  end
58
53
 
59
- def cache_key
54
+ def call(input)
60
55
  @cache_key ||= [
61
56
  self.class.name,
62
- VERSION,
63
57
  version,
64
- @include.map(&:to_s),
65
- @exclude.map(&:to_s),
66
- @babel_exclude.map(&:to_s),
67
- ]
68
- end
58
+ VERSION,
59
+ ].freeze
69
60
 
70
- def call(input)
71
61
  filename = input[:filename]
72
62
 
73
63
  return unless should_process?(filename)
@@ -80,14 +70,8 @@ module Sprockets
80
70
 
81
71
  babel_config = babelrc_data(filename)
82
72
 
83
- result = transform(input[:data], options(input), paths: @env.paths)
84
-
85
- if result['metadata'].has_key?('targetsToProcess')
86
- result['metadata']['targetsToProcess'].each do |t|
87
- unless should_process?(t)
88
- raise ExcludedFileError, "#{t} was imported from #{filename} but this file won't be processed by Sprockets::Commoner"
89
- end
90
- end
73
+ result = input[:cache].fetch([filename, @cache_key, input[:data], babel_config]) do
74
+ transform(input[:data], options(input), paths: @env.paths)
91
75
  end
92
76
 
93
77
  if result['metadata'].has_key?('required')
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Commoner
3
- VERSION = '0.1.4'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -1,4 +1,5 @@
1
1
  require 'sprockets'
2
+ require 'sprockets/commoner/json_processor'
2
3
  require 'sprockets/commoner/processor'
3
4
  require 'sprockets/commoner/bundle'
4
5
 
@@ -7,6 +8,7 @@ module Sprockets
7
8
  end
8
9
 
9
10
  register_postprocessor 'application/javascript', ::Sprockets::Commoner::Processor
11
+ register_transformer 'application/json', 'application/javascript', ::Sprockets::Commoner::JSONProcessor
10
12
  register_bundle_metadata_reducer 'application/javascript', :commoner_enabled, false, :|
11
13
  register_bundle_metadata_reducer 'application/javascript', :commoner_used_helpers, Set.new, :+
12
14
  register_bundle_processor 'application/javascript', ::Sprockets::Commoner::Bundle
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
  spec.add_dependency "sprockets", ">= 3", "< 4"
30
- spec.add_dependency "schmooze", "~> 0.1.6"
30
+ spec.add_dependency "schmooze", "~> 0.1.5"
31
31
 
32
32
  spec.add_development_dependency "rake", "~> 10.0"
33
33
  spec.add_development_dependency "minitest", "~> 5.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.1.4
4
+ version: 0.2.0
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-05-26 00:00:00.000000000 Z
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -36,14 +36,14 @@ dependencies:
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.1.6
39
+ version: 0.1.5
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.1.6
46
+ version: 0.1.5
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -124,6 +124,7 @@ extra_rdoc_files: []
124
124
  files:
125
125
  - ".gitignore"
126
126
  - ".nvmrc"
127
+ - CHANGELOG.md
127
128
  - CODE_OF_CONDUCT.md
128
129
  - Gemfile
129
130
  - README.md
@@ -153,6 +154,7 @@ files:
153
154
  - js/babel-plugin-sprockets-commoner/package.json
154
155
  - lib/sprockets/commoner.rb
155
156
  - lib/sprockets/commoner/bundle.rb
157
+ - lib/sprockets/commoner/json_processor.rb
156
158
  - lib/sprockets/commoner/processor.rb
157
159
  - lib/sprockets/commoner/railtie.rb
158
160
  - lib/sprockets/commoner/version.rb
@@ -178,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
180
  version: '0'
179
181
  requirements: []
180
182
  rubyforge_project:
181
- rubygems_version: 2.4.5.1
183
+ rubygems_version: 2.5.1
182
184
  signing_key:
183
185
  specification_version: 4
184
186
  summary: Use Babel in Sprockets to compile modules for the browser