shakapacker 6.2.0 → 6.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 +4 -4
- data/CHANGELOG.md +44 -3
- data/CONTRIBUTING.md +2 -2
- data/Gemfile.lock +1 -1
- data/README.md +46 -14
- data/docs/deployment.md +2 -2
- data/docs/react.md +262 -0
- data/docs/troubleshooting.md +18 -0
- data/docs/v6_upgrade.md +3 -1
- data/lib/install/config/webpacker.yml +6 -0
- data/lib/tasks/webpacker/clean.rake +1 -3
- data/lib/tasks/webpacker/clobber.rake +1 -3
- data/lib/tasks/webpacker/compile.rake +1 -4
- data/lib/webpacker/base_strategy.rb +24 -0
- data/lib/webpacker/compiler.rb +4 -67
- data/lib/webpacker/compiler_strategy.rb +20 -0
- data/lib/webpacker/configuration.rb +14 -0
- data/lib/webpacker/digest_strategy.rb +59 -0
- data/lib/webpacker/helper.rb +26 -1
- data/lib/webpacker/instance.rb +4 -0
- data/lib/webpacker/manifest.rb +2 -2
- data/lib/webpacker/mtime_strategy.rb +40 -0
- data/lib/webpacker/version.rb +1 -1
- data/package/babel/preset.js +0 -1
- data/package/rules/file.js +2 -2
- data/package.json +12 -11
- data/test/compiler_strategy_test.rb +27 -0
- data/test/compiler_test.rb +26 -34
- data/test/configuration_test.rb +29 -4
- data/test/digest_strategy_test.rb +33 -0
- data/test/helper_test.rb +22 -0
- data/test/manifest_test.rb +3 -3
- data/test/mtime_strategy_test.rb +42 -0
- data/test/test_app/config/webpacker_no_precompile.yml +7 -0
- data/yarn.lock +917 -884
- metadata +16 -3
@@ -33,10 +33,7 @@ namespace :webpacker do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
|
37
|
-
skip_webpacker_precompile = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
|
38
|
-
|
39
|
-
unless skip_webpacker_precompile
|
36
|
+
if Webpacker.config.webpacker_precompile?
|
40
37
|
if Rake::Task.task_defined?("assets:precompile")
|
41
38
|
enhance_assets_precompile
|
42
39
|
else
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Webpacker
|
2
|
+
class BaseStrategy
|
3
|
+
def initialize
|
4
|
+
@config = Webpacker.config
|
5
|
+
end
|
6
|
+
|
7
|
+
def after_compile_hook
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
attr_reader :config
|
14
|
+
|
15
|
+
def default_watched_paths
|
16
|
+
[
|
17
|
+
*config.additional_paths.map { |path| "#{path}{,/**/*}" },
|
18
|
+
"#{config.source_path}{,/**/*}",
|
19
|
+
"yarn.lock", "package.json",
|
20
|
+
"config/webpack{,/**/*}"
|
21
|
+
].freeze
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/webpacker/compiler.rb
CHANGED
@@ -1,18 +1,13 @@
|
|
1
1
|
require "open3"
|
2
|
-
require "
|
2
|
+
require "webpacker/compiler_strategy"
|
3
3
|
|
4
4
|
class Webpacker::Compiler
|
5
|
-
# Additional paths that test compiler needs to watch
|
6
|
-
# Webpacker::Compiler.watched_paths << 'bower_components'
|
7
|
-
#
|
8
|
-
# Deprecated. Use additional_paths in the YAML configuration instead.
|
9
|
-
cattr_accessor(:watched_paths) { [] }
|
10
|
-
|
11
5
|
# Additional environment variables that the compiler is being run with
|
12
6
|
# Webpacker::Compiler.env['FRONTEND_API_KEY'] = 'your_secret_key'
|
13
7
|
cattr_accessor(:env) { {} }
|
14
8
|
|
15
|
-
delegate :config, :logger, to: :webpacker
|
9
|
+
delegate :config, :logger, :strategy, to: :webpacker
|
10
|
+
delegate :fresh?, :stale?, :after_compile_hook, to: :strategy
|
16
11
|
|
17
12
|
def initialize(webpacker)
|
18
13
|
@webpacker = webpacker
|
@@ -21,11 +16,7 @@ class Webpacker::Compiler
|
|
21
16
|
def compile
|
22
17
|
if stale?
|
23
18
|
run_webpack.tap do |success|
|
24
|
-
|
25
|
-
# However, the output file is still written on error, meaning that the digest should still be updated.
|
26
|
-
# If it's not, you can end up in a situation where a recompile doesn't take place when it should.
|
27
|
-
# See https://github.com/rails/webpacker/issues/2113
|
28
|
-
record_compilation_digest
|
19
|
+
after_compile_hook
|
29
20
|
end
|
30
21
|
else
|
31
22
|
logger.debug "Everything's up-to-date. Nothing to do"
|
@@ -33,50 +24,9 @@ class Webpacker::Compiler
|
|
33
24
|
end
|
34
25
|
end
|
35
26
|
|
36
|
-
# Returns true if all the compiled packs are up to date with the underlying asset files.
|
37
|
-
def fresh?
|
38
|
-
last_compilation_digest&.== watched_files_digest
|
39
|
-
end
|
40
|
-
|
41
|
-
# Returns true if the compiled packs are out of date with the underlying asset files.
|
42
|
-
def stale?
|
43
|
-
!fresh?
|
44
|
-
end
|
45
|
-
|
46
27
|
private
|
47
28
|
attr_reader :webpacker
|
48
29
|
|
49
|
-
def last_compilation_digest
|
50
|
-
compilation_digest_path.read if compilation_digest_path.exist? && config.manifest_path.exist?
|
51
|
-
rescue Errno::ENOENT, Errno::ENOTDIR
|
52
|
-
end
|
53
|
-
|
54
|
-
def watched_files_digest
|
55
|
-
if Rails.env.development?
|
56
|
-
warn <<~MSG.strip
|
57
|
-
Webpacker::Compiler - Slow setup for development
|
58
|
-
|
59
|
-
Prepare JS assets with either:
|
60
|
-
1. Running `bin/webpacker-dev-server`
|
61
|
-
2. Set `compile` to false in webpacker.yml and run `bin/webpacker -w`
|
62
|
-
MSG
|
63
|
-
end
|
64
|
-
|
65
|
-
warn "Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in webpacker.yml instead." unless watched_paths.empty?
|
66
|
-
root_path = Pathname.new(File.expand_path(config.root_path))
|
67
|
-
expanded_paths = [*default_watched_paths, *watched_paths].map do |path|
|
68
|
-
root_path.join(path)
|
69
|
-
end
|
70
|
-
files = Dir[*expanded_paths].reject { |f| File.directory?(f) }
|
71
|
-
file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
|
72
|
-
Digest::SHA1.hexdigest(file_ids.join("/"))
|
73
|
-
end
|
74
|
-
|
75
|
-
def record_compilation_digest
|
76
|
-
config.cache_path.mkpath
|
77
|
-
compilation_digest_path.write(watched_files_digest)
|
78
|
-
end
|
79
|
-
|
80
30
|
def optionalRubyRunner
|
81
31
|
bin_webpack_path = config.root_path.join("bin/webpacker")
|
82
32
|
first_line = File.readlines(bin_webpack_path).first.chomp
|
@@ -107,19 +57,6 @@ class Webpacker::Compiler
|
|
107
57
|
status.success?
|
108
58
|
end
|
109
59
|
|
110
|
-
def default_watched_paths
|
111
|
-
[
|
112
|
-
*config.additional_paths,
|
113
|
-
"#{config.source_path}/**/*",
|
114
|
-
"yarn.lock", "package.json",
|
115
|
-
"config/webpack/**/*"
|
116
|
-
].freeze
|
117
|
-
end
|
118
|
-
|
119
|
-
def compilation_digest_path
|
120
|
-
config.cache_path.join("last-compilation-digest-#{webpacker.env}")
|
121
|
-
end
|
122
|
-
|
123
60
|
def webpack_env
|
124
61
|
return env unless defined?(ActionController::Base)
|
125
62
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "webpacker/mtime_strategy"
|
2
|
+
require "webpacker/digest_strategy"
|
3
|
+
|
4
|
+
module Webpacker
|
5
|
+
class CompilerStrategy
|
6
|
+
def self.from_config
|
7
|
+
strategy_from_config = Webpacker.config.compiler_strategy
|
8
|
+
|
9
|
+
case strategy_from_config
|
10
|
+
when "mtime"
|
11
|
+
Webpacker::MtimeStrategy.new
|
12
|
+
when "digest"
|
13
|
+
Webpacker::DigestStrategy.new
|
14
|
+
else
|
15
|
+
raise "Unknown strategy '#{strategy_from_config}'. " \
|
16
|
+
"Available options are 'mtime' and 'digest'."
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -27,6 +27,12 @@ class Webpacker::Configuration
|
|
27
27
|
fetch(:ensure_consistent_versioning)
|
28
28
|
end
|
29
29
|
|
30
|
+
def webpacker_precompile?
|
31
|
+
# ENV of false takes precedence
|
32
|
+
return false if %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
|
33
|
+
return %w(yes true t).include?(ENV["WEBPACKER_PRECOMPILE"]) || fetch(:webpacker_precompile)
|
34
|
+
end
|
35
|
+
|
30
36
|
def source_path
|
31
37
|
root_path.join(fetch(:source_path))
|
32
38
|
end
|
@@ -47,6 +53,10 @@ class Webpacker::Configuration
|
|
47
53
|
end
|
48
54
|
end
|
49
55
|
|
56
|
+
def public_manifest_path
|
57
|
+
manifest_path
|
58
|
+
end
|
59
|
+
|
50
60
|
def public_path
|
51
61
|
root_path.join(fetch(:public_root_path))
|
52
62
|
end
|
@@ -75,6 +85,10 @@ class Webpacker::Configuration
|
|
75
85
|
fetch(:webpack_compile_output)
|
76
86
|
end
|
77
87
|
|
88
|
+
def compiler_strategy
|
89
|
+
fetch(:compiler_strategy)
|
90
|
+
end
|
91
|
+
|
78
92
|
def fetch(key)
|
79
93
|
data.fetch(key, defaults[key])
|
80
94
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "digest/sha1"
|
2
|
+
require "webpacker/base_strategy"
|
3
|
+
|
4
|
+
module Webpacker
|
5
|
+
class DigestStrategy < BaseStrategy
|
6
|
+
# Returns true if all the compiled packs are up to date with the underlying asset files.
|
7
|
+
def fresh?
|
8
|
+
last_compilation_digest&.== watched_files_digest
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns true if the compiled packs are out of date with the underlying asset files.
|
12
|
+
def stale?
|
13
|
+
!fresh?
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_compile_hook
|
17
|
+
# We used to only record the digest on success
|
18
|
+
# However, the output file is still written on error, meaning that the digest should still be updated.
|
19
|
+
# If it's not, you can end up in a situation where a recompile doesn't take place when it should.
|
20
|
+
# See https://github.com/rails/webpacker/issues/2113
|
21
|
+
record_compilation_digest
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def last_compilation_digest
|
27
|
+
compilation_digest_path.read if compilation_digest_path.exist? && config.manifest_path.exist?
|
28
|
+
rescue Errno::ENOENT, Errno::ENOTDIR
|
29
|
+
end
|
30
|
+
|
31
|
+
def watched_files_digest
|
32
|
+
if Rails.env.development?
|
33
|
+
warn <<~MSG.strip
|
34
|
+
Webpacker::Compiler - Slow setup for development
|
35
|
+
Prepare JS assets with either:
|
36
|
+
1. Running `bin/webpacker-dev-server`
|
37
|
+
2. Set `compile` to false in webpacker.yml and run `bin/webpacker -w`
|
38
|
+
MSG
|
39
|
+
end
|
40
|
+
|
41
|
+
root_path = Pathname.new(File.expand_path(config.root_path))
|
42
|
+
expanded_paths = [*default_watched_paths].map do |path|
|
43
|
+
root_path.join(path)
|
44
|
+
end
|
45
|
+
files = Dir[*expanded_paths].reject { |f| File.directory?(f) }
|
46
|
+
file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
|
47
|
+
Digest::SHA1.hexdigest(file_ids.join("/"))
|
48
|
+
end
|
49
|
+
|
50
|
+
def record_compilation_digest
|
51
|
+
config.cache_path.mkpath
|
52
|
+
compilation_digest_path.write(watched_files_digest)
|
53
|
+
end
|
54
|
+
|
55
|
+
def compilation_digest_path
|
56
|
+
config.cache_path.join("last-compilation-digest-#{Webpacker.env}")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/webpacker/helper.rb
CHANGED
@@ -101,9 +101,17 @@ module Webpacker::Helper
|
|
101
101
|
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide"
|
102
102
|
end
|
103
103
|
|
104
|
+
append_javascript_pack_tag(*names, defer: defer)
|
105
|
+
non_deferred = sources_from_manifest_entrypoints(javascript_pack_tag_queue[:non_deferred], type: :javascript)
|
106
|
+
deferred = sources_from_manifest_entrypoints(javascript_pack_tag_queue[:deferred], type: :javascript) - non_deferred
|
107
|
+
|
104
108
|
@javascript_pack_tag_loaded = true
|
105
109
|
|
106
|
-
|
110
|
+
capture do
|
111
|
+
concat javascript_include_tag(*deferred, **options.tap { |o| o[:defer] = true })
|
112
|
+
concat "\n" if non_deferred.any? && deferred.any?
|
113
|
+
concat javascript_include_tag(*non_deferred, **options.tap { |o| o[:defer] = false })
|
114
|
+
end
|
107
115
|
end
|
108
116
|
|
109
117
|
# Creates a link tag, for preloading, that references a given Webpacker asset.
|
@@ -153,8 +161,25 @@ module Webpacker::Helper
|
|
153
161
|
stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
|
154
162
|
end
|
155
163
|
|
164
|
+
def append_javascript_pack_tag(*names, defer: true)
|
165
|
+
if @javascript_pack_tag_loaded
|
166
|
+
raise "You can only call append_javascript_pack_tag before javascript_pack_tag helper. " \
|
167
|
+
"Please refer to https://github.com/shakacode/shakapacker/blob/master/README.md#usage for the usage guide"
|
168
|
+
end
|
169
|
+
|
170
|
+
hash_key = defer ? :deferred : :non_deferred
|
171
|
+
javascript_pack_tag_queue[hash_key] |= names
|
172
|
+
end
|
173
|
+
|
156
174
|
private
|
157
175
|
|
176
|
+
def javascript_pack_tag_queue
|
177
|
+
@javascript_pack_tag_queue ||= {
|
178
|
+
deferred: [],
|
179
|
+
non_deferred: []
|
180
|
+
}
|
181
|
+
end
|
182
|
+
|
158
183
|
def sources_from_manifest_entrypoints(names, type:)
|
159
184
|
names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name.to_s, type: type) }.flatten.uniq
|
160
185
|
end
|
data/lib/webpacker/instance.rb
CHANGED
data/lib/webpacker/manifest.rb
CHANGED
@@ -104,12 +104,12 @@ class Webpacker::Manifest
|
|
104
104
|
|
105
105
|
def missing_file_from_manifest_error(bundle_name)
|
106
106
|
<<-MSG
|
107
|
-
|
107
|
+
Shakapacker can't find #{bundle_name} in #{config.manifest_path}. Possible causes:
|
108
108
|
1. You forgot to install node packages (try `yarn install`) or are running an incompatible version of Node
|
109
109
|
2. Your app has code with a non-standard extension (like a `.jsx` file) but the extension is not in the `extensions` config in `config/webpacker.yml`
|
110
110
|
3. You have set compile: false (see `config/webpacker.yml`) for this environment
|
111
111
|
(unless you are using the `bin/webpacker -w` or the `bin/webpacker-dev-server`, in which case maybe you aren't running the dev server in the background?)
|
112
|
-
4. webpack has not yet
|
112
|
+
4. webpack has not yet FINISHED running to reflect updates.
|
113
113
|
5. You have misconfigured Webpacker's `config/webpacker.yml` file.
|
114
114
|
6. Your webpack configuration is not creating a manifest.
|
115
115
|
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "webpacker/base_strategy"
|
2
|
+
|
3
|
+
module Webpacker
|
4
|
+
class MtimeStrategy < BaseStrategy
|
5
|
+
# Returns true if manifest file mtime is newer than the timestamp of the last modified watched file
|
6
|
+
def fresh?
|
7
|
+
manifest_mtime > latest_modified_timestamp
|
8
|
+
end
|
9
|
+
|
10
|
+
# Returns true if manifest file mtime is older than the timestamp of the last modified watched file
|
11
|
+
def stale?
|
12
|
+
!fresh?
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def manifest_mtime
|
18
|
+
config.manifest_path.exist? ? File.mtime(config.manifest_path).to_i : 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def latest_modified_timestamp
|
22
|
+
if Rails.env.development?
|
23
|
+
warn <<~MSG.strip
|
24
|
+
Webpacker::Compiler - Slow setup for development
|
25
|
+
|
26
|
+
Prepare JS assets with either:
|
27
|
+
1. Running `bin/webpacker-dev-server`
|
28
|
+
2. Set `compile` to false in webpacker.yml and run `bin/webpacker -w`
|
29
|
+
MSG
|
30
|
+
end
|
31
|
+
|
32
|
+
root_path = Pathname.new(File.expand_path(config.root_path))
|
33
|
+
expanded_paths = [*default_watched_paths].map do |path|
|
34
|
+
root_path.join(path)
|
35
|
+
end
|
36
|
+
latest_modified = Dir[*expanded_paths].max_by { |f| File.mtime(f) }
|
37
|
+
File.mtime(latest_modified).to_i
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/webpacker/version.rb
CHANGED
data/package/babel/preset.js
CHANGED
data/package/rules/file.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
const { dirname
|
1
|
+
const { dirname } = require('path')
|
2
2
|
const { source_path: sourcePath } = require('../config')
|
3
3
|
|
4
4
|
module.exports = {
|
@@ -12,7 +12,7 @@ module.exports = {
|
|
12
12
|
.split('/')
|
13
13
|
.slice(1)
|
14
14
|
|
15
|
-
const foldersWithStatic =
|
15
|
+
const foldersWithStatic = ['static', ...folders].join('/')
|
16
16
|
return `${foldersWithStatic}/[name]-[hash][ext][query]`
|
17
17
|
}
|
18
18
|
}
|
data/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "shakapacker",
|
3
|
-
"version": "6.
|
3
|
+
"version": "6.3.0",
|
4
4
|
"description": "Use webpack to manage app-like JavaScript modules in Rails",
|
5
5
|
"main": "package/index.js",
|
6
6
|
"files": [
|
@@ -12,16 +12,17 @@
|
|
12
12
|
"yarn": ">=1 <4"
|
13
13
|
},
|
14
14
|
"peerDependencies": {
|
15
|
-
"@babel/core": "^7.
|
16
|
-
"@babel/plugin-transform-runtime": "^7.
|
17
|
-
"@babel/preset-env": "^7.
|
18
|
-
"@babel/runtime": "^7.
|
19
|
-
"babel-loader": "^8.2.
|
15
|
+
"@babel/core": "^7.17.9",
|
16
|
+
"@babel/plugin-transform-runtime": "^7.17.0",
|
17
|
+
"@babel/preset-env": "^7.16.11",
|
18
|
+
"@babel/runtime": "^7.17.9",
|
19
|
+
"babel-loader": "^8.2.4",
|
20
20
|
"compression-webpack-plugin": "^9.0.0",
|
21
|
-
"terser-webpack-plugin": "^5.
|
22
|
-
"webpack": "^5.
|
21
|
+
"terser-webpack-plugin": "^5.3.1",
|
22
|
+
"webpack": "^5.72.0",
|
23
23
|
"webpack-assets-manifest": "^5.0.6",
|
24
|
-
"webpack-cli": "^4.
|
24
|
+
"webpack-cli": "^4.9.2",
|
25
|
+
"webpack-dev-server": "^4.9.0",
|
25
26
|
"webpack-merge": "^5.8.0"
|
26
27
|
},
|
27
28
|
"dependencies": {
|
@@ -30,7 +31,7 @@
|
|
30
31
|
"path-complete-extname": "^1.0.0"
|
31
32
|
},
|
32
33
|
"devDependencies": {
|
33
|
-
"babel-loader": "^8.2.
|
34
|
+
"babel-loader": "^8.2.4",
|
34
35
|
"compression-webpack-plugin": "^9.0.0",
|
35
36
|
"esbuild-loader": "^2.18.0",
|
36
37
|
"eslint": "^7.32.0",
|
@@ -41,7 +42,7 @@
|
|
41
42
|
"eslint-plugin-react": "^7.26.0",
|
42
43
|
"jest": "^27.2.1",
|
43
44
|
"swc-loader": "^0.1.15",
|
44
|
-
"webpack": "^5.
|
45
|
+
"webpack": "^5.72.0",
|
45
46
|
"webpack-assets-manifest": "^5.0.6",
|
46
47
|
"webpack-merge": "^5.8.0"
|
47
48
|
},
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class CompilerStrategyTest < Minitest::Test
|
4
|
+
def test_mtime_strategy_returned
|
5
|
+
Webpacker.config.stub :compiler_strategy, "mtime" do
|
6
|
+
assert_instance_of Webpacker::MtimeStrategy, Webpacker::CompilerStrategy.from_config
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_digest_strategy_returned
|
11
|
+
Webpacker.config.stub :compiler_strategy, "digest" do
|
12
|
+
assert_instance_of Webpacker::DigestStrategy, Webpacker::CompilerStrategy.from_config
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_raise_on_unknown_strategy
|
17
|
+
Webpacker.config.stub :compiler_strategy, "other" do
|
18
|
+
error = assert_raises do
|
19
|
+
Webpacker::CompilerStrategy.from_config
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_equal \
|
23
|
+
"Unknown strategy 'other'. Available options are 'mtime' and 'digest'.",
|
24
|
+
error.message
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/compiler_test.rb
CHANGED
@@ -1,20 +1,6 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
3
|
class CompilerTest < Minitest::Test
|
4
|
-
def remove_compilation_digest_path
|
5
|
-
Webpacker.compiler.send(:compilation_digest_path).tap do |path|
|
6
|
-
path.delete if path.exist?
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def setup
|
11
|
-
remove_compilation_digest_path
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
remove_compilation_digest_path
|
16
|
-
end
|
17
|
-
|
18
4
|
def test_custom_environment_variables
|
19
5
|
assert_nil Webpacker.compiler.send(:webpack_env)["FOO"]
|
20
6
|
Webpacker.compiler.env["FOO"] = "BAR"
|
@@ -23,37 +9,43 @@ class CompilerTest < Minitest::Test
|
|
23
9
|
Webpacker.compiler.env = {}
|
24
10
|
end
|
25
11
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
12
|
+
def test_compile_true_when_fresh
|
13
|
+
mock = Minitest::Mock.new
|
14
|
+
mock.expect(:stale?, false)
|
15
|
+
Webpacker.compiler.stub(:strategy, mock) do
|
16
|
+
assert Webpacker.compiler.compile
|
17
|
+
end
|
18
|
+
assert_mock mock
|
29
19
|
end
|
30
20
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
21
|
+
def test_after_compile_hook_called_on_success
|
22
|
+
mock = Minitest::Mock.new
|
23
|
+
mock.expect(:stale?, true)
|
24
|
+
mock.expect(:after_compile_hook, nil)
|
34
25
|
|
35
|
-
def test_freshness_on_compile_success
|
36
26
|
status = OpenStruct.new(success?: true)
|
37
27
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
28
|
+
Webpacker.compiler.stub(:strategy, mock) do
|
29
|
+
Open3.stub :capture3, [:sterr, :stdout, status] do
|
30
|
+
Webpacker.compiler.compile
|
31
|
+
end
|
42
32
|
end
|
33
|
+
assert_mock mock
|
43
34
|
end
|
44
35
|
|
45
|
-
def
|
36
|
+
def test_after_compile_hook_called_on_failure
|
37
|
+
mock = Minitest::Mock.new
|
38
|
+
mock.expect(:stale?, true)
|
39
|
+
mock.expect(:after_compile_hook, nil)
|
40
|
+
|
46
41
|
status = OpenStruct.new(success?: false)
|
47
42
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
43
|
+
Webpacker.compiler.stub(:strategy, mock) do
|
44
|
+
Open3.stub :capture3, [:sterr, :stdout, status] do
|
45
|
+
Webpacker.compiler.compile
|
46
|
+
end
|
52
47
|
end
|
53
|
-
|
54
|
-
|
55
|
-
def test_compilation_digest_path
|
56
|
-
assert_equal Webpacker.compiler.send(:compilation_digest_path).basename.to_s, "last-compilation-digest-#{Webpacker.env}"
|
48
|
+
assert_mock mock
|
57
49
|
end
|
58
50
|
|
59
51
|
def test_external_env_variables
|
data/test/configuration_test.rb
CHANGED
@@ -28,28 +28,33 @@ class ConfigurationTest < Webpacker::Test
|
|
28
28
|
public_output_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs").to_s
|
29
29
|
assert_equal @config.public_output_path.to_s, public_output_path
|
30
30
|
|
31
|
-
@
|
31
|
+
@public_root_config = Webpacker::Configuration.new(
|
32
32
|
root_path: @config.root_path,
|
33
33
|
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_public_root.yml", __dir__)),
|
34
34
|
env: "production"
|
35
35
|
)
|
36
36
|
|
37
37
|
public_output_path = File.expand_path File.join(File.dirname(__FILE__), "public/packs").to_s
|
38
|
-
assert_equal @
|
38
|
+
assert_equal @public_root_config.public_output_path.to_s, public_output_path
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_public_manifest_path
|
42
|
+
public_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s
|
43
|
+
assert_equal @config.public_manifest_path.to_s, public_manifest_path
|
39
44
|
end
|
40
45
|
|
41
46
|
def test_manifest_path
|
42
47
|
manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/public/packs", "manifest.json").to_s
|
43
48
|
assert_equal @config.manifest_path.to_s, manifest_path
|
44
49
|
|
45
|
-
@
|
50
|
+
@manifest_config = Webpacker::Configuration.new(
|
46
51
|
root_path: @config.root_path,
|
47
52
|
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_manifest_path.yml", __dir__)),
|
48
53
|
env: "production"
|
49
54
|
)
|
50
55
|
|
51
56
|
manifest_path = File.expand_path File.join(File.dirname(__FILE__), "test_app/app/packs", "manifest.json").to_s
|
52
|
-
assert_equal @
|
57
|
+
assert_equal @manifest_config.manifest_path.to_s, manifest_path
|
53
58
|
end
|
54
59
|
|
55
60
|
def test_cache_path
|
@@ -95,5 +100,25 @@ class ConfigurationTest < Webpacker::Test
|
|
95
100
|
with_rails_env("test") do
|
96
101
|
refute Webpacker.config.ensure_consistent_versioning?
|
97
102
|
end
|
103
|
+
|
104
|
+
def test_webpacker_precompile
|
105
|
+
assert @config.webpacker_precompile
|
106
|
+
|
107
|
+
ENV["WEBPACKER_PRECOMPILE"] = "false"
|
108
|
+
|
109
|
+
refute Webpacker.config.webpacker_precompile?
|
110
|
+
|
111
|
+
ENV["WEBPACKER_PRECOMPILE"] = "yes"
|
112
|
+
|
113
|
+
assert Webpacker.config.webpacker_precompile?
|
114
|
+
|
115
|
+
@no_precompile_config = Webpacker::Configuration.new(
|
116
|
+
root_path: @config.root_path,
|
117
|
+
config_path: Pathname.new(File.expand_path("./test_app/config/webpacker_no_precompile.yml", __dir__)),
|
118
|
+
env: "production"
|
119
|
+
)
|
120
|
+
|
121
|
+
refute @no_precompile_config.webpacker_precompile
|
122
|
+
end
|
98
123
|
end
|
99
124
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class DigestStrategyTest < Minitest::Test
|
4
|
+
def remove_compilation_digest_path
|
5
|
+
@digest_strategy.send(:compilation_digest_path).tap do |path|
|
6
|
+
path.delete if path.exist?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@digest_strategy = Webpacker::DigestStrategy.new
|
12
|
+
remove_compilation_digest_path
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
remove_compilation_digest_path
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_freshness
|
20
|
+
assert @digest_strategy.stale?
|
21
|
+
assert !@digest_strategy.fresh?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_freshness_after_compilation_hook
|
25
|
+
@digest_strategy.after_compile_hook
|
26
|
+
assert @digest_strategy.fresh?
|
27
|
+
assert !@digest_strategy.stale?
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_compilation_digest_path
|
31
|
+
assert_equal @digest_strategy.send(:compilation_digest_path).basename.to_s, "last-compilation-digest-#{Webpacker.env}"
|
32
|
+
end
|
33
|
+
end
|