Skip to content

Commit 4fad8b3

Browse files
committed
Reinitialize webpacker with default config except for the following diff in configuration.js
- `http://${devServer.host}:${devServer.port}/` : `/${paths.entry}/` + `http://${devServer.host}:${devServer.port}/${paths.entry}/` : `/${paths.entry}/`
1 parent 8c88a3b commit 4fad8b3

File tree

18 files changed

+117
-177
lines changed

18 files changed

+117
-177
lines changed

test/dummy_webpacker1/.gitignore

Whitespace-only changes.

test/dummy_webpacker1/bin/webpack

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,36 @@ require "shellwords"
55
require "yaml"
66

77
ENV["RAILS_ENV"] ||= "development"
8-
RAILS_ENV = ENV["RAILS_ENV"]
8+
RAILS_ENV = ENV["RAILS_ENV"]
99

1010
ENV["NODE_ENV"] ||= RAILS_ENV
11-
NODE_ENV = ENV["NODE_ENV"]
11+
NODE_ENV = ENV["NODE_ENV"]
1212

13-
APP_PATH = File.expand_path("../", __dir__)
13+
APP_PATH = File.expand_path("../", __dir__)
14+
CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
15+
DEV_SERVER_CONFIG_PATH = File.join(APP_PATH, "config/webpack/development.server.yml")
1416

15-
def load_yaml_config(config_file)
16-
YAML.load_file(File.join(APP_PATH, config_file))[NODE_ENV]
17+
begin
18+
paths = YAML.load(File.read(CONFIG_PATH))
19+
dev_server = YAML.load(File.read(DEV_SERVER_CONFIG_PATH))
20+
21+
NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
22+
WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
23+
24+
if NODE_ENV == "development" && dev_server["enabled"]
25+
puts "Warning: webpack-dev-server is currently enabled in #{DEV_SERVER_CONFIG_PATH}. " \
26+
"Disable to serve assets directly from public/packs directory"
27+
end
1728
rescue Errno::ENOENT, NoMethodError
18-
puts "Configuration not found in #{config_file}."
29+
puts "Configuration not found in config/webpack/paths.yml or config/webpack/development.server.yml."
1930
puts "Please run bundle exec rails webpacker:install to install webpacker"
2031
exit!
2132
end
2233

23-
paths = load_yaml_config("config/webpack/paths.yml")
24-
NODE_MODULES_PATH = File.join(APP_PATH, paths["node_modules"])
25-
WEBPACK_CONFIG = File.join(APP_PATH, paths["config"], "#{NODE_ENV}.js")
26-
27-
newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
28-
cmdline = ["yarn", "run", "webpack", "--", "--config", WEBPACK_CONFIG] + ARGV
34+
WEBPACK_BIN = "#{NODE_MODULES_PATH}/.bin/webpack"
35+
WEBPACK_CONFIG = "#{WEBPACK_CONFIG_PATH}/#{NODE_ENV}.js"
2936

3037
Dir.chdir(APP_PATH) do
31-
exec newenv, *cmdline
38+
exec "NODE_PATH=#{NODE_MODULES_PATH} #{WEBPACK_BIN} --config #{WEBPACK_CONFIG}" \
39+
" #{ARGV.join(" ")}"
3240
end

test/dummy_webpacker1/bin/webpack-dev-server

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@ RAILS_ENV = ENV["RAILS_ENV"]
1010
ENV["NODE_ENV"] ||= RAILS_ENV
1111
NODE_ENV = ENV["NODE_ENV"]
1212

13-
APP_PATH = File.expand_path("../", __dir__)
13+
APP_PATH = File.expand_path("../", __dir__)
14+
CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
1415

15-
def load_yaml_config(config_file)
16-
YAML.load_file(File.join(APP_PATH, config_file))[NODE_ENV]
16+
begin
17+
paths = YAML.load(File.read(CONFIG_PATH))
18+
19+
NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
20+
WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
21+
22+
WEBPACK_BIN = "#{NODE_MODULES_PATH}/.bin/webpack-dev-server"
23+
DEV_SERVER_CONFIG = "#{WEBPACK_CONFIG_PATH}/development.server.js"
1724
rescue Errno::ENOENT, NoMethodError
18-
puts "Configuration not found in #{config_file}."
25+
puts "Configuration not found in config/webpacker/paths.yml."
1926
puts "Please run bundle exec rails webpacker:install to install webpacker"
2027
exit!
2128
end
2229

23-
paths = load_yaml_config("config/webpack/paths.yml")
24-
NODE_MODULES_PATH = File.join(APP_PATH, paths["node_modules"])
25-
WEBPACK_CONFIG = File.join(APP_PATH, paths["config"], "development.server.js")
26-
27-
dev_server = load_yaml_config("config/webpack/development.server.yml")
28-
DEV_SERVER_HOST = "http#{"s" if dev_server["https"]}://#{dev_server["host"]}:#{dev_server["port"]}"
29-
30-
newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape, "ASSET_HOST" => DEV_SERVER_HOST.shellescape }
31-
cmdline = ["yarn", "run", "webpack-dev-server", "--", "--progress", "--color", "--config", WEBPACK_CONFIG] + ARGV
32-
3330
Dir.chdir(APP_PATH) do
34-
exec newenv, *cmdline
31+
exec "NODE_PATH=#{NODE_MODULES_PATH} #{WEBPACK_BIN} --progress --color " \
32+
"--config #{DEV_SERVER_CONFIG}"
3533
end

test/dummy_webpacker1/config/application.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
require "action_controller/railtie"
66
require "action_mailer/railtie"
77

8-
# Test no-sprockets environment by testing the gemfile name
9-
if SprocketsHelpers.available?
10-
require "sprockets/railtie"
11-
end
12-
138
require "rails/test_unit/railtie"
149

1510
# Make sure gems in development group are required, for example, react-rails and turbolinks.
@@ -35,9 +30,5 @@ class Application < Rails::Application
3530
config.react.server_renderer_options = {
3631
replay_console: true,
3732
}
38-
39-
if SprocketsHelpers.available?
40-
config.assets.enabled = true
41-
end
4233
end
4334
end

test/dummy_webpacker1/config/environments/development.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@
1818

1919
# Print deprecation notices to the Rails logger.
2020
config.active_support.deprecation = :log
21-
22-
# Debug mode disables concatenation and preprocessing of assets.
23-
# This option may cause significant delays in view rendering with a large
24-
# number of complex assets.
25-
config.assets.debug = true
2621
end

test/dummy_webpacker1/config/environments/test.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
# we need this to reload the jsx transformer when different version is dropped in
1010
config.cache_classes = false
1111
config.reload_plugins = true
12-
if SprocketsHelpers.available?
13-
config.assets.cache_store = :null_store
14-
end
1512

1613
# Do not eager load code on boot. This avoids loading your whole application
1714
# just for the purpose of running a single test. If you are using a tool that

test/dummy_webpacker1/config/webpack/configuration.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,15 @@ const { readFileSync } = require('fs')
77

88
const configPath = resolve('config', 'webpack')
99
const loadersDir = join(__dirname, 'loaders')
10-
const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))[env.NODE_ENV]
11-
const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))[env.NODE_ENV]
12-
13-
function removeOuterSlashes(string) {
14-
return string.replace(/^\/*/, '').replace(/\/*$/, '')
15-
}
16-
17-
function formatPublicPath(host = '', path = '') {
18-
let formattedHost = removeOuterSlashes(host)
19-
if (formattedHost && !/^http/i.test(formattedHost)) {
20-
formattedHost = `//${formattedHost}`
21-
}
22-
const formattedPath = removeOuterSlashes(path)
23-
return `${formattedHost}/${formattedPath}/`
24-
}
25-
26-
const output = {
27-
path: resolve('public', paths.output),
28-
publicPath: formatPublicPath(env.ASSET_HOST, paths.output)
29-
}
10+
const paths = safeLoad(readFileSync(join(configPath, 'paths.yml'), 'utf8'))
11+
const devServer = safeLoad(readFileSync(join(configPath, 'development.server.yml'), 'utf8'))
12+
const publicPath = env.NODE_ENV !== 'production' && devServer.enabled ?
13+
`http://${devServer.host}:${devServer.port}/${paths.entry}/` : `/${paths.entry}/`
3014

3115
module.exports = {
3216
devServer,
3317
env,
3418
paths,
3519
loadersDir,
36-
output
20+
publicPath
3721
}
Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
try {
2-
const environment = require('./environment')
1+
// Note: You must restart bin/webpack-watcher for changes to take effect
32

4-
module.exports = environment.toWebpackConfig()
5-
} catch (e) {
6-
const merge = require('webpack-merge')
7-
const sharedConfig = require('./shared.js')
3+
const merge = require('webpack-merge')
4+
const sharedConfig = require('./shared.js')
85

9-
module.exports = merge(sharedConfig, {
10-
devtool: 'sourcemap',
6+
module.exports = merge(sharedConfig, {
7+
devtool: 'sourcemap',
118

12-
stats: {
13-
errorDetails: true
14-
},
9+
stats: {
10+
errorDetails: true
11+
},
1512

16-
output: {
17-
pathinfo: true
18-
}
19-
})
20-
}
13+
output: {
14+
pathinfo: true
15+
}
16+
})
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// Note: You must restart bin/webpack-dev-server for changes to take effect
22

3+
const { resolve } = require('path')
34
const merge = require('webpack-merge')
45
const devConfig = require('./development.js')
5-
const { devServer, output } = require('./configuration.js')
6+
const { devServer, publicPath, paths } = require('./configuration.js')
67

78
module.exports = merge(devConfig, {
89
devServer: {
910
host: devServer.host,
1011
port: devServer.port,
11-
contentBase: output.path,
12-
publicPath: output.publicPath,
1312
compress: true,
14-
headers: { 'Access-Control-Allow-Origin': '*' },
15-
historyApiFallback: true
13+
historyApiFallback: true,
14+
contentBase: resolve(paths.output, paths.entry),
15+
publicPath
1616
}
1717
})
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
# Note: You must restart bin/webpack-dev-server for changes to take effect
2-
3-
default: &default
4-
enabled: true
5-
host: localhost
6-
port: 8080
7-
8-
development:
9-
<<: *default
10-
11-
test:
12-
<<: *default
13-
enabled: false
14-
15-
production:
16-
<<: *default
17-
enabled: false
1+
# Restart webpack-dev-server if you make changes here
2+
enabled: true
3+
host: localhost
4+
port: 8080

0 commit comments

Comments
 (0)