sprockets 3.7.2 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +2 -295
  3. data/README.md +21 -35
  4. data/bin/sprockets +11 -8
  5. data/lib/rake/sprocketstask.rb +2 -2
  6. data/lib/sprockets.rb +79 -34
  7. data/lib/sprockets/asset.rb +8 -21
  8. data/lib/sprockets/autoload.rb +3 -0
  9. data/lib/sprockets/autoload/babel.rb +7 -0
  10. data/lib/sprockets/autoload/jsminc.rb +7 -0
  11. data/lib/sprockets/autoload/sassc.rb +7 -0
  12. data/lib/sprockets/babel_processor.rb +58 -0
  13. data/lib/sprockets/base.rb +8 -8
  14. data/lib/sprockets/bower.rb +4 -2
  15. data/lib/sprockets/bundle.rb +1 -1
  16. data/lib/sprockets/cache.rb +2 -4
  17. data/lib/sprockets/closure_compressor.rb +1 -2
  18. data/lib/sprockets/coffee_script_processor.rb +9 -3
  19. data/lib/sprockets/compressing.rb +2 -2
  20. data/lib/sprockets/configuration.rb +1 -7
  21. data/lib/sprockets/context.rb +10 -18
  22. data/lib/sprockets/digest_utils.rb +40 -52
  23. data/lib/sprockets/directive_processor.rb +10 -13
  24. data/lib/sprockets/http_utils.rb +19 -4
  25. data/lib/sprockets/jsminc_compressor.rb +31 -0
  26. data/lib/sprockets/jst_processor.rb +10 -10
  27. data/lib/sprockets/loader.rb +34 -28
  28. data/lib/sprockets/manifest.rb +3 -35
  29. data/lib/sprockets/manifest_utils.rb +0 -2
  30. data/lib/sprockets/mime.rb +7 -42
  31. data/lib/sprockets/path_dependency_utils.rb +2 -11
  32. data/lib/sprockets/path_digest_utils.rb +1 -1
  33. data/lib/sprockets/path_utils.rb +43 -18
  34. data/lib/sprockets/preprocessors/default_source_map.rb +24 -0
  35. data/lib/sprockets/processing.rb +30 -61
  36. data/lib/sprockets/processor_utils.rb +27 -28
  37. data/lib/sprockets/resolve.rb +172 -92
  38. data/lib/sprockets/sass_cache_store.rb +1 -6
  39. data/lib/sprockets/sass_compressor.rb +14 -1
  40. data/lib/sprockets/sass_processor.rb +18 -8
  41. data/lib/sprockets/sassc_compressor.rb +30 -0
  42. data/lib/sprockets/sassc_processor.rb +68 -0
  43. data/lib/sprockets/server.rb +9 -20
  44. data/lib/sprockets/source_map_comment_processor.rb +29 -0
  45. data/lib/sprockets/source_map_processor.rb +40 -0
  46. data/lib/sprockets/source_map_utils.rb +345 -0
  47. data/lib/sprockets/transformers.rb +62 -35
  48. data/lib/sprockets/uglifier_compressor.rb +12 -5
  49. data/lib/sprockets/unloaded_asset.rb +12 -11
  50. data/lib/sprockets/uri_tar.rb +4 -2
  51. data/lib/sprockets/uri_utils.rb +5 -5
  52. data/lib/sprockets/utils.rb +30 -78
  53. data/lib/sprockets/version.rb +1 -1
  54. metadata +62 -20
  55. data/LICENSE +0 -21
  56. data/lib/sprockets/coffee_script_template.rb +0 -17
  57. data/lib/sprockets/deprecation.rb +0 -90
  58. data/lib/sprockets/eco_template.rb +0 -17
  59. data/lib/sprockets/ejs_template.rb +0 -17
  60. data/lib/sprockets/engines.rb +0 -92
  61. data/lib/sprockets/erb_template.rb +0 -11
  62. data/lib/sprockets/legacy.rb +0 -330
  63. data/lib/sprockets/legacy_proc_processor.rb +0 -35
  64. data/lib/sprockets/legacy_tilt_processor.rb +0 -29
  65. data/lib/sprockets/sass_template.rb +0 -19
@@ -124,7 +124,7 @@ module Rake
124
124
  end
125
125
  end
126
126
 
127
- task :clobber => ["clobber_#{name}"]
127
+ task clobber: ["clobber_#{name}"]
128
128
 
129
129
  desc name == :assets ? "Clean old assets" : "Clean old #{name} assets"
130
130
  task "clean_#{name}" do
@@ -133,7 +133,7 @@ module Rake
133
133
  end
134
134
  end
135
135
 
136
- task :clean => ["clean_#{name}"]
136
+ task clean: ["clean_#{name}"]
137
137
  end
138
138
 
139
139
  private
@@ -4,7 +4,6 @@ require 'sprockets/cache'
4
4
  require 'sprockets/environment'
5
5
  require 'sprockets/errors'
6
6
  require 'sprockets/manifest'
7
- require 'sprockets/deprecation'
8
7
 
9
8
  module Sprockets
10
9
  require 'sprockets/processor_utils'
@@ -23,21 +22,19 @@ module Sprockets
23
22
  dependencies: Set.new.freeze,
24
23
  dependency_resolvers: {}.freeze,
25
24
  digest_class: Digest::SHA256,
26
- engine_mime_types: {}.freeze,
27
- engines: {}.freeze,
28
25
  mime_exts: {}.freeze,
29
26
  mime_types: {}.freeze,
30
27
  paths: [].freeze,
31
28
  pipelines: {}.freeze,
29
+ pipeline_exts: {}.freeze,
32
30
  postprocessors: Hash.new { |h, k| [].freeze }.freeze,
33
31
  preprocessors: Hash.new { |h, k| [].freeze }.freeze,
34
- registered_transformers: Hash.new { |h, k| {}.freeze }.freeze,
35
- root: File.expand_path('..', __FILE__).freeze,
32
+ registered_transformers: [].freeze,
33
+ root: __dir__.dup.freeze,
36
34
  transformers: Hash.new { |h, k| {}.freeze }.freeze,
37
35
  version: "",
38
36
  gzip_enabled: true
39
37
  }.freeze
40
- self.computed_config = {}
41
38
 
42
39
  @context_class = Context
43
40
 
@@ -48,6 +45,7 @@ module Sprockets
48
45
  # Common asset text types
49
46
  register_mime_type 'application/javascript', extensions: ['.js'], charset: :unicode
50
47
  register_mime_type 'application/json', extensions: ['.json'], charset: :unicode
48
+ register_mime_type 'application/ruby', extensions: ['.rb']
51
49
  register_mime_type 'application/xml', extensions: ['.xml']
52
50
  register_mime_type 'text/css', extensions: ['.css'], charset: :css
53
51
  register_mime_type 'text/html', extensions: ['.html', '.htm'], charset: :html
@@ -80,26 +78,44 @@ module Sprockets
80
78
  register_mime_type 'application/x-font-opentype', extensions: ['.otf']
81
79
  register_mime_type 'application/x-font-ttf', extensions: ['.ttf']
82
80
  register_mime_type 'application/font-woff', extensions: ['.woff']
81
+ register_mime_type 'application/font-woff2', extensions: ['.woff2']
82
+
83
+ require 'sprockets/source_map_processor'
84
+ register_mime_type 'application/js-sourcemap+json', extensions: ['.js.map']
85
+ register_mime_type 'application/css-sourcemap+json', extensions: ['.css.map']
86
+ register_transformer 'application/javascript', 'application/js-sourcemap+json', SourceMapProcessor
87
+ register_transformer 'text/css', 'application/css-sourcemap+json', SourceMapProcessor
83
88
 
84
89
  register_pipeline :source do |env|
85
90
  []
86
91
  end
87
92
 
88
- register_pipeline :self do |env, type, file_type, engine_extnames|
89
- env.self_processors_for(type, file_type, engine_extnames)
93
+ register_pipeline :self do |env, type, file_type|
94
+ env.self_processors_for(type, file_type)
95
+ end
96
+
97
+ register_pipeline :default do |env, type, file_type|
98
+ # TODO: Hack for to inject source map transformer
99
+ if (type == "application/js-sourcemap+json" && file_type != "application/js-sourcemap+json") ||
100
+ (type == "application/css-sourcemap+json" && file_type != "application/css-sourcemap+json")
101
+ [SourceMapProcessor]
102
+ else
103
+ env.default_processors_for(type, file_type)
104
+ end
90
105
  end
91
106
 
92
- register_pipeline :default do |env, type, file_type, engine_extnames|
93
- env.default_processors_for(type, file_type, engine_extnames)
107
+ require 'sprockets/source_map_comment_processor'
108
+ register_pipeline :debug do
109
+ [SourceMapCommentProcessor]
94
110
  end
95
111
 
112
+ require 'sprockets/preprocessors/default_source_map'
113
+ register_preprocessor 'text/css', Preprocessors::DefaultSourceMap.new
114
+ register_preprocessor 'application/javascript', Preprocessors::DefaultSourceMap.new
115
+
96
116
  require 'sprockets/directive_processor'
97
- register_preprocessor 'text/css', DirectiveProcessor.new(
98
- comments: ["//", ["/*", "*/"]]
99
- )
100
- register_preprocessor 'application/javascript', DirectiveProcessor.new(
101
- comments: ["//", ["/*", "*/"]] + ["#", ["###", "###"]]
102
- )
117
+ register_preprocessor 'text/css', DirectiveProcessor.new(comments: ["//", ["/*", "*/"]])
118
+ register_preprocessor 'application/javascript', DirectiveProcessor.new(comments: ["//", ["/*", "*/"]])
103
119
 
104
120
  require 'sprockets/bundle'
105
121
  register_bundle_processor 'application/javascript', Bundle
@@ -108,46 +124,77 @@ module Sprockets
108
124
  register_bundle_metadata_reducer '*/*', :data, proc { "" }, :concat
109
125
  register_bundle_metadata_reducer 'application/javascript', :data, proc { "" }, Utils.method(:concat_javascript_sources)
110
126
  register_bundle_metadata_reducer '*/*', :links, :+
127
+ register_bundle_metadata_reducer '*/*', :map, SourceMapUtils.method(:concat_source_maps)
111
128
 
112
129
  require 'sprockets/closure_compressor'
113
130
  require 'sprockets/sass_compressor'
131
+ require 'sprockets/sassc_compressor'
132
+ require 'sprockets/jsminc_compressor'
114
133
  require 'sprockets/uglifier_compressor'
115
134
  require 'sprockets/yui_compressor'
116
135
  register_compressor 'text/css', :sass, SassCompressor
136
+ register_compressor 'text/css', :sassc, SasscCompressor
117
137
  register_compressor 'text/css', :scss, SassCompressor
138
+ register_compressor 'text/css', :scssc, SasscCompressor
118
139
  register_compressor 'text/css', :yui, YUICompressor
119
140
  register_compressor 'application/javascript', :closure, ClosureCompressor
141
+ register_compressor 'application/javascript', :jsmin, JSMincCompressor
142
+ register_compressor 'application/javascript', :jsminc, JSMincCompressor
120
143
  register_compressor 'application/javascript', :uglifier, UglifierCompressor
121
144
  register_compressor 'application/javascript', :uglify, UglifierCompressor
122
145
  register_compressor 'application/javascript', :yui, YUICompressor
123
146
 
147
+ # Babel, TheFuture™ is now
148
+ require 'sprockets/babel_processor'
149
+ register_mime_type 'application/ecmascript-6', extensions: ['.es6'], charset: :unicode
150
+ register_transformer 'application/ecmascript-6', 'application/javascript', BabelProcessor
151
+ register_preprocessor 'application/ecmascript-6', DirectiveProcessor.new(comments: ["//", ["/*", "*/"]])
152
+
124
153
  # Mmm, CoffeeScript
125
154
  require 'sprockets/coffee_script_processor'
126
- Deprecation.silence do
127
- register_engine '.coffee', CoffeeScriptProcessor, mime_type: 'application/javascript', silence_deprecation: true
128
- end
155
+ register_mime_type 'text/coffeescript', extensions: ['.coffee', '.js.coffee']
156
+ register_transformer 'text/coffeescript', 'application/javascript', CoffeeScriptProcessor
157
+ register_preprocessor 'text/coffeescript', DirectiveProcessor.new(comments: ["#", ["###", "###"]])
129
158
 
130
- # JST engines
159
+ # JST processors
131
160
  require 'sprockets/eco_processor'
132
161
  require 'sprockets/ejs_processor'
133
162
  require 'sprockets/jst_processor'
134
- Deprecation.silence do
135
- register_engine '.jst', JstProcessor, mime_type: 'application/javascript', silence_deprecation: true
136
- register_engine '.eco', EcoProcessor, mime_type: 'application/javascript', silence_deprecation: true
137
- register_engine '.ejs', EjsProcessor, mime_type: 'application/javascript', silence_deprecation: true
138
- end
163
+ register_mime_type 'text/eco', extensions: ['.eco', '.jst.eco']
164
+ register_mime_type 'text/ejs', extensions: ['.ejs', '.jst.ejs']
165
+ register_transformer 'text/eco', 'application/javascript+function', EcoProcessor
166
+ register_transformer 'text/ejs', 'application/javascript+function', EjsProcessor
167
+ register_transformer 'application/javascript+function', 'application/javascript', JstProcessor
139
168
 
140
- # CSS engines
169
+ # CSS processors
141
170
  require 'sprockets/sass_processor'
142
- Deprecation.silence do
143
- register_engine '.sass', SassProcessor, mime_type: 'text/css', silence_deprecation: true
144
- register_engine '.scss', ScssProcessor, mime_type: 'text/css', silence_deprecation: true
145
- end
171
+ register_mime_type 'text/sass', extensions: ['.sass', '.css.sass']
172
+ register_mime_type 'text/scss', extensions: ['.scss', '.css.scss']
173
+ register_transformer 'text/sass', 'text/css', SassProcessor
174
+ register_transformer 'text/scss', 'text/css', ScssProcessor
175
+ register_preprocessor 'text/sass', DirectiveProcessor.new(comments: ["//", ["/*", "*/"]])
176
+ register_preprocessor 'text/scss', DirectiveProcessor.new(comments: ["//", ["/*", "*/"]])
146
177
  register_bundle_metadata_reducer 'text/css', :sass_dependencies, Set.new, :+
147
178
 
148
- # Other
179
+ # ERB
149
180
  require 'sprockets/erb_processor'
150
- register_engine '.erb', ERBProcessor, mime_type: 'text/plain', silence_deprecation: true
181
+ register_transformer_suffix(%w(
182
+ application/ecmascript-6
183
+ application/javascript
184
+ application/json
185
+ application/xml
186
+ text/coffeescript
187
+ text/css
188
+ text/html
189
+ text/plain
190
+ text/sass
191
+ text/scss
192
+ text/yaml
193
+ ), 'application/\2+ruby', '.erb', ERBProcessor)
194
+
195
+ register_mime_type 'application/html+ruby', extensions: ['.html.erb', '.erb', '.rhtml'], charset: :html
196
+ register_mime_type 'application/xml+ruby', extensions: ['.xml.erb', '.rxml']
197
+
151
198
 
152
199
  register_dependency_resolver 'environment-version' do |env|
153
200
  env.version
@@ -165,5 +212,3 @@ module Sprockets
165
212
  depend_on 'environment-version'
166
213
  depend_on 'environment-paths'
167
214
  end
168
-
169
- require 'sprockets/legacy'
@@ -13,8 +13,7 @@ module Sprockets
13
13
  # attributes - Hash of ivars
14
14
  #
15
15
  # Returns Asset.
16
- def initialize(environment, attributes = {})
17
- @environment = environment
16
+ def initialize(attributes = {})
18
17
  @attributes = attributes
19
18
  @content_type = attributes[:content_type]
20
19
  @filename = attributes[:filename]
@@ -22,7 +21,6 @@ module Sprockets
22
21
  @load_path = attributes[:load_path]
23
22
  @logical_path = attributes[:logical_path]
24
23
  @metadata = attributes[:metadata]
25
- @mtime = attributes[:mtime]
26
24
  @name = attributes[:name]
27
25
  @source = attributes[:source]
28
26
  @uri = attributes[:uri]
@@ -80,14 +78,6 @@ module Sprockets
80
78
  metadata[:links] || Set.new
81
79
  end
82
80
 
83
- # Public: Get all internally required assets that were concated into this
84
- # asset.
85
- #
86
- # Returns Array of String asset URIs.
87
- def included
88
- metadata[:included]
89
- end
90
-
91
81
  # Public: Return `String` of concatenated source.
92
82
  #
93
83
  # Returns String.
@@ -120,22 +110,22 @@ module Sprockets
120
110
  end
121
111
  alias_method :bytesize, :length
122
112
 
113
+ # Public: Returns String byte digest of source.
114
+ def digest
115
+ metadata[:digest]
116
+ end
117
+
123
118
  # Public: Returns String hexdigest of source.
124
119
  def hexdigest
125
- DigestUtils.pack_hexdigest(metadata[:digest])
120
+ DigestUtils.pack_hexdigest(digest)
126
121
  end
127
122
 
128
- # Deprecated: Returns String hexdigest of source.
129
- #
130
- # In 4.x this will be changed to return a raw Digest byte String.
131
- alias_method :digest, :hexdigest
132
-
133
123
  # Pubic: ETag String of Asset.
134
124
  alias_method :etag, :hexdigest
135
125
 
136
126
  # Public: Returns String base64 digest of source.
137
127
  def base64digest
138
- DigestUtils.pack_base64digest(metadata[:digest])
128
+ DigestUtils.pack_base64digest(digest)
139
129
  end
140
130
 
141
131
  # Public: A "named information" URL for subresource integrity.
@@ -166,9 +156,6 @@ module Sprockets
166
156
  f.write source
167
157
  end
168
158
 
169
- # Set mtime correctly
170
- File.utime(mtime, mtime, filename)
171
-
172
159
  nil
173
160
  end
174
161
 
@@ -1,10 +1,13 @@
1
1
  module Sprockets
2
2
  module Autoload
3
+ autoload :Babel, 'sprockets/autoload/babel'
3
4
  autoload :Closure, 'sprockets/autoload/closure'
4
5
  autoload :CoffeeScript, 'sprockets/autoload/coffee_script'
5
6
  autoload :Eco, 'sprockets/autoload/eco'
6
7
  autoload :EJS, 'sprockets/autoload/ejs'
8
+ autoload :JSMinC, 'sprockets/autoload/jsminc'
7
9
  autoload :Sass, 'sprockets/autoload/sass'
10
+ autoload :SassC, 'sprockets/autoload/sassc'
8
11
  autoload :Uglifier, 'sprockets/autoload/uglifier'
9
12
  autoload :YUI, 'sprockets/autoload/yui'
10
13
  end
@@ -0,0 +1,7 @@
1
+ require 'babel/transpiler'
2
+
3
+ module Sprockets
4
+ module Autoload
5
+ Babel = ::Babel
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'jsminc'
2
+
3
+ module Sprockets
4
+ module Autoload
5
+ JSMinC = ::JSMinC
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'sassc'
2
+
3
+ module Sprockets
4
+ module Autoload
5
+ SassC = ::SassC
6
+ end
7
+ end
@@ -0,0 +1,58 @@
1
+ require 'sprockets/autoload'
2
+ require 'sprockets/path_utils'
3
+ require 'sprockets/source_map_utils'
4
+ require 'json'
5
+
6
+ module Sprockets
7
+ class BabelProcessor
8
+ VERSION = '1'
9
+
10
+ def self.instance
11
+ @instance ||= new
12
+ end
13
+
14
+ def self.call(input)
15
+ instance.call(input)
16
+ end
17
+
18
+ def initialize(options = {})
19
+ @options = options.merge({
20
+ 'blacklist' => (options['blacklist'] || []) + ['useStrict'],
21
+ 'sourceMap' => true
22
+ }).freeze
23
+
24
+ @cache_key = [
25
+ self.class.name,
26
+ Autoload::Babel::Transpiler::VERSION,
27
+ Autoload::Babel::Source::VERSION,
28
+ VERSION,
29
+ @options
30
+ ].freeze
31
+ end
32
+
33
+ def call(input)
34
+ data = input[:data]
35
+
36
+ result = input[:cache].fetch(@cache_key + [input[:filename]] + [data]) do
37
+ opts = {
38
+ 'sourceRoot' => input[:load_path],
39
+ 'moduleRoot' => nil,
40
+ 'filename' => input[:filename],
41
+ 'filenameRelative' => PathUtils.split_subpath(input[:load_path], input[:filename])
42
+ }.merge(@options)
43
+
44
+ if opts['moduleIds'] && opts['moduleRoot']
45
+ opts['moduleId'] ||= File.join(opts['moduleRoot'], input[:name])
46
+ elsif opts['moduleIds']
47
+ opts['moduleId'] ||= input[:name]
48
+ end
49
+ Autoload::Babel::Transpiler.transform(data, opts)
50
+ end
51
+
52
+ map = SourceMapUtils.decode_json_source_map(JSON.generate(result['map']))
53
+ map = SourceMapUtils.combine_source_maps(input[:metadata][:map], map["mappings"])
54
+
55
+ { data: result['code'], map: map }
56
+ end
57
+ end
58
+ end
@@ -5,18 +5,18 @@ require 'sprockets/configuration'
5
5
  require 'sprockets/digest_utils'
6
6
  require 'sprockets/errors'
7
7
  require 'sprockets/loader'
8
- require 'sprockets/path_digest_utils'
9
8
  require 'sprockets/path_dependency_utils'
9
+ require 'sprockets/path_digest_utils'
10
10
  require 'sprockets/path_utils'
11
11
  require 'sprockets/resolve'
12
12
  require 'sprockets/server'
13
- require 'sprockets/loader'
13
+ require 'sprockets/source_map_utils'
14
14
  require 'sprockets/uri_tar'
15
15
 
16
16
  module Sprockets
17
17
  # `Base` class for `Environment` and `Cached`.
18
18
  class Base
19
- include PathUtils, PathDependencyUtils, PathDigestUtils, DigestUtils
19
+ include PathUtils, PathDependencyUtils, PathDigestUtils, DigestUtils, SourceMapUtils
20
20
  include Configuration
21
21
  include Server
22
22
  include Resolve, Loader
@@ -60,17 +60,17 @@ module Sprockets
60
60
  end
61
61
 
62
62
  # Find asset by logical path or expanded path.
63
- def find_asset(path, options = {})
64
- uri, _ = resolve(path, options.merge(compat: false))
63
+ def find_asset(*args)
64
+ uri, _ = resolve(*args)
65
65
  if uri
66
66
  load(uri)
67
67
  end
68
68
  end
69
69
 
70
- def find_all_linked_assets(path, options = {})
71
- return to_enum(__method__, path, options) unless block_given?
70
+ def find_all_linked_assets(*args)
71
+ return to_enum(__method__, *args) unless block_given?
72
72
 
73
- asset = find_asset(path, options)
73
+ asset = find_asset(*args)
74
74
  return unless asset
75
75
 
76
76
  yield asset
@@ -17,7 +17,7 @@ module Sprockets
17
17
  candidates, deps = super
18
18
 
19
19
  # bower.json can only be nested one level deep
20
- if !logical_path.index('/')
20
+ if !logical_path.index('/'.freeze)
21
21
  dirname = File.join(load_path, logical_path)
22
22
 
23
23
  if directory?(dirname)
@@ -27,7 +27,9 @@ module Sprockets
27
27
  if filename
28
28
  deps << build_file_digest_uri(filename)
29
29
  read_bower_main(dirname, filename) do |path|
30
- candidates << path
30
+ if file?(path)
31
+ candidates << path
32
+ end
31
33
  end
32
34
  end
33
35
  end
@@ -17,7 +17,7 @@ module Sprockets
17
17
  type = input[:content_type]
18
18
  dependencies = Set.new(input[:metadata][:dependencies])
19
19
 
20
- processed_uri, deps = env.resolve(input[:filename], accept: type, pipeline: :self, compat: false)
20
+ processed_uri, deps = env.resolve(input[:filename], accept: type, pipeline: :self)
21
21
  dependencies.merge(deps)
22
22
 
23
23
  find_required = proc { |uri| env.load(uri).metadata[:required] }