sprockets-commoner 0.5.1 → 0.6.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: 8c966579c74d914919277d60a4d2dc6f3738e4dc
4
- data.tar.gz: e9121bab180fabdc0ba4336149f58a91847bcde5
3
+ metadata.gz: b5d2ada3e40387a555b4b1be5c0284eeb8b82078
4
+ data.tar.gz: 00145faf36173d45a06d3fd4e8bd840b31a80c72
5
5
  SHA512:
6
- metadata.gz: 94ed97e08219884f90f18bbf8829ce061c9cc51d47adbeb21e3b3cf91209dc55b6307348f5e631a3d17f63658f07b369cae7420f5cf81d80185a0f7ef6b9baba
7
- data.tar.gz: 2bedd0b57bfcbaa99e8ddb6e52f876d344619ad6212e8e010dd434cb8665407621fed4ddc95829c4b4142a69f9e482b4f95ed855692196e4aca8c3921a20f7bb
6
+ metadata.gz: 609da3bec1c0a44a191105feafbe84e29bea5d299651c391b58b2fa8b76c867afccb1e44e4c8b7fe41d596305a6d955515a72a545d544cf313e2ae17fdec4f36
7
+ data.tar.gz: a46ce177fc4f3d19aa70c1bb872526da7251dc59866df310b2ccacd7064395f938c4cb1990c3a39407d66a926c96a0b752276ee8863918c5b13119300f22186a
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.6.0
4
+
5
+ * Adds support for resolving files in other bundles to global variables.
6
+
3
7
  ## v0.5.1
4
8
 
5
9
  * Make sure we error out if an identifier is requested from a CoffeeScript file and there is none present.
@@ -28,6 +28,7 @@ var dirname = require('path').dirname;
28
28
  var join = require('path').join;
29
29
  var resolve = require('browser-resolve').sync;
30
30
  var emptyModule = join(__dirname, 'node_modules', 'browser-resolve', 'empty.js');
31
+ var pathToIdentifier = require('./path-to-identifier');
31
32
 
32
33
  module.exports = function (context) {
33
34
  var t = context.types;
@@ -106,19 +107,6 @@ module.exports = function (context) {
106
107
  return null;
107
108
  }
108
109
 
109
-
110
- // Transform a path into a variable name
111
- function pathToIdentifier(path) {
112
- var escapedPath = path.replace(rootRegex, '').replace(/[^a-zA-Z0-9_]/g, function (match) {
113
- if (match === '/') {
114
- return '$';
115
- } else {
116
- return '_';
117
- }
118
- });
119
- return '__commoner_module__' + escapedPath;
120
- }
121
-
122
110
  function resolveTarget(file, path, ensureTargetIsProcessed) {
123
111
  var name = void 0;
124
112
  if (opts.globals != null && (name = opts.globals[path]) != null) {
@@ -145,7 +133,7 @@ module.exports = function (context) {
145
133
  file.metadata.targetsToProcess.push(resolvedPath);
146
134
  }
147
135
  // Otherwise we just look for the module by referencing its Special Identifier™.
148
- return pathToIdentifier(resolvedPath);
136
+ return pathToIdentifier(resolvedPath.replace(rootRegex, ''));
149
137
  }
150
138
  }
151
139
  }
@@ -265,10 +253,11 @@ module.exports = function (context) {
265
253
  state.file.metadata.commonerEnabled = true;
266
254
 
267
255
  var node = path.node;
268
- var identifier = pathToIdentifier(state.file.opts.filename);
256
+ var identifier = pathToIdentifier(state.file.opts.filename.replace(rootRegex, ''));
269
257
  var expose = findExpose(node.directives);
270
258
  if (expose != null) {
271
259
  node.body.push(exposeTemplate(t.identifier(expose)));
260
+ state.file.metadata.globalIdentifier = expose;
272
261
  }
273
262
 
274
263
  // Transform module to a variable assignment.
@@ -0,0 +1,11 @@
1
+ // Transform a path into a variable name
2
+ module.exports = function pathToIdentifier(path) {
3
+ var escapedPath = path.replace(/[^a-zA-Z0-9_]/g, function (match) {
4
+ if (match === '/') {
5
+ return '$';
6
+ } else {
7
+ return '_';
8
+ }
9
+ });
10
+ return '__commoner_module__' + escapedPath;
11
+ };
@@ -10,6 +10,7 @@ module Sprockets
10
10
  register_postprocessor 'application/javascript', ::Sprockets::Commoner::Processor
11
11
  register_transformer 'application/json', 'application/javascript', ::Sprockets::Commoner::JSONProcessor
12
12
  register_bundle_metadata_reducer 'application/javascript', :commoner_enabled, false, :|
13
+ register_bundle_metadata_reducer 'application/javascript', :commoner_required, Set.new, :+
13
14
  register_bundle_metadata_reducer 'application/javascript', :commoner_used_helpers, Set.new, :+
14
15
  register_bundle_processor 'application/javascript', ::Sprockets::Commoner::Bundle
15
16
  end
@@ -3,20 +3,27 @@ require 'schmooze'
3
3
  module Sprockets
4
4
  module Commoner
5
5
  class Bundle < Schmooze::Base
6
+
7
+ InaccessibleStubbedFileError = Class.new(::StandardError)
8
+
9
+ JS_PACKAGE_PATH = File.expand_path('../../../js', __dir__)
10
+
6
11
  dependencies generator: 'babel-generator.default',
7
12
  babelHelpers: 'babel-helpers',
8
- t: 'babel-types'
13
+ t: 'babel-types',
14
+ pathToIdentifier: 'babel-plugin-sprockets-commoner-internal/path-to-identifier'
9
15
 
10
- method :generate_helpers, <<-JS
11
- function(helpers) {
12
- if (helpers.length === 0) {
16
+ method :generate_header, <<-JS
17
+ function(helpers, globalIdentifiers) {
18
+ var declarators = helpers.map(function(helper) {
19
+ return t.variableDeclarator(t.identifier('__commoner_helper__' + helper), babelHelpers.get(helper));
20
+ }).concat(globalIdentifiers.map(function(item) {
21
+ return t.variableDeclarator(t.identifier(pathToIdentifier(item[0])), t.identifier(item[1]));
22
+ }));
23
+ if (declarators.length === 0) {
13
24
  return '';
14
25
  }
15
- var declaration = t.variableDeclaration('var',
16
- helpers.map(function(helper) {
17
- return t.variableDeclarator(t.identifier('__commoner_helper__' + helper), babelHelpers.get(helper));
18
- })
19
- );
26
+ var declaration = t.variableDeclaration('var', declarators);
20
27
  return generator(declaration).code;
21
28
  }
22
29
  JS
@@ -34,21 +41,40 @@ JS
34
41
  OUTRO = <<-JS.freeze
35
42
  }();
36
43
  JS
44
+ def initialize(root)
45
+ super(root, 'NODE_PATH' => JS_PACKAGE_PATH)
46
+ end
47
+
37
48
  def self.instance(env)
38
49
  @instance ||= new(env.root)
39
50
  end
40
51
 
41
52
  def self.call(input)
42
- instance(input[:environment]).call(input)
53
+ env = input[:environment]
54
+ instance(env).call(input)
43
55
  end
44
56
 
45
57
  def call(input)
46
58
  return unless input[:metadata][:commoner_enabled]
59
+ env = input[:environment]
60
+ # Get the filenames of all the assets that are included in the bundle
61
+ assets_in_bundle = Set.new(input[:metadata][:included]) { |uri| env.load(uri).filename }
62
+ # Subtract the assets in the bundle from those that are required. The missing assets were excluded through stubbing.
63
+ assets_missing = input[:metadata][:commoner_required] - assets_in_bundle
64
+
65
+ global_identifiers = assets_missing.map do |filename|
66
+ uri, _ = env.resolve(filename, type: input[:content_type], pipeline: :self, compat: false)
67
+ asset = env.load(uri)
68
+ # Retrieve the global variable the file is exposed through
69
+ global = asset.metadata[:commoner_global_identifier]
70
+ raise InaccessibleStubbedFileError, "#{filename} is stubbed in #{input[:filename]} but doesn't define a global. Add an 'expose' directive." if global.nil?
71
+ [filename.slice(env.root.size + 1, filename.size), global]
72
+ end
47
73
 
48
- used_helpers = input[:metadata][:commoner_used_helpers]
49
- helpers = generate_helpers(used_helpers.to_a)
74
+ used_helpers = input[:metadata][:commoner_used_helpers].to_a
75
+ header_code = generate_header(used_helpers, global_identifiers)
50
76
  {
51
- data: "#{PRELUDE}#{helpers}\n#{input[:data]}#{OUTRO}"
77
+ data: "#{PRELUDE}#{header_code}\n#{input[:data]}#{OUTRO}"
52
78
  }
53
79
  end
54
80
  end
@@ -87,19 +87,17 @@ module Sprockets
87
87
  babel_config = babelrc_data(filename)
88
88
  result = transform(input[:data], options(input), commoner_options(input))
89
89
 
90
- if result['metadata'].has_key?('targetsToProcess')
91
- result['metadata']['targetsToProcess'].each do |t|
92
- unless should_process?(t)
93
- raise ExcludedFileError, "#{t} was imported from #{filename} but this file won't be processed by Sprockets::Commoner"
94
- end
90
+ commoner_required = Set.new(input[:metadata][:commoner_required])
91
+ result['metadata']['targetsToProcess'].each do |t|
92
+ unless should_process?(t)
93
+ raise ExcludedFileError, "#{t} was imported from #{filename} but this file won't be processed by Sprockets::Commoner"
95
94
  end
95
+ commoner_required.add(t)
96
96
  end
97
97
 
98
- if result['metadata'].has_key?('required')
99
- result['metadata']['required'].each do |r|
100
- asset = resolve(r, accept: input[:content_type], pipeline: :self)
101
- @required.insert(insertion_index, asset)
102
- end
98
+ result['metadata']['required'].each do |r|
99
+ asset = resolve(r, accept: input[:content_type], pipeline: :self)
100
+ @required.insert(insertion_index, asset)
103
101
  end
104
102
 
105
103
  {
@@ -107,6 +105,8 @@ module Sprockets
107
105
  dependencies: @dependencies,
108
106
  required: Set.new(@required),
109
107
 
108
+ commoner_global_identifier: result['metadata']['globalIdentifier'],
109
+ commoner_required: commoner_required,
110
110
  commoner_used_helpers: Set.new(input[:metadata][:commoner_used_helpers]) + result['metadata']['usedHelpers'],
111
111
  commoner_enabled: input[:metadata][:commoner_enabled] | result['metadata']['commonerEnabled'],
112
112
  }
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Commoner
3
- VERSION = '0.5.1'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
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.5.1
4
+ version: 0.6.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-07-07 00:00:00.000000000 Z
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -153,6 +153,7 @@ files:
153
153
  - js/babel-plugin-sprockets-commoner-internal/node_modules/resolve/lib/sync.js
154
154
  - js/babel-plugin-sprockets-commoner-internal/node_modules/resolve/package.json
155
155
  - js/babel-plugin-sprockets-commoner-internal/package.json
156
+ - js/babel-plugin-sprockets-commoner-internal/path-to-identifier.js
156
157
  - lib/sprockets/commoner.rb
157
158
  - lib/sprockets/commoner/bundle.rb
158
159
  - lib/sprockets/commoner/json_processor.rb