Skip to content

Commit b6dc2aa

Browse files
committed
Regenerate webpacker configs for webpacker 2
1 parent c587945 commit b6dc2aa

File tree

16 files changed

+1260
-1724
lines changed

16 files changed

+1260
-1724
lines changed

lib/react/server_rendering/webpacker_manifest_container.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "open-uri"
2-
require 'pry'
2+
# require 'pry'
33

44
module React
55
module ServerRendering

test/dummy_webpacker2/.babelrc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"presets": [
3-
[
4-
"env",
5-
{
6-
"modules": false,
7-
"targets": {
8-
"browsers": "> 1%",
9-
"uglify": true
10-
},
11-
"useBuiltIns": true
12-
}
13-
],
14-
"react"
3+
["env", {
4+
"modules": false,
5+
"targets": {
6+
"browsers": "> 1%",
7+
"uglify": true
8+
},
9+
"useBuiltIns": true
10+
}]
11+
],
12+
13+
"plugins": [
14+
"syntax-dynamic-import",
15+
["transform-class-properties", { "spec": true }]
1516
]
1617
}

test/dummy_webpacker2/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/public/packs
2+
/node_modules

test/dummy_webpacker2/bin/webpack

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,24 @@ 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__)
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")
13+
APP_PATH = File.expand_path("../", __dir__)
14+
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
15+
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/#{NODE_ENV}.js")
1616

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
28-
rescue Errno::ENOENT, NoMethodError
29-
puts "Configuration not found in config/webpack/paths.yml or config/webpack/development.server.yml."
17+
unless File.exist?(WEBPACK_CONFIG)
18+
puts "Webpack configuration not found."
3019
puts "Please run bundle exec rails webpacker:install to install webpacker"
3120
exit!
3221
end
3322

34-
WEBPACK_BIN = "#{NODE_MODULES_PATH}/.bin/webpack"
35-
WEBPACK_CONFIG = "#{WEBPACK_CONFIG_PATH}/#{NODE_ENV}.js"
23+
newenv = { "NODE_PATH" => NODE_MODULES_PATH.shellescape }
24+
cmdline = ["yarn", "run", "webpack", "--", "--config", WEBPACK_CONFIG] + ARGV
3625

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

test/dummy_webpacker2/bin/webpack-dev-server

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,34 @@ 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__)
14+
CONFIG_FILE = File.join(APP_PATH, "config/webpacker.yml")
15+
NODE_MODULES_PATH = File.join(APP_PATH, "node_modules")
16+
WEBPACK_CONFIG = File.join(APP_PATH, "config/webpack/development.js")
17+
18+
def args(key)
19+
index = ARGV.index(key)
20+
index ? ARGV[index + 1] : nil
21+
end
1522

1623
begin
17-
paths = YAML.load(File.read(CONFIG_PATH))
24+
dev_server = YAML.load_file(CONFIG_FILE)["development"]["dev_server"]
1825

19-
NODE_MODULES_PATH = File.join(APP_PATH.shellescape, paths["node_modules"])
20-
WEBPACK_CONFIG_PATH = File.join(APP_PATH.shellescape, paths["config"])
26+
DEV_SERVER_HOST = "http#{"s" if args('--https') || dev_server["https"]}://#{args('--host') || dev_server["host"]}:#{args('--port') || dev_server["port"]}"
2127

22-
WEBPACK_BIN = "#{NODE_MODULES_PATH}/.bin/webpack-dev-server"
23-
DEV_SERVER_CONFIG = "#{WEBPACK_CONFIG_PATH}/development.server.js"
2428
rescue Errno::ENOENT, NoMethodError
25-
puts "Configuration not found in config/webpacker/paths.yml."
29+
puts "Webpack dev_server configuration not found in #{CONFIG_FILE}."
2630
puts "Please run bundle exec rails webpacker:install to install webpacker"
2731
exit!
2832
end
2933

34+
newenv = {
35+
"NODE_PATH" => NODE_MODULES_PATH.shellescape,
36+
"ASSET_HOST" => DEV_SERVER_HOST.shellescape
37+
}.freeze
38+
39+
cmdline = ["yarn", "run", "webpack-dev-server", "--", "--progress", "--color", "--config", WEBPACK_CONFIG] + ARGV
40+
3041
Dir.chdir(APP_PATH) do
31-
exec "NODE_PATH=#{NODE_MODULES_PATH} #{WEBPACK_BIN} --progress --color " \
32-
"--config #{DEV_SERVER_CONFIG}"
42+
exec newenv, *cmdline
3343
end
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
// Common configuration for webpacker loaded from config/webpack/paths.yml
1+
// Common configuration for webpacker loaded from config/webpacker.yml
22

33
const { join, resolve } = require('path')
44
const { env } = require('process')
55
const { safeLoad } = require('js-yaml')
66
const { readFileSync } = require('fs')
77

8-
const configPath = resolve('config', 'webpack')
8+
const configPath = resolve('config', 'webpacker.yml')
99
const loadersDir = join(__dirname, 'loaders')
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}/`
10+
const settings = safeLoad(readFileSync(configPath), 'utf8')[env.NODE_ENV]
11+
12+
function removeOuterSlashes(string) {
13+
return string.replace(/^\/*/, '').replace(/\/*$/, '')
14+
}
15+
16+
function formatPublicPath(host = '', path = '') {
17+
let formattedHost = removeOuterSlashes(host)
18+
if (formattedHost && !/^http/i.test(formattedHost)) {
19+
formattedHost = `//${formattedHost}`
20+
}
21+
const formattedPath = removeOuterSlashes(path)
22+
return `${formattedHost}/${formattedPath}/`
23+
}
24+
25+
const output = {
26+
path: resolve('public', settings.public_output_path),
27+
publicPath: formatPublicPath(env.ASSET_HOST, settings.public_output_path)
28+
}
1429

1530
module.exports = {
16-
devServer,
31+
settings,
1732
env,
18-
paths,
1933
loadersDir,
20-
publicPath
34+
output
2135
}
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
// Note: You must restart bin/webpack-watcher for changes to take effect
1+
// Note: You must restart bin/webpack-dev-server for changes to take effect
22

33
const merge = require('webpack-merge')
44
const sharedConfig = require('./shared.js')
5+
const { settings, output } = require('./configuration.js')
56

67
module.exports = merge(sharedConfig, {
7-
devtool: 'sourcemap',
8+
devtool: 'cheap-eval-source-map',
89

910
stats: {
1011
errorDetails: true
1112
},
1213

1314
output: {
1415
pathinfo: true
16+
},
17+
18+
devServer: {
19+
clientLogLevel: 'none',
20+
https: settings.dev_server.https,
21+
host: settings.dev_server.host,
22+
port: settings.dev_server.port,
23+
contentBase: output.path,
24+
publicPath: output.publicPath,
25+
compress: true,
26+
headers: { 'Access-Control-Allow-Origin': '*' },
27+
historyApiFallback: true,
28+
watchOptions: {
29+
ignored: /node_modules/
30+
}
1531
}
1632
})

test/dummy_webpacker2/config/webpack/loaders/assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { env, publicPath } = require('../configuration.js')
22

33
module.exports = {
4-
test: /\.(jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
4+
test: /\.(jpg|jpeg|png|gif|svg|eot|ttf|woff|woff2)$/i,
55
use: [{
66
loader: 'file-loader',
77
options: {
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
module.exports = {
22
test: /\.js(\.erb)?$/,
33
exclude: /node_modules/,
4-
loader: 'babel-loader',
5-
options: {
6-
presets: [
7-
['env', { modules: false }]
8-
]
9-
}
4+
loader: 'babel-loader'
105
}

test/dummy_webpacker2/config/webpack/loaders/erb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = {
44
exclude: /node_modules/,
55
loader: 'rails-erb-loader',
66
options: {
7-
runner: 'DISABLE_SPRING=1 bin/rails runner'
7+
runner: 'bin/rails runner'
88
}
99
}

0 commit comments

Comments
 (0)