nanoc-webpack.rb 0.7.0 → 0.8.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
  SHA256:
3
- metadata.gz: 6089712cbc312cd503ad06f0a1d54e27351901f86b870460f1c6d3c205566d4c
4
- data.tar.gz: 8c932b943c81803365dcd2e9a7c46b5cfec2790fb05964c73b60e557fcf1461a
3
+ metadata.gz: 18e7707c0d80e1bb53739670f0a914aadb99117e3888847c7994c02467916401
4
+ data.tar.gz: 2fab4baa367a41c9e3c12fe076ca1b494504871ac70f775d540d8dc529378969
5
5
  SHA512:
6
- metadata.gz: 9b9f6f1cc40ada346daac98c1b401dc613c327e41c5bf48b1bf4c6ecad8b50c267b3a46164336e677dd8c28415f2c7262b28cab9ff4ea897c7f6a96491cc3c2a
7
- data.tar.gz: 62595779fdff296640fa1ddd0030ab3d97e580378f0129c5a5d9260fe13b92e9a08a006316c8ad5de420133557d7f9542a776f7d2c909c2e37642d1c788a8996
6
+ metadata.gz: 446b7975cad4fc591237fc5189fae9ac46e65a3544047227cf7e2fdf4130df7ff10251a506cf65519ca76a06e832ce66042f4d45bf629ee8237a601d3fe2ef08
7
+ data.tar.gz: f97f315d4a5d8fcc788fc88d41730ec2e1704b6a36304edb48128f196a12aaf066734fc9e1e9991ebd673dc92d1238b0f8b022364738791cb3605e88da0a3b65
data/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  .gems/
3
3
  /spec/fakefs/
4
4
  /pkg/
5
+ /tmp/
5
6
  Gemfile.lock
data/README.md CHANGED
@@ -5,7 +5,7 @@ nanoc-webpack.rb is a
5
5
  filter that integrates
6
6
  [webpack](https://webpack.js.org/)
7
7
  into nanoc. <br>
8
- The filter acts as a bridge that connects nanoc,
8
+ The filter acts as a bridge that connects nanoc,
9
9
  and the JavaScript, TypeScript, and nodejs ecosystems.
10
10
 
11
11
  ## Examples
@@ -56,19 +56,18 @@ compile "/js/main/App.tsx" do
56
56
  end
57
57
  ```
58
58
 
59
- **Option: cli**
59
+ **Option: argv**
60
60
 
61
- The `cli` option forwards command-line options directly
61
+ The `argv` option forwards command-line options directly
62
62
  to the webpack executable. <br>
63
- [Nanoc::Webpack.default_options](https://0x1eef.github.io/x/nanoc-webpack.rb/Nanoc/Webpack.html#default_options-class_method)
64
- returns the default options nanoc-webpack.rb will
65
- forward to webpack:
63
+ [Nanoc::Webpack.default_argv](https://0x1eef.github.io/x/nanoc-webpack.rb/Nanoc/Webpack.html#default_argv-class_method)
64
+ returns the default command-line options forwarded to webpack:
66
65
 
67
66
  ```ruby
68
67
  # Rules
69
68
  require "nanoc-webpack"
70
69
  compile "/js/main/App.tsx" do
71
- filter(:webpack, cli: {"--no-stats" => true})
70
+ filter(:webpack, argv: ["--node-env", "production"])
72
71
  write("/js/main/app.js")
73
72
  end
74
73
  ```
@@ -77,7 +76,7 @@ end
77
76
 
78
77
  **Rubygems.org**
79
78
 
80
- nanoc-webpack.rb can also be installed via rubygems.org.
79
+ nanoc-webpack.rb can be installed via rubygems.org.
81
80
 
82
81
  gem install nanoc-webpack.rb
83
82
 
@@ -2,81 +2,66 @@
2
2
 
3
3
  ##
4
4
  # Compiles a nanoc item with webpack.
5
- class Nanoc::Webpack::Filter < Nanoc::Filter
6
- require_relative "filter/dependable"
7
- include FileUtils
8
- include Dependable
9
- Error = Class.new(RuntimeError)
5
+ module Nanoc::Webpack
6
+ class Filter < Nanoc::Filter
7
+ require_relative "filter/dependable"
8
+ require_relative "spawn"
9
+ include Dependable
10
+ include Spawn
11
+ include FileUtils
10
12
 
11
- identifier :webpack
12
- type :text
13
+ identifier :webpack
14
+ type :text
13
15
 
14
- ##
15
- # @example
16
- # Nanoc::Webpack.default_options.merge!(
17
- # "--cache-type" => "memory"
18
- # )
19
- #
20
- # @return [Hash]
21
- # Returns the default command-line options forwarded to webpack.
22
- def self.default_options
23
- @default_options ||= {"--cache-type" => "filesystem"}
24
- end
16
+ ##
17
+ # @example
18
+ # Nanoc::Webpack.default_argv.replace ["--cache-type", "memory"]
19
+ #
20
+ # @return [Hash]
21
+ # The default command-line options forwarded to webpack.
22
+ def self.default_argv
23
+ @default_argv ||= ["--cache-type", "filesystem"]
24
+ end
25
25
 
26
- ##
27
- # @param [String] content
28
- # The contents of a file.
29
- #
30
- # @param [Hash] options
31
- # A hash of options.
32
- #
33
- # @return [void]
34
- def run(content, options = {})
35
- options = Ryo.from(options)
36
- depend_on dependable(paths: options.depend_on, reject: options.reject)
37
- .map { items[_1] }
38
- webpack temporary_file(content),
39
- cli: self.class.default_options.merge(options.cli || {})
40
- end
26
+ ##
27
+ # @param [String] content
28
+ # The contents of a file.
29
+ #
30
+ # @param [Hash] options
31
+ # A hash of options.
32
+ #
33
+ # @return [void]
34
+ def run(content, options = {})
35
+ options = Ryo.from(options)
36
+ path = temporary_file(content).path
37
+ depend_on dependable(paths: options.depend_on, reject: options.reject)
38
+ .map { items[_1] }
39
+ spawn "node",
40
+ ["./node_modules/webpack/bin/webpack.js",
41
+ "--entry", File.join(Dir.getwd, item.attributes[:content_filename]),
42
+ "--output-path", File.dirname(path),
43
+ "--output-filename", File.basename(path),
44
+ *default_argv, *(options.argv || [])],
45
+ log: File.join(tmpdir, "webpack.log")
46
+ ensure
47
+ rm(path)
48
+ end
41
49
 
42
- private
50
+ private
43
51
 
44
- def webpack(file, cli:)
45
- sh "node",
46
- "./node_modules/webpack/bin/webpack.js",
47
- "--entry", File.join(Dir.getwd, item.attributes[:content_filename]),
48
- "--output-path", File.dirname(file.path),
49
- "--output-filename", File.basename(file.path),
50
- *webpack_args(cli)
51
- if $?.success?
52
- File.read(file.path).tap { file.tap(&:unlink).close }
53
- else
54
- rm_f Dir.glob(File.join(File.dirname(file.path), "*"))
55
- file.close
56
- raise Error, "webpack.js exited unsuccessfully (exit code: #{$?.exitstatus})", []
52
+ def default_argv
53
+ self.class.default_argv
57
54
  end
58
- end
59
55
 
60
- def webpack_args(args)
61
- args.each_with_object([]) do |(key, value), ary|
62
- if value.equal?(true)
63
- ary << key
64
- else
65
- ary.concat [key, value.to_s]
66
- end
56
+ def temporary_file(content)
57
+ mkdir_p(tmpdir)
58
+ file = Tempfile.new(File.basename(item.identifier.to_s), tmpdir)
59
+ file.write(content)
60
+ file.tap(&:flush)
67
61
  end
68
- end
69
-
70
- def temporary_file(content)
71
- dir = File.join(Dir.getwd, "tmp", "nanoc-webpack.rb")
72
- mkdir_p(dir) unless Dir.exist?(dir)
73
- file = Tempfile.new(File.basename(@item.identifier.to_s), dir)
74
- file.write(content)
75
- file.tap(&:flush)
76
- end
77
62
 
78
- def sh(*args)
79
- print "webpack: ", args.join(" "), "\n"
80
- system(*args)
63
+ def tmpdir
64
+ File.join(Dir.getwd, "tmp", "webpack")
65
+ end
81
66
  end
82
67
  end
@@ -0,0 +1,19 @@
1
+ module Nanoc::Webpack
2
+ module Spawn
3
+ Error = Class.new(RuntimeError)
4
+ def spawn(exe, argv, log:)
5
+ Kernel.spawn(
6
+ exe, *argv, { STDOUT => log, STDERR => log }
7
+ )
8
+ Process.wait
9
+ unless $?.success?
10
+ raise Error,
11
+ "#{File.basename(exe)} exited unsuccessfully " \
12
+ "(exit code: #{$?.exitstatus}, " \
13
+ "item: #{item.identifier}, " \
14
+ "log: #{log.gsub(Dir.getwd, '')[1..]})",
15
+ []
16
+ end
17
+ end
18
+ end
19
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Nanoc
4
4
  module Webpack
5
- VERSION = "0.7.0"
5
+ VERSION = "0.8.0"
6
6
  end
7
7
  end
data/lib/nanoc/webpack.rb CHANGED
@@ -5,9 +5,9 @@ module Nanoc::Webpack
5
5
  require "ryo"
6
6
  require_relative "webpack/filter"
7
7
  ##
8
- # @example (see Nanoc::Webpack::Filter.default_options)
9
- # @return (see Nanoc::Webpack::Filter.default_options)
10
- def self.default_options
11
- Nanoc::Webpack::Filter.default_options
8
+ # @example (see Nanoc::Webpack::Filter.default_argv)
9
+ # @return (see Nanoc::Webpack::Filter.default_argv)
10
+ def self.default_argv
11
+ Nanoc::Webpack::Filter.default_argv
12
12
  end
13
13
  end
data/spec/filter_spec.rb CHANGED
@@ -136,7 +136,8 @@ RSpec.describe Nanoc::Webpack::Filter do
136
136
 
137
137
  context "with default arguments" do
138
138
  it "executes nodejs" do
139
- expect(filter).to receive(:system).with(*cmdline)
139
+ expect(Kernel).to receive(:spawn).with(*cmdline, instance_of(Hash))
140
+ expect(Process).to receive(:wait)
140
141
  filter.run(item)
141
142
  end
142
143
  end
@@ -145,8 +146,9 @@ RSpec.describe Nanoc::Webpack::Filter do
145
146
  let(:cmdline) { super().concat(["--no-cache"]) }
146
147
 
147
148
  it "executes nodejs with an argument" do
148
- expect(filter).to receive(:system).with(*cmdline)
149
- filter.run(item, cli: {"--no-cache" => true})
149
+ expect(Kernel).to receive(:spawn).with(*cmdline, instance_of(Hash))
150
+ expect(Process).to receive(:wait)
151
+ filter.run(item, argv: ["--no-cache"])
150
152
  end
151
153
  end
152
154
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nanoc-webpack.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-25 00:00:00.000000000 Z
11
+ date: 2024-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ryo.rb
@@ -114,6 +114,7 @@ files:
114
114
  - lib/nanoc/webpack.rb
115
115
  - lib/nanoc/webpack/filter.rb
116
116
  - lib/nanoc/webpack/filter/dependable.rb
117
+ - lib/nanoc/webpack/spawn.rb
117
118
  - lib/nanoc/webpack/version.rb
118
119
  - nanoc-webpack.rb.gemspec
119
120
  - spec/filter_spec.rb