-
Notifications
You must be signed in to change notification settings - Fork 166
Replace Webpack dev server with zero-dependency alternative #11485
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f3d6a9
Replace Webpack dev server with zero-dependency alternative
aduth ab9584b
Update example to conditionally include plugin for development
aduth 996a145
End server on compilation close
aduth b1059a2
Remove unnecessary build promise initialization
aduth 739d4cb
Avoid setting specific encoding for file read stream
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| web: WEBPACK_PORT=${WEBPACK_PORT:-3035} bundle exec rackup config.ru --port ${PORT:-3000} --host ${FOREMAN_HOST:-${HOST:-localhost}} | ||
| worker: bundle exec good_job start | ||
| js: WEBPACK_PORT=${WEBPACK_PORT:-3035} yarn webpack $([ -n "$HTTPS" ] && echo "--watch" || echo "serve") | ||
| js: WEBPACK_PORT=${WEBPACK_PORT:-3035} yarn webpack --watch | ||
| css: yarn build:css --watch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # `@18f/identity-lite-webpack-dev-server` | ||
|
|
||
| Minimal, zero-dependency alternative to Webpack's default development web server. | ||
|
|
||
| **What does it do the same as `webpack-dev-server`?** | ||
|
|
||
| - Serves static assets from the built output directory | ||
| - Pauses page loads during compilation to guarantee that a page loads with the latest JavaScript | ||
|
|
||
| **What doesn't it do that `webpack-dev-server` does?** | ||
|
|
||
| Most everything else! Notably, it does not: | ||
|
|
||
| - Automatically reload the page when compilation finishes | ||
| - Handle anything other than JavaScript | ||
|
|
||
| ## Usage | ||
|
|
||
| If migrating from `webpack-dev-server`: | ||
|
|
||
| - Remove your `devServer` configuration from `webpack.config.js` | ||
|
|
||
| Add an instance of `LiteWebpackDevServerPlugin` to your Webpack `plugins` array. The example below | ||
| shows how you might conditionally include the plugin in local development only, to avoid the server | ||
| being run in production environments. | ||
|
|
||
| ```ts | ||
| // webpack.config.js | ||
|
|
||
| const { DEV_SERVER_PORT } = process.env; | ||
|
|
||
| export default { | ||
| // ... | ||
| plugins: [ | ||
| // ... | ||
| DEV_SERVER_PORT && | ||
| new LiteWebpackDevServerPlugin({ publicPath: './public', port: Number(DEV_SERVER_PORT) }), | ||
| ] | ||
| }; | ||
| ``` | ||
|
|
||
| Supported options: | ||
|
|
||
| - `publicPath` (`string`): Relative path to the root of the static file server | ||
| - `port` (`number`): Port on which the static file server should listen | ||
| - `headers` (`object`): Additional headers to include in every response | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "@18f/identity-lite-webpack-dev-server", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "sideEffects": false, | ||
| "main": "./webpack-plugin.js" | ||
| } |
85 changes: 85 additions & 0 deletions
85
app/javascript/packages/lite-webpack-dev-server/webpack-plugin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| const http = require('node:http'); | ||
| const { join } = require('node:path'); | ||
| const { createReadStream } = require('node:fs'); | ||
|
|
||
| /** | ||
| * @typedef PluginOptions | ||
| * @prop {string} [publicPath] | ||
| * @prop {number} [port] | ||
| * @prop {Record<string, string>} [headers] | ||
| */ | ||
|
|
||
| /** | ||
| * Webpack plugin name. | ||
| * | ||
| * @type {string} | ||
| */ | ||
| const PLUGIN = 'LiteWebpackDevServerPlugin'; | ||
|
|
||
| class LiteWebpackDevServerPlugin { | ||
| /** | ||
| * @type {string} | ||
| */ | ||
| publicPath; | ||
|
|
||
| /** | ||
| * @type {number} | ||
| */ | ||
| port; | ||
|
|
||
| /** | ||
| * @type {Record<string, string>} | ||
| */ | ||
| headers; | ||
|
|
||
| /** | ||
| * @param {PluginOptions} options | ||
| */ | ||
| constructor(options) { | ||
| Object.assign(this, { | ||
| publicPath: '.', | ||
| port: 3035, | ||
| headers: { | ||
| 'content-type': 'text/javascript', | ||
| ...options.headers, | ||
| }, | ||
| ...options, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param {import('webpack').Compiler} compiler | ||
| */ | ||
| apply(compiler) { | ||
| /** @type {Promise<void>} */ | ||
| let build; | ||
|
|
||
| /** @type {() => void} */ | ||
| let onCompileFinished; | ||
|
|
||
| const server = http.createServer(async (request, response) => { | ||
| for (const [key, value] of Object.entries(this.headers)) { | ||
| response.setHeader(key, value); | ||
| } | ||
|
|
||
| await build; | ||
| const url = new URL(request.url ?? '', 'file:///'); | ||
| const filePath = join(process.cwd(), this.publicPath, url.pathname); | ||
| createReadStream(filePath).pipe(response); | ||
| }); | ||
|
|
||
| server.listen(this.port); | ||
|
|
||
| compiler.hooks.beforeCompile.tap(PLUGIN, () => { | ||
| build = new Promise((resolve) => { | ||
| onCompileFinished = resolve; | ||
| }); | ||
| }); | ||
|
|
||
| compiler.hooks.afterCompile.tap(PLUGIN, () => onCompileFinished()); | ||
|
|
||
| compiler.hooks.shutdown.tap(PLUGIN, () => server.close()); | ||
| } | ||
| } | ||
|
|
||
| module.exports = LiteWebpackDevServerPlugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.