jetpacker 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/.eslintignore +4 -0
  3. data/.eslintrc.js +14 -0
  4. data/.gitignore +11 -11
  5. data/.node-version +1 -0
  6. data/.rubocop.yml +125 -0
  7. data/.travis.yml +54 -5
  8. data/CHANGELOG.jetpacker.md +7 -0
  9. data/CHANGELOG.md +1000 -0
  10. data/CONTRIBUTING.md +33 -0
  11. data/Gemfile +9 -3
  12. data/Gemfile.lock +157 -21
  13. data/MIT-LICENSE +20 -0
  14. data/README.md +671 -16
  15. data/Rakefile +8 -3
  16. data/docs/assets.md +119 -0
  17. data/docs/cloud9.md +310 -0
  18. data/docs/css.md +253 -0
  19. data/docs/deployment.md +130 -0
  20. data/docs/docker.md +68 -0
  21. data/docs/engines.md +200 -0
  22. data/docs/env.md +65 -0
  23. data/docs/es6.md +72 -0
  24. data/docs/folder-structure.md +66 -0
  25. data/docs/misc.md +23 -0
  26. data/docs/props.md +223 -0
  27. data/docs/testing.md +137 -0
  28. data/docs/troubleshooting.md +156 -0
  29. data/docs/typescript.md +126 -0
  30. data/docs/v4-upgrade.md +142 -0
  31. data/docs/webpack-dev-server.md +92 -0
  32. data/docs/webpack.md +364 -0
  33. data/docs/yarn.md +23 -0
  34. data/gemfiles/Gemfile-rails-edge +12 -0
  35. data/gemfiles/Gemfile-rails.4.2.x +9 -0
  36. data/gemfiles/Gemfile-rails.5.0.x +9 -0
  37. data/gemfiles/Gemfile-rails.5.1.x +9 -0
  38. data/gemfiles/Gemfile-rails.5.2.x +9 -0
  39. data/gemfiles/Gemfile-rails.6.0.x +9 -0
  40. data/jetpacker.gemspec +28 -22
  41. data/lib/install/angular.rb +23 -0
  42. data/lib/install/bin/webpack +18 -0
  43. data/lib/install/bin/webpack-dev-server +18 -0
  44. data/lib/install/binstubs.rb +4 -0
  45. data/lib/install/coffee.rb +25 -0
  46. data/lib/install/config/.browserslistrc +1 -0
  47. data/lib/install/config/babel.config.js +72 -0
  48. data/lib/install/config/postcss.config.js +12 -0
  49. data/lib/install/config/webpack/development.js +5 -0
  50. data/lib/install/config/webpack/environment.js +3 -0
  51. data/lib/install/config/webpack/production.js +5 -0
  52. data/lib/install/config/webpack/test.js +5 -0
  53. data/lib/install/config/webpacker.yml +96 -0
  54. data/lib/install/elm.rb +39 -0
  55. data/lib/install/erb.rb +25 -0
  56. data/lib/install/examples/angular/hello_angular.js +7 -0
  57. data/lib/install/examples/angular/hello_angular/app/app.component.ts +9 -0
  58. data/lib/install/examples/angular/hello_angular/app/app.module.ts +16 -0
  59. data/lib/install/examples/angular/hello_angular/index.ts +8 -0
  60. data/lib/install/examples/angular/hello_angular/polyfills.ts +73 -0
  61. data/lib/install/examples/coffee/hello_coffee.coffee +4 -0
  62. data/lib/install/examples/elm/Main.elm +55 -0
  63. data/lib/install/examples/elm/hello_elm.js +16 -0
  64. data/lib/install/examples/erb/hello_erb.js.erb +6 -0
  65. data/lib/install/examples/react/babel.config.js +87 -0
  66. data/lib/install/examples/react/hello_react.jsx +26 -0
  67. data/lib/install/examples/react/tsconfig.json +20 -0
  68. data/lib/install/examples/stimulus/application.js +1 -0
  69. data/lib/install/examples/stimulus/controllers/hello_controller.js +18 -0
  70. data/lib/install/examples/stimulus/controllers/index.js +9 -0
  71. data/lib/install/examples/svelte/app.svelte +11 -0
  72. data/lib/install/examples/svelte/hello_svelte.js +20 -0
  73. data/lib/install/examples/typescript/hello_typescript.ts +4 -0
  74. data/lib/install/examples/typescript/tsconfig.json +23 -0
  75. data/lib/install/examples/vue/app.vue +22 -0
  76. data/lib/install/examples/vue/hello_vue.js +72 -0
  77. data/lib/install/javascript/packs/application.js +18 -0
  78. data/lib/install/loaders/coffee.js +6 -0
  79. data/lib/install/loaders/elm.js +25 -0
  80. data/lib/install/loaders/erb.js +11 -0
  81. data/lib/install/loaders/svelte.js +9 -0
  82. data/lib/install/loaders/typescript.js +11 -0
  83. data/lib/install/loaders/vue.js +6 -0
  84. data/lib/install/react.rb +18 -0
  85. data/lib/install/stimulus.rb +12 -0
  86. data/lib/install/svelte.rb +29 -0
  87. data/lib/install/template.rb +55 -0
  88. data/lib/install/typescript.rb +46 -0
  89. data/lib/install/vue.rb +49 -0
  90. data/lib/jetpacker/version.rb +2 -1
  91. data/lib/tasks/installers.rake +37 -0
  92. data/lib/tasks/webpacker.rake +28 -0
  93. data/lib/tasks/webpacker/binstubs.rake +11 -0
  94. data/lib/tasks/webpacker/check_binstubs.rake +12 -0
  95. data/lib/tasks/webpacker/check_node.rake +24 -0
  96. data/lib/tasks/webpacker/check_yarn.rake +24 -0
  97. data/lib/tasks/webpacker/clean.rake +21 -0
  98. data/lib/tasks/webpacker/clobber.rake +16 -0
  99. data/lib/tasks/webpacker/compile.rake +43 -0
  100. data/lib/tasks/webpacker/info.rake +20 -0
  101. data/lib/tasks/webpacker/install.rake +13 -0
  102. data/lib/tasks/webpacker/verify_install.rake +13 -0
  103. data/lib/tasks/webpacker/yarn_install.rake +21 -0
  104. data/lib/webpacker.rb +46 -0
  105. data/lib/webpacker/commands.rb +50 -0
  106. data/lib/webpacker/compiler.rb +107 -0
  107. data/lib/webpacker/configuration.rb +113 -0
  108. data/lib/webpacker/dev_server.rb +66 -0
  109. data/lib/webpacker/dev_server_proxy.rb +31 -0
  110. data/lib/webpacker/dev_server_runner.rb +72 -0
  111. data/lib/webpacker/env.rb +39 -0
  112. data/lib/webpacker/helper.rb +176 -0
  113. data/lib/webpacker/instance.rb +37 -0
  114. data/lib/webpacker/manifest.rb +118 -0
  115. data/lib/webpacker/railtie.rb +98 -0
  116. data/lib/webpacker/rake_tasks.rb +6 -0
  117. data/lib/webpacker/runner.rb +22 -0
  118. data/lib/webpacker/version.rb +4 -0
  119. data/lib/webpacker/webpack_runner.rb +32 -0
  120. data/package.json +82 -0
  121. data/package/__tests__/config.js +55 -0
  122. data/package/__tests__/dev_server.js +43 -0
  123. data/package/__tests__/development.js +30 -0
  124. data/package/__tests__/env.js +46 -0
  125. data/package/__tests__/production.js +29 -0
  126. data/package/__tests__/staging.js +29 -0
  127. data/package/__tests__/test.js +26 -0
  128. data/package/config.js +37 -0
  129. data/package/config_types/__tests__/config_list.js +118 -0
  130. data/package/config_types/__tests__/config_object.js +43 -0
  131. data/package/config_types/config_list.js +75 -0
  132. data/package/config_types/config_object.js +55 -0
  133. data/package/config_types/index.js +7 -0
  134. data/package/dev_server.js +20 -0
  135. data/package/env.js +19 -0
  136. data/package/environments/__tests__/base.js +74 -0
  137. data/package/environments/base.js +166 -0
  138. data/package/environments/development.js +51 -0
  139. data/package/environments/production.js +79 -0
  140. data/package/environments/test.js +3 -0
  141. data/package/index.js +24 -0
  142. data/package/rules/babel.js +21 -0
  143. data/package/rules/css.js +3 -0
  144. data/package/rules/file.js +20 -0
  145. data/package/rules/index.js +20 -0
  146. data/package/rules/module.css.js +3 -0
  147. data/package/rules/module.sass.js +8 -0
  148. data/package/rules/node_modules.js +24 -0
  149. data/package/rules/sass.js +8 -0
  150. data/package/utils/__tests__/deep_assign.js +32 -0
  151. data/package/utils/__tests__/deep_merge.js +10 -0
  152. data/package/utils/__tests__/get_style_rule.js +65 -0
  153. data/package/utils/__tests__/objectify.js +9 -0
  154. data/package/utils/deep_assign.js +22 -0
  155. data/package/utils/deep_merge.js +22 -0
  156. data/package/utils/get_style_rule.js +45 -0
  157. data/package/utils/helpers.js +58 -0
  158. data/package/utils/objectify.js +3 -0
  159. data/test/command_test.rb +33 -0
  160. data/test/compiler_test.rb +75 -0
  161. data/test/configuration_test.rb +108 -0
  162. data/test/dev_server_runner_test.rb +51 -0
  163. data/test/dev_server_test.rb +47 -0
  164. data/test/env_test.rb +23 -0
  165. data/test/helper_test.rb +142 -0
  166. data/test/manifest_test.rb +42 -0
  167. data/test/rake_tasks_test.rb +69 -0
  168. data/test/test_app/Rakefile +3 -0
  169. data/test/test_app/app/javascript/packs/application.js +10 -0
  170. data/test/test_app/bin/webpack +14 -0
  171. data/test/test_app/bin/webpack-dev-server +14 -0
  172. data/test/test_app/config.ru +5 -0
  173. data/test/test_app/config/application.rb +12 -0
  174. data/test/test_app/config/environment.rb +4 -0
  175. data/test/test_app/config/webpack/development.js +0 -0
  176. data/test/test_app/config/webpacker.yml +97 -0
  177. data/test/test_app/config/webpacker_public_root.yml +19 -0
  178. data/test/test_app/package.json +13 -0
  179. data/test/test_app/public/packs/manifest.json +31 -0
  180. data/test/test_app/yarn.lock +11 -0
  181. data/test/test_helper.rb +33 -0
  182. data/test/webpack_runner_test.rb +51 -0
  183. data/test/webpacker_test.rb +13 -0
  184. data/yarn.lock +8321 -0
  185. metadata +267 -29
  186. data/.rspec +0 -3
  187. data/LICENSE.txt +0 -21
  188. data/bin/console +0 -14
  189. data/bin/setup +0 -8
  190. data/lib/jetpacker.rb +0 -6
@@ -0,0 +1,29 @@
1
+ /* test expect, describe, afterAll, beforeEach */
2
+
3
+ const { resolve } = require('path')
4
+ const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5
+
6
+ chdirTestApp()
7
+
8
+ describe('Custom environment', () => {
9
+ afterAll(chdirCwd)
10
+
11
+ describe('toWebpackConfig', () => {
12
+ beforeEach(() => jest.resetModules())
13
+
14
+ test('should use staging config and default production environment', () => {
15
+ process.env.RAILS_ENV = 'staging'
16
+ delete process.env.NODE_ENV
17
+
18
+ const { environment } = require('../index')
19
+ const config = environment.toWebpackConfig()
20
+
21
+ expect(config.output.path).toEqual(resolve('public', 'packs-staging'))
22
+ expect(config.output.publicPath).toEqual('/packs-staging/')
23
+ expect(config).toMatchObject({
24
+ devtool: 'source-map',
25
+ stats: 'normal'
26
+ })
27
+ })
28
+ })
29
+ })
@@ -0,0 +1,26 @@
1
+ /* test expect, describe, afterAll, beforeEach */
2
+
3
+ const { resolve } = require('path')
4
+ const { chdirTestApp, chdirCwd } = require('../utils/helpers')
5
+
6
+ chdirTestApp()
7
+
8
+ describe('Test environment', () => {
9
+ afterAll(chdirCwd)
10
+
11
+ describe('toWebpackConfig', () => {
12
+ beforeEach(() => jest.resetModules())
13
+
14
+ test('should use test config and production environment', () => {
15
+ process.env.RAILS_ENV = 'test'
16
+ process.env.NODE_ENV = 'test'
17
+
18
+ const { environment } = require('../index')
19
+ const config = environment.toWebpackConfig()
20
+
21
+ expect(config.output.path).toEqual(resolve('public', 'packs-test'))
22
+ expect(config.output.publicPath).toEqual('/packs-test/')
23
+ expect(config.devServer).toEqual(undefined)
24
+ })
25
+ })
26
+ })
@@ -0,0 +1,37 @@
1
+ const { resolve } = require('path')
2
+ const { safeLoad } = require('js-yaml')
3
+ const { readFileSync } = require('fs')
4
+ const deepMerge = require('./utils/deep_merge')
5
+ const { isArray, ensureTrailingSlash } = require('./utils/helpers')
6
+ const { railsEnv } = require('./env')
7
+
8
+ const defaultConfigPath = require.resolve('../lib/install/config/webpacker.yml')
9
+ const configPath = resolve('config', 'webpacker.yml')
10
+
11
+ const getDefaultConfig = () => {
12
+ const defaultConfig = safeLoad(readFileSync(defaultConfigPath), 'utf8')
13
+ return defaultConfig[railsEnv] || defaultConfig.production
14
+ }
15
+
16
+ const defaults = getDefaultConfig()
17
+ const app = safeLoad(readFileSync(configPath), 'utf8')[railsEnv]
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)
25
+ config.outputPath = resolve(config.public_root_path, config.public_output_path)
26
+
27
+ // Ensure that the publicPath includes our asset host so dynamic imports
28
+ // (code-splitting chunks and static assets) load from the CDN instead of a relative path.
29
+ const getPublicPath = () => {
30
+ const rootUrl = ensureTrailingSlash(process.env.WEBPACKER_ASSET_HOST || '/')
31
+ return `${rootUrl}${config.public_output_path}/`
32
+ }
33
+
34
+ config.publicPath = getPublicPath()
35
+ config.publicPathWithoutCDN = `/${config.public_output_path}/`
36
+
37
+ module.exports = config
@@ -0,0 +1,118 @@
1
+ /* global test expect */
2
+
3
+ const ConfigList = require('../config_list')
4
+
5
+ test('new', () => {
6
+ const list = new ConfigList()
7
+ expect(list).toBeInstanceOf(ConfigList)
8
+ expect(list).toBeInstanceOf(Array)
9
+ })
10
+
11
+ test('get', () => {
12
+ const list = new ConfigList()
13
+ list.append('key', 'value')
14
+ expect(list.get('key')).toEqual('value')
15
+ })
16
+
17
+ test('append', () => {
18
+ const list = new ConfigList()
19
+ list.append('key', 'value')
20
+ expect(list.append('key1', 'value1')).toEqual([
21
+ { key: 'key', value: 'value' },
22
+ { key: 'key1', value: 'value1' }
23
+ ])
24
+ })
25
+
26
+ test('prepend', () => {
27
+ const list = new ConfigList()
28
+ list.append('key', 'value')
29
+ expect(list.prepend('key1', 'value1')).toEqual([
30
+ { key: 'key1', value: 'value1' },
31
+ { key: 'key', value: 'value' }
32
+ ])
33
+ })
34
+
35
+ test('insert without position', () => {
36
+ const list = new ConfigList()
37
+ list.append('key', 'value')
38
+
39
+ expect(list.insert('key1', 'value1')).toEqual([
40
+ { key: 'key', value: 'value' },
41
+ { key: 'key1', value: 'value1' }
42
+ ])
43
+
44
+ expect(list.insert('key2', 'value2')).toEqual([
45
+ { key: 'key', value: 'value' },
46
+ { key: 'key1', value: 'value1' },
47
+ { key: 'key2', value: 'value2' }
48
+ ])
49
+ })
50
+
51
+ test('insert before an item', () => {
52
+ const list = new ConfigList()
53
+ list.append('key', 'value')
54
+ list.append('key1', 'value1')
55
+
56
+ expect(list.insert('key2', 'value2', { before: 'key' })).toEqual([
57
+ { key: 'key2', value: 'value2' },
58
+ { key: 'key', value: 'value' },
59
+ { key: 'key1', value: 'value1' }
60
+ ])
61
+
62
+ expect(list.insert('key3', 'value3', { before: 'key2' })).toEqual([
63
+ { key: 'key3', value: 'value3' },
64
+ { key: 'key2', value: 'value2' },
65
+ { key: 'key', value: 'value' },
66
+ { key: 'key1', value: 'value1' }
67
+ ])
68
+ })
69
+
70
+ test('insert after an item', () => {
71
+ const list = new ConfigList()
72
+ list.append('key', 'value')
73
+ list.append('key1', 'value1')
74
+
75
+ expect(list.insert('key2', 'value2', { after: 'key' })).toEqual([
76
+ { key: 'key', value: 'value' },
77
+ { key: 'key2', value: 'value2' },
78
+ { key: 'key1', value: 'value1' }
79
+ ])
80
+
81
+ expect(list.insert('key3', 'value3', { after: 'key2' })).toEqual([
82
+ { key: 'key', value: 'value' },
83
+ { key: 'key2', value: 'value2' },
84
+ { key: 'key3', value: 'value3' },
85
+ { key: 'key1', value: 'value1' }
86
+ ])
87
+ })
88
+
89
+ test('delete', () => {
90
+ const list = new ConfigList()
91
+ list.append('key', 'value')
92
+ list.append('key1', 'value1')
93
+ expect(list.delete('key')).toEqual([{ key: 'key1', value: 'value1' }])
94
+ expect(list.delete('key1')).toEqual([])
95
+ })
96
+
97
+ test('getIndex', () => {
98
+ const list = new ConfigList()
99
+ list.append('key', 'value')
100
+ list.append('key1', 'value1')
101
+ expect(list.getIndex('key')).toEqual(0)
102
+ expect(list.getIndex('key2')).toEqual(-1)
103
+ expect(() => list.getIndex('key2', true)).toThrow('Item key2 not found')
104
+ })
105
+
106
+ test('values', () => {
107
+ const list = new ConfigList()
108
+ list.append('key', 'value')
109
+ list.append('key1', 'value1')
110
+ expect(list.values()).toEqual(['value', 'value1'])
111
+ })
112
+
113
+ test('keys', () => {
114
+ const list = new ConfigList()
115
+ list.append('key', 'value')
116
+ list.append('key1', 'value1')
117
+ expect(list.keys()).toEqual(['key', 'key1'])
118
+ })
@@ -0,0 +1,43 @@
1
+ /* global test expect */
2
+
3
+ const ConfigObject = require('../config_object')
4
+
5
+ test('new', () => {
6
+ const object = new ConfigObject()
7
+ expect(object).toBeInstanceOf(ConfigObject)
8
+ expect(object).toBeInstanceOf(Object)
9
+ })
10
+
11
+ test('set', () => {
12
+ const object = new ConfigObject()
13
+ expect(object.set('key', 'value')).toEqual({ key: 'value' })
14
+ })
15
+
16
+ test('get', () => {
17
+ const object = new ConfigObject()
18
+ object.set('key', 'value')
19
+ object.set('key1', 'value1')
20
+ expect(object.get('key')).toEqual('value')
21
+ })
22
+
23
+ test('delete', () => {
24
+ const object = new ConfigObject()
25
+ object.set('key', { key1: 'value' })
26
+ expect(object.delete('key.key1')).toEqual({ key: {} })
27
+ expect(object.delete('key')).toEqual({})
28
+ })
29
+
30
+ test('toObject', () => {
31
+ const object = new ConfigObject()
32
+ object.set('key', 'value')
33
+ object.set('key1', 'value1')
34
+ expect(object.toObject()).toEqual({ key: 'value', key1: 'value1' })
35
+ })
36
+
37
+ test('merge', () => {
38
+ const object = new ConfigObject()
39
+ object.set('foo', {})
40
+ expect(object.merge({ key: 'foo', value: 'bar' })).toEqual(
41
+ { foo: {}, key: 'foo', value: 'bar' }
42
+ )
43
+ })
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @class
3
+ * @extends { Array }
4
+ */
5
+ class ConfigList extends Array {
6
+ static get [Symbol.species]() { return Array }
7
+
8
+ get(key) {
9
+ const index = this.getIndex(key, true)
10
+ return this[index].value
11
+ }
12
+
13
+ append(key, value) {
14
+ return this.add({ key, value })
15
+ }
16
+
17
+ prepend(key, value) {
18
+ return this.add({ key, value }, 'prepend')
19
+ }
20
+
21
+ insert(key, value, pos = {}) {
22
+ if (!(pos.before || pos.after)) return this.append(key, value)
23
+
24
+ const currentIndex = this.getIndex(key)
25
+ if (currentIndex >= 0) this.splice(currentIndex, 1)
26
+
27
+ let newIndex = this.getIndex(pos.before || pos.after)
28
+ if (pos.after) newIndex += 1
29
+
30
+ this.splice(newIndex, 0, { key, value })
31
+ return this
32
+ }
33
+
34
+ delete(key) {
35
+ const index = this.getIndex(key, true)
36
+ this.splice(index, 1)
37
+ return this
38
+ }
39
+
40
+ getIndex(key, shouldThrow = false) {
41
+ const index = this.findIndex((entry) => (
42
+ entry === key
43
+ || entry.key === key
44
+ || (entry.constructor && entry.constructor.name === key)
45
+ ))
46
+
47
+ if (shouldThrow && index < 0) throw new Error(`Item ${key} not found`)
48
+ return index
49
+ }
50
+
51
+ add({ key, value }, strategy = 'append') {
52
+ const index = this.getIndex(key)
53
+ if (index >= 0) this.delete(key)
54
+
55
+ switch (strategy) {
56
+ case 'prepend':
57
+ this.unshift({ key, value })
58
+ break
59
+ default:
60
+ this.push({ key, value })
61
+ }
62
+
63
+ return this
64
+ }
65
+
66
+ values() {
67
+ return this.map((item) => item.value)
68
+ }
69
+
70
+ keys() {
71
+ return this.map((item) => item.key)
72
+ }
73
+ }
74
+
75
+ module.exports = ConfigList
@@ -0,0 +1,55 @@
1
+ const objectify = require('../utils/objectify')
2
+ const deepAssign = require('../utils/deep_assign')
3
+ const deepMerge = require('../utils/deep_merge')
4
+ const { isStrPath, prettyPrint } = require('../utils/helpers')
5
+
6
+ /**
7
+ * @class
8
+ * @extends { Object }
9
+ */
10
+ class ConfigObject extends Object {
11
+ constructor(props = {}) {
12
+ super()
13
+ this.merge(props)
14
+ }
15
+
16
+ get(key) {
17
+ return isStrPath(key) ? objectify(key, this) : this[key]
18
+ }
19
+
20
+ set(key, value) {
21
+ Object.assign(this, deepAssign(this, key, value))
22
+ return this
23
+ }
24
+
25
+ delete(key) {
26
+ let obj = this
27
+ let propKey = key
28
+
29
+ if (isStrPath(key)) {
30
+ const keys = key.split('.')
31
+ propKey = keys.pop()
32
+ const parentObjPath = keys.join('.')
33
+ obj = objectify(parentObjPath, this)
34
+ }
35
+
36
+ if (!obj) throw new Error(`Prop not found: ${key} in ${prettyPrint(obj)}`)
37
+ delete obj[propKey]
38
+
39
+ return this
40
+ }
41
+
42
+ toObject() {
43
+ const object = {}
44
+ /* eslint no-return-assign: 0 */
45
+ Object.keys(this).forEach((key) => (object[key] = this[key]))
46
+ return object
47
+ }
48
+
49
+ merge(config) {
50
+ Object.assign(this, deepMerge(this, config))
51
+ return this
52
+ }
53
+ }
54
+
55
+ module.exports = ConfigObject
@@ -0,0 +1,7 @@
1
+ const ConfigObject = require('./config_object')
2
+ const ConfigList = require('./config_list')
3
+
4
+ module.exports = {
5
+ ConfigObject,
6
+ ConfigList
7
+ }
@@ -0,0 +1,20 @@
1
+ const { isBoolean } = require('./utils/helpers')
2
+ const config = require('./config')
3
+
4
+ const fetch = (key) => {
5
+ const value = process.env[key]
6
+ return isBoolean(value) ? JSON.parse(value) : value
7
+ }
8
+
9
+ const devServerConfig = config.dev_server
10
+
11
+ if (devServerConfig) {
12
+ const envPrefix = config.dev_server.env_prefix || 'WEBPACKER_DEV_SERVER'
13
+
14
+ Object.keys(devServerConfig).forEach((key) => {
15
+ const envValue = fetch(`${envPrefix}_${key.toUpperCase().replace(/_/g, '')}`)
16
+ if (envValue !== undefined) devServerConfig[key] = envValue
17
+ })
18
+ }
19
+
20
+ module.exports = devServerConfig || {}
@@ -0,0 +1,19 @@
1
+ const { resolve } = require('path')
2
+ const { safeLoad } = require('js-yaml')
3
+ const { readFileSync } = require('fs')
4
+
5
+ const NODE_ENVIRONMENTS = ['development', 'production', 'test']
6
+ const DEFAULT = 'production'
7
+ const configPath = resolve('config', 'webpacker.yml')
8
+
9
+ const railsEnv = process.env.RAILS_ENV
10
+ const nodeEnv = process.env.NODE_ENV
11
+
12
+ const config = safeLoad(readFileSync(configPath), 'utf8')
13
+ const availableEnvironments = Object.keys(config).join('|')
14
+ const regex = new RegExp(`^(${availableEnvironments})$`, 'g')
15
+
16
+ module.exports = {
17
+ railsEnv: railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT,
18
+ nodeEnv: nodeEnv && NODE_ENVIRONMENTS.includes(nodeEnv) ? nodeEnv : DEFAULT
19
+ }
@@ -0,0 +1,74 @@
1
+ /* global test expect, describe, afterAll, beforeEach */
2
+
3
+ // environment.js expects to find config/webpacker.yml and resolved modules from
4
+ // the root of a Rails project
5
+
6
+ const { chdirTestApp, chdirCwd } = require('../../utils/helpers')
7
+
8
+ chdirTestApp()
9
+
10
+ const { resolve } = require('path')
11
+ const rules = require('../../rules')
12
+ const { ConfigList } = require('../../config_types')
13
+ const Environment = require('../base')
14
+
15
+ describe('Environment', () => {
16
+ afterAll(chdirCwd)
17
+
18
+ let environment
19
+
20
+ describe('toWebpackConfig', () => {
21
+ beforeEach(() => {
22
+ environment = new Environment()
23
+ })
24
+
25
+ test('should return entry', () => {
26
+ const config = environment.toWebpackConfig()
27
+ expect(config.entry.application).toEqual(
28
+ resolve('app', 'javascript', 'packs', 'application.js')
29
+ )
30
+ })
31
+
32
+ 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')
36
+ })
37
+
38
+ test('should return default loader rules for each file in config/loaders', () => {
39
+ const config = environment.toWebpackConfig()
40
+ const defaultRules = Object.keys(rules)
41
+ const configRules = config.module.rules
42
+
43
+ expect(defaultRules.length).toEqual(7)
44
+ expect(configRules.length).toEqual(8)
45
+ })
46
+
47
+ test('should return default plugins', () => {
48
+ const config = environment.toWebpackConfig()
49
+ expect(config.plugins.length).toEqual(4)
50
+ })
51
+
52
+ test('should return default resolveLoader', () => {
53
+ const config = environment.toWebpackConfig()
54
+ expect(config.resolveLoader.modules).toEqual(['node_modules'])
55
+ })
56
+
57
+ test('should return default resolve.modules with additions', () => {
58
+ const config = environment.toWebpackConfig()
59
+ expect(config.resolve.modules).toEqual([
60
+ resolve('app', 'javascript'),
61
+ resolve('app/assets'),
62
+ resolve('/etc/yarn'),
63
+ 'node_modules'
64
+ ])
65
+ })
66
+
67
+ 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)
72
+ })
73
+ })
74
+ })