-
Notifications
You must be signed in to change notification settings - Fork 214
Clean up dev-server options #1175
Changes from all commits
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,48 +1,16 @@ | ||
const merge = require('deepmerge'); | ||
|
||
const isLocal = host => host === 'localhost' || host === '127.0.0.1'; | ||
const getHost = publicHost => (isLocal(publicHost) ? 'localhost' : '0.0.0.0'); | ||
const getPort = opts => opts.port || 5000; | ||
const getPublic = options => { | ||
const port = getPort(options); | ||
|
||
if (options.public) { | ||
const normalizedPath = options.public.split(':'); | ||
|
||
return normalizedPath.length === 2 | ||
? options.public | ||
: `${normalizedPath[0]}:${port}`; | ||
} | ||
|
||
return !options.host || isLocal(options.host) | ||
? `localhost:${port}` | ||
: `${options.host}:${port}`; | ||
}; | ||
|
||
module.exports = (neutrino, opts = {}) => { | ||
const port = getPort(opts); | ||
const publicHost = getPublic(opts); | ||
const host = getHost(publicHost); | ||
|
||
neutrino.config.devServer.merge(merge.all([ | ||
{ | ||
port, | ||
https: false, | ||
hot: true, | ||
historyApiFallback: true, | ||
publicPath: '/', | ||
headers: { | ||
host: publicHost | ||
}, | ||
// Only display compile duration and errors/warnings, to reduce noise when rebuilding. | ||
stats: { | ||
all: false, | ||
errors: true, | ||
timings: true, | ||
warnings: true | ||
} | ||
module.exports = (neutrino, options = {}) => { | ||
neutrino.config.devServer.merge({ | ||
port: 5000, | ||
hot: true, | ||
// Redirect 404s to index.html, so that apps that use the HTML 5 History API work. | ||
historyApiFallback: true, | ||
// Only display compile duration and errors/warnings, to reduce noise when rebuilding. | ||
stats: { | ||
all: false, | ||
errors: true, | ||
timings: true, | ||
warnings: true | ||
}, | ||
opts, | ||
{ host, public: publicHost } | ||
])); | ||
...options | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ const htmlTemplate = require('@neutrinojs/html-template'); | |
const clean = require('@neutrinojs/clean'); | ||
const loaderMerge = require('@neutrinojs/loader-merge'); | ||
const devServer = require('@neutrinojs/dev-server'); | ||
const { resolve } = require('url'); | ||
const merge = require('deepmerge'); | ||
const { ConfigurationError } = require('neutrino/errors'); | ||
|
||
|
@@ -22,9 +21,8 @@ module.exports = (neutrino, opts = {}) => { | |
} | ||
|
||
const isProduction = process.env.NODE_ENV === 'production'; | ||
const publicPath = opts.publicPath || './'; | ||
const options = merge({ | ||
publicPath, | ||
publicPath: '', | ||
env: false, | ||
hot: true, | ||
html: {}, | ||
|
@@ -34,8 +32,7 @@ module.exports = (neutrino, opts = {}) => { | |
test: 'source-map' | ||
}, | ||
devServer: { | ||
hot: opts.hot !== false, | ||
publicPath: resolve('/', publicPath) | ||
hot: opts.hot !== false | ||
}, | ||
style: { | ||
hot: opts.hot !== false, | ||
|
@@ -85,6 +82,14 @@ module.exports = (neutrino, opts = {}) => { | |
); | ||
} | ||
|
||
if (typeof options.devServer.proxy === 'string') { | ||
throw new ConfigurationError( | ||
'The shorthand of setting `devServer.proxy` to a string is no longer supported. ' + | ||
'Use an object and the options listed here instead: ' + | ||
'https://webpack.js.org/configuration/dev-server/#devserver-proxy' | ||
); | ||
} | ||
|
||
if (typeof options.devtool === 'string' || typeof options.devtool === 'boolean') { | ||
options.devtool = { | ||
development: options.devtool, | ||
|
@@ -93,18 +98,6 @@ module.exports = (neutrino, opts = {}) => { | |
}; | ||
} | ||
|
||
if (typeof options.devServer.proxy === 'string') { | ||
options.devServer.proxy = { | ||
'**': { | ||
target: options.devServer.proxy, | ||
changeOrigin: true, | ||
headers: { | ||
Forwarded: 'by=_webpack-dev-server' | ||
} | ||
} | ||
}; | ||
} | ||
|
||
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. Should we include the 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. I think we could easily write several pages on how to set up SSR. Multiple other parts of the webpack config needs adjusting, eg I'm pretty sure html-webpack-plugin doesn't fully work with it out of the box and needs some experimental options enabled. I think this example is perhaps best aimed at the more common case of "I have a standard SPA but it uses a backend and I want to proxy to it in development". (Not that it precludes adding a more advanced SSR guide somewhere else in the future) 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. Also, perhaps an SSR guide is best suited as an extra page in webpack's docs? Ultimately Neutrino is not meant to be a replacement for all of webpack/...'s documentation, but a very thin layer of abstraction that assists with configuration generation, sets some sensible defaults, and that makes it easy to mentally map between a webpack guide and how to add that configuration via 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. #1177 :) 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.
I underestimated how many pages...! 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. Also: it's really 2 different things if you're talking about SSR with React/Vue vs. using webpack/neutrino for "assets", and something else for SSR of html (PHP, Ruby, etc) 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. Yeah true |
||
// Force @babel/preset-env default behavior (.browserslistrc) | ||
if (options.targets === false) { | ||
options.targets = {}; | ||
|
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.
Now that the
@neutrinojs/dev-server
preset only sets thedevServer
option, there's no real reason why the middleware shouldn't always be used.