-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix asset host support & improve output path #397
Changes from 3 commits
70eace2
28fc868
4ae0faf
2a94cdf
1a778af
c07dc9b
b3732f3
cc0dfb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
// Note: You must restart bin/webpack-dev-server for changes to take effect | ||
|
||
const { resolve } = require('path') | ||
const merge = require('webpack-merge') | ||
const ManifestPlugin = require('webpack-manifest-plugin') | ||
const devConfig = require('./development.js') | ||
const { devServer, publicPath, paths } = require('./configuration.js') | ||
const { devServer, paths, output, formatPublicPath } = require('./configuration.js') | ||
|
||
const { host, port } = devServer | ||
const contentBase = output.path | ||
const publicPath = formatPublicPath(`http://${host}:${port}`, paths.output) | ||
|
||
// Remove ManifestPlugin so we can replace it with a new one | ||
devConfig.plugins = devConfig.plugins.filter(plugin => plugin.constructor.name !== 'ManifestPlugin') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we consider using |
||
|
||
module.exports = merge(devConfig, { | ||
output: { | ||
publicPath | ||
}, | ||
|
||
devServer: { | ||
host: devServer.host, | ||
port: devServer.port, | ||
host, | ||
port, | ||
contentBase, | ||
publicPath, | ||
compress: true, | ||
headers: { 'Access-Control-Allow-Origin': '*' }, | ||
historyApiFallback: true, | ||
contentBase: resolve(paths.output, paths.entry), | ||
publicPath | ||
} | ||
historyApiFallback: true | ||
}, | ||
|
||
plugins: [ | ||
new ManifestPlugin({ fileName: paths.manifest, publicPath, writeToFileEmit: true }) | ||
] | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
# Note: You must restart bin/webpack-dev-server for changes to take effect | ||
|
||
default: &default | ||
config: config/webpack | ||
entry: packs | ||
output: public | ||
manifest: manifest.json | ||
node_modules: node_modules | ||
source: app/javascript | ||
default: &default # ~ = Rails.root | ||
source: app/javascript # ~/:source | ||
entry: packs # ~/:source/:entry | ||
output: packs # ~/public/:output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome 👍 been thinking to do this |
||
manifest: manifest.json # ~/public/:output/:manifest | ||
config: config/webpack # ~/:config | ||
node_modules: node_modules # ~/:node_modules | ||
extensions: | ||
- .coffee | ||
- .js | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,42 +4,54 @@ | |
|
||
class Webpacker::Configuration < Webpacker::FileLoader | ||
class << self | ||
def config_path | ||
Rails.root.join(paths.fetch(:config, "config/webpack")) | ||
end | ||
|
||
def entry_path | ||
Rails.root.join(source_path, paths.fetch(:entry, "packs")) | ||
source_path.join(fetch(:entry)) | ||
end | ||
|
||
def file_path | ||
Rails.root.join("config", "webpack", "paths.yml") | ||
def output_path | ||
public_path.join(fetch(:output)) | ||
end | ||
|
||
def manifest_path | ||
Rails.root.join(packs_path, paths.fetch(:manifest, "manifest.json")) | ||
output_path.join(fetch(:manifest)) | ||
end | ||
|
||
def packs_path | ||
Rails.root.join(output_path, paths.fetch(:entry, "packs")) | ||
def source_path | ||
Rails.root.join(source) | ||
end | ||
|
||
def paths | ||
load if Webpacker.env.development? | ||
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance | ||
instance.data | ||
def public_path | ||
Rails.root.join("public") | ||
end | ||
|
||
def output_path | ||
Rails.root.join(paths.fetch(:output, "public")) | ||
def config_path | ||
Rails.root.join(fetch(:config)) | ||
end | ||
|
||
def file_path(root: Rails.root) | ||
root.join("config/webpack/paths.yml") | ||
end | ||
|
||
def default_file_path | ||
file_path(root: Pathname.new(__dir__).join("../install")) | ||
end | ||
|
||
def source | ||
paths.fetch(:source, "app/javascript") | ||
fetch(:source) | ||
end | ||
|
||
def source_path | ||
Rails.root.join(source) | ||
def fetch(key) | ||
paths.fetch(key, default_paths[key]) | ||
end | ||
|
||
def paths | ||
load if Webpacker.env.development? | ||
raise Webpacker::FileLoader::FileLoaderError.new("Webpacker::Configuration.load must be called first") unless instance | ||
instance.data | ||
end | ||
|
||
def default_paths | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🍰 🎉 |
||
@default_paths ||= HashWithIndifferentAccess.new(YAML.load(default_file_path.read)["default"]) | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we set
ASSET_HOST
to dev server host in development when it's enabled? That way we don't need to replace manifest plugin.OR
We would be able to do this in development config without
if
, but guess we can't since we have much shared logic here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I described the reason for not doing it this way in the PR:
That said, I cleaned things up in 2a94cdf by constructing and setting
ASSET_HOST
inbin/webpack-dev-server
.