webpack_driver 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44ba342eead35d1724a2bca92475e051e606bdcc
4
- data.tar.gz: 01bf6577333fd5b749a815812ed840d000cca105
3
+ metadata.gz: 0dabddeeea6cb7372add80de628a6171c7ff8f91
4
+ data.tar.gz: 02d915f02ce6c35b51713a85fa728c35e6f8b092
5
5
  SHA512:
6
- metadata.gz: e1fe4ad83a82af9fdc95258a0f22575bb2841f26c0ce4edf0caf8d71e8a79827f84467da153e0e45294b9c675b0b7e180252a06c85f215261f790227f5f0a63f
7
- data.tar.gz: 40356b5bab34e82640968b84048a3b25a023f923eed7268c9ea697bf52b144a301053b65d2d9fa70c1e01b0e415bdac99abe4b44549cf51b95c7205e5557c2d9
6
+ metadata.gz: 71fa04b99cc53dd36488abeebdc72a81216c82b35ec489ad0567f6151e09f23e3b42e895aca3ea13311441e743d84cec005cdba85fea777c496e1095f3b42ae5
7
+ data.tar.gz: 8127fbf1d6d221eaaff0d4db177d8ccd8ea5ae3e548d8af6c10b7841bcfe4e5084ff601fe9241d9d9b0f55880a77d4788b6641693c737d6f58860047a57b2d05
data/.gitignore CHANGED
@@ -11,4 +11,5 @@ yarn.lock
11
11
  /pkg/
12
12
  /spec/reports/
13
13
  /tmp/
14
- /test/fixtures
14
+ test/fixtures
15
+ test/tmp
data/lib/status-plugin.js CHANGED
@@ -18,13 +18,13 @@ WebpackDriverStatusPlugin.prototype.apply = function(compiler) {
18
18
  log("dev-server", { port, host, outputPath });
19
19
  }
20
20
 
21
- // compiler.apply(new webpack.ProgressPlugin(function (percent, msg) {
22
- // log("compile", {
23
- // "progress": percent,
24
- // "operation": msg,
25
- // "ms": Date.now() - timer
26
- // });
27
- // }));
21
+ compiler.apply(new webpack.ProgressPlugin(function (percent, msg) {
22
+ log("compile", {
23
+ "progress": percent,
24
+ "operation": msg,
25
+ "ms": Date.now() - timer
26
+ });
27
+ }));
28
28
 
29
29
  compiler.plugin("compile", function() {
30
30
  timer = Date.now();
@@ -38,6 +38,10 @@ WebpackDriverStatusPlugin.prototype.apply = function(compiler) {
38
38
  log("status", "invalid");
39
39
  });
40
40
 
41
+ compiler.plugin("after-environment", function() {
42
+ log("config", { output_path: compiler.options.output.path });
43
+ })
44
+
41
45
  compiler.plugin("failed-module", function(module) {
42
46
  log("failed-module", module);
43
47
  });
@@ -64,12 +68,14 @@ WebpackDriverStatusPlugin.prototype.apply = function(compiler) {
64
68
  log("status", "valid");
65
69
  });
66
70
 
67
- compiler.plugin("after-emit", function(compilation, callback) {
68
- for (var k in compilation.assets){
69
- if(!compilation.assets[k].parents){
70
- log("asset", {name: k, size: compilation.assets[k].size()});
71
+ // https://gist.github.com/kisenka/b31d3dd1d1a9182dde0bb3e3efe1a038
72
+ compiler.plugin("emit", function(compilation, callback) {
73
+ const assets = compilation.getStats().toJson().assets;
74
+ assets.forEach(function(asset) {
75
+ if (asset.chunkNames.length) {
76
+ log("asset", {id: asset.chunkNames[0], file: asset.name, size: asset.size});
71
77
  }
72
- }
78
+ });
73
79
  callback();
74
80
  });
75
81
 
@@ -1,13 +1,25 @@
1
1
  module WebpackDriver
2
2
 
3
3
  class Asset
4
- attr_reader :file, :size
5
4
 
6
- def initialize(file, size)
7
- @file = file
8
- @size = size
5
+ attr_reader :id, :file, :size
6
+ attr_accessor :has_source_map
7
+
8
+ def initialize(attrs)
9
+ @id = attrs['id'].to_sym
10
+ @size = attrs['size']
11
+ @file = attrs['file']
9
12
  end
10
13
 
14
+ def self.record(map, attrs)
15
+ id = attrs['id'].to_sym
16
+ file = attrs['file']
17
+ if map[id] && file && file.end_with?('.map')
18
+ map[id].has_source_map = true
19
+ else
20
+ map[id] = Asset.new(attrs)
21
+ end
22
+ end
11
23
  end
12
24
 
13
25
  end
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+
1
3
  module WebpackDriver
2
4
 
3
5
  class Compile < Process
@@ -5,12 +7,37 @@ module WebpackDriver
5
7
  def initialize(config)
6
8
  config.environment ||= { NODE_ENV: 'production' }
7
9
  super('webpack', config)
10
+ read_manifest
8
11
  end
9
12
 
10
13
  def valid?
11
- !alive? && super
14
+ !alive? && complete? && last_status == 'success'
12
15
  end
13
16
 
14
- end
17
+ def complete?
18
+ progress == 1
19
+ end
20
+
21
+ def record_progress(progress, msg)
22
+ super
23
+ write_manifest if complete?
24
+ end
15
25
 
26
+ def write_manifest
27
+ manifest = {}
28
+ assets.each do | id, asset |
29
+ manifest[id] = asset.file
30
+ end
31
+ config.manifest_file.write JSON.generate manifest
32
+ end
33
+
34
+ def read_manifest
35
+ return unless config.manifest_file.exist?
36
+ manifest = JSON.parse config.manifest_file.read
37
+ manifest.each do |id, file|
38
+ asset = Asset.new({ 'id' => id, 'file' => file })
39
+ assets[asset.id] = asset
40
+ end
41
+ end
42
+ end
16
43
  end
@@ -9,12 +9,31 @@ module WebpackDriver
9
9
 
10
10
  class_option :config
11
11
 
12
+ attr_reader :config_directory, :path, :generated_directory
13
+
14
+ def set_variables
15
+ @generated_directory = options[:config].tmp_directory
16
+ @config_directory = options[:config].file.dirname
17
+ @path = generated_directory.join('generated.config.js')
18
+ end
19
+
12
20
  def self.source_root
13
21
  Pathname.new(__FILE__).dirname.join("..","..","..","templates")
14
22
  end
15
23
 
24
+
16
25
  def output
17
- template("generated.config.js", options[:config].path, verbose: false, force: true)
26
+ opts = { verbose: false, force: true }
27
+ template(
28
+ options[:config].file.relative_path_from(self.class.source_root),
29
+ options[:config].tmp_directory.join('webpack.config.js'),
30
+ opts
31
+ )
32
+ template(
33
+ 'generated.config.js',
34
+ path,
35
+ opts
36
+ )
18
37
  end
19
38
 
20
39
  end
@@ -10,22 +10,34 @@ module WebpackDriver
10
10
  attr_accessor :port
11
11
  attr_accessor :logger
12
12
  attr_accessor :compile_script
13
- attr_accessor :directory
13
+ attr_accessor :tmp_directory
14
14
  attr_accessor :cmd_line_flags
15
+ attr_accessor :output_path
15
16
  attr_writer :environment
16
-
17
- attr_reader :file
18
- attr_reader :generated
17
+ attr_accessor :file
18
+ attr_accessor :directory
19
19
  attr_accessor :logger
20
20
 
21
+ attr_reader :process
22
+
23
+
24
+
21
25
  ROOT = Pathname.new(__FILE__).dirname.join('..', '..')
22
26
 
23
- def initialize(file = './webpack.config.js', options = {})
27
+ def initialize(options = {})
24
28
  options.each { |k, v| send("#{k}=", v) }
25
- @directory ||= '.'
26
- @file = Pathname.new(file)
27
- @generated = Tempfile.new(['webpack.config', '.js'])
28
- Generated.new([], config: self).invoke_all
29
+ @file = Pathname.new(file) unless file.nil?
30
+ @directory ||= Pathname.getwd
31
+ @output_path ||= @directory.join('public', 'assets')
32
+ @tmp_directory ||= @directory.join('tmp')
33
+ if file.exist?
34
+ @generated = Generated.new([], config: self)
35
+ @generated.invoke_all
36
+ end
37
+ end
38
+
39
+ def manifest_file
40
+ output_path.join('manifiest.json')
29
41
  end
30
42
 
31
43
  def generate!
@@ -36,10 +48,6 @@ module WebpackDriver
36
48
  file.exist?
37
49
  end
38
50
 
39
- def path
40
- @generated.path
41
- end
42
-
43
51
  def gem_root
44
52
  ROOT
45
53
  end
@@ -49,7 +57,7 @@ module WebpackDriver
49
57
  end
50
58
 
51
59
  def flags
52
- opts = ['--config', path]
60
+ opts = ['--config', @generated.path.to_s]
53
61
  opts += ['--port', port] if port
54
62
  opts += cmd_line_flags if cmd_line_flags
55
63
  opts
@@ -59,6 +67,11 @@ module WebpackDriver
59
67
  @logger ||= Logger.new(STDOUT)
60
68
  end
61
69
 
62
- end
70
+ def launch(development:)
71
+ raise "Already launched" unless @process.nil?
72
+ logger.info "Startint"
73
+ @process = development ? DevServer.new(self) : Compile.new(self)
74
+ end
63
75
 
76
+ end
64
77
  end
@@ -10,7 +10,7 @@ module WebpackDriver
10
10
  end
11
11
 
12
12
  def valid?
13
- alive? && super
13
+ alive? && last_status == 'success'
14
14
  end
15
15
 
16
16
  private
@@ -12,20 +12,23 @@ module WebpackDriver
12
12
 
13
13
  def_delegators :@proc, :alive?, :environment, :wait
14
14
 
15
- attr_reader :assets, :messages
16
- attr_reader :config
15
+ attr_reader :config, :assets, :messages, :progress, :error,
16
+ :last_compilation_message, :last_status
17
17
 
18
18
  def initialize(script, config)
19
19
  self.reset!
20
20
  @config = config
21
21
  args = ["./node_modules/.bin/#{script}"] + config.flags
22
-
22
+ config.logger.info("Starting webpack using command:\n#{args.join(' ')}")
23
23
  @proc = ::ChildProcess.build(*args)
24
24
 
25
25
  @proc.environment.merge!(
26
26
  config.environment
27
27
  )
28
- @proc.cwd = config.directory
28
+ if config.directory
29
+ config.logger.info("In directory: #{config.directory}")
30
+ @proc.cwd = config.directory
31
+ end
29
32
  end
30
33
 
31
34
  def start
@@ -43,8 +46,8 @@ module WebpackDriver
43
46
  @listener.join
44
47
  end
45
48
 
46
- def valid?
47
- last_compilation_message['operation'] == 'emit'
49
+ def in_progress?
50
+ !@error && @progress && @progress != 1
48
51
  end
49
52
 
50
53
  protected
@@ -52,42 +55,54 @@ module WebpackDriver
52
55
  def reset!
53
56
  @assets = Concurrent::Map.new
54
57
  @messages = Concurrent::Array.new
55
- end
56
-
57
- def last_compilation_message
58
- msg = @messages.reverse_each.detect{ |l| l['type'] == 'compile' }
59
- msg ? msg['value'] : {}
58
+ @last_compilation_message = {}
60
59
  end
61
60
 
62
61
  def record_error(error)
62
+ @error = error
63
63
  config.logger.error(
64
64
  "#{error['name']}: #{error['resource']}\n#{error['message']}"
65
65
  )
66
66
  end
67
67
 
68
+ def record_progress(progress, msg)
69
+ @progress = progress
70
+ @last_compilation_message = msg['value']
71
+ end
72
+
68
73
  def record_message(msg)
74
+ @messages << msg unless msg['type'] == 'progress'
69
75
  case msg['type']
76
+ when 'status'
77
+ @last_status = msg['value']
70
78
  when 'asset'
71
- name = msg['value']['name']
72
- @assets[name] = Asset.new(name, msg['value']['size'])
79
+ Asset.record(@assets, msg['value'])
80
+ when 'compile'
81
+ record_progress msg['value']['progress'], msg
73
82
  when 'error'
74
83
  record_error(msg['value'])
84
+ when 'config'
85
+ config.output_path = Pathname.new(msg['value']['output_path'])
75
86
  else
76
87
  config.logger.debug(msg)
77
88
  end
78
- @messages << msg
79
89
  end
80
90
 
81
91
 
82
92
  def listen_for_status_updates
83
93
  Thread.new do
84
94
  @output.each_line do | l |
85
- match = l.match(/^STATUS: (.*)/)
86
- if match
87
- record_message(JSON.parse(match[1]))
88
- config.logger.debug(l.chomp)
89
- else
90
- config.logger.info(l.chomp)
95
+ puts l
96
+ begin
97
+ match = l.match(/^STATUS: (.*)/)
98
+ if match
99
+ record_message(JSON.parse(match[1]))
100
+ config.logger.debug(l.chomp)
101
+ else
102
+ config.logger.info(l.chomp)
103
+ end
104
+ rescue => e
105
+ config.logger.error "Exception #{e} encountered while processing line #{l}"
91
106
  end
92
107
  end
93
108
  end
@@ -1,3 +1,3 @@
1
1
  module WebpackDriver
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- var config = require('<%= options[:config].file %>');
1
+ var config = require('./webpack.config.js');
2
2
  var WebpackDriverStatusPlugin = require('<%= options[:config].gem_root %>/lib/status-plugin.js');
3
3
 
4
4
  config.plugins = (config.plugins || []);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpack_driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Stitt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-28 00:00:00.000000000 Z
11
+ date: 2017-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: knitter
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  version: '0'
179
179
  requirements: []
180
180
  rubyforge_project:
181
- rubygems_version: 2.5.1
181
+ rubygems_version: 2.6.11
182
182
  signing_key:
183
183
  specification_version: 4
184
184
  summary: Run webpack-dev-server in a sub process