sprockets-iife 1.1.0 → 1.1.1

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: 9054960a6ef80b43a4d1c456cdb5c8bbb5d8a9d9
4
- data.tar.gz: b19de6947d03fedc86186bc391c5049eb6c5d21e
3
+ metadata.gz: dc9c42d81e929c3447ccd9d074e7359dec690652
4
+ data.tar.gz: 36c33c227d7493971368b18078ca0bd5f44c2da4
5
5
  SHA512:
6
- metadata.gz: 048f6a8ea56c515e51a30e7f2ee40519315df7da21b83da343b8050f24601324f41ca5ba42a8bf0b73ce5e21dedfead6fe83cc088c51598e5910699292b09144
7
- data.tar.gz: 6bbc62993e0efd4ed04a5e08253936f43ec8f944957687e031051071dfe2667c1ff20bd28ab929ab56a5062216eb067450fa5dd5ff53ff5258ac65e1e27e1d87
6
+ metadata.gz: 1438e7d82e2a29efcfacb889e67b60efed30a4045734e407e131af506ac359ab8f034c518dc75956bc5acd1ca3f9d0355c85bcfb65dc196f85115f6dce3ffbe4
7
+ data.tar.gz: c25d1c3bfff8429fe6254f50608308ff3840e52ec4807295b35d3de9956978dbab14913b8b1c30a9887167dc407f75f7227e1daa24e0b23275703c0a63289169
@@ -3,5 +3,8 @@
3
3
 
4
4
  require 'sprockets'
5
5
  require 'sprockets-iife/wrap'
6
- require 'sprockets-iife/processor'
7
- require 'sprockets-iife/railtie' if defined?(Rails)
6
+ require 'sprockets-iife/template'
7
+ require 'sprockets-iife/utils'
8
+ require 'sprockets-iife/item_processor'
9
+ require 'sprockets-iife/bundle_processor'
10
+ require 'sprockets-iife/railtie'
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module SprocketsIIFE
5
+ class BundleProcessor
6
+ def initialize(script_path, &block)
7
+ @script_path = script_path
8
+ @script_source = block.call
9
+ end
10
+
11
+ def render(_, _)
12
+ self.class.wrap(@script_path, @script_source)
13
+ end
14
+
15
+ class << self
16
+ def call(input)
17
+ script_path = input[:filename]
18
+ script_source = input[:data]
19
+ context = input[:environment].context_class.new(input)
20
+ context.metadata.merge(data: wrap(script_path, script_source))
21
+ end
22
+
23
+ def wrap(script_path, script_source)
24
+ script_iife_path = SprocketsIIFE::Utils.build_iife_path(script_path)
25
+
26
+ if File.readable?(script_iife_path) && SprocketsIIFE::Utils.bundle?(script_path)
27
+ SprocketsIIFE::Template.new(script_iife_path, script_source).result
28
+ else
29
+ script_source
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module SprocketsIIFE
5
+ class ItemProcessor
6
+ def initialize(script_path, &block)
7
+ @script_path = script_path
8
+ @script_source = block.call
9
+ end
10
+
11
+ def render(script_context, _)
12
+ script_path = @script_path
13
+ script_source = @script_source
14
+ script_iife_path = SprocketsIIFE::Utils.build_iife_path(script_path)
15
+
16
+ if File.readable?(script_iife_path) && !SprocketsIIFE::Utils.bundle?(script_path)
17
+ script_context.depend_on(script_iife_path)
18
+
19
+ # noinspection RubyResolve
20
+ script_requires = script_context._required_paths.to_a
21
+ script_source_included = false
22
+
23
+ script_assets = script_requires.map do |path|
24
+ if path.include?(script_path)
25
+ script_source_included = true
26
+ script_source
27
+ else
28
+ script_context.environment.find_asset(path)
29
+ end
30
+ end
31
+
32
+ script_assets << script_source unless script_source_included
33
+
34
+ script_source = script_assets.map(&:to_s).join('')
35
+
36
+ script_context._required_paths.clear
37
+
38
+ SprocketsIIFE.wrap(script_path, script_source)
39
+ else
40
+ script_source
41
+ end
42
+ end
43
+
44
+ class << self
45
+ def call(input)
46
+ script_path = input[:filename]
47
+ script_source = input[:data]
48
+ script_iife_path = SprocketsIIFE::Utils.build_iife_path(script_path)
49
+ script_context = input[:environment].context_class.new(input)
50
+
51
+ if File.readable?(script_iife_path) && !SprocketsIIFE::Utils.bundle?(script_path)
52
+ script_context.depend_on(script_iife_path)
53
+
54
+ script_requires = script_context.metadata[:required]
55
+ script_source_included = false
56
+
57
+ script_assets = script_requires.map do |uri|
58
+ if uri.include?(script_path)
59
+ script_source_included = true
60
+ script_source
61
+ else
62
+ # noinspection RubyResolve
63
+ script_context.environment.load(uri)
64
+ end
65
+ end
66
+
67
+ script_assets << script_source unless script_source_included
68
+
69
+ script_source = script_assets.map(&:to_s).join('')
70
+
71
+ script_context.metadata.merge(required: [].to_set,
72
+ data: SprocketsIIFE.wrap(script_path, script_source))
73
+ else
74
+ script_context.metadata.merge(data: script_source)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -7,7 +7,7 @@ module SprocketsIIFE
7
7
  class Railtie < Rails::Railtie
8
8
  def configure_assets(app)
9
9
  if config.respond_to?(:assets) && config.assets.respond_to?(:configure)
10
- # Rails 4.x
10
+ # Rails 4.x 5.x
11
11
  config.assets.configure { |env| yield(env) }
12
12
  else
13
13
  # Rails 3.2
@@ -19,7 +19,6 @@ module SprocketsIIFE
19
19
  configure_assets(app) do |env|
20
20
  # Sprockets 2, 3, and 4
21
21
  env.register_bundle_processor 'application/javascript', SprocketsIIFE::BundleProcessor
22
- env.register_mime_type 'application/javascript', extensions: ['.js']
23
22
  env.register_postprocessor 'application/javascript', SprocketsIIFE::ItemProcessor
24
23
  end
25
24
  end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module SprocketsIIFE
5
+ module Utils
6
+ class << self
7
+ def bundle?(script_name_or_path)
8
+ script_name = "#{File.basename(script_name_or_path, '.*')}.js"
9
+ Rails.application.config.assets.precompile.any? do |x|
10
+ case x
11
+ when String then x == script_name
12
+ when Regexp then x =~ script_name
13
+ else false
14
+ end
15
+ end
16
+ end
17
+
18
+ def build_iife_path(script_path)
19
+ File.join(File.dirname(script_path), "#{File.basename(script_path, '.*')}-iife.js.erb")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module SprocketsIIFE
5
- VERSION = '1.1.0'
5
+ VERSION = '1.1.1'
6
6
  end
@@ -1,18 +1,11 @@
1
1
  # encoding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'sprockets-iife/template'
5
-
6
4
  module SprocketsIIFE
7
5
  class << self
8
6
  def wrap(script_path, script_source)
9
- script_iife_path = File.join(File.dirname(script_path), "#{File.basename(script_path, '.*')}-iife.js.erb")
10
-
11
- if File.readable?(script_iife_path)
12
- Template.new(script_iife_path, script_source).result
13
- else
14
- script_source
15
- end
7
+ script_iife_path = SprocketsIIFE::Utils.build_iife_path(script_path)
8
+ SprocketsIIFE::Template.new(script_iife_path, script_source).result
16
9
  end
17
10
  end
18
11
  end
@@ -1,6 +1,5 @@
1
1
  #= require_self
2
2
  #= require ./mixedbundle/foo
3
3
  #= require ./mixedbundle/bar
4
- #= require ./mixedbundle/baz
5
4
 
6
5
  mixedbundle.coffee()
@@ -1 +1,4 @@
1
+ #= require_self
2
+ #= require ./baz
3
+
1
4
  bar.coffee()
@@ -50,13 +50,19 @@ class SprocketsIIFETest < Test::Unit::TestCase
50
50
  expected = <<-JAVASCRIPT.squish
51
51
  (function(/* mixedbundle-iife */) {
52
52
  (function() { mixedbundle.coffee(); }).call(this);
53
+
53
54
  foo.js();
55
+
54
56
  (function(/* bar-iife */) {
55
- (function() { bar.coffee(); }).call(this);
56
- }).call(this);
57
- (function(/* baz-iife */) {
58
- baz.js();
57
+ (function() {
58
+ bar.coffee();
59
+ }).call(this);
60
+
61
+ (function(/* baz-iife */) {
62
+ baz.js();
63
+ }).call(this);
59
64
  }).call(this);
65
+
60
66
  }).call(this);
61
67
  JAVASCRIPT
62
68
  assert_equal expected, app.assets['mixedbundle.js'].to_s.squish
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-iife
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Konoplov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-22 00:00:00.000000000 Z
11
+ date: 2016-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sprockets
@@ -117,9 +117,11 @@ files:
117
117
  - gemfiles/rails_4_with_sprockets_3.gemfile
118
118
  - gemfiles/rails_5.gemfile
119
119
  - lib/sprockets-iife.rb
120
- - lib/sprockets-iife/processor.rb
120
+ - lib/sprockets-iife/bundle_processor.rb
121
+ - lib/sprockets-iife/item_processor.rb
121
122
  - lib/sprockets-iife/railtie.rb
122
123
  - lib/sprockets-iife/template.rb
124
+ - lib/sprockets-iife/utils.rb
123
125
  - lib/sprockets-iife/version.rb
124
126
  - lib/sprockets-iife/wrap.rb
125
127
  - sprockets-iife.gemspec
@@ -1,57 +0,0 @@
1
- # encoding: utf-8
2
- # frozen_string_literal: true
3
-
4
- module SprocketsIIFE
5
- class AbstractProcessor
6
- def initialize(script_path, &block)
7
- @script_path = script_path
8
- @script_source = block.call
9
- end
10
-
11
- def render(_, _)
12
- self.class.wrap(@script_path, @script_source)
13
- end
14
-
15
- class << self
16
- def call(input)
17
- script_path = input[:filename]
18
- script_source = input[:data]
19
- context = input[:environment].context_class.new(input)
20
- context.metadata.merge(data: wrap(script_path, script_source))
21
- end
22
-
23
- def wrap(script_path, script_source)
24
- raise NotImplementedError
25
- end
26
- end
27
- end
28
-
29
- class ItemProcessor < AbstractProcessor
30
- class << self
31
- def wrap(script_path, script_source)
32
- script_name = "#{File.basename(script_path, '.*')}.js"
33
- seems_to_be_a_bundle = Rails.application.config.assets.precompile.any? do |x|
34
- case x
35
- when String then x == script_name
36
- when Regexp then x =~ script_name
37
- else false
38
- end
39
- end
40
-
41
- if seems_to_be_a_bundle
42
- script_source
43
- else
44
- SprocketsIIFE.wrap(script_path, script_source)
45
- end
46
- end
47
- end
48
- end
49
-
50
- class BundleProcessor < AbstractProcessor
51
- class << self
52
- def wrap(script_path, script_source)
53
- SprocketsIIFE.wrap(script_path, script_source)
54
- end
55
- end
56
- end
57
- end