shakapacker 7.0.3 → 7.2.0.rc.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dummy.yml +3 -1
  3. data/.github/workflows/generator.yml +6 -1
  4. data/.github/workflows/jest.yml +4 -2
  5. data/.github/workflows/js-lint.yml +4 -2
  6. data/.github/workflows/rubocop.yml +3 -1
  7. data/.github/workflows/ruby-backward-compatibility.yml +9 -1
  8. data/.github/workflows/ruby.yml +11 -2
  9. data/CHANGELOG.md +16 -3
  10. data/README.md +58 -23
  11. data/docs/troubleshooting.md +4 -8
  12. data/docs/using_esbuild_loader.md +2 -4
  13. data/docs/v6_upgrade.md +1 -1
  14. data/lib/install/template.rb +58 -18
  15. data/lib/shakapacker/compiler.rb +5 -3
  16. data/lib/shakapacker/configuration.rb +8 -0
  17. data/lib/shakapacker/deprecation_helper.rb +1 -1
  18. data/lib/shakapacker/dev_server_runner.rb +13 -6
  19. data/lib/shakapacker/runner.rb +19 -1
  20. data/lib/shakapacker/utils/misc.rb +12 -0
  21. data/lib/shakapacker/version.rb +1 -1
  22. data/lib/shakapacker/version_checker.rb +35 -2
  23. data/lib/shakapacker/webpack_runner.rb +16 -8
  24. data/lib/tasks/shakapacker/binstubs.rake +1 -1
  25. data/lib/tasks/shakapacker/check_manager.rake +27 -0
  26. data/lib/tasks/shakapacker/check_yarn.rake +2 -1
  27. data/lib/tasks/shakapacker/info.rake +20 -3
  28. data/lib/tasks/shakapacker/install.rake +1 -1
  29. data/lib/tasks/shakapacker/verify_install.rake +1 -1
  30. data/lib/tasks/shakapacker.rake +2 -2
  31. data/lib/tasks/webpacker/check_yarn.rake +1 -1
  32. data/package/__tests__/index.js +24 -0
  33. data/package/index.d.ts +1 -1
  34. data/package/index.js +8 -2
  35. data/package.json +2 -2
  36. data/shakapacker.gemspec +1 -0
  37. data/spec/backward_compatibility_specs/compiler_strategy_spec.rb +5 -3
  38. data/spec/backward_compatibility_specs/configuration_spec.rb +12 -4
  39. data/spec/backward_compatibility_specs/dev_server_runner_spec.rb +103 -42
  40. data/spec/backward_compatibility_specs/dev_server_spec.rb +1 -1
  41. data/spec/backward_compatibility_specs/digest_strategy_spec.rb +2 -0
  42. data/spec/backward_compatibility_specs/engine_rake_tasks_spec.rb +30 -7
  43. data/spec/backward_compatibility_specs/helper_spec.rb +20 -20
  44. data/spec/backward_compatibility_specs/instance_spec.rb +3 -3
  45. data/spec/backward_compatibility_specs/manifest_spec.rb +12 -12
  46. data/spec/backward_compatibility_specs/mtime_strategy_spec.rb +3 -3
  47. data/spec/backward_compatibility_specs/rake_tasks_spec.rb +9 -5
  48. data/spec/backward_compatibility_specs/webpack_runner_spec.rb +55 -25
  49. data/spec/dummy/config/webpack/commonWebpackConfig.js +1 -1
  50. data/spec/fixtures/beta_pnpm-lock.v7.yaml +116 -0
  51. data/spec/fixtures/beta_pnpm-lock.v8.yaml +2537 -0
  52. data/spec/fixtures/git_url_pnpm-lock.v7.yaml +126 -0
  53. data/spec/fixtures/git_url_pnpm-lock.v8.yaml +3728 -0
  54. data/spec/fixtures/github_url_pnpm-lock.v7.yaml +126 -0
  55. data/spec/fixtures/github_url_pnpm-lock.v8.yaml +3728 -0
  56. data/spec/fixtures/relative_path_pnpm-lock.v7.yaml +18 -0
  57. data/spec/fixtures/relative_path_pnpm-lock.v8.yaml +22 -0
  58. data/spec/fixtures/semver_caret_pnpm-lock.v7.yaml +117 -0
  59. data/spec/fixtures/semver_caret_pnpm-lock.v8.yaml +2558 -0
  60. data/spec/fixtures/semver_exact_pnpm-lock.v7.yaml +117 -0
  61. data/spec/fixtures/semver_exact_pnpm-lock.v8.yaml +2558 -0
  62. data/spec/fixtures/semver_tilde_pnpm-lock.v7.yaml +116 -0
  63. data/spec/fixtures/semver_tilde_pnpm-lock.v8.yaml +2558 -0
  64. data/spec/fixtures/without_pnpm-lock.v7.yaml +23 -0
  65. data/spec/fixtures/without_pnpm-lock.v8.yaml +27 -0
  66. data/spec/generator_specs/e2e_template/template.rb +31 -10
  67. data/spec/generator_specs/fake-bin/bun +10 -0
  68. data/spec/generator_specs/fake-bin/npm +10 -0
  69. data/spec/generator_specs/fake-bin/pnpm +10 -0
  70. data/spec/generator_specs/fake-bin/yarn +10 -0
  71. data/spec/generator_specs/generator_spec.rb +172 -18
  72. data/spec/mounted_app/package.json +1 -0
  73. data/spec/shakapacker/compiler_spec.rb +9 -5
  74. data/spec/shakapacker/compiler_strategy_spec.rb +5 -3
  75. data/spec/shakapacker/configuration_spec.rb +60 -2
  76. data/spec/shakapacker/dev_server_runner_spec.rb +108 -43
  77. data/spec/shakapacker/dev_server_spec.rb +1 -1
  78. data/spec/shakapacker/digest_strategy_spec.rb +2 -0
  79. data/spec/shakapacker/engine_rake_tasks_spec.rb +28 -6
  80. data/spec/shakapacker/helper_spec.rb +20 -20
  81. data/spec/shakapacker/instance_spec.rb +2 -2
  82. data/spec/shakapacker/manifest_spec.rb +12 -12
  83. data/spec/shakapacker/mtime_strategy_spec.rb +3 -3
  84. data/spec/shakapacker/rake_tasks_spec.rb +10 -6
  85. data/spec/shakapacker/shakapacker_spec.rb +4 -4
  86. data/spec/shakapacker/spec_helper_initializer.rb +18 -0
  87. data/spec/shakapacker/version_checker_spec.rb +468 -121
  88. data/spec/shakapacker/webpack_runner_spec.rb +55 -25
  89. data/spec/spec_helper.rb +2 -0
  90. data/spec/support/package_json_helpers.rb +16 -0
  91. metadata +42 -5
@@ -8,6 +8,18 @@ module Shakapacker
8
8
  class Misc
9
9
  extend FileUtils
10
10
 
11
+ def self.use_package_json_gem
12
+ ENV.fetch("SHAKAPACKER_USE_PACKAGE_JSON_GEM", "false").casecmp("true").zero?
13
+ end
14
+
15
+ def self.require_package_json_gem
16
+ unless use_package_json_gem
17
+ raise "PackageJson should not be used unless SHAKAPACKER_USE_PACKAGE_JSON_GEM is true"
18
+ end
19
+
20
+ require "package_json"
21
+ end
22
+
11
23
  def self.uncommitted_changes?(message_handler)
12
24
  return false if ENV["COVERAGE"] == "true"
13
25
 
@@ -1,4 +1,4 @@
1
1
  module Shakapacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "7.0.3".freeze
3
+ VERSION = "7.2.0.rc.0".freeze
4
4
  end
@@ -91,7 +91,7 @@ module Shakapacker
91
91
  attr_reader :package_json
92
92
 
93
93
  def self.build
94
- new(package_json_path, yarn_lock_path, package_lock_path)
94
+ new(package_json_path, yarn_lock_path, package_lock_path, pnpm_lock_path)
95
95
  end
96
96
 
97
97
  def self.package_json_path
@@ -106,10 +106,15 @@ module Shakapacker
106
106
  Rails.root.join("package-lock.json")
107
107
  end
108
108
 
109
- def initialize(package_json, yarn_lock, package_lock)
109
+ def self.pnpm_lock_path
110
+ Rails.root.join("pnpm-lock.yaml")
111
+ end
112
+
113
+ def initialize(package_json, yarn_lock, package_lock, pnpm_lock)
110
114
  @package_json = package_json
111
115
  @yarn_lock = yarn_lock
112
116
  @package_lock = package_lock
117
+ @pnpm_lock = pnpm_lock
113
118
  end
114
119
 
115
120
  def raw
@@ -170,6 +175,12 @@ module Shakapacker
170
175
  return version unless version.nil?
171
176
  end
172
177
 
178
+ if File.exist?(@pnpm_lock)
179
+ version = from_pnpm_lock
180
+
181
+ return version unless version.nil?
182
+ end
183
+
173
184
  parsed_package_contents = JSON.parse(package_json_contents)
174
185
  parsed_package_contents.dig("dependencies", "shakapacker").to_s
175
186
  end
@@ -210,6 +221,28 @@ module Shakapacker
210
221
 
211
222
  version
212
223
  end
224
+
225
+ def from_pnpm_lock
226
+ require "yaml"
227
+
228
+ content = YAML.load_file(@pnpm_lock)
229
+
230
+ content.fetch("packages", {}).each do |key, value|
231
+ return value["version"] if value["name"] == "shakapacker"
232
+
233
+ parts = key.split("/")
234
+
235
+ return parts[2] if parts[1] == "shakapacker"
236
+ next unless parts[1].start_with?("shakapacker@")
237
+
238
+ _, version = parts[1].split("@")
239
+
240
+ return version[0, version.index("(")] if version.include?("(")
241
+ return version
242
+ end
243
+
244
+ nil
245
+ end
213
246
  end
214
247
  end
215
248
  end
@@ -19,12 +19,9 @@ module Shakapacker
19
19
  def run
20
20
  env = Shakapacker::Compiler.env
21
21
  env["SHAKAPACKER_CONFIG"] = @shakapacker_config
22
+ env["NODE_OPTIONS"] = ENV["NODE_OPTIONS"] || ""
22
23
 
23
- cmd = if node_modules_bin_exist?
24
- ["#{@node_modules_bin_path}/webpack"]
25
- else
26
- ["yarn", "webpack"]
27
- end
24
+ cmd = build_cmd
28
25
 
29
26
  if @argv.include?("--debug-webpacker")
30
27
  Shakapacker.puts_deprecation_message(
@@ -36,15 +33,15 @@ module Shakapacker
36
33
  end
37
34
 
38
35
  if @argv.delete("--debug-shakapacker") || @argv.delete("--debug-webpacker")
39
- cmd = ["node", "--inspect-brk"] + cmd
36
+ env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --inspect-brk"
40
37
  end
41
38
 
42
39
  if @argv.delete "--trace-deprecation"
43
- cmd = ["node", "--trace-deprecation"] + cmd
40
+ env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --trace-deprecation"
44
41
  end
45
42
 
46
43
  if @argv.delete "--no-deprecation"
47
- cmd = ["node", "--no-deprecation"] + cmd
44
+ env["NODE_OPTIONS"] = "#{env["NODE_OPTIONS"]} --no-deprecation"
48
45
  end
49
46
 
50
47
  # Webpack commands are not compatible with --config option.
@@ -60,6 +57,17 @@ module Shakapacker
60
57
  end
61
58
 
62
59
  private
60
+
61
+ def build_cmd
62
+ if Shakapacker::Utils::Misc.use_package_json_gem
63
+ return package_json.manager.native_exec_command("webpack")
64
+ end
65
+
66
+ return ["#{@node_modules_bin_path}/webpack"] if node_modules_bin_exist?
67
+
68
+ ["yarn", "webpack"]
69
+ end
70
+
63
71
  def node_modules_bin_exist?
64
72
  File.exist?("#{@node_modules_bin_path}/webpack")
65
73
  end
@@ -3,7 +3,7 @@ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
3
 
4
4
  namespace :shakapacker do
5
5
  desc "Installs Shakapacker binstubs in this application"
6
- task binstubs: [:check_node, :check_yarn] do |task|
6
+ task binstubs: [:check_node, :check_manager] do |task|
7
7
  prefix = task.name.split(/#|shakapacker:binstubs/).first
8
8
 
9
9
  if Rails::VERSION::MAJOR >= 5
@@ -0,0 +1,27 @@
1
+ require "shakapacker/utils/misc"
2
+
3
+ namespace :shakapacker do
4
+ desc "Verifies if the expected JS package manager is installed"
5
+ task :check_manager do |task|
6
+ unless Shakapacker::Utils::Misc.use_package_json_gem
7
+ prefix = task.name.split(/#|shakapacker:/).first
8
+ Rake::Task["#{prefix}shakapacker:check_manager"].invoke
9
+ next
10
+ end
11
+
12
+ require "package_json"
13
+
14
+ package_json = PackageJson.read
15
+ pm = package_json.manager.binary
16
+
17
+ begin
18
+ version = package_json.manager.version
19
+
20
+ $stdout.puts "using #{pm}@#{version} to manage dependencies and scripts in package.json"
21
+ rescue PackageJson::Error
22
+ $stderr.puts "#{pm} not installed - please ensure it is installed before trying again"
23
+ $stderr.puts "Exiting!"
24
+ exit!
25
+ end
26
+ end
27
+ end
@@ -1,7 +1,8 @@
1
- require "semantic_range"
2
1
  namespace :shakapacker do
3
2
  desc "Verifies if Yarn is installed"
4
3
  task :check_yarn do
4
+ require "semantic_range"
5
+
5
6
  begin
6
7
  which_command = Gem.win_platform? ? "where" : "which"
7
8
  raise Errno::ENOENT if `#{which_command} yarn`.strip.empty?
@@ -1,4 +1,5 @@
1
1
  require "shakapacker/version"
2
+ require "shakapacker/utils/misc"
2
3
 
3
4
  namespace :shakapacker do
4
5
  desc "Provide information on Shakapacker's environment"
@@ -8,14 +9,30 @@ namespace :shakapacker do
8
9
  $stdout.puts "Rails: #{Rails.version}"
9
10
  $stdout.puts "Shakapacker: #{Shakapacker::VERSION}"
10
11
  $stdout.puts "Node: #{`node --version`}"
11
- $stdout.puts "Yarn: #{`yarn --version`}"
12
+ if Shakapacker::Utils::Misc.use_package_json_gem
13
+ require "package_json"
14
+
15
+ pj_manager = PackageJson.read.manager
16
+
17
+ $stdout.puts "#{pj_manager.binary}: #{pj_manager.version}"
18
+ else
19
+ $stdout.puts "Yarn: #{`yarn --version`}"
20
+ end
21
+
22
+ if Shakapacker::Utils::Misc.use_package_json_gem
23
+ node_package_version = Shakapacker::VersionChecker.build.node_package_version.raw
24
+ else
25
+ node_package_version = `npm list shakapacker version`
26
+ end
12
27
 
13
28
  $stdout.puts "\n"
14
- $stdout.puts "shakapacker: \n#{`npm list shakapacker version`}"
29
+ $stdout.puts "shakapacker: #{node_package_version}"
15
30
 
16
31
  $stdout.puts "Is bin/shakapacker present?: #{File.exist? 'bin/shakapacker'}"
17
32
  $stdout.puts "Is bin/shakapacker-dev-server present?: #{File.exist? 'bin/shakapacker-dev-server'}"
18
- $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
33
+ unless Shakapacker::Utils::Misc.use_package_json_gem
34
+ $stdout.puts "Is bin/yarn present?: #{File.exist? 'bin/yarn'}"
35
+ end
19
36
  end
20
37
  end
21
38
  end
@@ -3,7 +3,7 @@ bin_path = ENV["BUNDLE_BIN"] || Rails.root.join("bin")
3
3
 
4
4
  namespace :shakapacker do
5
5
  desc "Install Shakapacker in this application"
6
- task install: [:check_node, :check_yarn] do |task|
6
+ task install: [:check_node] do |task|
7
7
  Shakapacker::Configuration.installing = true
8
8
 
9
9
  prefix = task.name.split(/#|shakapacker:install/).first
@@ -1,4 +1,4 @@
1
1
  namespace :shakapacker do
2
2
  desc "Verifies if Shakapacker is installed"
3
- task verify_install: [:verify_config, :check_node, :check_yarn, :check_binstubs]
3
+ task verify_install: [:verify_config, :check_node, :check_manager, :check_binstubs]
4
4
  end
@@ -1,11 +1,11 @@
1
1
  tasks = {
2
2
  "shakapacker:info" => "Provides information on Shakapacker's environment",
3
- "shakapacker:install" => "Installs and setup webpack with Yarn",
3
+ "shakapacker:install" => "Installs and setup webpack",
4
4
  "shakapacker:compile" => "Compiles webpack bundles based on environment",
5
5
  "shakapacker:clean" => "Remove old compiled bundles",
6
6
  "shakapacker:clobber" => "Removes the webpack compiled output directory",
7
7
  "shakapacker:check_node" => "Verifies if Node.js is installed",
8
- "shakapacker:check_yarn" => "Verifies if Yarn is installed",
8
+ "shakapacker:check_manager" => "Verifies if the expected JS package manager is available",
9
9
  "shakapacker:check_binstubs" => "Verifies that bin/shakapacker is present",
10
10
  "shakapacker:binstubs" => "Installs Shakapacker binstubs in this application",
11
11
  "shakapacker:verify_install" => "Verifies if Shakapacker is installed",
@@ -4,6 +4,6 @@ namespace :webpacker do
4
4
  Shakapacker.puts_rake_deprecation_message(task.name)
5
5
 
6
6
  prefix = task.name.split(/#|webpacker:/).first
7
- Rake::Task["#{prefix}shakapacker:check_yarn"].invoke
7
+ Rake::Task["#{prefix}shakapacker:check_manager"].invoke
8
8
  end
9
9
  end
@@ -1,4 +1,5 @@
1
1
  const index = require('../index')
2
+ const { generateWebpackConfig } = require("../index");
2
3
 
3
4
  describe('index', () => {
4
5
  test('exports webpack-merge v5 functions', () => {
@@ -19,4 +20,27 @@ describe('index', () => {
19
20
  expect(webpackConfig2).not.toHaveProperty('newKey')
20
21
  expect(webpackConfig2.output.path).not.toEqual('new value')
21
22
  })
23
+
24
+ test('webpackConfig merges extra config', () => {
25
+ const { generateWebpackConfig } = require('../index')
26
+
27
+ const webpackConfig = generateWebpackConfig({
28
+ newKey: 'new value',
29
+ output: {
30
+ path: 'new path'
31
+ }
32
+ })
33
+
34
+ expect(webpackConfig).toHaveProperty('newKey', 'new value')
35
+ expect(webpackConfig).toHaveProperty('output.path', 'new path')
36
+ expect(webpackConfig).toHaveProperty('output.publicPath', '/packs/')
37
+ })
38
+
39
+ test('webpackConfig errors if multiple configs are provided', () => {
40
+ const { generateWebpackConfig } = require('../index')
41
+
42
+ expect(() => generateWebpackConfig({}, {})).toThrow(
43
+ 'use webpack-merge to merge configs before passing them to Shakapacker'
44
+ )
45
+ })
22
46
  })
data/package/index.d.ts CHANGED
@@ -34,7 +34,7 @@ declare module 'shakapacker' {
34
34
 
35
35
  export const config: Config
36
36
  export const devServer: Record<string, unknown>
37
- export function generateWebpackConfig(): Configuration
37
+ export function generateWebpackConfig(extraConfig?: Configuration): Configuration
38
38
  export const globalMutableWebpackConfig: Configuration
39
39
  export const baseConfig: Configuration
40
40
  export const env: Env
data/package/index.js CHANGED
@@ -19,9 +19,15 @@ const globalMutableWebpackConfig = () => {
19
19
  return environmentConfig
20
20
  }
21
21
 
22
- const generateWebpackConfig = () => {
22
+ const generateWebpackConfig = (extraConfig = {}, ...extraArgs) => {
23
+ if (extraArgs.length > 0) {
24
+ throw new Error(
25
+ 'Only one extra config may be passed here - use webpack-merge to merge configs before passing them to Shakapacker'
26
+ )
27
+ }
28
+
23
29
  const environmentConfig = globalMutableWebpackConfig()
24
- const immutable = webpackMerge.merge({}, environmentConfig)
30
+ const immutable = webpackMerge.merge({}, environmentConfig, extraConfig)
25
31
  return immutable
26
32
  }
27
33
 
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shakapacker",
3
- "version": "7.0.3",
3
+ "version": "7.2.0-rc.0",
4
4
  "description": "Use webpack to manage app-like JavaScript modules in Rails",
5
5
  "main": "package/index.js",
6
6
  "types": "package/index.d.ts",
@@ -10,7 +10,7 @@
10
10
  ],
11
11
  "engines": {
12
12
  "node": "^12.13.0 || ^14 || >=16",
13
- "yarn": ">=1 <4"
13
+ "yarn": ">=1 <5"
14
14
  },
15
15
  "peerDependencies": {
16
16
  "@types/babel__core": "^7.0.0",
data/shakapacker.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.required_ruby_version = ">= 2.6.0"
19
19
 
20
20
  s.add_dependency "activesupport", ">= 5.2"
21
+ s.add_dependency "package_json"
21
22
  s.add_dependency "railties", ">= 5.2"
22
23
  s.add_dependency "rack-proxy", ">= 0.6.1"
23
24
  s.add_dependency "semantic_range", ">= 2.3.0"
@@ -2,17 +2,19 @@ require_relative "spec_helper_initializer"
2
2
 
3
3
  describe "Webpacker::CompilerStrategy" do
4
4
  describe "#from_config" do
5
- it "returns and instance of MtimeStrategy when compiler_strategy is set to mtime" do
5
+ it "returns an instance of MtimeStrategy when compiler_strategy is set to mtime" do
6
6
  allow(Webpacker.config).to receive(:compiler_strategy).and_return("mtime")
7
+
7
8
  expect(Webpacker::CompilerStrategy.from_config).to be_an_instance_of(Webpacker::MtimeStrategy)
8
9
  end
9
10
 
10
- it "returns and instance of DigestStrategy when compiler_strategy is set to digest" do
11
+ it "returns an instance of DigestStrategy when compiler_strategy is set to digest" do
11
12
  allow(Webpacker.config).to receive(:compiler_strategy).and_return("digest")
13
+
12
14
  expect(Webpacker::CompilerStrategy.from_config).to be_an_instance_of(Webpacker::DigestStrategy)
13
15
  end
14
16
 
15
- it "raise exception for unknown compiler_strategy in the config file" do
17
+ it "raise an exception for unknown compiler_strategy in the config file" do
16
18
  expected_error_message = "Unknown strategy 'other'. Available options are 'mtime' and 'digest'."
17
19
  allow(Webpacker.config).to receive(:compiler_strategy).and_return("other")
18
20
 
@@ -14,36 +14,43 @@ describe "Webpacker::Configuration" do
14
14
 
15
15
  it "#source_path returns correct path" do
16
16
  source_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/app/packs").to_s
17
+
17
18
  expect(config.source_path.to_s).to eq source_path
18
19
  end
19
20
 
20
21
  it "#source_entry_path returns correct path" do
21
22
  source_entry_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/app/packs", "entrypoints").to_s
23
+
22
24
  expect(config.source_entry_path.to_s).to eq source_entry_path
23
25
  end
24
26
 
25
27
  it "#public_root_path returns correct path" do
26
28
  public_root_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public").to_s
29
+
27
30
  expect(config.public_path.to_s).to eq public_root_path
28
31
  end
29
32
 
30
33
  it "#public_output_path returns correct path" do
31
34
  public_output_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public/packs").to_s
35
+
32
36
  expect(config.public_output_path.to_s).to eq public_output_path
33
37
  end
34
38
 
35
39
  it "#public_manifest_path returns correct path" do
36
40
  public_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public/packs", "manifest.json").to_s
41
+
37
42
  expect(config.public_manifest_path.to_s).to eq public_manifest_path
38
43
  end
39
44
 
40
45
  it "#manifest_path returns correct path" do
41
46
  manifest_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/public/packs", "manifest.json").to_s
47
+
42
48
  expect(config.manifest_path.to_s).to eq manifest_path
43
49
  end
44
50
 
45
51
  it "#cache_path returns correct path" do
46
52
  cache_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/tmp/webpacker").to_s
53
+
47
54
  expect(config.cache_path.to_s).to eq cache_path
48
55
  end
49
56
 
@@ -186,6 +193,7 @@ describe "Webpacker::Configuration" do
186
193
 
187
194
  it "#public_output_path returns correct path" do
188
195
  expected_public_output_path = File.expand_path File.join(File.dirname(__FILE__), "public/packs").to_s
196
+
189
197
  expect(config.public_output_path.to_s).to eq expected_public_output_path
190
198
  end
191
199
  end
@@ -199,6 +207,7 @@ describe "Webpacker::Configuration" do
199
207
 
200
208
  it "#manifest_path returns correct expected value" do
201
209
  expected_manifest_path = File.expand_path File.join(File.dirname(__FILE__), "webpacker_test_app/app/packs", "manifest.json").to_s
210
+
202
211
  expect(config.manifest_path.to_s).to eq expected_manifest_path
203
212
  end
204
213
  end
@@ -206,8 +215,8 @@ describe "Webpacker::Configuration" do
206
215
  context "with webpacker_precompile entry set to false" do
207
216
  describe "#webpacker_precompile?" do
208
217
  before :each do
209
- ENV.delete("WEBPACKER_PRECOMPILE")
210
218
  ENV.delete("SHAKAPACKER_PRECOMPILE")
219
+ ENV.delete("WEBPACKER_PRECOMPILE")
211
220
  end
212
221
 
213
222
  let(:config) {
@@ -229,9 +238,8 @@ describe "Webpacker::Configuration" do
229
238
  expect(subject).to be true
230
239
  end
231
240
 
232
- it "returns false with WEBPACKER_PRECOMPILE set to falsy value" do
233
- # ENV["WEBPACKER_PRECOMPILE"] = "no"
234
- ENV.delete("WEBPACKER_PRECOMPILE")
241
+ it "returns false with WEBPACKER_PRECOMPILE set to nil" do
242
+ ENV["SHAKAPACKER_PRECOMPILE"] = nil
235
243
  expect(subject).to be false
236
244
  end
237
245
  end
@@ -17,63 +17,124 @@ describe "DevServerRunner" do
17
17
 
18
18
  let(:test_app_path) { File.expand_path("webpacker_test_app", __dir__) }
19
19
 
20
- it "run cmd via node modules" do
21
- cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
22
- verify_command(cmd, use_node_modules: true)
23
- end
20
+ NODE_PACKAGE_MANAGERS.each do |fallback_manager|
21
+ context "when using package_json with #{fallback_manager} as the manager" do
22
+ with_use_package_json_gem(enabled: true, fallback_manager: fallback_manager)
24
23
 
25
- it "run cmd via yarn" do
26
- cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
27
- verify_command(cmd, use_node_modules: false)
28
- end
24
+ let(:package_json) { PackageJson.read(test_app_path) }
29
25
 
30
- it "run cmd argv" do
31
- cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"]
32
- verify_command(cmd, argv: (["--quiet"]))
33
- end
26
+ require "package_json"
27
+
28
+ it "uses the expected package manager", unless: fallback_manager == "yarn_classic" do
29
+ cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
30
+
31
+ manager_name = fallback_manager.split("_")[0]
32
+
33
+ expect(cmd).to start_with(manager_name)
34
+ end
35
+
36
+ it "runs the command using the manager" do
37
+ cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
38
+
39
+ verify_command(cmd)
40
+ end
34
41
 
35
- it "run cmd argv with https" do
36
- cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]
42
+ it "passes on arguments" do
43
+ cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"])
37
44
 
38
- dev_server = double()
39
- allow(dev_server).to receive(:host).and_return("localhost")
40
- allow(dev_server).to receive(:port).and_return("3035")
41
- allow(dev_server).to receive(:pretty?).and_return(false)
42
- allow(dev_server).to receive(:https?).and_return(true)
43
- allow(dev_server).to receive(:hmr?).and_return(false)
45
+ verify_command(cmd, argv: (["--quiet"]))
46
+ end
44
47
 
45
- allow(Webpacker::DevServer).to receive(:new) do
46
- verify_command(cmd, argv: (["--https"]))
47
- end.and_return(dev_server)
48
+ it "supports the https flag" do
49
+ cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"])
50
+
51
+ dev_server = double()
52
+ allow(dev_server).to receive(:host).and_return("localhost")
53
+ allow(dev_server).to receive(:port).and_return("3035")
54
+ allow(dev_server).to receive(:pretty?).and_return(false)
55
+ allow(dev_server).to receive(:https?).and_return(true)
56
+ allow(dev_server).to receive(:hmr?).and_return(false)
57
+
58
+ allow(Shakapacker::DevServer).to receive(:new) do
59
+ verify_command(cmd, argv: (["--https"]))
60
+ end.and_return(dev_server)
61
+ end
62
+
63
+ it "accepts environment variables" do
64
+ cmd = package_json.manager.native_exec_command("webpack", ["serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"])
65
+ env = Webpacker::Compiler.env.dup
66
+
67
+ # ENV["WEBPACKER_CONFIG"] is the interface and env["SHAKAPACKER_CONFIG"] is internal
68
+ ENV["WEBPACKER_CONFIG"] = env["SHAKAPACKER_CONFIG"] = "#{test_app_path}/config/webpacker_other_location.yml"
69
+ env["WEBPACK_SERVE"] = "true"
70
+
71
+ verify_command(cmd, env: env)
72
+ end
73
+ end
48
74
  end
49
75
 
50
- it "accepts environment variables" do
51
- cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
52
- env = Webpacker::Compiler.env.dup
76
+ context "when not using package_json" do
77
+ with_use_package_json_gem(enabled: false)
53
78
 
54
- # ENV["WEBPACKER_CONFIG"] is the interface and env["SHAKAPACKER_CONFIG"] is internal
55
- ENV["WEBPACKER_CONFIG"] = env["SHAKAPACKER_CONFIG"] = "#{test_app_path}/config/webpacker_other_location.yml"
56
- env["WEBPACK_SERVE"] = "true"
57
- verify_command(cmd, env: env)
79
+ it "supports running via node modules" do
80
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
81
+
82
+ verify_command(cmd, use_node_modules: true)
83
+ end
84
+
85
+ it "supports running via yarn" do
86
+ cmd = ["yarn", "webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
87
+
88
+ verify_command(cmd, use_node_modules: false)
89
+ end
90
+
91
+ it "passes on arguments" do
92
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--quiet"]
93
+
94
+ verify_command(cmd, argv: (["--quiet"]))
95
+ end
96
+
97
+ it "supports the https flag" do
98
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js", "--https"]
99
+
100
+ dev_server = double()
101
+ allow(dev_server).to receive(:host).and_return("localhost")
102
+ allow(dev_server).to receive(:port).and_return("3035")
103
+ allow(dev_server).to receive(:pretty?).and_return(false)
104
+ allow(dev_server).to receive(:https?).and_return(true)
105
+ allow(dev_server).to receive(:hmr?).and_return(false)
106
+
107
+ allow(Shakapacker::DevServer).to receive(:new) do
108
+ verify_command(cmd, argv: (["--https"]))
109
+ end.and_return(dev_server)
110
+ end
111
+
112
+ it "accepts environment variables" do
113
+ cmd = ["#{test_app_path}/node_modules/.bin/webpack", "serve", "--config", "#{test_app_path}/config/webpack/webpack.config.js"]
114
+ env = Webpacker::Compiler.env.dup
115
+
116
+ # ENV["WEBPACKER_CONFIG"] is the interface and env["SHAKAPACKER_CONFIG"] is internal
117
+ ENV["WEBPACKER_CONFIG"] = env["SHAKAPACKER_CONFIG"] = "#{test_app_path}/config/webpacker_other_location.yml"
118
+ env["WEBPACK_SERVE"] = "true"
119
+
120
+ verify_command(cmd, env: env)
121
+ end
58
122
  end
59
123
 
60
124
  private
61
125
 
62
126
  def verify_command(cmd, use_node_modules: true, argv: [], env: Webpacker::Compiler.env)
63
- cwd = Dir.pwd
64
- Dir.chdir(test_app_path)
65
- klass = Webpacker::DevServerRunner
66
- instance = klass.new(argv)
67
-
68
- allow(klass).to receive(:new).and_return(instance)
69
- allow(instance).to receive(:node_modules_bin_exist?).and_return(use_node_modules)
70
- allow(Kernel).to receive(:exec).with(env, *cmd)
127
+ Dir.chdir(test_app_path) do
128
+ klass = Webpacker::DevServerRunner
129
+ instance = klass.new(argv)
71
130
 
72
- klass.run(argv)
131
+ allow(klass).to receive(:new).and_return(instance)
132
+ allow(instance).to receive(:node_modules_bin_exist?).and_return(use_node_modules)
133
+ allow(Kernel).to receive(:exec).with(env, *cmd)
73
134
 
74
- expect(Kernel).to have_received(:exec).with(env, *cmd)
135
+ klass.run(argv)
75
136
 
76
- ensure
77
- Dir.chdir(cwd)
137
+ expect(Kernel).to have_received(:exec).with(env, *cmd)
138
+ end
78
139
  end
79
140
  end
@@ -41,7 +41,7 @@ describe "DevServer" do
41
41
  end
42
42
  end
43
43
 
44
- it "users SHAKAPACKER_DEV_SERVER for DEFAULT_ENV_PREFIX" do
44
+ it "uses SHAKAPACKER_DEV_SERVER for DEFAULT_ENV_PREFIX" do
45
45
  expect(Webpacker::DevServer::DEFAULT_ENV_PREFIX).to eq "SHAKAPACKER_DEV_SERVER"
46
46
  end
47
47
  end