nanoc-webpack.rb 0.5.5 → 0.7.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
  SHA256:
3
- metadata.gz: c3e798ff00aa45852e59880aac435054ddf29b3682655e8a9935de50300cfc72
4
- data.tar.gz: 1acef686fc1ff408aa5681d307c7879a9c1aada3a85c51912a6adc25a4fd5480
3
+ metadata.gz: 6089712cbc312cd503ad06f0a1d54e27351901f86b870460f1c6d3c205566d4c
4
+ data.tar.gz: 8c932b943c81803365dcd2e9a7c46b5cfec2790fb05964c73b60e557fcf1461a
5
5
  SHA512:
6
- metadata.gz: c47e5a09a5a71e485eedb9a9a6e781d89067c3b86d7128fcb3b8a08ec51cfbb0c851c61b6709b1e7ec332573ea8209a467b2f64eb1d84082562def20920ec3b1
7
- data.tar.gz: c3cd143a1b31f035c30339d5c434b1859a4d450a313879a517c843009f9f109419b5632d87650070c20be2c971234ed8820be52213fe1f5f418377fc76d575c7
6
+ metadata.gz: 9b9f6f1cc40ada346daac98c1b401dc613c327e41c5bf48b1bf4c6ecad8b50c267b3a46164336e677dd8c28415f2c7262b28cab9ff4ea897c7f6a96491cc3c2a
7
+ data.tar.gz: 62595779fdff296640fa1ddd0030ab3d97e580378f0129c5a5d9260fe13b92e9a08a006316c8ad5de420133557d7f9542a776f7d2c909c2e37642d1c788a8996
data/.rubocop.yml CHANGED
@@ -45,6 +45,10 @@ RSpec/DescribeClass:
45
45
  Enabled: false
46
46
  RSpec/ImplicitSubject:
47
47
  Enabled: false
48
+ RSpec/MessageSpies:
49
+ Enabled: false
50
+ RSpec/MultipleMemoizedHelpers:
51
+ Enabled: false
48
52
  Style/LambdaCall:
49
53
  Enabled: false
50
54
  Layout/MultilineMethodCallIndentation:
data/README.md CHANGED
@@ -2,44 +2,44 @@
2
2
 
3
3
  nanoc-webpack.rb is a
4
4
  [nanoc](https://nanoc.app)
5
- filter
6
- that integrates
5
+ filter that integrates
7
6
  [webpack](https://webpack.js.org/)
8
- into nanoc-powered websites. The filter provides a bridge that
9
- connects nanoc, and the JavaScript, TypeScript, and nodejs ecosystems.
7
+ into nanoc. <br>
8
+ The filter acts as a bridge that connects nanoc,
9
+ and the JavaScript, TypeScript, and nodejs ecosystems.
10
10
 
11
11
  ## Examples
12
12
 
13
- __app.ts__
13
+ **Defaults**
14
14
 
15
- The following example forwards the entry point `app.ts` to webpack, and
16
- then writes the result of the webpack compilation to `app.js`:
15
+ The following example forwards the entry point `/js/main/App.tsx`
16
+ to webpack. <br> The result is then written to `/js/main/app.js`:
17
17
 
18
18
  ``` ruby
19
19
  # Rules
20
20
  require "nanoc-webpack"
21
- compile "/js/app.ts" do
21
+ compile "/js/main/App.tsx" do
22
22
  filter(:webpack)
23
- write("/js/app.js")
23
+ write("/js/main/app.js")
24
24
  end
25
25
  ```
26
26
 
27
- __Option: "depend_on"__
27
+ **Option: depend_on**
28
28
 
29
- The `depend_on` option tells nanoc what files an entry point imports or requires.
30
- When a file being tracked by the `depend_on` option undergoes a change, nanoc
31
- will initiate a recompilation of the entry point:
29
+ When a file or directory tracked by the `depend_on` option
30
+ is observed to have changed, nanoc will initiate a recompilation
31
+ of the entry point:
32
32
 
33
33
  ```ruby
34
34
  # Rules
35
35
  require "nanoc-webpack"
36
- compile "/js/ReactApp.jsx" do
36
+ compile "/js/main/App.tsx" do
37
37
  filter(:webpack, depend_on: ["/js/lib", "/js/components", "/js/hooks"])
38
- write("/js/app.js")
38
+ write("/js/main/app.js")
39
39
  end
40
40
  ```
41
41
 
42
- __Option: "reject"__
42
+ **Option: reject**
43
43
 
44
44
  The `depend_on` option can be combined with the `reject` option to exclude
45
45
  certain files or directories from being tracked. For example, maybe you want
@@ -48,65 +48,44 @@ to track `/js/lib/` but not `/js/lib/foo/`:
48
48
  ```ruby
49
49
  # Rules
50
50
  require "nanoc-webpack"
51
- compile "/js/ReactApp.jsx" do
51
+ compile "/js/main/App.tsx" do
52
52
  filter :webpack,
53
53
  depend_on: ["/js/lib", "/js/components", "/js/hooks"],
54
54
  reject: proc { |path| path.start_with?("/js/lib/foo/") }
55
- write("/js/app.js")
55
+ write("/js/main/app.js")
56
56
  end
57
57
  ```
58
58
 
59
- __Option: "args"__
59
+ **Option: cli**
60
60
 
61
- The `args` option can be used to forward command-line options directly
62
- to the webpack executable. See `$ webpack build --help` for the list of
63
- options that are available:
61
+ The `cli` option forwards command-line options directly
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:
64
66
 
65
67
  ```ruby
66
68
  # Rules
67
69
  require "nanoc-webpack"
68
- compile "/js/ReactApp.jsx" do
69
- filter :webpack, args: {"--no-stats" => true}
70
- write("/js/app.js")
70
+ compile "/js/main/App.tsx" do
71
+ filter(:webpack, cli: {"--no-stats" => true})
72
+ write("/js/main/app.js")
71
73
  end
72
74
  ```
73
75
 
74
-
75
- ## Requirements
76
-
77
- nanoc-webpack.rb assumes that:
78
-
79
- * A "node" executable is available in $PATH.
80
- * [npm](https://www.npmjs.com) or [yarn](https://yarnpkg.com/) are used for
81
- package management.
82
- * "webpack" / "webpack-cli" exist as dependencies in package.json.
83
-
84
- ## Sources
85
-
86
- * [Source code (GitHub)](https://github.com/0x1eef/nanoc-webpack.rb)
87
- * [Source code (GitLab)](https://gitlab.com/0x1eef/nanoc-webpack.rb)
88
-
89
76
  ## <a id='install'>Install</a>
90
77
 
91
- **Git**
92
-
93
- nanoc-webpack.rb is distributed as a RubyGem through its git repositories. <br>
94
- [GitHub](https://github.com/0x1eef/nanoc-webpack.rb),
95
- and
96
- [GitLab](https://gitlab.com/0x1eef/nanoc-webpack.rb)
97
- are available as sources.
98
-
99
- ```ruby
100
- # Gemfile
101
- gem "nanoc-webpack.rb", github: "0x1eef/nanoc-webpack.rb", tag: "v0.5.4"
102
- ```
103
-
104
78
  **Rubygems.org**
105
79
 
106
80
  nanoc-webpack.rb can also be installed via rubygems.org.
107
81
 
108
82
  gem install nanoc-webpack.rb
109
83
 
84
+ ## Sources
85
+
86
+ * [GitHub](https://github.com/0x1eef/nanoc-webpack.rb#readme)
87
+ * [GitLab](https://gitlab.com/0x1eef/nanoc-webpack.rb#about)
88
+
110
89
  ## License
111
90
 
112
91
  [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
@@ -1,40 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  ##
4
- # Compiles a textual nanoc item with webpack.
4
+ # Compiles a nanoc item with webpack.
5
5
  class Nanoc::Webpack::Filter < Nanoc::Filter
6
6
  require_relative "filter/dependable"
7
- Error = Class.new(RuntimeError)
8
7
  include FileUtils
9
8
  include Dependable
9
+ Error = Class.new(RuntimeError)
10
10
 
11
11
  identifier :webpack
12
12
  type :text
13
13
 
14
14
  ##
15
+ # @example
16
+ # Nanoc::Webpack.default_options.merge!(
17
+ # "--cache-type" => "memory"
18
+ # )
19
+ #
15
20
  # @return [Hash]
16
- # Returns the default command-line options given
17
- # to the webpack executable.
21
+ # Returns the default command-line options forwarded to webpack.
18
22
  def self.default_options
19
- @default_options ||= {}
23
+ @default_options ||= {"--cache-type" => "filesystem"}
20
24
  end
21
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]
22
34
  def run(content, options = {})
23
- depend_on dependable(paths: options[:depend_on], reject: options[:reject])
35
+ options = Ryo.from(options)
36
+ depend_on dependable(paths: options.depend_on, reject: options.reject)
24
37
  .map { items[_1] }
25
- webpack temporary_file_for(content),
26
- args: self.class.default_options.merge(options[:args] || {})
38
+ webpack temporary_file(content),
39
+ cli: self.class.default_options.merge(options.cli || {})
27
40
  end
28
41
 
29
42
  private
30
43
 
31
- def webpack(file, args: {})
44
+ def webpack(file, cli:)
32
45
  sh "node",
33
46
  "./node_modules/webpack/bin/webpack.js",
34
47
  "--entry", File.join(Dir.getwd, item.attributes[:content_filename]),
35
48
  "--output-path", File.dirname(file.path),
36
49
  "--output-filename", File.basename(file.path),
37
- *webpack_args(args)
50
+ *webpack_args(cli)
38
51
  if $?.success?
39
52
  File.read(file.path).tap { file.tap(&:unlink).close }
40
53
  else
@@ -54,10 +67,10 @@ class Nanoc::Webpack::Filter < Nanoc::Filter
54
67
  end
55
68
  end
56
69
 
57
- def temporary_file_for(content)
70
+ def temporary_file(content)
58
71
  dir = File.join(Dir.getwd, "tmp", "nanoc-webpack.rb")
59
72
  mkdir_p(dir) unless Dir.exist?(dir)
60
- file = Tempfile.new(File.basename(item.identifier.to_s), dir)
73
+ file = Tempfile.new(File.basename(@item.identifier.to_s), dir)
61
74
  file.write(content)
62
75
  file.tap(&:flush)
63
76
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Nanoc
4
4
  module Webpack
5
- VERSION = "0.5.5"
5
+ VERSION = "0.7.0"
6
6
  end
7
7
  end
data/lib/nanoc/webpack.rb CHANGED
@@ -1,7 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "nanoc"
4
- require "ryo"
5
4
  module Nanoc::Webpack
5
+ require "ryo"
6
6
  require_relative "webpack/filter"
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
12
+ end
7
13
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |gem|
12
12
  gem.require_paths = ["lib"]
13
13
  gem.summary = "nanoc-webpack.rb integrates webpack into nanoc."
14
14
  gem.description = gem.summary
15
- gem.add_runtime_dependency "ryo.rb", "~> 0.4"
15
+ gem.add_runtime_dependency "ryo.rb", "~> 0.5"
16
16
  gem.add_development_dependency "yard", "~> 0.9"
17
17
  gem.add_development_dependency "redcarpet", "~> 3.5"
18
18
  gem.add_development_dependency "rspec", "~> 3.10"
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "setup"
4
+ RSpec.describe Nanoc::Webpack::Filter do
5
+ let(:paths) { ["/1", "/1/1.txt", "/1/2", "/1/2/1.txt"] }
6
+ let(:root) { File.join("spec", "fakefs", "content") }
7
+ let(:reject) { nil }
8
+ let(:item) do
9
+ Ryo.from(
10
+ identifier: {to_s: "test.ts"},
11
+ attributes: {content_filename: "test.ts"}
12
+ )
13
+ end
14
+ let(:items) do
15
+ paths.each_with_object({}) { _2[_1] = _1 }
16
+ end
17
+ let(:config) do
18
+ Ryo.from(
19
+ each: {to_h: {data_sources: [{content_dir: root}]}},
20
+ data_sources: [{content_dir: root}]
21
+ )
22
+ end
23
+ let(:filter) do
24
+ Nanoc::Filter
25
+ .named!(:webpack)
26
+ .new(item:, items:, config:)
27
+ end
28
+ include FileUtils
29
+
30
+ context "when given dependables" do
31
+ subject { filter.dependable(paths: target, reject:) }
32
+
33
+ before do
34
+ mkdir_p(root)
35
+ paths.each do
36
+ path = File.join(root, _1)
37
+ File.extname(path).empty? ? mkdir_p(path) : touch(path)
38
+ end
39
+ end
40
+
41
+ after { rm_rf(root) }
42
+
43
+ context "when the directory depth is 2" do
44
+ context "when given /1 as a path" do
45
+ let(:target) { "/1" }
46
+ it { is_expected.to contain_exactly("/1/1.txt", "/1/2/1.txt") }
47
+ end
48
+
49
+ context "when given /1/2 as a path" do
50
+ let(:target) { "/1/2" }
51
+ it { is_expected.to contain_exactly("/1/2/1.txt") }
52
+ end
53
+ end
54
+
55
+ context "when the directory depth is 5" do
56
+ let(:paths) { super().concat(["/1/2/3/4/5", "/1/2/3/4/5/1.txt"]) }
57
+
58
+ context "when given /1 as a path" do
59
+ let(:target) { "/1" }
60
+ it do
61
+ is_expected.to contain_exactly(
62
+ "/1/1.txt",
63
+ "/1/2/1.txt",
64
+ "/1/2/3/4/5/1.txt"
65
+ )
66
+ end
67
+ end
68
+
69
+ context "when given /1/2 as a path" do
70
+ let(:target) { "/1/2" }
71
+ it { is_expected.to contain_exactly("/1/2/1.txt", "/1/2/3/4/5/1.txt") }
72
+ end
73
+
74
+ context "when given /1/2/3 as a path" do
75
+ let(:target) { "/1/2/3" }
76
+ it { is_expected.to contain_exactly("/1/2/3/4/5/1.txt") }
77
+ end
78
+
79
+ context "when depth 5 has descendant directories" do
80
+ let(:paths) do
81
+ super().concat([
82
+ "/1/2/3/4/5/a", "/1/2/3/4/5/a/a.txt",
83
+ "/1/2/3/4/5/b", "/1/2/3/4/5/b/b.txt",
84
+ "/1/2/3/4/5/c", "/1/2/3/4/5/c/c.txt"
85
+ ])
86
+ end
87
+
88
+ context "when given /1 as a path" do
89
+ let(:target) { "/1" }
90
+ it do
91
+ is_expected.to contain_exactly(
92
+ "/1/1.txt", "/1/2/1.txt", "/1/2/3/4/5/1.txt",
93
+ "/1/2/3/4/5/a/a.txt", "/1/2/3/4/5/b/b.txt",
94
+ "/1/2/3/4/5/c/c.txt"
95
+ )
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ context "when one or more directories are excluded" do
102
+ let(:paths) { super().concat(["/2/3", "/2/3/1.txt", "/3/4", "/3/4/1.txt"]) }
103
+
104
+ context "when the target is [!12]" do
105
+ let(:target) { "/[!12]/" }
106
+ it { is_expected.to contain_exactly("/3/4/1.txt") }
107
+ end
108
+
109
+ context "when the reject filter is used" do
110
+ let(:paths) do
111
+ ["/lib/WebPackage/", "/lib/WebPackage/foo.ts",
112
+ "/lib/Web/", "/lib/Web/foo.ts",
113
+ "/lib/foo/", "/lib/foo/foo.ts"]
114
+ end
115
+ let(:target) { "/lib/" }
116
+ let(:reject) { proc { |path| path.include?("WebPackage") } }
117
+
118
+ it do
119
+ is_expected.to contain_exactly("/lib/Web/foo.ts", "/lib/foo/foo.ts")
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ context "when nodejs is executed" do
126
+ let(:cmdline) do
127
+ [
128
+ "node",
129
+ "./node_modules/webpack/bin/webpack.js",
130
+ "--entry", File.join(Dir.getwd, "test.ts"),
131
+ "--output-path", instance_of(String),
132
+ "--output-filename", instance_of(String),
133
+ "--cache-type", "filesystem"
134
+ ]
135
+ end
136
+
137
+ context "with default arguments" do
138
+ it "executes nodejs" do
139
+ expect(filter).to receive(:system).with(*cmdline)
140
+ filter.run(item)
141
+ end
142
+ end
143
+
144
+ context "with --no-cache" do
145
+ let(:cmdline) { super().concat(["--no-cache"]) }
146
+
147
+ it "executes nodejs with an argument" do
148
+ expect(filter).to receive(:system).with(*cmdline)
149
+ filter.run(item, cli: {"--no-cache" => true})
150
+ end
151
+ end
152
+ end
153
+ 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.5.5
4
+ version: 0.7.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-01-11 00:00:00.000000000 Z
11
+ date: 2024-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ryo.rb
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.4'
19
+ version: '0.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.4'
26
+ version: '0.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: yard
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -116,7 +116,7 @@ files:
116
116
  - lib/nanoc/webpack/filter/dependable.rb
117
117
  - lib/nanoc/webpack/version.rb
118
118
  - nanoc-webpack.rb.gemspec
119
- - spec/dependable_spec.rb
119
+ - spec/filter_spec.rb
120
120
  - spec/setup.rb
121
121
  homepage: https://github.com/0x1eef/nanoc-webpack.rb#readme
122
122
  licenses:
@@ -1,110 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "setup"
4
-
5
- RSpec.describe Nanoc::Webpack::Filter::Dependable do
6
- subject { filter.dependable(paths: target, reject:) }
7
- let(:paths) { ["/1", "/1/1.txt", "/1/2", "/1/2/1.txt"] }
8
- let(:root) { File.join("spec", "fakefs", "content") }
9
- let(:reject) { nil }
10
- let(:filter) do
11
- Object.new.instance_exec(root, paths) do |root, paths|
12
- extend Nanoc::Webpack::Filter::Dependable
13
- define_singleton_method(:items) { paths.each_with_object({}) { _2[_1] = _1 } }
14
- define_singleton_method(:root) { root }
15
- self
16
- end
17
- end
18
- include FileUtils
19
-
20
- before do
21
- mkdir_p(root)
22
- paths.each do
23
- path = File.join(root, _1)
24
- File.extname(path).empty? ? mkdir_p(path) : touch(path)
25
- end
26
- end
27
-
28
- after { rm_rf(root) }
29
-
30
- context "when the directory depth is 2" do
31
- context "when given /1 as a path" do
32
- let(:target) { "/1" }
33
- it { is_expected.to contain_exactly("/1/1.txt", "/1/2/1.txt") }
34
- end
35
-
36
- context "when given /1/2 as a path" do
37
- let(:target) { "/1/2" }
38
- it { is_expected.to contain_exactly("/1/2/1.txt") }
39
- end
40
- end
41
-
42
- context "when the directory depth is 5" do
43
- let(:paths) { super().concat(["/1/2/3/4/5", "/1/2/3/4/5/1.txt"]) }
44
-
45
- context "when given /1 as a path" do
46
- let(:target) { "/1" }
47
- it do
48
- is_expected.to contain_exactly(
49
- "/1/1.txt",
50
- "/1/2/1.txt",
51
- "/1/2/3/4/5/1.txt"
52
- )
53
- end
54
- end
55
-
56
- context "when given /1/2 as a path" do
57
- let(:target) { "/1/2" }
58
- it { is_expected.to contain_exactly("/1/2/1.txt", "/1/2/3/4/5/1.txt") }
59
- end
60
-
61
- context "when given /1/2/3 as a path" do
62
- let(:target) { "/1/2/3" }
63
- it { is_expected.to contain_exactly("/1/2/3/4/5/1.txt") }
64
- end
65
-
66
- context "when depth 5 has descendant directories" do
67
- let(:paths) do
68
- super().concat([
69
- "/1/2/3/4/5/a", "/1/2/3/4/5/a/a.txt",
70
- "/1/2/3/4/5/b", "/1/2/3/4/5/b/b.txt",
71
- "/1/2/3/4/5/c", "/1/2/3/4/5/c/c.txt"
72
- ])
73
- end
74
-
75
- context "when given /1 as a path" do
76
- let(:target) { "/1" }
77
- it do
78
- is_expected.to contain_exactly(
79
- "/1/1.txt", "/1/2/1.txt", "/1/2/3/4/5/1.txt",
80
- "/1/2/3/4/5/a/a.txt", "/1/2/3/4/5/b/b.txt",
81
- "/1/2/3/4/5/c/c.txt"
82
- )
83
- end
84
- end
85
- end
86
- end
87
-
88
- context "when one or more directories are excluded" do
89
- let(:paths) { super().concat(["/2/3", "/2/3/1.txt", "/3/4", "/3/4/1.txt"]) }
90
-
91
- context "when the target is [!12]" do
92
- let(:target) { "/[!12]/" }
93
- it { is_expected.to contain_exactly("/3/4/1.txt") }
94
- end
95
-
96
- context "when the reject filter is used" do
97
- let(:paths) do
98
- ["/lib/WebPackage/", "/lib/WebPackage/foo.ts",
99
- "/lib/Web/", "/lib/Web/foo.ts",
100
- "/lib/foo/", "/lib/foo/foo.ts"]
101
- end
102
- let(:target) { "/lib/" }
103
- let(:reject) { proc { |path| path.include?("WebPackage") } }
104
-
105
- it do
106
- is_expected.to contain_exactly("/lib/Web/foo.ts", "/lib/foo/foo.ts")
107
- end
108
- end
109
- end
110
- end