webpacker 4.3.0 → 6.0.0.rc.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintrc.js +8 -8
  3. data/.github/workflows/jest.yml +30 -0
  4. data/.github/workflows/js-lint.yml +31 -0
  5. data/.github/workflows/rubocop.yml +39 -0
  6. data/.github/workflows/ruby.yml +48 -0
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +117 -16
  10. data/CHANGELOG.md +91 -5
  11. data/CONTRIBUTING.md +20 -1
  12. data/Gemfile +1 -0
  13. data/Gemfile.lock +121 -109
  14. data/README.md +365 -387
  15. data/config/README.md +3 -0
  16. data/config/webpacker.yml +1 -0
  17. data/docs/deployment.md +9 -11
  18. data/docs/developing_webpacker.md +29 -0
  19. data/docs/troubleshooting.md +64 -26
  20. data/docs/v6_upgrade.md +113 -0
  21. data/gemfiles/Gemfile-rails-edge +1 -1
  22. data/gemfiles/{Gemfile-rails.4.2.x → Gemfile-rails.6.1.x} +4 -1
  23. data/lib/install/{javascript/packs/application.js → application.js} +2 -5
  24. data/lib/install/bin/webpack +4 -7
  25. data/lib/install/bin/yarn +18 -0
  26. data/lib/install/config/webpack/base.js +3 -0
  27. data/lib/install/config/webpack/development.js +2 -2
  28. data/lib/install/config/webpack/production.js +2 -2
  29. data/lib/install/config/webpack/test.js +2 -2
  30. data/lib/install/config/webpacker.yml +22 -54
  31. data/lib/install/package.json +15 -0
  32. data/lib/install/template.rb +66 -23
  33. data/lib/tasks/webpacker/binstubs.rake +6 -4
  34. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  35. data/lib/tasks/webpacker/check_node.rake +18 -8
  36. data/lib/tasks/webpacker/check_yarn.rake +19 -10
  37. data/lib/tasks/webpacker/clean.rake +12 -6
  38. data/lib/tasks/webpacker/clobber.rake +9 -5
  39. data/lib/tasks/webpacker/compile.rake +4 -2
  40. data/lib/tasks/webpacker/info.rake +12 -10
  41. data/lib/tasks/webpacker/install.rake +6 -4
  42. data/lib/tasks/webpacker/verify_config.rake +14 -0
  43. data/lib/tasks/webpacker/verify_install.rake +1 -10
  44. data/lib/tasks/webpacker/yarn_install.rake +13 -16
  45. data/lib/tasks/webpacker.rake +2 -11
  46. data/lib/tasks/yarn.rake +38 -0
  47. data/lib/webpacker/commands.rb +40 -11
  48. data/lib/webpacker/compiler.rb +23 -12
  49. data/lib/webpacker/configuration.rb +24 -33
  50. data/lib/webpacker/dev_server.rb +6 -0
  51. data/lib/webpacker/dev_server_proxy.rb +3 -1
  52. data/lib/webpacker/dev_server_runner.rb +34 -10
  53. data/lib/webpacker/env.rb +5 -1
  54. data/lib/webpacker/helper.rb +72 -87
  55. data/lib/webpacker/instance.rb +4 -0
  56. data/lib/webpacker/manifest.rb +6 -7
  57. data/lib/webpacker/railtie.rb +8 -45
  58. data/lib/webpacker/runner.rb +1 -0
  59. data/lib/webpacker/version.rb +1 -1
  60. data/lib/webpacker/webpack_runner.rb +30 -4
  61. data/lib/webpacker.rb +1 -1
  62. data/package/__tests__/config.js +6 -27
  63. data/package/__tests__/dev_server.js +2 -0
  64. data/package/__tests__/development.js +18 -13
  65. data/package/__tests__/env.js +16 -4
  66. data/package/__tests__/index.js +9 -0
  67. data/package/__tests__/production.js +6 -6
  68. data/package/__tests__/staging.js +7 -6
  69. data/package/__tests__/test.js +4 -5
  70. data/package/babel/preset.js +54 -0
  71. data/package/config.js +7 -12
  72. data/package/configPath.js +3 -0
  73. data/package/dev_server.js +1 -1
  74. data/package/env.js +14 -6
  75. data/package/environments/__tests__/base.js +26 -31
  76. data/package/environments/base.js +78 -128
  77. data/package/environments/development.js +47 -43
  78. data/package/environments/production.js +66 -66
  79. data/package/environments/test.js +2 -2
  80. data/package/index.js +15 -8
  81. data/package/inliningCss.js +7 -0
  82. data/package/rules/babel.js +20 -11
  83. data/package/rules/coffee.js +6 -0
  84. data/package/rules/erb.js +15 -0
  85. data/package/rules/file.js +21 -18
  86. data/package/rules/index.js +16 -18
  87. data/package/rules/less.js +22 -0
  88. data/package/rules/raw.js +5 -0
  89. data/package/rules/sass.js +14 -6
  90. data/package/rules/stylus.js +26 -0
  91. data/package/utils/get_style_rule.js +28 -36
  92. data/package/utils/helpers.js +28 -35
  93. data/package.json +26 -46
  94. data/test/command_test.rb +76 -0
  95. data/test/compiler_test.rb +4 -11
  96. data/test/configuration_test.rb +5 -35
  97. data/test/dev_server_runner_test.rb +36 -6
  98. data/test/engine_rake_tasks_test.rb +39 -0
  99. data/test/helper_test.rb +77 -60
  100. data/test/manifest_test.rb +53 -6
  101. data/test/mounted_app/Rakefile +4 -0
  102. data/test/mounted_app/test/dummy/Rakefile +3 -0
  103. data/test/mounted_app/test/dummy/bin/rails +3 -0
  104. data/test/mounted_app/test/dummy/bin/rake +3 -0
  105. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  106. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  107. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  108. data/test/mounted_app/test/dummy/config.ru +5 -0
  109. data/test/mounted_app/test/dummy/package.json +7 -0
  110. data/test/rake_tasks_test.rb +12 -10
  111. data/test/test_app/app/{javascript/packs → packs/entrypoints}/application.js +1 -1
  112. data/test/test_app/app/packs/entrypoints/multi_entry.css +4 -0
  113. data/test/test_app/app/packs/entrypoints/multi_entry.js +4 -0
  114. data/test/test_app/config/application.rb +0 -1
  115. data/test/test_app/config/initializers/inspect_autoload_paths.rb +1 -0
  116. data/test/test_app/config/webpacker.yml +7 -27
  117. data/test/test_app/config/webpacker_other_location.yml +79 -0
  118. data/test/test_app/config/webpacker_public_root.yml +0 -1
  119. data/test/test_app/public/packs/manifest.json +36 -17
  120. data/test/test_app/some.config.js +0 -0
  121. data/test/webpack_runner_test.rb +10 -4
  122. data/test/webpacker_test.rb +21 -0
  123. data/webpacker.gemspec +5 -4
  124. data/yarn.lock +3387 -6898
  125. metadata +90 -99
  126. data/.travis.yml +0 -57
  127. data/docs/assets.md +0 -119
  128. data/docs/cloud9.md +0 -310
  129. data/docs/css.md +0 -254
  130. data/docs/docker.md +0 -68
  131. data/docs/engines.md +0 -200
  132. data/docs/env.md +0 -65
  133. data/docs/es6.md +0 -72
  134. data/docs/folder-structure.md +0 -66
  135. data/docs/misc.md +0 -23
  136. data/docs/props.md +0 -223
  137. data/docs/testing.md +0 -137
  138. data/docs/typescript.md +0 -126
  139. data/docs/v4-upgrade.md +0 -142
  140. data/docs/webpack-dev-server.md +0 -92
  141. data/docs/webpack.md +0 -364
  142. data/docs/yarn.md +0 -23
  143. data/gemfiles/Gemfile-rails.5.0.x +0 -9
  144. data/gemfiles/Gemfile-rails.5.1.x +0 -9
  145. data/lib/install/angular.rb +0 -23
  146. data/lib/install/coffee.rb +0 -25
  147. data/lib/install/config/.browserslistrc +0 -1
  148. data/lib/install/config/babel.config.js +0 -72
  149. data/lib/install/config/postcss.config.js +0 -12
  150. data/lib/install/config/webpack/environment.js +0 -3
  151. data/lib/install/elm.rb +0 -39
  152. data/lib/install/erb.rb +0 -25
  153. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  154. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  155. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  156. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  157. data/lib/install/examples/angular/hello_angular.js +0 -7
  158. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  159. data/lib/install/examples/elm/Main.elm +0 -55
  160. data/lib/install/examples/elm/hello_elm.js +0 -16
  161. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  162. data/lib/install/examples/react/babel.config.js +0 -87
  163. data/lib/install/examples/react/hello_react.jsx +0 -26
  164. data/lib/install/examples/react/tsconfig.json +0 -20
  165. data/lib/install/examples/stimulus/application.js +0 -1
  166. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  167. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  168. data/lib/install/examples/svelte/app.svelte +0 -11
  169. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  170. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  171. data/lib/install/examples/typescript/tsconfig.json +0 -23
  172. data/lib/install/examples/vue/app.vue +0 -22
  173. data/lib/install/examples/vue/hello_vue.js +0 -72
  174. data/lib/install/loaders/coffee.js +0 -6
  175. data/lib/install/loaders/elm.js +0 -25
  176. data/lib/install/loaders/erb.js +0 -11
  177. data/lib/install/loaders/svelte.js +0 -9
  178. data/lib/install/loaders/typescript.js +0 -11
  179. data/lib/install/loaders/vue.js +0 -6
  180. data/lib/install/react.rb +0 -18
  181. data/lib/install/stimulus.rb +0 -12
  182. data/lib/install/svelte.rb +0 -29
  183. data/lib/install/typescript.rb +0 -46
  184. data/lib/install/vue.rb +0 -49
  185. data/lib/tasks/installers.rake +0 -42
  186. data/package/config_types/__tests__/config_list.js +0 -118
  187. data/package/config_types/__tests__/config_object.js +0 -43
  188. data/package/config_types/config_list.js +0 -75
  189. data/package/config_types/config_object.js +0 -55
  190. data/package/config_types/index.js +0 -7
  191. data/package/rules/module.css.js +0 -3
  192. data/package/rules/module.sass.js +0 -8
  193. data/package/rules/node_modules.js +0 -24
  194. data/package/utils/__tests__/deep_assign.js +0 -32
  195. data/package/utils/__tests__/deep_merge.js +0 -10
  196. data/package/utils/__tests__/get_style_rule.js +0 -65
  197. data/package/utils/__tests__/objectify.js +0 -9
  198. data/package/utils/deep_assign.js +0 -22
  199. data/package/utils/deep_merge.js +0 -22
  200. data/package/utils/objectify.js +0 -3
@@ -7,52 +7,8 @@ class Webpacker::Engine < ::Rails::Engine
7
7
  # Allows Webpacker config values to be set via Rails env config files
8
8
  config.webpacker = ActiveSupport::OrderedOptions.new
9
9
 
10
- initializer "webpacker.set_configs" do |app|
11
- if app.config.webpacker.key?(:check_yarn_integrity)
12
- Webpacker.config.check_yarn_integrity = app.config.webpacker.check_yarn_integrity
13
- end
14
- end
15
-
16
- # ================================
17
- # Check Yarn Integrity Initializer
18
- # ================================
19
- #
20
- # development (on by default):
21
- #
22
- # to turn off:
23
- # - edit config/environments/development.rb
24
- # - add `config.webpacker.check_yarn_integrity = false`
25
- #
26
- # production (off by default):
27
- #
28
- # to turn on:
29
- # - edit config/environments/production.rb
30
- # - add `config.webpacker.check_yarn_integrity = true`
31
- initializer "webpacker.yarn_check" do |app|
32
- if File.exist?("yarn.lock") && Webpacker.config.config_path.exist? && Webpacker.config.check_yarn_integrity?
33
- output = `yarn check --integrity && yarn check --verify-tree 2>&1`
34
-
35
- unless $?.success?
36
- $stderr.puts "\n\n"
37
- $stderr.puts "========================================"
38
- $stderr.puts " Your Yarn packages are out of date!"
39
- $stderr.puts " Please run `yarn install --check-files` to update."
40
- $stderr.puts "========================================"
41
- $stderr.puts "\n\n"
42
- $stderr.puts "To disable this check, please change `check_yarn_integrity`"
43
- $stderr.puts "to `false` in your webpacker config file (config/webpacker.yml)."
44
- $stderr.puts "\n\n"
45
- $stderr.puts output
46
- $stderr.puts "\n\n"
47
-
48
- exit(1)
49
- end
50
- end
51
- end
52
-
53
10
  initializer "webpacker.proxy" do |app|
54
- insert_middleware = Webpacker.config.dev_server.present? rescue nil
55
- if insert_middleware
11
+ if (Webpacker.config.dev_server.present? rescue nil)
56
12
  app.middleware.insert_before 0,
57
13
  Rails::VERSION::MAJOR >= 5 ?
58
14
  Webpacker::DevServerProxy : "Webpacker::DevServerProxy", ssl_verify_none: true
@@ -95,4 +51,11 @@ class Webpacker::Engine < ::Rails::Engine
95
51
  app.config.javascript_path = Webpacker.config.source_path.relative_path_from(Rails.root.join("app")).to_s
96
52
  end
97
53
  end
54
+
55
+ initializer "webpacker.remove_app_packs_from_the_autoload_paths" do
56
+ Rails.application.config.before_initialize do
57
+ source_path = Webpacker.config.source_path.to_s
58
+ ActiveSupport::Dependencies.autoload_paths.delete(source_path)
59
+ end
60
+ end
98
61
  end
@@ -12,6 +12,7 @@ module Webpacker
12
12
  @app_path = File.expand_path(".", Dir.pwd)
13
13
  @node_modules_bin_path = ENV["WEBPACKER_NODE_MODULES_BIN_PATH"] || `yarn bin`.chomp
14
14
  @webpack_config = File.join(@app_path, "config/webpack/#{ENV["NODE_ENV"]}.js")
15
+ @webpacker_config = ENV["WEBPACKER_CONFIG"] || File.join(@app_path, "config/webpacker.yml")
15
16
 
16
17
  unless File.exist?(@webpack_config)
17
18
  $stderr.puts "webpack config #{@webpack_config} not found, please run 'bundle exec rails webpacker:install' to install Webpacker with default configs or add the missing config file for your custom environment."
@@ -1,4 +1,4 @@
1
1
  module Webpacker
2
2
  # Change the version in package.json too, please!
3
- VERSION = "4.3.0".freeze
3
+ VERSION = "6.0.0.rc.6".freeze
4
4
  end
@@ -3,8 +3,22 @@ require "webpacker/runner"
3
3
 
4
4
  module Webpacker
5
5
  class WebpackRunner < Webpacker::Runner
6
+ WEBPACK_COMMANDS = [
7
+ "help",
8
+ "h",
9
+ "--help",
10
+ "-h",
11
+ "version",
12
+ "v",
13
+ "--version",
14
+ "-v",
15
+ "info",
16
+ "i"
17
+ ].freeze
18
+
6
19
  def run
7
20
  env = Webpacker::Compiler.env
21
+ env["WEBPACKER_CONFIG"] = @webpacker_config
8
22
 
9
23
  cmd = if node_modules_bin_exist?
10
24
  ["#{@node_modules_bin_path}/webpack"]
@@ -12,12 +26,24 @@ module Webpacker
12
26
  ["yarn", "webpack"]
13
27
  end
14
28
 
15
- if ARGV.include?("--debug")
16
- cmd = [ "node", "--inspect-brk"] + cmd
17
- ARGV.delete("--debug")
29
+ if @argv.delete "--debug-webpacker"
30
+ cmd = ["node", "--inspect-brk"] + cmd
31
+ end
32
+
33
+ if @argv.delete "--trace-deprecation"
34
+ cmd = ["node", "--trace-deprecation"] + cmd
35
+ end
36
+
37
+ if @argv.delete "--no-deprecation"
38
+ cmd = ["node", "--no-deprecation"] + cmd
39
+ end
40
+
41
+ # Webpack commands are not compatible with --config option.
42
+ if (@argv & WEBPACK_COMMANDS).empty?
43
+ cmd += ["--config", @webpack_config]
18
44
  end
19
45
 
20
- cmd += ["--config", @webpack_config] + @argv
46
+ cmd += @argv
21
47
 
22
48
  Dir.chdir(@app_path) do
23
49
  Kernel.exec env, *cmd
data/lib/webpacker.rb CHANGED
@@ -30,7 +30,7 @@ module Webpacker
30
30
  Webpacker.logger = old_logger
31
31
  end
32
32
 
33
- delegate :logger, :logger=, :env, to: :instance
33
+ delegate :logger, :logger=, :env, :inlining_css?, to: :instance
34
34
  delegate :config, :compiler, :manifest, :commands, :dev_server, to: :instance
35
35
  delegate :bootstrap, :clean, :clobber, :compile, to: :commands
36
36
  end
@@ -23,33 +23,12 @@ describe('Config', () => {
23
23
  expect(config.publicPath).toEqual('http://foo.com/packs/')
24
24
  })
25
25
 
26
- test('should return extensions as listed in app config', () => {
27
- expect(config.extensions).toEqual([
28
- '.mjs',
29
- '.js',
30
- '.sass',
31
- '.scss',
32
- '.css',
33
- '.module.sass',
34
- '.module.scss',
35
- '.module.css',
36
- '.png',
37
- '.svg',
38
- '.gif',
39
- '.jpeg',
40
- '.jpg'
41
- ])
42
- })
43
-
44
- test('should return static assets extensions as listed in app config', () => {
45
- expect(config.static_assets_extensions).toEqual([
46
- '.jpg',
47
- '.jpeg',
48
- '.png',
49
- '.gif',
50
- '.tiff',
51
- '.ico',
52
- '.svg',
26
+ test('should return additional paths as listed in app config, with resolved paths', () => {
27
+ expect(config.additional_paths).toEqual([
28
+ 'app/assets',
29
+ '/etc/yarn',
30
+ 'some.config.js',
31
+ 'app/elm'
53
32
  ])
54
33
  })
55
34
  })
@@ -13,11 +13,13 @@ describe('DevServer', () => {
13
13
  process.env.RAILS_ENV = 'development'
14
14
  process.env.WEBPACKER_DEV_SERVER_HOST = '0.0.0.0'
15
15
  process.env.WEBPACKER_DEV_SERVER_PORT = 5000
16
+ process.env.WEBPACKER_DEV_SERVER_DISABLE_HOST_CHECK = false
16
17
 
17
18
  const devServer = require('../dev_server')
18
19
  expect(devServer).toBeDefined()
19
20
  expect(devServer.host).toEqual('0.0.0.0')
20
21
  expect(devServer.port).toEqual('5000')
22
+ expect(devServer.disable_host_check).toBe(false)
21
23
  })
22
24
 
23
25
  test('with custom env prefix', () => {
@@ -8,23 +8,28 @@ chdirTestApp()
8
8
  describe('Development environment', () => {
9
9
  afterAll(chdirCwd)
10
10
 
11
- describe('toWebpackConfig', () => {
11
+ describe('webpackConfig', () => {
12
12
  beforeEach(() => jest.resetModules())
13
13
 
14
- test('should use development config and environment', () => {
14
+ test('should use development config and environment including devServer if WEBPACK_SERVE', () => {
15
15
  process.env.RAILS_ENV = 'development'
16
16
  process.env.NODE_ENV = 'development'
17
- const { environment } = require('../index')
18
-
19
- const config = environment.toWebpackConfig()
20
- expect(config.output.path).toEqual(resolve('public', 'packs'))
21
- expect(config.output.publicPath).toEqual('/packs/')
22
- expect(config).toMatchObject({
23
- devServer: {
24
- host: 'localhost',
25
- port: 3035
26
- }
27
- })
17
+ process.env.WEBPACK_SERVE = 'true'
18
+ const { webpackConfig } = require('../index')
19
+
20
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
21
+ expect(webpackConfig.output.publicPath).toEqual('/packs/')
22
+ })
23
+
24
+ test('should use development config and environment if WEBPACK_SERVE', () => {
25
+ process.env.RAILS_ENV = 'development'
26
+ process.env.NODE_ENV = 'development'
27
+ process.env.WEBPACK_SERVE = undefined
28
+ const { webpackConfig } = require('../index')
29
+
30
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
31
+ expect(webpackConfig.output.publicPath).toEqual('/packs/')
32
+ expect(webpackConfig.devServer).toEqual(undefined)
28
33
  })
29
34
  })
30
35
  })
@@ -13,7 +13,10 @@ describe('Env', () => {
13
13
  process.env.NODE_ENV = 'development'
14
14
  expect(require('../env')).toEqual({
15
15
  railsEnv: 'development',
16
- nodeEnv: 'development'
16
+ nodeEnv: 'development',
17
+ isProduction: false,
18
+ isDevelopment: true,
19
+ runningWebpackDevServer: false
17
20
  })
18
21
  })
19
22
 
@@ -22,7 +25,10 @@ describe('Env', () => {
22
25
  delete process.env.NODE_ENV
23
26
  expect(require('../env')).toEqual({
24
27
  railsEnv: 'development',
25
- nodeEnv: 'production'
28
+ nodeEnv: 'production',
29
+ isProduction: true,
30
+ isDevelopment: false,
31
+ runningWebpackDevServer: false
26
32
  })
27
33
  })
28
34
 
@@ -31,7 +37,10 @@ describe('Env', () => {
31
37
  delete process.env.RAILS_ENV
32
38
  expect(require('../env')).toEqual({
33
39
  railsEnv: 'production',
34
- nodeEnv: 'production'
40
+ nodeEnv: 'production',
41
+ isProduction: true,
42
+ isDevelopment: false,
43
+ runningWebpackDevServer: false
35
44
  })
36
45
  })
37
46
 
@@ -40,7 +49,10 @@ describe('Env', () => {
40
49
  process.env.NODE_ENV = 'staging'
41
50
  expect(require('../env')).toEqual({
42
51
  railsEnv: 'staging',
43
- nodeEnv: 'production'
52
+ nodeEnv: 'production',
53
+ isProduction: true,
54
+ isDevelopment: false,
55
+ runningWebpackDevServer: false
44
56
  })
45
57
  })
46
58
  })
@@ -0,0 +1,9 @@
1
+ const index = require('../index')
2
+
3
+ describe('index', () => {
4
+ test('exports webpack-merge v5 functions', () => {
5
+ expect(index.merge).toBeInstanceOf(Function)
6
+ expect(index.mergeWithRules).toBeInstanceOf(Function)
7
+ expect(index.mergeWithCustomize).toBeInstanceOf(Function)
8
+ })
9
+ })
@@ -8,19 +8,19 @@ chdirTestApp()
8
8
  describe('Production environment', () => {
9
9
  afterAll(chdirCwd)
10
10
 
11
- describe('toWebpackConfig', () => {
11
+ describe('webpackConfig', () => {
12
12
  beforeEach(() => jest.resetModules())
13
13
 
14
14
  test('should use production config and environment', () => {
15
15
  process.env.RAILS_ENV = 'production'
16
16
  process.env.NODE_ENV = 'production'
17
17
 
18
- const { environment } = require('../index')
19
- const config = environment.toWebpackConfig()
18
+ const { webpackConfig } = require('../index')
20
19
 
21
- expect(config.output.path).toEqual(resolve('public', 'packs'))
22
- expect(config.output.publicPath).toEqual('/packs/')
23
- expect(config).toMatchObject({
20
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs'))
21
+ expect(webpackConfig.output.publicPath).toEqual('/packs/')
22
+
23
+ expect(webpackConfig).toMatchObject({
24
24
  devtool: 'source-map',
25
25
  stats: 'normal'
26
26
  })
@@ -8,19 +8,20 @@ chdirTestApp()
8
8
  describe('Custom environment', () => {
9
9
  afterAll(chdirCwd)
10
10
 
11
- describe('toWebpackConfig', () => {
11
+ describe('webpackConfig', () => {
12
12
  beforeEach(() => jest.resetModules())
13
13
 
14
14
  test('should use staging config and default production environment', () => {
15
15
  process.env.RAILS_ENV = 'staging'
16
16
  delete process.env.NODE_ENV
17
17
 
18
- const { environment } = require('../index')
19
- const config = environment.toWebpackConfig()
18
+ const { webpackConfig } = require('../index')
20
19
 
21
- expect(config.output.path).toEqual(resolve('public', 'packs-staging'))
22
- expect(config.output.publicPath).toEqual('/packs-staging/')
23
- expect(config).toMatchObject({
20
+ expect(webpackConfig.output.path).toEqual(
21
+ resolve('public', 'packs-staging')
22
+ )
23
+ expect(webpackConfig.output.publicPath).toEqual('/packs-staging/')
24
+ expect(webpackConfig).toMatchObject({
24
25
  devtool: 'source-map',
25
26
  stats: 'normal'
26
27
  })
@@ -15,12 +15,11 @@ describe('Test environment', () => {
15
15
  process.env.RAILS_ENV = 'test'
16
16
  process.env.NODE_ENV = 'test'
17
17
 
18
- const { environment } = require('../index')
19
- const config = environment.toWebpackConfig()
18
+ const { webpackConfig } = require('../index')
20
19
 
21
- expect(config.output.path).toEqual(resolve('public', 'packs-test'))
22
- expect(config.output.publicPath).toEqual('/packs-test/')
23
- expect(config.devServer).toEqual(undefined)
20
+ expect(webpackConfig.output.path).toEqual(resolve('public', 'packs-test'))
21
+ expect(webpackConfig.output.publicPath).toEqual('/packs-test/')
22
+ expect(webpackConfig.devServer).toEqual(undefined)
24
23
  })
25
24
  })
26
25
  })
@@ -0,0 +1,54 @@
1
+ const { moduleExists } = require('@rails/webpacker')
2
+
3
+ module.exports = function config(api) {
4
+ const validEnv = ['development', 'test', 'production']
5
+ const currentEnv = api.env()
6
+ const isDevelopmentEnv = api.env('development')
7
+ const isProductionEnv = api.env('production')
8
+ const isTestEnv = api.env('test')
9
+
10
+ if (!validEnv.includes(currentEnv)) {
11
+ throw new Error(
12
+ `Please specify a valid NODE_ENV or BABEL_ENV environment variable. Valid values are "development", "test", and "production". Instead, received: "${JSON.stringify(
13
+ currentEnv
14
+ )}".`
15
+ )
16
+ }
17
+
18
+ return {
19
+ presets: [
20
+ isTestEnv && ['@babel/preset-env', { targets: { node: 'current' } }],
21
+ (isProductionEnv || isDevelopmentEnv) && [
22
+ '@babel/preset-env',
23
+ {
24
+ useBuiltIns: 'entry',
25
+ corejs: '3.8',
26
+ modules: 'auto',
27
+ bugfixes: true,
28
+ loose: true,
29
+ exclude: ['transform-typeof-symbol']
30
+ }
31
+ ],
32
+ moduleExists('@babel/preset-typescript') && [
33
+ '@babel/preset-typescript',
34
+ { allExtensions: true, isTSX: true }
35
+ ],
36
+ moduleExists('@babel/preset-react') && [
37
+ '@babel/preset-react',
38
+ {
39
+ development: isDevelopmentEnv || isTestEnv,
40
+ useBuiltIns: true
41
+ }
42
+ ]
43
+ ].filter(Boolean),
44
+ plugins: [
45
+ ['@babel/plugin-proposal-class-properties', { loose: true }],
46
+ ['@babel/plugin-transform-runtime', { helpers: false }],
47
+ isProductionEnv &&
48
+ moduleExists('babel-plugin-transform-react-remove-prop-types') && [
49
+ 'babel-plugin-transform-react-remove-prop-types',
50
+ { removeImport: true }
51
+ ]
52
+ ].filter(Boolean)
53
+ }
54
+ }
data/package/config.js CHANGED
@@ -1,27 +1,22 @@
1
1
  const { resolve } = require('path')
2
- const { safeLoad } = require('js-yaml')
2
+ const { load } = require('js-yaml')
3
3
  const { readFileSync } = require('fs')
4
- const deepMerge = require('./utils/deep_merge')
5
- const { isArray, ensureTrailingSlash } = require('./utils/helpers')
4
+ const { merge } = require('webpack-merge')
5
+ const { ensureTrailingSlash } = require('./utils/helpers')
6
6
  const { railsEnv } = require('./env')
7
+ const configPath = require('./configPath')
7
8
 
8
9
  const defaultConfigPath = require.resolve('../lib/install/config/webpacker.yml')
9
- const configPath = resolve('config', 'webpacker.yml')
10
10
 
11
11
  const getDefaultConfig = () => {
12
- const defaultConfig = safeLoad(readFileSync(defaultConfigPath), 'utf8')
12
+ const defaultConfig = load(readFileSync(defaultConfigPath), 'utf8')
13
13
  return defaultConfig[railsEnv] || defaultConfig.production
14
14
  }
15
15
 
16
16
  const defaults = getDefaultConfig()
17
- const app = safeLoad(readFileSync(configPath), 'utf8')[railsEnv]
17
+ const app = load(readFileSync(configPath), 'utf8')[railsEnv]
18
18
 
19
- if (isArray(app.extensions) && app.extensions.length) delete defaults.extensions
20
- if (isArray(app.static_assets_extensions) && app.static_assets_extensions.length) {
21
- delete defaults.static_assets_extensions
22
- }
23
-
24
- const config = deepMerge(defaults, app)
19
+ const config = merge(defaults, app)
25
20
  config.outputPath = resolve(config.public_root_path, config.public_output_path)
26
21
 
27
22
  // Ensure that the publicPath includes our asset host so dynamic imports
@@ -0,0 +1,3 @@
1
+ const { resolve } = require('path')
2
+
3
+ module.exports = process.env.WEBPACKER_CONFIG || resolve('config', 'webpacker.yml')
@@ -12,7 +12,7 @@ if (devServerConfig) {
12
12
  const envPrefix = config.dev_server.env_prefix || 'WEBPACKER_DEV_SERVER'
13
13
 
14
14
  Object.keys(devServerConfig).forEach((key) => {
15
- const envValue = fetch(`${envPrefix}_${key.toUpperCase().replace(/_/g, '')}`)
15
+ const envValue = fetch(`${envPrefix}_${key.toUpperCase()}`)
16
16
  if (envValue !== undefined) devServerConfig[key] = envValue
17
17
  })
18
18
  }
data/package/env.js CHANGED
@@ -1,19 +1,27 @@
1
- const { resolve } = require('path')
2
- const { safeLoad } = require('js-yaml')
1
+ const { load } = require('js-yaml')
3
2
  const { readFileSync } = require('fs')
4
3
 
5
4
  const NODE_ENVIRONMENTS = ['development', 'production', 'test']
6
5
  const DEFAULT = 'production'
7
- const configPath = resolve('config', 'webpacker.yml')
6
+ const configPath = require('./configPath')
8
7
 
9
8
  const railsEnv = process.env.RAILS_ENV
10
- const nodeEnv = process.env.NODE_ENV
9
+ const rawNodeEnv = process.env.NODE_ENV
10
+ const nodeEnv
11
+ = rawNodeEnv && NODE_ENVIRONMENTS.includes(rawNodeEnv) ? rawNodeEnv : DEFAULT
12
+ const isProduction = nodeEnv === 'production'
13
+ const isDevelopment = nodeEnv === 'development'
11
14
 
12
- const config = safeLoad(readFileSync(configPath), 'utf8')
15
+ const config = load(readFileSync(configPath), 'utf8')
13
16
  const availableEnvironments = Object.keys(config).join('|')
14
17
  const regex = new RegExp(`^(${availableEnvironments})$`, 'g')
15
18
 
19
+ const runningWebpackDevServer = process.env.WEBPACK_SERVE === 'true'
20
+
16
21
  module.exports = {
17
22
  railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,
18
- nodeEnv: nodeEnv && NODE_ENVIRONMENTS.includes(nodeEnv) ? nodeEnv : DEFAULT
23
+ nodeEnv,
24
+ isProduction,
25
+ isDevelopment,
26
+ runningWebpackDevServer
19
27
  }
@@ -9,66 +9,61 @@ chdirTestApp()
9
9
 
10
10
  const { resolve } = require('path')
11
11
  const rules = require('../../rules')
12
- const { ConfigList } = require('../../config_types')
13
- const Environment = require('../base')
12
+ const baseConfig = require('../base')
14
13
 
15
- describe('Environment', () => {
14
+ describe('Base config', () => {
16
15
  afterAll(chdirCwd)
17
16
 
18
- let environment
19
-
20
- describe('toWebpackConfig', () => {
21
- beforeEach(() => {
22
- environment = new Environment()
23
- })
24
-
17
+ describe('config', () => {
25
18
  test('should return entry', () => {
26
- const config = environment.toWebpackConfig()
27
- expect(config.entry.application).toEqual(
28
- resolve('app', 'javascript', 'packs', 'application.js')
19
+ expect(baseConfig.entry.application).toEqual(
20
+ resolve('app', 'packs', 'entrypoints', 'application.js')
29
21
  )
30
22
  })
31
23
 
24
+ test('should return multi file entry points', () => {
25
+ expect(baseConfig.entry.multi_entry.sort()).toEqual([
26
+ resolve('app', 'packs', 'entrypoints', 'multi_entry.css'),
27
+ resolve('app', 'packs', 'entrypoints', 'multi_entry.js')
28
+ ])
29
+ })
30
+
32
31
  test('should return output', () => {
33
- const config = environment.toWebpackConfig()
34
- expect(config.output.filename).toEqual('js/[name]-[contenthash].js')
35
- expect(config.output.chunkFilename).toEqual('js/[name]-[contenthash].chunk.js')
32
+ expect(baseConfig.output.filename).toEqual('js/[name].js')
33
+ expect(baseConfig.output.chunkFilename).toEqual(
34
+ 'js/[name].chunk.js'
35
+ )
36
36
  })
37
37
 
38
38
  test('should return default loader rules for each file in config/loaders', () => {
39
- const config = environment.toWebpackConfig()
40
39
  const defaultRules = Object.keys(rules)
41
- const configRules = config.module.rules
40
+ const configRules = baseConfig.module.rules
42
41
 
43
- expect(defaultRules.length).toEqual(7)
44
- expect(configRules.length).toEqual(8)
42
+ expect(defaultRules.length).toEqual(3)
43
+ expect(configRules.length).toEqual(3)
45
44
  })
46
45
 
47
46
  test('should return default plugins', () => {
48
- const config = environment.toWebpackConfig()
49
- expect(config.plugins.length).toEqual(4)
47
+ expect(baseConfig.plugins.length).toEqual(2)
50
48
  })
51
49
 
52
50
  test('should return default resolveLoader', () => {
53
- const config = environment.toWebpackConfig()
54
- expect(config.resolveLoader.modules).toEqual(['node_modules'])
51
+ expect(baseConfig.resolveLoader.modules).toEqual(['node_modules'])
55
52
  })
56
53
 
57
54
  test('should return default resolve.modules with additions', () => {
58
- const config = environment.toWebpackConfig()
59
- expect(config.resolve.modules).toEqual([
60
- resolve('app', 'javascript'),
55
+ expect(baseConfig.resolve.modules).toEqual([
56
+ resolve('app', 'packs'),
61
57
  resolve('app/assets'),
62
58
  resolve('/etc/yarn'),
59
+ resolve('some.config.js'),
60
+ resolve('app/elm'),
63
61
  'node_modules'
64
62
  ])
65
63
  })
66
64
 
67
65
  test('returns plugins property as Array', () => {
68
- const config = environment.toWebpackConfig()
69
-
70
- expect(config.plugins).toBeInstanceOf(Array)
71
- expect(config.plugins).not.toBeInstanceOf(ConfigList)
66
+ expect(baseConfig.plugins).toBeInstanceOf(Array)
72
67
  })
73
68
  })
74
69
  })