webpacker 4.0.7 → 5.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +8 -8
  3. data/.github/workflows/jest.yml +38 -0
  4. data/.github/workflows/js-lint.yml +39 -0
  5. data/.github/workflows/rubocop.yml +39 -0
  6. data/.github/workflows/ruby.yml +46 -0
  7. data/.node-version +1 -1
  8. data/.rubocop.yml +118 -14
  9. data/CHANGELOG.md +301 -138
  10. data/CONTRIBUTING.md +1 -1
  11. data/Gemfile +1 -0
  12. data/Gemfile.lock +123 -92
  13. data/README.md +86 -211
  14. data/docs/css.md +72 -6
  15. data/docs/deployment.md +40 -11
  16. data/docs/docker.md +33 -14
  17. data/docs/engines.md +53 -3
  18. data/docs/env.md +0 -2
  19. data/docs/es6.md +19 -1
  20. data/docs/integrations.md +220 -0
  21. data/docs/target.md +22 -0
  22. data/docs/testing.md +2 -3
  23. data/docs/troubleshooting.md +39 -9
  24. data/docs/typescript.md +93 -26
  25. data/docs/webpack-dev-server.md +2 -2
  26. data/docs/webpack.md +18 -3
  27. data/gemfiles/Gemfile-rails-edge +1 -1
  28. data/gemfiles/{Gemfile-rails.4.2.x → Gemfile-rails.6.0.x} +1 -1
  29. data/lib/install/bin/webpack +0 -1
  30. data/lib/install/bin/webpack-dev-server +0 -1
  31. data/lib/install/coffee.rb +1 -1
  32. data/lib/install/config/babel.config.js +23 -13
  33. data/lib/install/config/webpacker.yml +3 -6
  34. data/lib/install/elm.rb +1 -1
  35. data/lib/install/erb.rb +2 -2
  36. data/lib/install/examples/angular/hello_angular/polyfills.ts +2 -2
  37. data/lib/install/examples/react/babel.config.js +28 -14
  38. data/lib/install/examples/react/tsconfig.json +2 -1
  39. data/lib/install/examples/svelte/app.svelte +11 -0
  40. data/lib/install/examples/svelte/hello_svelte.js +20 -0
  41. data/lib/install/examples/typescript/tsconfig.json +2 -1
  42. data/lib/install/loaders/elm.js +9 -6
  43. data/lib/install/loaders/svelte.js +9 -0
  44. data/lib/install/svelte.rb +29 -0
  45. data/lib/install/template.rb +14 -6
  46. data/lib/install/typescript.rb +6 -13
  47. data/lib/install/vue.rb +1 -1
  48. data/lib/tasks/installers.rake +1 -0
  49. data/lib/tasks/webpacker/check_node.rake +15 -8
  50. data/lib/tasks/webpacker/check_yarn.rake +16 -10
  51. data/lib/tasks/webpacker/clean.rake +25 -0
  52. data/lib/tasks/webpacker/clobber.rake +8 -4
  53. data/lib/tasks/webpacker/compile.rake +2 -10
  54. data/lib/tasks/webpacker/yarn_install.rake +11 -1
  55. data/lib/tasks/webpacker.rake +2 -0
  56. data/lib/webpacker/commands.rb +53 -1
  57. data/lib/webpacker/compiler.rb +21 -10
  58. data/lib/webpacker/configuration.rb +36 -13
  59. data/lib/webpacker/dev_server.rb +1 -1
  60. data/lib/webpacker/dev_server_proxy.rb +5 -9
  61. data/lib/webpacker/dev_server_runner.rb +6 -6
  62. data/lib/webpacker/env.rb +6 -2
  63. data/lib/webpacker/helper.rb +70 -25
  64. data/lib/webpacker/manifest.rb +4 -4
  65. data/lib/webpacker/railtie.rb +6 -43
  66. data/lib/webpacker/runner.rb +1 -0
  67. data/lib/webpacker/version.rb +1 -1
  68. data/lib/webpacker/webpack_runner.rb +2 -2
  69. data/lib/webpacker.rb +9 -1
  70. data/package/__tests__/config.js +12 -24
  71. data/package/__tests__/dev_server.js +2 -0
  72. data/package/__tests__/development.js +14 -1
  73. data/package/config.js +6 -11
  74. data/package/configPath.js +3 -0
  75. data/package/config_types/config_list.js +3 -3
  76. data/package/config_types/config_object.js +1 -1
  77. data/package/dev_server.js +1 -1
  78. data/package/env.js +1 -2
  79. data/package/environments/__tests__/base.js +29 -2
  80. data/package/environments/base.js +18 -8
  81. data/package/environments/development.js +39 -37
  82. data/package/environments/production.js +12 -2
  83. data/package/rules/babel.js +12 -5
  84. data/package/rules/file.js +3 -2
  85. data/package/rules/node_modules.js +3 -5
  86. data/package/rules/sass.js +11 -2
  87. data/package/utils/__tests__/get_style_rule.js +9 -0
  88. data/package/utils/deep_merge.js +5 -5
  89. data/package/utils/get_style_rule.js +7 -12
  90. data/package/utils/helpers.js +10 -10
  91. data/package.json +43 -43
  92. data/test/command_test.rb +6 -0
  93. data/test/compiler_test.rb +10 -6
  94. data/test/configuration_test.rb +40 -30
  95. data/test/dev_server_runner_test.rb +1 -1
  96. data/test/dev_server_test.rb +22 -0
  97. data/test/helper_test.rb +58 -9
  98. data/test/manifest_test.rb +37 -6
  99. data/test/rake_tasks_test.rb +17 -0
  100. data/test/test_app/app/javascript/packs/multi_entry.css +4 -0
  101. data/test/test_app/app/javascript/packs/multi_entry.js +4 -0
  102. data/test/test_app/bin/webpack +0 -1
  103. data/test/test_app/bin/webpack-dev-server +0 -1
  104. data/test/test_app/config/application.rb +0 -1
  105. data/test/test_app/config/webpacker.yml +8 -1
  106. data/test/test_app/public/packs/manifest.json +4 -0
  107. data/test/webpack_runner_test.rb +1 -1
  108. data/webpacker.gemspec +6 -4
  109. data/yarn.lock +4350 -4099
  110. metadata +63 -23
  111. data/.travis.yml +0 -53
  112. data/gemfiles/Gemfile-rails.5.0.x +0 -9
  113. data/gemfiles/Gemfile-rails.5.1.x +0 -9
  114. data/lib/install/loaders/typescript.js +0 -11
@@ -1,7 +1,7 @@
1
1
  # Install Webpacker
2
2
  copy_file "#{__dir__}/config/webpacker.yml", "config/webpacker.yml"
3
3
 
4
- puts "Copying webpack core config"
4
+ say "Copying webpack core config"
5
5
  directory "#{__dir__}/config/webpack", "config/webpack"
6
6
 
7
7
  say "Copying postcss.config.js to app root directory"
@@ -13,7 +13,7 @@ copy_file "#{__dir__}/config/babel.config.js", "babel.config.js"
13
13
  say "Copying .browserslistrc to app root directory"
14
14
  copy_file "#{__dir__}/config/.browserslistrc", ".browserslistrc"
15
15
 
16
- if Dir.exists?(Webpacker.config.source_path)
16
+ if Dir.exist?(Webpacker.config.source_path)
17
17
  say "The JavaScript app source directory already exists"
18
18
  else
19
19
  say "Creating JavaScript app source directory"
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  apply "#{__dir__}/binstubs.rb"
24
24
 
25
- if File.exists?(".gitignore")
25
+ if File.exist?(".gitignore")
26
26
  append_to_file ".gitignore" do
27
27
  "\n" +
28
28
  "/public/packs\n" +
@@ -34,16 +34,24 @@ if File.exists?(".gitignore")
34
34
  end
35
35
  end
36
36
 
37
- if Webpacker::VERSION =~ /^[0-9]+\.[0-9]+\.[0-9]+$/
37
+ if Webpacker::VERSION.match?(/^[0-9]+\.[0-9]+\.[0-9]+$/)
38
38
  say "Installing all JavaScript dependencies [#{Webpacker::VERSION}]"
39
- run "yarn add @rails/webpacker"
39
+ run "yarn add @rails/webpacker@#{Webpacker::VERSION}"
40
40
  else
41
41
  say "Installing all JavaScript dependencies [from prerelease rails/webpacker]"
42
42
  run "yarn add @rails/webpacker@next"
43
43
  end
44
44
 
45
+ package_json = File.read("#{__dir__}/../../package.json")
46
+ webpack_version = package_json.match(/"webpack": "(.*)"/)[1]
47
+ webpack_cli_version = package_json.match(/"webpack-cli": "(.*)"/)[1]
48
+
49
+ # needed for experimental Yarn 2 support and should not harm Yarn 1
50
+ say "Installing webpack and webpack-cli as direct dependencies"
51
+ run "yarn add webpack@#{webpack_version} webpack-cli@#{webpack_cli_version}"
52
+
45
53
  say "Installing dev server for live reloading"
46
- run "yarn add --dev webpack-dev-server"
54
+ run "yarn add --dev webpack-dev-server@^3"
47
55
 
48
56
  if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
49
57
  say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
@@ -9,23 +9,16 @@ if File.exist?(package_json)
9
9
  package = JSON.parse(File.read(package_json))
10
10
  package["dependencies"] ||= {}
11
11
 
12
- if package["dependencies"].keys.include?("react")
12
+ if package["dependencies"].key?("react")
13
13
  additional_packages = "@types/react @types/react-dom"
14
14
  example_source = "react"
15
15
  end
16
16
  end
17
17
 
18
- say "Copying typescript loader to config/webpack/loaders"
19
- copy_file "#{__dir__}/loaders/typescript.js", Rails.root.join("config/webpack/loaders/typescript.js").to_s
20
-
21
- say "Adding typescript loader to config/webpack/environment.js"
22
- insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
23
- "const typescript = require('./loaders/typescript')\n",
24
- after: "require('@rails/webpacker')\n"
25
-
26
- insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
27
- "environment.loaders.prepend('typescript', typescript)\n",
28
- before: "module.exports"
18
+ say "Adding TypeScript preset to babel.config.js"
19
+ insert_into_file Rails.root.join("babel.config.js").to_s,
20
+ ",\n ['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]",
21
+ before: /\s*\].filter\(Boolean\),\n\s*plugins: \[/
29
22
 
30
23
  say "Copying tsconfig.json to the Rails root directory for typescript"
31
24
  copy_file "#{__dir__}/examples/#{example_source}/tsconfig.json", "tsconfig.json"
@@ -41,6 +34,6 @@ copy_file "#{__dir__}/examples/typescript/hello_typescript.ts",
41
34
  "#{Webpacker.config.source_entry_path}/hello_typescript.ts"
42
35
 
43
36
  say "Installing all typescript dependencies"
44
- run "yarn add typescript ts-loader #{additional_packages}"
37
+ run "yarn add typescript @babel/preset-typescript #{additional_packages}"
45
38
 
46
39
  say "Webpacker now supports typescript 🎉", :green
data/lib/install/vue.rb CHANGED
@@ -6,7 +6,7 @@ copy_file "#{__dir__}/loaders/vue.js", Rails.root.join("config/webpack/loaders/v
6
6
  say "Adding vue loader plugin to config/webpack/environment.js"
7
7
  insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
8
8
  "const { VueLoaderPlugin } = require('vue-loader')\n",
9
- after: "require('@rails/webpacker')\n"
9
+ after: /require\(('|")@rails\/webpacker\1\);?\n/
10
10
 
11
11
  insert_into_file Rails.root.join("config/webpack/environment.js").to_s,
12
12
  "environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin())\n",
@@ -6,6 +6,7 @@ installers = {
6
6
  "Erb": :erb,
7
7
  "Coffee": :coffee,
8
8
  "Typescript": :typescript,
9
+ "Svelte": :svelte,
9
10
  "Stimulus": :stimulus
10
11
  }.freeze
11
12
 
@@ -1,24 +1,31 @@
1
+ require "semantic_range"
1
2
  namespace :webpacker do
2
3
  desc "Verifies if Node.js is installed"
3
4
  task :check_node do
4
5
  begin
5
- node_version = `node -v || nodejs -v`
6
+ node_version = `node -v || nodejs -v`.strip
6
7
  raise Errno::ENOENT if node_version.blank?
7
8
 
8
9
  pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
9
- node_requirement = JSON.parse(pkg_path.read)["engines"]["node"]
10
+ node_range = JSON.parse(pkg_path.read)["engines"]["node"]
11
+ is_valid = SemanticRange.satisfies?(node_version, node_range) rescue false
12
+ semver_major = node_version[/\d+/] rescue nil
13
+ is_unstable = semver_major.to_i.odd? rescue false
10
14
 
11
- requirement = Gem::Requirement.new(node_requirement)
12
- version = Gem::Version.new(node_version.strip.tr("v", ""))
15
+ if is_unstable
16
+ $stderr.puts "Warning: you are using an unstable release of Node.js (#{node_version}). If you encounter issues with Node.js, consider switching to an Active LTS release. More info: https://docs.npmjs.com/try-the-latest-stable-version-of-node"
17
+ end
13
18
 
14
- unless requirement.satisfied_by?(version)
15
- $stderr.puts "Webpacker requires Node.js #{requirement} and you are using #{version}"
19
+ unless is_valid
20
+ $stderr.puts "Webpacker requires Node.js \"#{node_range}\" and you are using #{node_version}"
16
21
  $stderr.puts "Please upgrade Node.js https://nodejs.org/en/download/"
17
- $stderr.puts "Exiting!" && exit!
22
+ $stderr.puts "Exiting!"
23
+ exit!
18
24
  end
19
25
  rescue Errno::ENOENT
20
26
  $stderr.puts "Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/"
21
- $stderr.puts "Exiting!" && exit!
27
+ $stderr.puts "Exiting!"
28
+ exit!
22
29
  end
23
30
  end
24
31
  end
@@ -1,24 +1,30 @@
1
+ require "semantic_range"
1
2
  namespace :webpacker do
2
3
  desc "Verifies if Yarn is installed"
3
4
  task :check_yarn do
4
5
  begin
5
- yarn_version = `yarn --version`
6
+ yarn_version = `yarn --version`.strip
6
7
  raise Errno::ENOENT if yarn_version.blank?
7
8
 
8
9
  pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
9
- yarn_requirement = JSON.parse(pkg_path.read)["engines"]["yarn"]
10
+ yarn_range = JSON.parse(pkg_path.read)["engines"]["yarn"]
11
+ is_valid = SemanticRange.satisfies?(yarn_version, yarn_range) rescue false
12
+ is_unsupported = SemanticRange.satisfies?(yarn_version, ">=4.0.0") rescue false
10
13
 
11
- requirement = Gem::Requirement.new(yarn_requirement)
12
- version = Gem::Version.new(yarn_version)
13
-
14
- unless requirement.satisfied_by?(version)
15
- $stderr.puts "Webpacker requires Yarn #{requirement} and you are using #{version}"
16
- $stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
17
- $stderr.puts "Exiting!" && exit!
14
+ unless is_valid
15
+ $stderr.puts "Webpacker requires Yarn \"#{yarn_range}\" and you are using #{yarn_version}"
16
+ if is_unsupported
17
+ $stderr.puts "This version of Webpacker does not support Yarn #{yarn_version}. Please downgrade to a supported version of Yarn https://yarnpkg.com/lang/en/docs/install/"
18
+ else
19
+ $stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
20
+ end
21
+ $stderr.puts "Exiting!"
22
+ exit!
18
23
  end
19
24
  rescue Errno::ENOENT
20
25
  $stderr.puts "Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/"
21
- $stderr.puts "Exiting!" && exit!
26
+ $stderr.puts "Exiting!"
27
+ exit!
22
28
  end
23
29
  end
24
30
  end
@@ -0,0 +1,25 @@
1
+ $stdout.sync = true
2
+
3
+ require "webpacker/configuration"
4
+
5
+ namespace :webpacker do
6
+ desc "Remove old compiled webpacks"
7
+ task :clean, [:keep, :age] => ["webpacker:verify_install", :environment] do |_, args|
8
+ Webpacker.ensure_log_goes_to_stdout do
9
+ Webpacker.clean(Integer(args.keep || 2), Integer(args.age || 3600))
10
+ end
11
+ end
12
+ end
13
+
14
+ skip_webpacker_clean = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
15
+
16
+ unless skip_webpacker_clean
17
+ # Run clean if the assets:clean is run
18
+ if Rake::Task.task_defined?("assets:clean")
19
+ Rake::Task["assets:clean"].enhance do
20
+ Rake::Task["webpacker:clean"].invoke
21
+ end
22
+ else
23
+ Rake::Task.define_task("assets:clean" => "webpacker:clean")
24
+ end
25
+ end
@@ -8,9 +8,13 @@ namespace :webpacker do
8
8
  end
9
9
  end
10
10
 
11
- # Run clobber if the assets:clobber is run
12
- if Rake::Task.task_defined?("assets:clobber")
13
- Rake::Task["assets:clobber"].enhance do
14
- Rake::Task["webpacker:clobber"].invoke
11
+ skip_webpacker_clobber = %w(no false n f).include?(ENV["WEBPACKER_PRECOMPILE"])
12
+
13
+ unless skip_webpacker_clobber
14
+ # Run clobber if the assets:clobber is run
15
+ if Rake::Task.task_defined?("assets:clobber")
16
+ Rake::Task["assets:clobber"].enhance do
17
+ Rake::Task["webpacker:clobber"].invoke
18
+ end
15
19
  end
16
20
  end
@@ -1,13 +1,5 @@
1
1
  $stdout.sync = true
2
2
 
3
- def ensure_log_goes_to_stdout
4
- old_logger = Webpacker.logger
5
- Webpacker.logger = ActiveSupport::Logger.new(STDOUT)
6
- yield
7
- ensure
8
- Webpacker.logger = old_logger
9
- end
10
-
11
3
  def yarn_install_available?
12
4
  rails_major = Rails::VERSION::MAJOR
13
5
  rails_minor = Rails::VERSION::MINOR
@@ -26,8 +18,8 @@ end
26
18
  namespace :webpacker do
27
19
  desc "Compile JavaScript packs using webpack for production with digests"
28
20
  task compile: ["webpacker:verify_install", :environment] do
29
- Webpacker.with_node_env("production") do
30
- ensure_log_goes_to_stdout do
21
+ Webpacker.with_node_env(ENV.fetch("NODE_ENV", "production")) do
22
+ Webpacker.ensure_log_goes_to_stdout do
31
23
  if Webpacker.compile
32
24
  # Successful compilation!
33
25
  else
@@ -1,6 +1,16 @@
1
1
  namespace :webpacker do
2
2
  desc "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
3
3
  task :yarn_install do
4
- system "yarn install --no-progress"
4
+ valid_node_envs = %w[test development production]
5
+ node_env = ENV.fetch("NODE_ENV") do
6
+ valid_node_envs.include?(Rails.env) ? Rails.env : "production"
7
+ end
8
+ yarn_flags =
9
+ if `yarn --version`.start_with?("1")
10
+ "--no-progress --frozen-lockfile"
11
+ else
12
+ "--immutable"
13
+ end
14
+ system({ "NODE_ENV" => node_env }, "yarn install #{yarn_flags}")
5
15
  end
6
16
  end
@@ -2,6 +2,7 @@ tasks = {
2
2
  "webpacker:info" => "Provides information on Webpacker's environment",
3
3
  "webpacker:install" => "Installs and setup webpack with Yarn",
4
4
  "webpacker:compile" => "Compiles webpack bundles based on environment",
5
+ "webpacker:clean" => "Remove old compiled webpacks",
5
6
  "webpacker:clobber" => "Removes the webpack compiled output directory",
6
7
  "webpacker:check_node" => "Verifies if Node.js is installed",
7
8
  "webpacker:check_yarn" => "Verifies if Yarn is installed",
@@ -13,6 +14,7 @@ tasks = {
13
14
  "webpacker:install:vue" => "Installs and setup example Vue component",
14
15
  "webpacker:install:angular" => "Installs and setup example Angular component",
15
16
  "webpacker:install:elm" => "Installs and setup example Elm component",
17
+ "webpacker:install:svelte" => "Installs and setup example Svelte component",
16
18
  "webpacker:install:stimulus" => "Installs and setup example Stimulus component",
17
19
  "webpacker:install:erb" => "Installs Erb loader with an example",
18
20
  "webpacker:install:coffee" => "Installs CoffeeScript loader with an example",
@@ -1,10 +1,43 @@
1
1
  class Webpacker::Commands
2
- delegate :config, :compiler, :manifest, to: :@webpacker
2
+ delegate :config, :compiler, :manifest, :logger, to: :@webpacker
3
3
 
4
4
  def initialize(webpacker)
5
5
  @webpacker = webpacker
6
6
  end
7
7
 
8
+ # Cleanup old assets in the compile directory. By default it will
9
+ # keep the latest version, 2 backups created within the past hour.
10
+ #
11
+ # Examples
12
+ #
13
+ # To force only 1 backup to be kept, set count=1 and age=0.
14
+ #
15
+ # To only keep files created within the last 10 minutes, set count=0 and
16
+ # age=600.
17
+ #
18
+ def clean(count = 2, age = 3600)
19
+ if config.public_output_path.exist? && config.public_manifest_path.exist?
20
+ versions
21
+ .sort
22
+ .reverse
23
+ .each_with_index
24
+ .drop_while do |(mtime, _), index|
25
+ max_age = [0, Time.now - Time.at(mtime)].max
26
+ max_age < age || index < count
27
+ end
28
+ .each do |(_, files), index|
29
+ files.each do |file|
30
+ if File.file?(file)
31
+ File.delete(file)
32
+ logger.info "Removed #{file}"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ true
39
+ end
40
+
8
41
  def clobber
9
42
  config.public_output_path.rmtree if config.public_output_path.exist?
10
43
  config.cache_path.rmtree if config.cache_path.exist?
@@ -19,4 +52,23 @@ class Webpacker::Commands
19
52
  manifest.refresh if success
20
53
  end
21
54
  end
55
+
56
+ private
57
+ def versions
58
+ all_files = Dir.glob("#{config.public_output_path}/**/*")
59
+ manifest_config = Dir.glob("#{config.public_manifest_path}*")
60
+
61
+ packs = all_files - manifest_config - current_version
62
+ packs.reject { |file| File.directory?(file) }.group_by { |file| File.mtime(file).utc.to_i }
63
+ end
64
+
65
+ def current_version
66
+ packs = manifest.refresh.values.map do |value|
67
+ next if value.is_a?(Hash)
68
+
69
+ File.join(config.root_path, "public", "#{value}*")
70
+ end.compact
71
+
72
+ Dir.glob(packs).uniq
73
+ end
22
74
  end
@@ -4,6 +4,8 @@ require "digest/sha1"
4
4
  class Webpacker::Compiler
5
5
  # Additional paths that test compiler needs to watch
6
6
  # Webpacker::Compiler.watched_paths << 'bower_components'
7
+ #
8
+ # Deprecated. Use additional_paths in the YAML configuration instead.
7
9
  cattr_accessor(:watched_paths) { [] }
8
10
 
9
11
  # Additional environment variables that the compiler is being run with
@@ -19,9 +21,14 @@ class Webpacker::Compiler
19
21
  def compile
20
22
  if stale?
21
23
  run_webpack.tap do |success|
22
- record_compilation_digest if success
24
+ # We used to only record the digest on success
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
23
29
  end
24
30
  else
31
+ logger.info "Everything's up-to-date. Nothing to do"
25
32
  true
26
33
  end
27
34
  end
@@ -45,6 +52,8 @@ class Webpacker::Compiler
45
52
  end
46
53
 
47
54
  def watched_files_digest
55
+ warn "Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in webpacker.yml instead." unless watched_paths.empty?
56
+
48
57
  files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
49
58
  file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
50
59
  Digest::SHA1.hexdigest(file_ids.join("/"))
@@ -56,7 +65,7 @@ class Webpacker::Compiler
56
65
  end
57
66
 
58
67
  def run_webpack
59
- logger.info "Compiling"
68
+ logger.info "Compiling..."
60
69
 
61
70
  stdout, stderr, status = Open3.capture3(
62
71
  webpack_env,
@@ -67,12 +76,13 @@ class Webpacker::Compiler
67
76
  if status.success?
68
77
  logger.info "Compiled all packs in #{config.public_output_path}"
69
78
  logger.error "#{stderr}" unless stderr.empty?
70
- else
71
- logger.error "Compilation failed:\n#{stderr}"
72
- end
73
79
 
74
- if config.webpack_compile_output?
75
- logger.info stdout
80
+ if config.webpack_compile_output?
81
+ logger.info stdout
82
+ end
83
+ else
84
+ non_empty_streams = [stdout, stderr].delete_if(&:empty?)
85
+ logger.error "Compilation failed:\n#{non_empty_streams.join("\n\n")}"
76
86
  end
77
87
 
78
88
  status.success?
@@ -80,8 +90,8 @@ class Webpacker::Compiler
80
90
 
81
91
  def default_watched_paths
82
92
  [
83
- *config.resolved_paths_globbed,
84
- "#{config.source_path.relative_path_from(config.root_path)}/**/*",
93
+ *config.additional_paths_globbed,
94
+ config.source_path_globbed,
85
95
  "yarn.lock", "package.json",
86
96
  "config/webpack/**/*"
87
97
  ].freeze
@@ -95,6 +105,7 @@ class Webpacker::Compiler
95
105
  return env unless defined?(ActionController::Base)
96
106
 
97
107
  env.merge("WEBPACKER_ASSET_HOST" => ENV.fetch("WEBPACKER_ASSET_HOST", ActionController::Base.helpers.compute_asset_host),
98
- "WEBPACKER_RELATIVE_URL_ROOT" => ENV.fetch("WEBPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root))
108
+ "WEBPACKER_RELATIVE_URL_ROOT" => ENV.fetch("WEBPACKER_RELATIVE_URL_ROOT", ActionController::Base.relative_url_root),
109
+ "WEBPACKER_CONFIG" => webpacker.config_path.to_s)
99
110
  end
100
111
  end
@@ -23,12 +23,16 @@ class Webpacker::Configuration
23
23
  root_path.join(fetch(:source_path))
24
24
  end
25
25
 
26
- def resolved_paths
27
- fetch(:resolved_paths)
26
+ def source_path_globbed
27
+ globbed_path_with_extensions(source_path.relative_path_from(root_path))
28
28
  end
29
29
 
30
- def resolved_paths_globbed
31
- resolved_paths.map { |p| "#{p}/**/*" }
30
+ def additional_paths
31
+ fetch(:additional_paths) + resolved_paths
32
+ end
33
+
34
+ def additional_paths_globbed
35
+ additional_paths.map { |p| globbed_path_with_extensions(p) }
32
36
  end
33
37
 
34
38
  def source_entry_path
@@ -60,11 +64,7 @@ class Webpacker::Configuration
60
64
  end
61
65
 
62
66
  def check_yarn_integrity=(value)
63
- data[:check_yarn_integrity] = value
64
- end
65
-
66
- def check_yarn_integrity?
67
- fetch(:check_yarn_integrity)
67
+ warn "Webpacker::Configuration#check_yarn_integrity=(value) has been deprecated. The integrity check has been removed from Webpacker so changing this setting will have no effect."
68
68
  end
69
69
 
70
70
  def webpack_compile_output?
@@ -76,6 +76,14 @@ class Webpacker::Configuration
76
76
  end
77
77
 
78
78
  private
79
+ def resolved_paths
80
+ paths = data.fetch(:resolved_paths, [])
81
+
82
+ warn "The resolved_paths option has been deprecated. Use additional_paths instead." unless paths.empty?
83
+
84
+ paths
85
+ end
86
+
79
87
  def fetch(key)
80
88
  data.fetch(key, defaults[key])
81
89
  end
@@ -85,8 +93,12 @@ class Webpacker::Configuration
85
93
  end
86
94
 
87
95
  def load
88
- YAML.load(config_path.read)[env].deep_symbolize_keys
89
-
96
+ config = begin
97
+ YAML.load_file(config_path.to_s, aliases: true)
98
+ rescue ArgumentError
99
+ YAML.load_file(config_path.to_s)
100
+ end
101
+ config[env].deep_symbolize_keys
90
102
  rescue Errno::ENOENT => e
91
103
  raise "Webpacker configuration file not found #{config_path}. " \
92
104
  "Please run rails webpacker:install " \
@@ -99,7 +111,18 @@ class Webpacker::Configuration
99
111
  end
100
112
 
101
113
  def defaults
102
- @defaults ||= \
103
- HashWithIndifferentAccess.new(YAML.load_file(File.expand_path("../../install/config/webpacker.yml", __FILE__))[env])
114
+ @defaults ||= begin
115
+ path = File.expand_path("../../install/config/webpacker.yml", __FILE__)
116
+ config = begin
117
+ YAML.load_file(path, aliases: true)
118
+ rescue ArgumentError
119
+ YAML.load_file(path)
120
+ end
121
+ HashWithIndifferentAccess.new(config[env])
122
+ end
123
+ end
124
+
125
+ def globbed_path_with_extensions(path)
126
+ "#{path}/**/*{#{extensions.join(',')}}"
104
127
  end
105
128
  end
@@ -32,7 +32,7 @@ class Webpacker::DevServer
32
32
 
33
33
  def https?
34
34
  case fetch(:https)
35
- when true, "true"
35
+ when true, "true", Hash
36
36
  true
37
37
  else
38
38
  false
@@ -5,19 +5,15 @@ class Webpacker::DevServerProxy < Rack::Proxy
5
5
 
6
6
  def initialize(app = nil, opts = {})
7
7
  @webpacker = opts.delete(:webpacker) || Webpacker.instance
8
+ opts[:streaming] = false if Rails.env.test? && !opts.key?(:streaming)
8
9
  super
9
10
  end
10
11
 
11
- def rewrite_response(response)
12
- _status, headers, _body = response
13
- headers.delete "transfer-encoding"
14
- headers.delete "content-length" if dev_server.running? && dev_server.https?
15
- response
16
- end
17
-
18
12
  def perform_request(env)
19
13
  if env["PATH_INFO"].start_with?("/#{public_output_uri_path}") && dev_server.running?
20
- env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
14
+ env["HTTP_HOST"] = env["HTTP_X_FORWARDED_HOST"] = dev_server.host
15
+ env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
16
+ env["HTTP_PORT"] = env["HTTP_X_FORWARDED_PORT"] = dev_server.port.to_s
21
17
  env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = dev_server.protocol
22
18
  unless dev_server.https?
23
19
  env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = "off"
@@ -32,6 +28,6 @@ class Webpacker::DevServerProxy < Rack::Proxy
32
28
 
33
29
  private
34
30
  def public_output_uri_path
35
- config.public_output_path.relative_path_from(config.public_path)
31
+ config.public_output_path.relative_path_from(config.public_path).to_s + "/"
36
32
  end
37
33
  end
@@ -16,20 +16,20 @@ module Webpacker
16
16
  def load_config
17
17
  app_root = Pathname.new(@app_path)
18
18
 
19
- config = Configuration.new(
19
+ @config = Configuration.new(
20
20
  root_path: app_root,
21
21
  config_path: app_root.join("config/webpacker.yml"),
22
22
  env: ENV["RAILS_ENV"]
23
23
  )
24
24
 
25
- dev_server = DevServer.new(config)
25
+ dev_server = DevServer.new(@config)
26
26
 
27
27
  @hostname = dev_server.host
28
28
  @port = dev_server.port
29
29
  @pretty = dev_server.pretty?
30
30
 
31
31
  rescue Errno::ENOENT, NoMethodError
32
- $stdout.puts "webpack dev_server configuration not found in #{config.config_path}[#{ENV["RAILS_ENV"]}]."
32
+ $stdout.puts "webpack dev_server configuration not found in #{@config.config_path}[#{ENV["RAILS_ENV"]}]."
33
33
  $stdout.puts "Please run bundle exec rails webpacker:install to install Webpacker"
34
34
  exit!
35
35
  end
@@ -39,12 +39,13 @@ module Webpacker
39
39
  server.close
40
40
 
41
41
  rescue Errno::EADDRINUSE
42
- $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config_file} for dev_server"
42
+ $stdout.puts "Another program is running on port #{@port}. Set a new port in #{@config.config_path} for dev_server"
43
43
  exit!
44
44
  end
45
45
 
46
46
  def execute_cmd
47
47
  env = Webpacker::Compiler.env
48
+ env["WEBPACKER_CONFIG"] = @webpacker_config
48
49
 
49
50
  cmd = if node_modules_bin_exist?
50
51
  ["#{@node_modules_bin_path}/webpack-dev-server"]
@@ -52,9 +53,8 @@ module Webpacker
52
53
  ["yarn", "webpack-dev-server"]
53
54
  end
54
55
 
55
- if ARGV.include?("--debug")
56
+ if @argv.include?("--debug-webpacker")
56
57
  cmd = [ "node", "--inspect-brk"] + cmd
57
- ARGV.delete("--debug")
58
58
  end
59
59
 
60
60
  cmd += ["--config", @webpack_config]
data/lib/webpacker/env.rb CHANGED
@@ -12,7 +12,7 @@ class Webpacker::Env
12
12
  end
13
13
 
14
14
  def inquire
15
- fallback_env_warning unless current
15
+ fallback_env_warning if config_path.exist? && !current
16
16
  current || DEFAULT.inquiry
17
17
  end
18
18
 
@@ -27,7 +27,11 @@ class Webpacker::Env
27
27
 
28
28
  def available_environments
29
29
  if config_path.exist?
30
- YAML.load(config_path.read).keys
30
+ begin
31
+ YAML.load_file(config_path.to_s, aliases: true)
32
+ rescue ArgumentError
33
+ YAML.load_file(config_path.to_s)
34
+ end
31
35
  else
32
36
  [].freeze
33
37
  end