webpacker 5.0.1 → 6.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) 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 +70 -0
  7. data/.gitignore +2 -0
  8. data/.node-version +1 -1
  9. data/.rubocop.yml +11 -16
  10. data/CHANGELOG.md +33 -1
  11. data/CONTRIBUTING.md +1 -1
  12. data/Gemfile.lock +91 -85
  13. data/README.md +27 -44
  14. data/docs/assets.md +21 -5
  15. data/docs/css.md +80 -30
  16. data/docs/deployment.md +18 -0
  17. data/docs/docker.md +2 -2
  18. data/docs/engines.md +1 -1
  19. data/docs/env.md +4 -1
  20. data/docs/es6.md +1 -1
  21. data/docs/integrations.md +2 -2
  22. data/docs/props.md +2 -38
  23. data/docs/react.md +183 -0
  24. data/docs/target.md +22 -0
  25. data/docs/testing.md +13 -3
  26. data/docs/troubleshooting.md +3 -1
  27. data/docs/typescript.md +92 -27
  28. data/docs/webpack-dev-server.md +17 -15
  29. data/docs/webpack.md +58 -107
  30. data/lib/install/config/webpack/base.js +3 -0
  31. data/lib/install/config/webpack/development.js +2 -2
  32. data/lib/install/config/webpack/production.js +2 -2
  33. data/lib/install/config/webpack/test.js +2 -2
  34. data/lib/install/config/webpacker.yml +9 -42
  35. data/lib/install/examples/{vue → vue3}/app.vue +10 -5
  36. data/lib/install/examples/vue3/hello_vue.js +15 -0
  37. data/lib/install/javascript/packs/application.css +9 -0
  38. data/lib/install/template.rb +33 -19
  39. data/lib/tasks/webpacker.rake +2 -11
  40. data/lib/tasks/webpacker/binstubs.rake +6 -4
  41. data/lib/tasks/webpacker/check_binstubs.rake +4 -4
  42. data/lib/tasks/webpacker/check_node.rake +1 -1
  43. data/lib/tasks/webpacker/check_yarn.rake +2 -3
  44. data/lib/tasks/webpacker/compile.rake +4 -2
  45. data/lib/tasks/webpacker/info.rake +12 -10
  46. data/lib/tasks/webpacker/install.rake +6 -4
  47. data/lib/tasks/webpacker/verify_install.rake +2 -1
  48. data/lib/tasks/webpacker/yarn_install.rake +9 -1
  49. data/lib/webpacker/commands.rb +1 -1
  50. data/lib/webpacker/compiler.rb +14 -9
  51. data/lib/webpacker/configuration.rb +7 -27
  52. data/lib/webpacker/dev_server_proxy.rb +3 -1
  53. data/lib/webpacker/dev_server_runner.rb +23 -4
  54. data/lib/webpacker/helper.rb +55 -46
  55. data/lib/webpacker/manifest.rb +1 -1
  56. data/lib/webpacker/railtie.rb +0 -43
  57. data/lib/webpacker/runner.rb +1 -0
  58. data/lib/webpacker/version.rb +1 -1
  59. data/lib/webpacker/webpack_runner.rb +7 -2
  60. data/package.json +27 -43
  61. data/package/__tests__/config.js +6 -27
  62. data/package/__tests__/dev_server.js +2 -0
  63. data/package/__tests__/development.js +18 -7
  64. data/package/__tests__/env.js +12 -4
  65. data/package/__tests__/production.js +6 -6
  66. data/package/__tests__/staging.js +7 -6
  67. data/package/__tests__/test.js +4 -5
  68. data/package/babel/preset-react.js +62 -0
  69. data/package/babel/preset.js +44 -0
  70. data/package/config.js +4 -9
  71. data/package/configPath.js +3 -0
  72. data/package/dev_server.js +1 -1
  73. data/package/env.js +9 -4
  74. data/package/environments/__tests__/base.js +21 -36
  75. data/package/environments/base.js +67 -128
  76. data/package/environments/development.js +46 -39
  77. data/package/environments/production.js +69 -67
  78. data/package/environments/test.js +2 -2
  79. data/package/index.js +6 -6
  80. data/package/rules/babel.js +20 -11
  81. data/package/rules/coffee.js +6 -0
  82. data/package/rules/erb.js +13 -0
  83. data/package/rules/file.js +19 -18
  84. data/package/rules/index.js +24 -18
  85. data/package/rules/less.js +18 -0
  86. data/package/rules/sass.js +6 -7
  87. data/package/rules/svg.js +23 -0
  88. data/package/utils/get_style_rule.js +22 -28
  89. data/package/utils/helpers.js +3 -35
  90. data/test/compiler_test.rb +4 -11
  91. data/test/configuration_test.rb +2 -32
  92. data/test/dev_server_runner_test.rb +25 -6
  93. data/test/engine_rake_tasks_test.rb +39 -0
  94. data/test/helper_test.rb +18 -9
  95. data/test/mounted_app/Rakefile +4 -0
  96. data/test/mounted_app/test/dummy/Rakefile +3 -0
  97. data/test/mounted_app/test/dummy/bin/rails +3 -0
  98. data/test/mounted_app/test/dummy/bin/rake +3 -0
  99. data/test/mounted_app/test/dummy/config.ru +5 -0
  100. data/test/mounted_app/test/dummy/config/application.rb +10 -0
  101. data/test/mounted_app/test/dummy/config/environment.rb +3 -0
  102. data/test/mounted_app/test/dummy/config/webpacker.yml +75 -0
  103. data/test/mounted_app/test/dummy/package.json +7 -0
  104. data/test/rake_tasks_test.rb +1 -10
  105. data/test/test_app/config/application.rb +0 -1
  106. data/test/test_app/config/webpacker.yml +3 -21
  107. data/test/test_app/config/webpacker_public_root.yml +0 -1
  108. data/test/test_app/public/packs/manifest.json +18 -13
  109. data/test/test_app/some.config.js +0 -0
  110. data/test/webpack_runner_test.rb +10 -4
  111. data/webpacker.gemspec +1 -1
  112. data/yarn.lock +2657 -6224
  113. metadata +53 -71
  114. data/.travis.yml +0 -43
  115. data/lib/install/angular.rb +0 -23
  116. data/lib/install/coffee.rb +0 -25
  117. data/lib/install/config/.browserslistrc +0 -1
  118. data/lib/install/config/babel.config.js +0 -72
  119. data/lib/install/config/postcss.config.js +0 -12
  120. data/lib/install/config/webpack/environment.js +0 -3
  121. data/lib/install/elm.rb +0 -39
  122. data/lib/install/erb.rb +0 -25
  123. data/lib/install/examples/angular/hello_angular.js +0 -7
  124. data/lib/install/examples/angular/hello_angular/app/app.component.ts +0 -9
  125. data/lib/install/examples/angular/hello_angular/app/app.module.ts +0 -16
  126. data/lib/install/examples/angular/hello_angular/index.ts +0 -8
  127. data/lib/install/examples/angular/hello_angular/polyfills.ts +0 -73
  128. data/lib/install/examples/coffee/hello_coffee.coffee +0 -4
  129. data/lib/install/examples/elm/Main.elm +0 -55
  130. data/lib/install/examples/elm/hello_elm.js +0 -16
  131. data/lib/install/examples/erb/hello_erb.js.erb +0 -6
  132. data/lib/install/examples/react/babel.config.js +0 -87
  133. data/lib/install/examples/react/hello_react.jsx +0 -26
  134. data/lib/install/examples/react/tsconfig.json +0 -20
  135. data/lib/install/examples/stimulus/application.js +0 -1
  136. data/lib/install/examples/stimulus/controllers/hello_controller.js +0 -18
  137. data/lib/install/examples/stimulus/controllers/index.js +0 -9
  138. data/lib/install/examples/svelte/app.svelte +0 -11
  139. data/lib/install/examples/svelte/hello_svelte.js +0 -20
  140. data/lib/install/examples/typescript/hello_typescript.ts +0 -4
  141. data/lib/install/examples/typescript/tsconfig.json +0 -23
  142. data/lib/install/examples/vue/hello_vue.js +0 -72
  143. data/lib/install/loaders/coffee.js +0 -6
  144. data/lib/install/loaders/elm.js +0 -25
  145. data/lib/install/loaders/erb.js +0 -11
  146. data/lib/install/loaders/svelte.js +0 -9
  147. data/lib/install/loaders/typescript.js +0 -11
  148. data/lib/install/loaders/vue.js +0 -6
  149. data/lib/install/react.rb +0 -18
  150. data/lib/install/stimulus.rb +0 -12
  151. data/lib/install/svelte.rb +0 -29
  152. data/lib/install/typescript.rb +0 -46
  153. data/lib/install/vue.rb +0 -49
  154. data/lib/tasks/installers.rake +0 -42
  155. data/package/config_types/__tests__/config_list.js +0 -118
  156. data/package/config_types/__tests__/config_object.js +0 -43
  157. data/package/config_types/config_list.js +0 -75
  158. data/package/config_types/config_object.js +0 -55
  159. data/package/config_types/index.js +0 -7
  160. data/package/rules/module.css.js +0 -3
  161. data/package/rules/module.sass.js +0 -8
  162. data/package/rules/node_modules.js +0 -24
  163. data/package/utils/__tests__/deep_assign.js +0 -32
  164. data/package/utils/__tests__/deep_merge.js +0 -10
  165. data/package/utils/__tests__/get_style_rule.js +0 -65
  166. data/package/utils/__tests__/objectify.js +0 -9
  167. data/package/utils/deep_assign.js +0 -22
  168. data/package/utils/deep_merge.js +0 -22
  169. data/package/utils/objectify.js +0 -3
@@ -1,47 +1,54 @@
1
+ const { merge } = require('webpack-merge')
1
2
  const webpack = require('webpack')
2
- const Base = require('./base')
3
+
4
+ const baseConfig = require('./base')
3
5
  const devServer = require('../dev_server')
4
- const { outputPath: contentBase, publicPath } = require('../config')
5
6
 
6
- module.exports = class extends Base {
7
- constructor() {
8
- super()
7
+ const { outputPath: contentBase, publicPath } = require('../config')
9
8
 
10
- if (devServer.hmr) {
11
- this.plugins.append('HotModuleReplacement', new webpack.HotModuleReplacementPlugin())
12
- this.config.output.filename = '[name]-[hash].js'
13
- }
9
+ let devConfig = {
10
+ mode: 'development',
11
+ devtool: 'cheap-module-source-map'
12
+ }
14
13
 
15
- this.config.merge({
16
- mode: 'development',
17
- devtool: 'cheap-module-source-map',
18
- devServer: {
19
- clientLogLevel: 'none',
20
- compress: devServer.compress,
21
- quiet: devServer.quiet,
22
- disableHostCheck: devServer.disable_host_check,
23
- host: devServer.host,
24
- port: devServer.port,
25
- https: devServer.https,
26
- hot: devServer.hmr,
27
- contentBase,
28
- inline: devServer.inline,
29
- useLocalIp: devServer.use_local_ip,
30
- public: devServer.public,
31
- publicPath,
32
- historyApiFallback: {
33
- disableDotRule: true
34
- },
35
- headers: devServer.headers,
36
- overlay: devServer.overlay,
37
- stats: {
38
- entrypoints: false,
39
- errorDetails: true,
40
- modules: false,
41
- moduleTrace: false
42
- },
43
- watchOptions: devServer.watch_options
44
- }
14
+ if (
15
+ process.env.WEBPACK_DEV_SERVER &&
16
+ process.env.WEBPACK_DEV_SERVER !== 'undefined'
17
+ ) {
18
+ if (devServer.hmr) {
19
+ devConfig = merge(devConfig, {
20
+ output: { filename: '[name]-[hash].js' },
21
+ plugins: [new webpack.HotModuleReplacementPlugin()]
45
22
  })
46
23
  }
24
+
25
+ devConfig = merge(devConfig, {
26
+ devServer: {
27
+ clientLogLevel: 'none',
28
+ compress: devServer.compress,
29
+ quiet: devServer.quiet,
30
+ disableHostCheck: devServer.disable_host_check,
31
+ host: devServer.host,
32
+ port: devServer.port,
33
+ https: devServer.https,
34
+ hot: devServer.hmr,
35
+ contentBase,
36
+ inline: devServer.inline,
37
+ useLocalIp: devServer.use_local_ip,
38
+ public: devServer.public,
39
+ publicPath,
40
+ historyApiFallback: { disableDotRule: true },
41
+ headers: devServer.headers,
42
+ overlay: devServer.overlay,
43
+ stats: {
44
+ entrypoints: false,
45
+ errorDetails: true,
46
+ modules: false,
47
+ moduleTrace: false
48
+ },
49
+ watchOptions: devServer.watch_options
50
+ }
51
+ })
47
52
  }
53
+
54
+ module.exports = merge(baseConfig, devConfig)
@@ -1,79 +1,81 @@
1
- const TerserPlugin = require('terser-webpack-plugin')
1
+ /* eslint global-require: 0 */
2
+ /* eslint import/no-dynamic-require: 0 */
3
+
4
+ const { merge } = require('webpack-merge')
2
5
  const CompressionPlugin = require('compression-webpack-plugin')
3
- const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
4
- const safePostCssParser = require('postcss-safe-parser')
5
- const Base = require('./base')
6
+ const TerserPlugin = require('terser-webpack-plugin')
7
+ const baseConfig = require('./base')
6
8
 
7
- module.exports = class extends Base {
8
- constructor() {
9
- super()
9
+ const getPlugins = () => {
10
+ let compressionPlugin = new CompressionPlugin({
11
+ filename: '[path].gz[query]',
12
+ algorithm: 'gzip',
13
+ test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/
14
+ })
10
15
 
11
- this.plugins.append(
12
- 'Compression',
13
- new CompressionPlugin({
14
- filename: '[path].gz[query]',
15
- algorithm: 'gzip',
16
- cache: true,
17
- test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/
18
- })
19
- )
16
+ if ('brotli' in process.versions) {
17
+ compressionPlugin = new CompressionPlugin({
18
+ filename: '[path].br[query]',
19
+ algorithm: 'brotliCompress',
20
+ test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/
21
+ })
22
+ }
23
+
24
+ const plugins = [compressionPlugin]
20
25
 
21
- if ('brotli' in process.versions) {
22
- this.plugins.append(
23
- 'Compression Brotli',
24
- new CompressionPlugin({
25
- filename: '[path].br[query]',
26
- algorithm: 'brotliCompress',
27
- cache: true,
28
- test: /\.(js|css|html|json|ico|svg|eot|otf|ttf|map)$/
26
+ try {
27
+ if (require.resolve('css-loader')) {
28
+ const OptimizeCSSAssetsPlugin = require.resolve(
29
+ 'optimize-css-assets-webpack-plugin'
30
+ )
31
+ const safePostCssParser = require.resolve('postcss-safe-parser')
32
+ plugins.push(
33
+ new OptimizeCSSAssetsPlugin({
34
+ parser: safePostCssParser,
35
+ map: {
36
+ inline: false,
37
+ annotation: true
38
+ }
29
39
  })
30
40
  )
31
41
  }
42
+ } catch (e) {
43
+ /* Work out what to output without clutter */
44
+ }
45
+
46
+ return plugins
47
+ }
32
48
 
33
- this.plugins.append(
34
- 'OptimizeCSSAssets',
35
- new OptimizeCSSAssetsPlugin({
36
- parser: safePostCssParser,
37
- map: {
38
- inline: false,
39
- annotation: true
49
+ const productionConfig = {
50
+ devtool: 'source-map',
51
+ stats: 'normal',
52
+ bail: true,
53
+ plugins: getPlugins(),
54
+ optimization: {
55
+ minimizer: [
56
+ new TerserPlugin({
57
+ parallel: Number.parseInt(process.env.WEBPACKER_PARALLEL, 10) || true,
58
+ terserOptions: {
59
+ parse: {
60
+ // Let terser parse ecma 8 code but always output
61
+ // ES5 compliant code for older browsers
62
+ ecma: 8
63
+ },
64
+ compress: {
65
+ ecma: 5,
66
+ warnings: false,
67
+ comparisons: false
68
+ },
69
+ mangle: { safari10: true },
70
+ output: {
71
+ ecma: 5,
72
+ comments: false,
73
+ ascii_only: true
74
+ }
40
75
  }
41
76
  })
42
- )
43
-
44
- this.config.merge({
45
- devtool: 'source-map',
46
- stats: 'normal',
47
- bail: true,
48
- optimization: {
49
- minimizer: [
50
- new TerserPlugin({
51
- parallel: true,
52
- cache: true,
53
- sourceMap: true,
54
- terserOptions: {
55
- parse: {
56
- // Let terser parse ecma 8 code but always output
57
- // ES5 compliant code for older browsers
58
- ecma: 8
59
- },
60
- compress: {
61
- ecma: 5,
62
- warnings: false,
63
- comparisons: false
64
- },
65
- mangle: {
66
- safari10: true
67
- },
68
- output: {
69
- ecma: 5,
70
- comments: false,
71
- ascii_only: true
72
- }
73
- }
74
- })
75
- ]
76
- }
77
- })
77
+ ]
78
78
  }
79
79
  }
80
+
81
+ module.exports = merge(baseConfig, productionConfig)
@@ -1,3 +1,3 @@
1
- const Base = require('./base')
1
+ const baseConfig = require('./base')
2
2
 
3
- module.exports = class extends Base {}
3
+ module.exports = baseConfig
@@ -3,22 +3,22 @@
3
3
 
4
4
  const { resolve } = require('path')
5
5
  const { existsSync } = require('fs')
6
- const Environment = require('./environments/base')
6
+ const baseConfig = require('./environments/base')
7
7
  const loaders = require('./rules')
8
8
  const config = require('./config')
9
9
  const devServer = require('./dev_server')
10
10
  const { nodeEnv } = require('./env')
11
11
 
12
- const createEnvironment = () => {
12
+ const webpackConfig = () => {
13
13
  const path = resolve(__dirname, 'environments', `${nodeEnv}.js`)
14
- const constructor = existsSync(path) ? require(path) : Environment
15
- return new constructor()
14
+ const environmentConfig = existsSync(path) ? require(path) : baseConfig
15
+ return environmentConfig
16
16
  }
17
17
 
18
18
  module.exports = {
19
19
  config,
20
20
  devServer,
21
- environment: createEnvironment(),
22
- Environment,
21
+ webpackConfig: webpackConfig(),
22
+ baseConfig,
23
23
  loaders
24
24
  }
@@ -1,20 +1,29 @@
1
- const { join, resolve } = require('path')
2
- const { cache_path: cachePath, source_path: sourcePath, resolved_paths: resolvedPaths } = require('../config')
3
- const { nodeEnv } = require('../env')
1
+ const { resolve } = require('path')
2
+ const { realpathSync } = require('fs')
3
+
4
+ const {
5
+ source_path: sourcePath,
6
+ additional_paths: additionalPaths
7
+ } = require('../config')
8
+ const { isProduction } = require('../env')
4
9
 
5
- // Process application Javascript code with Babel.
6
- // Uses application .babelrc to apply any transformations
7
10
  module.exports = {
8
- test: /\.(js|jsx|mjs)?(\.erb)?$/,
9
- include: [sourcePath, ...resolvedPaths].map((p) => resolve(p)),
11
+ test: /\.(js|jsx|mjs|ts|tsx)?(\.erb)?$/,
12
+ include: [sourcePath, ...additionalPaths].map((p) => {
13
+ try {
14
+ return realpathSync(p)
15
+ } catch (e) {
16
+ return resolve(p)
17
+ }
18
+ }),
10
19
  exclude: /node_modules/,
11
20
  use: [
12
21
  {
13
- loader: 'babel-loader',
22
+ loader: require.resolve('babel-loader'),
14
23
  options: {
15
- cacheDirectory: join(cachePath, 'babel-loader-node-modules'),
16
- cacheCompression: nodeEnv === 'production',
17
- compact: nodeEnv === 'production'
24
+ cacheDirectory: true,
25
+ cacheCompression: isProduction,
26
+ compact: isProduction
18
27
  }
19
28
  }
20
29
  ]
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ test: /\.coffee(\.erb)?$/,
3
+ use: [
4
+ { loader: require.resolve('coffee-loader') }
5
+ ]
6
+ }
@@ -0,0 +1,13 @@
1
+ const runner = /^win/.test(process.platform) ? 'ruby ' : ''
2
+
3
+ module.exports = {
4
+ test: /\.erb$/,
5
+ enforce: 'pre',
6
+ exclude: /node_modules/,
7
+ use: [
8
+ {
9
+ loader: require.resolve('rails-erb-loader'),
10
+ options: { runner: `${runner}bin/rails runner` }
11
+ }
12
+ ]
13
+ }
@@ -1,20 +1,21 @@
1
- const { join } = require('path')
2
- const { source_path: sourcePath, static_assets_extensions: fileExtensions } = require('../config')
3
-
4
1
  module.exports = {
5
- test: new RegExp(`(${fileExtensions.join('|')})$`, 'i'),
6
- use: [
7
- {
8
- loader: 'file-loader',
9
- options: {
10
- name(file) {
11
- if (file.includes(sourcePath)) {
12
- return 'media/[path][name]-[hash].[ext]'
13
- }
14
- return 'media/[folder]/[name]-[hash:8].[ext]'
15
- },
16
- context: join(sourcePath)
17
- }
18
- }
19
- ]
2
+ test: [
3
+ /\.bmp$/,
4
+ /\.gif$/,
5
+ /\.jpe?g$/,
6
+ /\.png$/,
7
+ /\.tiff$/,
8
+ /\.ico$/,
9
+ /\.avif$/,
10
+ /\.webp$/,
11
+ /\.eot$/,
12
+ /\.otf$/,
13
+ /\.ttf$/,
14
+ /\.woff$/,
15
+ /\.woff2$/,
16
+ /\.html$/,
17
+ /\.json$/
18
+ ],
19
+ exclude: [/\.(js|mjs|jsx|ts|tsx)$/],
20
+ type: 'asset/resource'
20
21
  }
@@ -1,20 +1,26 @@
1
- const babel = require('./babel')
2
- const file = require('./file')
3
- const css = require('./css')
4
- const sass = require('./sass')
5
- const moduleCss = require('./module.css')
6
- const moduleSass = require('./module.sass')
7
- const nodeModules = require('./node_modules')
1
+ /* eslint global-require: 0 */
2
+ /* eslint import/no-dynamic-require: 0 */
8
3
 
9
- // Webpack loaders are processed in reverse order
10
- // https://webpack.js.org/concepts/loaders/#loader-features
11
- // Lastly, process static files using file loader
12
- module.exports = {
13
- file,
14
- css,
15
- sass,
16
- moduleCss,
17
- moduleSass,
18
- nodeModules,
19
- babel
4
+ const load = (name) => {
5
+ try {
6
+ return require(`./${name}`)
7
+ } catch (e) {
8
+ return null
9
+ }
20
10
  }
11
+
12
+ const rules = {
13
+ file: load('file'),
14
+ svg: load('svg'),
15
+ css: load('css'),
16
+ sass: load('sass'),
17
+ babel: load('babel'),
18
+ erb: load('erb'),
19
+ coffee: load('coffee'),
20
+ html: load('html'),
21
+ less: load('less')
22
+ }
23
+
24
+ module.exports = Object.keys(rules)
25
+ .filter((key) => !!rules[key])
26
+ .map((key) => rules[key])
@@ -0,0 +1,18 @@
1
+ const path = require('path')
2
+ const getStyleRule = require('../utils/get_style_rule')
3
+ const {
4
+ additional_paths: paths,
5
+ source_path: sourcePath
6
+ } = require('../config')
7
+
8
+ module.exports = getStyleRule(/\.(less)(\.erb)?$/i, [
9
+ {
10
+ loader: require.resolve('less-loader'),
11
+ options: {
12
+ lessOptions: {
13
+ paths: [path.resolve(__dirname, 'node_modules'), sourcePath, ...paths]
14
+ },
15
+ sourceMap: true
16
+ }
17
+ }
18
+ ])