nanoc-webpack.rb 0.4.5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b5fa317d727a05360e4e08e5d0e343862686385597adfde1780614f634006bb
4
+ data.tar.gz: 5824d38fcfc4327524856c94a80c81cb1f0d9fc636f3d540d06e2f32fbffa805
5
+ SHA512:
6
+ metadata.gz: 1cbd5df5b9e003e2cd5bb96213aa0852fa306f2ec91dab060d61a3930253a62591919fb15917a811c06afe6b322a3a7316dcf1b8a7e8c82827dcf361f8774b39
7
+ data.tar.gz: 2a5a8f3854cb9164b8b20d02e9920f20cb5143f57457a67db387f8225ce1382d6f100547c1e42e486f070985f302437e8a6d3fcdb4231cefd200d37a83a5cf43
@@ -0,0 +1,23 @@
1
+ name: nanoc-webpack.rb
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ specs:
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ os: [ubuntu-latest]
15
+ ruby: [3.1, 3.2]
16
+ runs-on: ${{ matrix.os }}
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+ - run: bundle install
23
+ - run: bundle exec rspec spec/
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .yardoc/
2
+ /spec/fakefs/
3
+ /pkg/
4
+ Gemfile.lock
data/.projectile ADDED
File without changes
data/.rubocop.yml ADDED
@@ -0,0 +1,53 @@
1
+ ##
2
+ # Plugins
3
+ require:
4
+ - standard
5
+ - rubocop-rspec
6
+
7
+ ##
8
+ # Defaults: standard-rb
9
+ inherit_gem:
10
+ standard: config/base.yml
11
+
12
+ AllCops:
13
+ TargetRubyVersion: 3.2
14
+ Include:
15
+ - 'lib/*.rb'
16
+ - 'lib/**/*.rb'
17
+ - 'spec/*.rb'
18
+
19
+ ##
20
+ # Enabled
21
+ Style/FrozenStringLiteralComment:
22
+ Enabled: true
23
+ Layout/ArrayAlignment:
24
+ Enabled: false
25
+
26
+ ##
27
+ # Disabled
28
+ RSpec/FilePath:
29
+ Enabled: false
30
+ RSpec/NestedGroups:
31
+ Enabled: false
32
+ RSpec/NotToNot:
33
+ Enabled: false
34
+ RSpec/EmptyLineAfterHook:
35
+ Enabled: false
36
+ RSpec/EmptyLineAfterSubject:
37
+ Enabled: false
38
+ RSpec/DescribedClass:
39
+ Enabled: false
40
+ RSpec/MultipleExpectations:
41
+ Enabled: false
42
+ RSpec/EmptyLineAfterFinalLet:
43
+ Enabled: false
44
+ RSpec/DescribeClass:
45
+ Enabled: false
46
+ RSpec/ImplicitSubject:
47
+ Enabled: false
48
+ Style/LambdaCall:
49
+ Enabled: false
50
+ Layout/MultilineMethodCallIndentation:
51
+ Enabled: false
52
+ Layout/ArgumentAlignment:
53
+ Enabled: false
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ -m markdown -M redcarpet --no-private
2
+ -
3
+ README.md
4
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+ gem "nanoc"
6
+ gem "ryo.rb", github: "0x1eef/ryo.rb", tag: "v0.3.0"
data/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ Copyright (C) 2023 by 0x1eef <0x1eef@protonmail.com>
2
+
3
+ Permission to use, copy, modify, and/or distribute this
4
+ software for any purpose with or without fee is hereby
5
+ granted.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
8
+ ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
9
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
10
+ EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
12
+ RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
15
+ OF THIS SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ ## About
2
+
3
+ nanoc-webpack.rb is a
4
+ [nanoc](https://nanoc.app)
5
+ filter
6
+ that integrates
7
+ [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.
10
+
11
+ ## Examples
12
+
13
+ __app.ts__
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`:
17
+
18
+ ``` ruby
19
+ # Rules
20
+ require "nanoc-webpack"
21
+ compile "/js/app.ts" do
22
+ filter(:webpack)
23
+ write("/js/app.js")
24
+ end
25
+ ```
26
+
27
+ __Option: "depend_on"__
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:
32
+
33
+ ```ruby
34
+ # Rules
35
+ require "nanoc-webpack"
36
+ compile "/js/ReactApp.jsx" do
37
+ filter(:webpack, depend_on: ["/js/lib", "/js/components", "/js/hooks"])
38
+ write("/js/app.js")
39
+ end
40
+ ```
41
+
42
+ __Option: "reject"__
43
+
44
+ The `depend_on` option can be combined with the `reject` option to exclude
45
+ certain files or directories from being tracked. For example, maybe you want
46
+ to track `/js/lib/` but not `/js/lib/foo/`:
47
+
48
+ ```ruby
49
+ # Rules
50
+ require "nanoc-webpack"
51
+ compile "/js/ReactApp.jsx" do
52
+ filter :webpack,
53
+ depend_on: ["/js/lib", "/js/components", "/js/hooks"],
54
+ reject: proc { |path| path.start_with?("/js/lib/foo/") }
55
+ write("/js/app.js")
56
+ end
57
+ ```
58
+
59
+ ## Requirements
60
+
61
+ nanoc-webpack.rb assumes that:
62
+
63
+ * A "node" executable is available in $PATH.
64
+ * [npm](https://www.npmjs.com) or [yarn](https://yarnpkg.com/) are used for
65
+ package management.
66
+ * "webpack" / "webpack-cli" exist as dependencies in package.json.
67
+
68
+ ## Sources
69
+
70
+ * [Source code (GitHub)](https://github.com/0x1eef/nanoc-webpack.rb)
71
+ * [Source code (GitLab)](https://gitlab.com/0x1eef/nanoc-webpack.rb)
72
+
73
+ ## <a id='install'>Install</a>
74
+
75
+ nanoc-webpack.rb is distributed as a RubyGem through its git repositories. <br>
76
+ [GitHub](https://github.com/0x1eef/nanoc-webpack.rb),
77
+ and
78
+ [GitLab](https://gitlab.com/0x1eef/nanoc-webpack.rb)
79
+ are available as sources.
80
+
81
+ ## License
82
+
83
+ [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
84
+ <br>
85
+ See [LICENSE](./LICENSE).
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Nanoc::Webpack::Filter
4
+ module Dependable
5
+ def dependable(paths:, reject: nil)
6
+ reject ||= proc {}
7
+ [*paths].flat_map do |path|
8
+ expand(path).flat_map do
9
+ node = File.join(root, _1)
10
+ File.directory?(node) ? dependable(paths: File.join(_1, "*"), reject:) : _1
11
+ end
12
+ end.compact
13
+ .reject(&reject)
14
+ end
15
+
16
+ def expand(path)
17
+ abs_path = File.join(Dir.getwd, root)
18
+ glob_str = File.expand_path(File.join(abs_path, path))
19
+ Dir.glob(glob_str).map { File.join("/", _1.sub(abs_path, "")) }
20
+ end
21
+
22
+ def root
23
+ @root ||= begin
24
+ nanoc = Ryo.from(config.each.to_h)
25
+ source = nanoc.data_sources.find(&:content_dir)
26
+ source&.content_dir || "content/"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Compiles a textual nanoc item with webpack.
5
+ class Nanoc::Webpack::Filter < Nanoc::Filter
6
+ require_relative "filter/dependable"
7
+ Error = Class.new(RuntimeError)
8
+ include FileUtils
9
+ include Dependable
10
+
11
+ identifier :webpack
12
+ type :text
13
+
14
+ def run(content, options = {})
15
+ depend_on dependable(paths: options[:depend_on], reject: options[:reject])
16
+ .map { items[_1] }
17
+ webpack(temp_file(content))
18
+ end
19
+
20
+ private
21
+
22
+ def webpack(file)
23
+ system "node",
24
+ "./node_modules/webpack/bin/webpack.js",
25
+ "--entry", File.join(Dir.getwd, item.attributes[:content_filename]),
26
+ "--output-path", File.dirname(file.path),
27
+ "--output-filename", File.basename(file.path)
28
+ if $?.success?
29
+ File.read(file.path).tap { file.tap(&:unlink).close }
30
+ else
31
+ rm_f Dir.glob(File.join(File.dirname(file.path), "*"))
32
+ file.close
33
+ raise Error, "webpack.js exited unsuccessfully (exit code: #{$?.exitstatus})", []
34
+ end
35
+ end
36
+
37
+ def temp_file(content)
38
+ dir = File.join(Dir.getwd, "tmp", "webpack")
39
+ mkdir_p(dir) unless Dir.exist?(dir)
40
+ file = Tempfile.new(File.basename(item.identifier.to_s), dir)
41
+ file.write(content)
42
+ file.tap(&:flush)
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nanoc
4
+ module Webpack
5
+ VERSION = "0.4.5"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nanoc"
4
+ require "ryo"
5
+ module Nanoc::Webpack
6
+ require_relative "webpack/filter"
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nanoc/webpack"
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/nanoc/webpack/version"
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "nanoc-webpack.rb"
6
+ gem.authors = ["0x1eef"]
7
+ gem.email = ["0x1eef@protonmail.com"]
8
+ gem.homepage = "https://github.com/0x1eef/nanoc-webpack.rb#readme"
9
+ gem.version = Nanoc::Webpack::VERSION
10
+ gem.licenses = ["0BSD"]
11
+ gem.files = `git ls-files`.split($/)
12
+ gem.require_paths = ["lib"]
13
+ gem.summary = "A nanoc filter that can compile textual items with webpack"
14
+ gem.description = gem.summary
15
+ gem.add_runtime_dependency "ryo.rb", "~> 0.4"
16
+ gem.add_development_dependency "yard", "~> 0.9"
17
+ gem.add_development_dependency "redcarpet", "~> 3.5"
18
+ gem.add_development_dependency "rspec", "~> 3.10"
19
+ gem.add_development_dependency "standard", "~> 1.13"
20
+ gem.add_development_dependency "rubocop-rspec", "~> 2.11"
21
+ end
@@ -0,0 +1,110 @@
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
data/spec/setup.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ require "nanoc-webpack"
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoc-webpack.rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.5
5
+ platform: ruby
6
+ authors:
7
+ - '0x1eef'
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ryo.rb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redcarpet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: standard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.11'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.11'
97
+ description: A nanoc filter that can compile textual items with webpack
98
+ email:
99
+ - 0x1eef@protonmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/workflows/specs.yml"
105
+ - ".gitignore"
106
+ - ".projectile"
107
+ - ".rubocop.yml"
108
+ - ".yardopts"
109
+ - Gemfile
110
+ - LICENSE
111
+ - README.md
112
+ - lib/nanoc-webpack.rb
113
+ - lib/nanoc/webpack.rb
114
+ - lib/nanoc/webpack/filter.rb
115
+ - lib/nanoc/webpack/filter/dependable.rb
116
+ - lib/nanoc/webpack/version.rb
117
+ - nanoc-webpack.rb.gemspec
118
+ - spec/dependable_spec.rb
119
+ - spec/setup.rb
120
+ homepage: https://github.com/0x1eef/nanoc-webpack.rb#readme
121
+ licenses:
122
+ - 0BSD
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.4.10
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: A nanoc filter that can compile textual items with webpack
143
+ test_files: []