condenser 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/lib/condenser.rb +4 -6
  3. data/lib/condenser/asset.rb +116 -69
  4. data/lib/condenser/build_cache.rb +107 -0
  5. data/lib/condenser/cache/file_store.rb +1 -3
  6. data/lib/condenser/environment.rb +1 -1
  7. data/lib/condenser/processors/babel_processor.rb +1 -1
  8. data/lib/condenser/processors/node_modules/@types/estree/LICENSE +21 -21
  9. data/lib/condenser/processors/node_modules/@types/estree/README.md +16 -16
  10. data/lib/condenser/processors/node_modules/@types/node/LICENSE +21 -21
  11. data/lib/condenser/processors/node_modules/@types/node/README.md +16 -16
  12. data/lib/condenser/processors/node_modules/@types/resolve/README.md +16 -16
  13. data/lib/condenser/processors/node_modules/babel-plugin-transform-class-extended-hook/test/index.js +107 -107
  14. data/lib/condenser/processors/node_modules/color-name/.npmignore +106 -106
  15. data/lib/condenser/processors/node_modules/color-name/LICENSE +7 -7
  16. data/lib/condenser/processors/node_modules/color-name/README.md +11 -11
  17. data/lib/condenser/processors/node_modules/color-name/index.js +152 -152
  18. data/lib/condenser/processors/node_modules/color-name/test.js +7 -7
  19. data/lib/condenser/processors/node_modules/is-reference/node_modules/@types/estree/LICENSE +21 -21
  20. data/lib/condenser/processors/node_modules/is-reference/node_modules/@types/estree/README.md +16 -16
  21. data/lib/condenser/processors/node_modules/is-reference/node_modules/@types/estree/index.d.ts +548 -548
  22. data/lib/condenser/processors/rollup_processor.rb +0 -8
  23. data/lib/condenser/resolve.rb +80 -60
  24. data/lib/condenser/transformers/sass_transformer.rb +1 -1
  25. data/lib/condenser/transformers/sass_transformer/importer.rb +1 -1
  26. data/lib/condenser/version.rb +1 -1
  27. data/test/cache_test.rb +72 -0
  28. data/test/test_helper.rb +1 -1
  29. data/test/transformers/scss_test.rb +2 -1
  30. metadata +17 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a3307e3793ca525115048d1d418cccdd1ec5727171475fc4909a15b178ac14b
4
- data.tar.gz: b991118f195efe01eaae6960862c1101e273f86f3bb1a3ccad009c3ad98f213d
3
+ metadata.gz: d330a97b14eba56db0e2404b7b5c8bc95880c5a17ef8886bc0fed653311dcb0f
4
+ data.tar.gz: 8b67113289b161903a55be74073ad979580710fd557f5ad97c6c1e65ef903200
5
5
  SHA512:
6
- metadata.gz: ce1137d43308c657fffaff3620a6a6095cf961d697a7c56c85dc685821f637b8e6efe73bfa341ae810b467170fa4ccc6b8e8a7a76cca1e42f24e4e112b47418d
7
- data.tar.gz: ffc9df1b3a0222888272d7bcff193854ca4eae42d379d974cbf377cd2302aca24bc839ba82de5ae49efa965e64b455523e19cb06845b936e47c1a115d30f16f8
6
+ metadata.gz: df64dbbbb0ec741c7d4af17d71d4dd0e855fc82f13220775353c361ae4da0254cd233fbeb143bca2536c8331b425c67b3247151fc59214fef5a34b8c1a13880d
7
+ data.tar.gz: 563064dd6255fb14cbd7bed0ae7bbc6bd64a1c019e7ed3f2837c449c05041333b5c4aca91391e918fbf8b99d4aa7e0725b120cb30819467f193ddd48347cb607
@@ -11,13 +11,10 @@ require 'condenser/encoding_utils'
11
11
  require 'condenser/asset'
12
12
  require 'condenser/manifest'
13
13
 
14
- require 'active_support/benchmarkable'
15
-
16
14
  class Condenser
17
-
18
- include ActiveSupport::Benchmarkable
15
+
19
16
  prepend Environment, Pipeline, Resolve
20
-
17
+
21
18
  autoload :BabelProcessor, 'condenser/processors/rollup_processor'
22
19
  autoload :RollupProcessor, 'condenser/processors/babel_processor'
23
20
  autoload :UglifyMinifier, 'condenser/minifiers/uglify_minifier'
@@ -28,7 +25,8 @@ class Condenser
28
25
  autoload :EjsTransformer, 'condenser/transformers/ejs'
29
26
  autoload :FileWriter, 'condenser/writers/file_writer'
30
27
  autoload :ZlibWriter, 'condenser/writers/zlib_writer'
31
-
28
+ autoload :BuildCache, 'condenser/build_cache'
29
+
32
30
  def self.configure(&block)
33
31
  instance_eval(&block)
34
32
  end
@@ -8,13 +8,12 @@ class Condenser
8
8
  class Asset
9
9
 
10
10
  include EncodingUtils
11
- include ActiveSupport::Benchmarkable
12
11
 
13
12
  attr_reader :environment, :filename, :content_types, :source_file, :source_path
14
13
  attr_reader :linked_assets, :content_types_digest
15
14
  attr_writer :source, :sourcemap
16
15
 
17
- attr_accessor :imports
16
+ attr_accessor :imports, :processed, :export
18
17
 
19
18
  def initialize(env, attributes={})
20
19
  @environment = env
@@ -26,14 +25,17 @@ class Condenser
26
25
  @source_file = attributes[:source_file]
27
26
  @source_path = attributes[:source_path]
28
27
 
29
- @linked_assets = Set.new
30
- @dependencies = Set.new
31
- @default_export = nil
32
- @exports = nil
33
- @processed = false
34
-
35
- @processors_loaded = false
36
- @processors = Set.new
28
+ @linked_assets = Set.new
29
+ @process_dependencies = Set.new
30
+ @export_dependencies = Set.new
31
+ @default_export = nil
32
+ @exports = nil
33
+ @processed = false
34
+ @pcv = nil
35
+ @export = nil
36
+ @ecv = nil
37
+ @processors_loaded = false
38
+ @processors = Set.new
37
39
  end
38
40
 
39
41
  def path
@@ -53,17 +55,21 @@ class Condenser
53
55
  @stat ||= File.stat(@source_file)
54
56
  end
55
57
 
58
+ def restat!
59
+ @stat = File.stat(@source_file)
60
+ end
61
+
56
62
  def inspect
57
63
  dirname, basename, extensions, mime_types = @environment.decompose_path(@filename)
58
64
  <<-TEXT
59
- #<#{self.class.name} @filename=#{@filename} @content_types=#{@content_types.inspect} @source_file=#{@source_file} @source_mime_types=#{mime_types.inspect}>
65
+ #<#{self.class.name}##{self.object_id} @filename=#{@filename} @content_types=#{@content_types.inspect} @source_file=#{@source_file} @source_mime_types=#{mime_types.inspect}>
60
66
  TEXT
61
67
  end
62
68
 
63
- def dependencies
64
- deps = @environment.cache.fetch("dependencies/#{cache_key(false)}") do
69
+ def process_dependencies
70
+ deps = @environment.cache.fetch "direct-deps/#{cache_key}" do
65
71
  process
66
- @dependencies
72
+ @process_dependencies
67
73
  end
68
74
 
69
75
  d = []
@@ -76,66 +82,114 @@ class Condenser
76
82
  d
77
83
  end
78
84
 
79
- def has_default_export?
80
- @environment.cache.fetch("has_default_export/#{cache_key(false)}") do
85
+ def export_dependencies
86
+ deps = @environment.cache.fetch "export-deps/#{cache_key}" do
81
87
  process
82
- @default_export
88
+ @export_dependencies + @process_dependencies
89
+ end
90
+
91
+ d = []
92
+ deps.each do |i|
93
+ i = [i, nil] if i.is_a?(String)
94
+ @environment.resolve(i[0], File.dirname(@source_file), accept: i[1]).each do |asset|
95
+ d << asset
96
+ end
83
97
  end
98
+ d
99
+ end
100
+
101
+ def has_default_export?
102
+ process
103
+ @default_export
84
104
  end
85
105
 
86
106
  def has_exports?
87
- @environment.cache.fetch("has_exports/#{cache_key(false)}") do
88
- process
89
- @exports
90
- end
107
+ process
108
+ @exports
91
109
  end
92
110
 
93
111
  def load_processors
94
112
  return if @processors_loaded
95
113
 
96
114
  @processors_loaded = true
97
- processors = @environment.cache.fetch("processors/#{cache_key(false)}") do
98
- process
99
- @processors
100
- end
101
- processors.map! { |p| p.is_a?(String) ? p.constantize : p }
102
- @environment.load_processors(*processors)
115
+ process
116
+ @processors.map! { |p| p.is_a?(String) ? p.constantize : p }
117
+ @environment.load_processors(*@processors)
103
118
  end
104
119
 
105
- def all_dependenies(deps, visited, &block)
120
+ def all_dependenies(deps, visited, meth, &block)
106
121
  deps.each do |dep|
107
122
  if !visited.include?(dep.source_file)
108
123
  visited << dep.source_file
109
124
  block.call(dep)
110
- all_dependenies(dep.dependencies, visited, &block)
125
+ all_dependenies(dep.send(meth), visited, meth, &block)
111
126
  end
112
127
  end
113
128
  end
114
129
 
115
- def cache_key(include_dependencies=true)
116
- key = []
117
- key << [Condenser::VERSION, @source_file, @content_types_digest, stat.ino, stat.mtime.to_f, stat.size]
130
+ def all_process_dependencies
131
+ f = [@source_file]
132
+ all_dependenies(process_dependencies, [], :process_dependencies) do |dep|
133
+ f << dep.source_file
134
+ end
135
+ f
136
+ end
137
+
138
+ def all_export_dependencies
139
+ f = [@source_file]
140
+ all_dependenies(export_dependencies, [], :export_dependencies) do |dep|
141
+ f << dep.source_file
142
+ end
143
+ f
144
+ end
145
+
146
+ def cache_key
147
+ Digest::SHA1.base64digest(JSON.generate([
148
+ Condenser::VERSION,
149
+ @source_file,
150
+ @content_types_digest
151
+ ]))
152
+ end
153
+
154
+ def process_cache_version
155
+ return @pcv if @pcv
118
156
 
119
- if include_dependencies
120
- all_dependenies(dependencies, []) do |dep|
121
- key << [dep.source_file, dep.content_types_digest, dep.stat.ino, dep.stat.mtime.to_f, dep.stat.size]
122
- end
157
+ f = [stat.ino, stat.mtime.to_f, stat.size]
158
+ all_dependenies(process_dependencies, [], :process_dependencies) do |dep|
159
+ f << [dep.source_file, dep.stat.ino, dep.stat.mtime.to_f, dep.stat.size]
123
160
  end
124
161
 
125
- Digest::SHA1.base64digest(JSON.generate(key))
162
+ @pcv = Digest::SHA1.base64digest(JSON.generate(f))
126
163
  end
127
164
 
128
- def logger
129
- @environment.logger
165
+ def export_cache_version
166
+ return @ecv if @ecv
167
+
168
+ f = [stat.ino, stat.mtime.to_f, stat.size]
169
+ all_dependenies(export_dependencies, [], :export_dependencies) do |dep|
170
+ f << [dep.source_file, dep.stat.ino, dep.stat.mtime.to_f, dep.stat.size]
171
+ end
172
+
173
+ @ecv = Digest::SHA1.base64digest(JSON.generate(f))
174
+ end
175
+
176
+ def needs_reprocessing!
177
+ @processed = false
178
+ @pcv = nil
179
+ needs_reexporting!
180
+ end
181
+
182
+ def needs_reexporting!
183
+ restat!
184
+ @export = nil
185
+ @ecv = nil
130
186
  end
131
187
 
132
188
  def process
133
189
  return if @processed
134
190
 
135
- benchmark "Processing Asset #{self.filename}", level: :info do
136
- result = @environment.cache.fetch_if(Proc.new { "process/#{cache_key}" }, "dependencies/#{cache_key(false)}") do
137
- @environment.build do
138
-
191
+ result = @environment.build do
192
+ @environment.cache.fetch_if(Proc.new {"process/#{cache_key}/#{process_cache_version}"}, "direct-deps/#{cache_key}") do
139
193
  @source = File.binread(@source_file)
140
194
  dirname, basename, extensions, mime_types = @environment.decompose_path(@source_file)
141
195
 
@@ -148,7 +202,8 @@ class Condenser
148
202
 
149
203
  map: nil,
150
204
  linked_assets: [],
151
- dependencies: [],
205
+ process_dependencies: [],
206
+ export_dependencies: [],
152
207
 
153
208
  processors: Set.new
154
209
  }
@@ -160,10 +215,7 @@ class Condenser
160
215
  data[:processors] << templator_klass.name
161
216
  @environment.load_processors(templator_klass)
162
217
 
163
- benchmark "Preprocessing #{data[:filename]} with #{templator.name}", level: :info do
164
- templator.call(@environment, data)
165
- end
166
-
218
+ templator.call(@environment, data)
167
219
  data[:filename] = data[:filename].gsub(/\.#{extensions.last}$/, '')
168
220
  end
169
221
 
@@ -184,9 +236,7 @@ class Condenser
184
236
  data[:processors] << processor_klass.name
185
237
  @environment.load_processors(processor_klass)
186
238
 
187
- benchmark "Preprocessing #{self.filename} with #{processor.name}", level: :info do
188
- processor.call(@environment, data)
189
- end
239
+ processor.call(@environment, data)
190
240
  end
191
241
  end
192
242
 
@@ -219,7 +269,8 @@ class Condenser
219
269
  @digest = data[:digest]
220
270
  @digest_name = data[:digest_name]
221
271
  @linked_assets = data[:linked_assets]
222
- @dependencies = data[:dependencies]
272
+ @process_dependencies = data[:process_dependencies]
273
+ @export_dependencies = data[:export_dependencies]
223
274
  @default_export = data[:default_export]
224
275
  @exports = data[:exports]
225
276
  @processors = data[:processors]
@@ -237,20 +288,21 @@ class Condenser
237
288
  @digest = result[:digest]
238
289
  @digest_name = result[:digest_name]
239
290
  @linked_assets = result[:linked_assets]
240
- @dependencies = result[:dependencies]
291
+ @process_dependencies = result[:process_dependencies]
292
+ @export_dependencies = result[:export_dependencies]
241
293
  @default_export = result[:default_export]
242
294
  @exports = result[:exports]
243
295
  @processors = result[:processors]
244
296
  load_processors
245
297
 
246
298
  @processed = true
247
- end
248
299
  end
249
300
 
250
301
  def export
251
- benchmark "Exporting Asset #{self.filename}", level: :info do
252
- @environment.build do
253
- result = @environment.cache.fetch("export/#{cache_key}") do
302
+ return @export if @export
303
+
304
+ @export = @environment.build do
305
+ data = @environment.cache.fetch_if(Proc.new {"export/#{cache_key}/#{export_cache_version}"}, "export-deps/#{cache_key}") do
254
306
  process
255
307
  dirname, basename, extensions, mime_types = @environment.decompose_path(@filename)
256
308
  data = {
@@ -262,28 +314,23 @@ class Condenser
262
314
 
263
315
  sourcemap: nil,
264
316
  linked_assets: [],
265
- dependencies: []
317
+ process_dependencies: [],
318
+ export_dependencies: []
266
319
  }
267
-
320
+
268
321
  if exporter = @environment.exporters[content_type]
269
- benchmark "Preprocessing #{data[:filename]} with #{exporter.name}", level: :info do
270
- exporter.call(@environment, data)
271
- end
322
+ exporter.call(@environment, data)
272
323
  end
273
324
 
274
325
  if minifier = @environment.minifier_for(content_type)
275
- benchmark "Preprocessing #{data[:filename]} with #{minifier.name}", level: :info do
276
- minifier.call(@environment, data)
277
- end
326
+ minifier.call(@environment, data)
278
327
  end
279
-
328
+
280
329
  data[:digest] = @environment.digestor.digest(data[:source])
281
330
  data[:digest_name] = @environment.digestor.name.sub(/^.*::/, '').downcase
282
331
  data
283
332
  end
284
-
285
- Export.new(@environment, result)
286
- end
333
+ Export.new(@environment, data)
287
334
  end
288
335
  end
289
336
 
@@ -0,0 +1,107 @@
1
+ require 'listen'
2
+
3
+ class Condenser
4
+ class BuildCache
5
+
6
+ attr_reader :semaphore
7
+
8
+ def initialize(path)
9
+ @path = path
10
+ @semaphore = Mutex.new
11
+ @polling = Listen::Adapter.select == Listen::Adapter::Polling
12
+ @map_cache = {}
13
+ @lookup_cache = {}
14
+ @process_dependencies = {}
15
+ @export_dependencies = {}
16
+
17
+ @listener = Listen.to(*path) do |modified, added, removed|
18
+ @semaphore.synchronize do
19
+ added = added.reduce([]) do |rt, added_file|
20
+ rt << added_file.match(/([^\.]+)(\.|$)/).to_a[1]
21
+ if path_match = @path.find { |p| added_file.start_with?(p) }
22
+ a = added_file.delete_prefix(path_match).match(/([^\.]+)(\.|$)/).to_a[1]
23
+ b = (File.dirname(a) + "/*")
24
+
25
+ rt << a << a.delete_prefix('/')
26
+ rt << a << b.delete_prefix('/')
27
+ end
28
+ end
29
+
30
+ removed.each do |file|
31
+ @map_cache&.delete_if do |k,v|
32
+ v.source_file == file
33
+ end
34
+
35
+ @process_dependencies[file]&.delete_if do |asset|
36
+ asset.source_file == file
37
+ end
38
+
39
+ @export_dependencies[file]&.delete_if do |asset|
40
+ asset.source_file == file
41
+ end
42
+ end
43
+
44
+ @lookup_cache.delete_if do |key, value|
45
+ if added.any?{ |a| key.starts_with?(a) }
46
+ value.each do |asset|
47
+ modified << asset.source_file
48
+ end
49
+ true
50
+ end
51
+ end
52
+ @map_cache&.delete_if do |k,v|
53
+ added.any?{ |a| k.starts_with?(a) }
54
+ end
55
+
56
+ modified.each do |file|
57
+ @process_dependencies[file]&.each do |asset|
58
+ asset.needs_reprocessing!
59
+ end
60
+
61
+ @export_dependencies[file]&.each do |asset|
62
+ asset.needs_reexporting!
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+ @listener.start
69
+ end
70
+
71
+ def map(key)
72
+ @map_cache[key] ||= yield
73
+ end
74
+
75
+ def []=(value, assets)
76
+ @lookup_cache[value] = assets
77
+
78
+ assets.each do |asset|
79
+ asset.all_process_dependencies.each do |pd|
80
+ @process_dependencies[pd] ||= Set.new
81
+ @process_dependencies[pd] << asset
82
+ end
83
+
84
+ asset.all_export_dependencies.each do |pd|
85
+ @export_dependencies[pd] ||= Set.new
86
+ @export_dependencies[pd] << asset
87
+ end
88
+ end
89
+ end
90
+
91
+ def [](value)
92
+ @lookup_cache[value]
93
+ end
94
+
95
+ def fetch(key)
96
+ value = self[key]
97
+
98
+ if value.nil?
99
+ value = yield
100
+ self[key] = value
101
+ end
102
+
103
+ value
104
+ end
105
+
106
+ end
107
+ end