Skip to content

Commit 2a94cdf

Browse files
committed
Construct dev server asset host in bin/webpack-dev-server
1 parent 4ae0faf commit 2a94cdf

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

lib/install/bin/webpack-dev-server.tt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ 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__)
14-
CONFIG_PATH = File.join(APP_PATH, "config/webpack/paths.yml")
13+
APP_PATH = File.expand_path("../", __dir__)
1514

16-
begin
17-
paths = YAML.load(File.read(CONFIG_PATH))[NODE_ENV]
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-
DEV_SERVER_CONFIG = "#{WEBPACK_CONFIG_PATH}/development.server.js"
15+
def load_yaml_config(config_file)
16+
YAML.load_file(File.join(APP_PATH, config_file))[NODE_ENV]
2317
rescue Errno::ENOENT, NoMethodError
24-
puts "Configuration not found in config/webpacker/paths.yml."
18+
puts "Configuration not found in #{config_file}."
2519
puts "Please run bundle exec rails webpacker:install to install webpacker"
2620
exit!
2721
end
2822

29-
DEV_SERVER_BIN = "yarn run webpack-dev-server"
30-
newenv = { "NODE_PATH" => NODE_MODULES_PATH }
31-
cmdline = [DEV_SERVER_BIN, "--", "--progress", "--color", "--config", DEV_SERVER_CONFIG] + ARGV
23+
paths = load_yaml_config("config/webpack/paths.yml")
24+
NODE_MODULES_PATH = File.join(APP_PATH, paths["node_modules"])
25+
DEV_SERVER_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", DEV_SERVER_CONFIG] + ARGV
3232

3333
Dir.chdir(APP_PATH) do
34-
exec newenv, cmdline.join(' ')
34+
exec newenv, *cmdline
3535
end

lib/install/config/webpack/configuration.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ module.exports = {
3333
env,
3434
paths,
3535
loadersDir,
36-
output,
37-
formatPublicPath
36+
output
3837
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
// Note: You must restart bin/webpack-dev-server for changes to take effect
22

33
const merge = require('webpack-merge')
4-
const ManifestPlugin = require('webpack-manifest-plugin')
54
const devConfig = require('./development.js')
6-
const { devServer, paths, output, formatPublicPath } = require('./configuration.js')
7-
8-
const { host, port } = devServer
9-
const contentBase = output.path
10-
const publicPath = formatPublicPath(`http://${host}:${port}`, paths.output)
11-
12-
// Remove ManifestPlugin so we can replace it with a new one
13-
devConfig.plugins = devConfig.plugins.filter(plugin => plugin.constructor.name !== 'ManifestPlugin')
5+
const { devServer, output } = require('./configuration.js')
146

157
module.exports = merge(devConfig, {
16-
output: {
17-
publicPath
18-
},
19-
208
devServer: {
21-
host,
22-
port,
23-
contentBase,
24-
publicPath,
9+
host: devServer.host,
10+
port: devServer.port,
11+
contentBase: output.path,
12+
publicPath: output.publicPath,
13+
inline: false,
2514
compress: true,
2615
headers: { 'Access-Control-Allow-Origin': '*' },
2716
historyApiFallback: true
28-
},
29-
30-
plugins: [
31-
new ManifestPlugin({ fileName: paths.manifest, publicPath, writeToFileEmit: true })
32-
]
17+
}
3318
})

0 commit comments

Comments
 (0)