GeezerVison is showing github.com — :construction: improved webpack servers · nullstack/nullstack@9e88bcb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: client/index.js +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,9 +109,18 @@ export default class Nullstack { | 109 | 109 |
| 110 | 110 | if (module.hot) { | 111 | 111 | const socket = new WebSocket('ws' + window.location.origin.slice(4) + '/ws'); | 112 | | - socket.onmessage = function (e) { | 113 | | - if (e.data.indexOf('still-ok') > -1) { | 114 | | - window.location.reload() | | 112 | + window.lastHash | | 113 | + socket.onmessage = async function (e) { | | 114 | + const data = JSON.parse(e.data) | | 115 | + if (data.type === 'NULLSTACK_SERVER_STARTED') { | | 116 | + window.needsReload && window.location.reload() | | 117 | + } else if (data.type === 'hash') { | | 118 | + const newHash = data.data.slice(20) | | 119 | + if (newHash === window.lastHash) { | | 120 | + window.needsReload = true | | 121 | + } else { | | 122 | + window.lastHash = newHash | | 123 | + } | 115 | 124 | } | 116 | 125 | }; | 117 | 126 | Nullstack.updateInstancesPrototypes = function updateInstancesPrototypes(hash, klass) { | |
Collapse file Copy file name to clipboardExpand all lines: package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | "fs-extra": "10.1.0", | 25 | 25 | "mini-css-extract-plugin": "2.6.0", | 26 | 26 | "node-fetch": "2.6.7", | | 27 | + "nodemon-webpack-plugin": "^4.8.1", | 27 | 28 | "sass": "1.51.0", | 28 | 29 | "sass-loader": "8.0.2", | 29 | 30 | "swc-loader": "0.2.1", | |
Collapse file Copy file name to clipboardExpand all lines: scripts/index.js +61-59Lines changed: 61 additions & 59 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,74 +21,76 @@ function getServerCompiler(options) { | 21 | 21 | return webpack(getConfig(options)[0]) | 22 | 22 | } | 23 | 23 |
| 24 | | -function getClientCompiler(options) { | 25 | | - return webpack(getConfig(options)[1]) | 26 | | -} | 27 | | - | 28 | | -async function start({ input, port, env, mode = 'ssr', hot }) { | 29 | | - const environment = 'development'; | 30 | | - const serverCompiler = getServerCompiler({ environment, input }); | 31 | | - let clientStarted = false | | 24 | +function loadEnv(env) { | 32 | 25 | let envPath = '.env' | 33 | 26 | if (env) { | 34 | 27 | envPath += `.${process.env.NULLSTACK_ENVIRONMENT_NAME}` | 35 | 28 | } | 36 | 29 | dotenv.config({ path: envPath }) | 37 | | - if (!port) { | 38 | | - port = process.env['NULLSTACK_SERVER_PORT'] || process.env['PORT'] || 3000 | 39 | | - } | 40 | | - process.env['NULLSTACK_ENVIRONMENT_MODE'] = mode | 41 | | - console.log(` 🚀️ Starting your application in ${environment} mode...`); | 42 | | - const WebpackDevServer = require('webpack-dev-server'); | | 30 | +} | | 31 | + | | 32 | +function getFreePorts() { | | 33 | + return new Promise((resolve, reject) => { | | 34 | + const app1 = require('express')(); | | 35 | + const app2 = require('express')(); | | 36 | + const server1 = app1.listen(0, () => { | | 37 | + const server2 = app2.listen(0, () => { | | 38 | + const ports = [ | | 39 | + server1.address().port, | | 40 | + server2.address().port | | 41 | + ] | | 42 | + server1.close() | | 43 | + server2.close() | | 44 | + resolve(ports) | | 45 | + }); | | 46 | + }); | | 47 | + }) | | 48 | +} | | 49 | + | | 50 | +function getPort(port) { | | 51 | + return port || process.env['NULLSTACK_SERVER_PORT'] || process.env['PORT'] || 3000 | | 52 | +} | | 53 | + | | 54 | +async function start({ input, port, env, mode = 'spa', hot }) { | | 55 | + loadEnv(env) | 43 | 56 | const { setLogLevel } = require('webpack/hot/log') | | 57 | + const WebpackDevServer = require('webpack-dev-server'); | 44 | 58 | setLogLevel('none') | | 59 | + process.env['NULLSTACK_ENVIRONMENT_MODE'] = mode | | 60 | + process.env['NULLSTACK_SERVER_PORT'] = getPort(port) | | 61 | + const ports = await getFreePorts() | | 62 | + process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT'] = ports[0] | | 63 | + process.env['NULSTACK_SERVER_SOCKET_PORT_YOU_SHOULD_NOT_CARE_ABOUT'] = ports[1] | | 64 | + const devServerOptions = { | | 65 | + hot: !!hot, | | 66 | + open: false, | | 67 | + devMiddleware: { | | 68 | + index: false | | 69 | + }, | | 70 | + client: { | | 71 | + overlay: { errors: true, warnings: false }, | | 72 | + logging: 'none', | | 73 | + progress: false, | | 74 | + }, | | 75 | + proxy: { | | 76 | + context: () => true, | | 77 | + target: `http://localhost:${process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT']}` | | 78 | + }, | | 79 | + webSocketServer: require.resolve('./socket'), | | 80 | + port: process.env['NULLSTACK_SERVER_PORT'] | | 81 | + }; | | 82 | + const environment = 'development' | | 83 | + const clientCompiler = getCompiler({ environment, input }); | | 84 | + const server = new WebpackDevServer(devServerOptions, clientCompiler); | | 85 | + const serverCompiler = getServerCompiler({ environment, input }); | | 86 | + let once = false | 45 | 87 | serverCompiler.watch({}, (error, stats) => { | 46 | | - if (stats.hasErrors()) { | 47 | | - console.log(`\n 💥️ There is an error preventing compilation`); | 48 | | - } else { | 49 | | - console.log('\x1b[36m%s\x1b[0m', `\n ✅️ Your application is ready at http://localhost:${port}\n`); | 50 | | - } | 51 | | - const bundlePath = path.resolve(process.cwd(), '.development/server.js') | 52 | | - delete require.cache[require.resolve(bundlePath)] | 53 | | - if (!clientStarted) { | 54 | | - clientStarted = true | 55 | | - const devServerOptions = { | 56 | | - hot: !!hot, | 57 | | - open: false, | 58 | | - devMiddleware: { | 59 | | - index: false | 60 | | - }, | 61 | | - client: { | 62 | | - overlay: true, | 63 | | - logging: 'none', | 64 | | - progress: false, | 65 | | - }, | 66 | | - setupMiddlewares: (middlewares, devServer) => { | 67 | | - if (!devServer) { | 68 | | - throw new Error('webpack-dev-server is not defined'); | 69 | | - } | 70 | | - middlewares.unshift((req, res, next) => { | 71 | | - if (req.originalUrl.indexOf('.hot-update.') === -1) { | 72 | | - if (req.originalUrl.startsWith('/nullstack/')) { | 73 | | - console.log(` ⚙️ [${req.method}] ${req.originalUrl}`) | 74 | | - } else { | 75 | | - console.log(` 🕸️ [${req.method}] ${req.originalUrl}`) | 76 | | - } | 77 | | - } | 78 | | - const serverBundle = require(bundlePath) | 79 | | - const server = serverBundle.default.server | 80 | | - server.less = true | 81 | | - server.port = port | 82 | | - server.start() | 83 | | - server(req, res, next) | 84 | | - }); | 85 | | - return middlewares; | 86 | | - }, | 87 | | - port | 88 | | - }; | 89 | | - const clientCompiler = getClientCompiler({ environment, input }); | 90 | | - const server = new WebpackDevServer(devServerOptions, clientCompiler); | | 88 | + if (!once) { | 91 | 89 | server.start(); | | 90 | + once = true | | 91 | + } | | 92 | + if (stats.hasErrors()) { | | 93 | + console.log(stats.toString({ colors: true })) | 92 | 94 | } | 93 | 95 | }); | 94 | 96 | } | |
Collapse file Copy file name to clipboardExpand all lines: server/server.js +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,20 +11,20 @@ import { generateFile } from './files'; | 11 | 11 | import generateManifest from './manifest'; | 12 | 12 | import { prerender } from './prerender'; | 13 | 13 | import printError from './printError'; | 14 | | -import project from './project'; | 15 | 14 | import registry from './registry'; | 16 | 15 | import generateRobots from './robots'; | 17 | 16 | import template from './template'; | 18 | 17 | import { generateServiceWorker } from './worker'; | 19 | 18 | import reqres from './reqres' | | 19 | +import WebSocket from 'ws'; | 20 | 20 |
| 21 | 21 | if (!global.fetch) { | 22 | 22 | global.fetch = fetch; | 23 | 23 | } | 24 | 24 |
| 25 | 25 | const server = express(); | 26 | 26 |
| 27 | | -server.port ??= process.env['NULLSTACK_SERVER_PORT'] || process.env['PORT'] || 3000 | | 27 | +server.port = process.env['NULSTACK_SERVER_PORT_YOU_SHOULD_NOT_CARE_ABOUT'] || process.env['NULLSTACK_SERVER_PORT'] || process.env['PORT'] || 3000 | 28 | 28 |
| 29 | 29 | let contextStarted = false | 30 | 30 | let serverStarted = false | @@ -215,9 +215,11 @@ server.start = function () { | 215 | 215 | } | 216 | 216 |
| 217 | 217 | server.listen(server.port, () => { | 218 | | - if (environment.production) { | 219 | | - const name = project.name ? project.name : 'Nullstack' | 220 | | - console.log('\x1b[36m%s\x1b[0m', ` ✅️ ${name} is ready at http://127.0.0.1:${server.port}\n`); | | 218 | + if (environment.development) { | | 219 | + const socket = new WebSocket(`ws://localhost:${process.env['NULLSTACK_SERVER_PORT']}/ws`); | | 220 | + socket.onopen = async function (e) { | | 221 | + socket.send('{"type":"NULLSTACK_SERVER_STARTED"}') | | 222 | + } | 221 | 223 | } | 222 | 224 | }); | 223 | 225 | } | |
Collapse file Copy file name to clipboardExpand all lines: webpack.config.js +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ const MiniCssExtractPlugin = require("mini-css-extract-plugin"); | 3 | 3 | const TerserPlugin = require('terser-webpack-plugin'); | 4 | 4 | const crypto = require("crypto"); | 5 | 5 | const { readdirSync } = require('fs'); | 6 | | -const { HotModuleReplacementPlugin } = require('webpack') | | 6 | +const NodemonPlugin = require('nodemon-webpack-plugin'); | 7 | 7 |
| 8 | 8 | const buildKey = crypto.randomBytes(20).toString('hex'); | 9 | 9 |
| @@ -117,7 +117,6 @@ function server(env, argv) { | 117 | 117 | const minimize = !isDev; | 118 | 118 | const plugins = [] | 119 | 119 | return { | 120 | | - name: 'server', | 121 | 120 | mode: argv.environment, | 122 | 121 | entry: './server.js', | 123 | 122 | output: { | @@ -238,11 +237,15 @@ function client(env, argv) { | 238 | 237 | }) | 239 | 238 | ] | 240 | 239 | if (isDev) { | 241 | | - plugins.push(new HotModuleReplacementPlugin()) | | 240 | + plugins.push(new NodemonPlugin({ | | 241 | + ext: '*', | | 242 | + watch: [".env", ".env.*", './.development/*.*'], | | 243 | + script: './.development/server.js', | | 244 | + nodeArgs: ['--enable-source-maps'], | | 245 | + quiet: true | | 246 | + })) | 242 | 247 | } | 243 | 248 | return { | 244 | | - name: 'client', | 245 | | - dependencies: ['server'], | 246 | 249 | infrastructureLogging: { | 247 | 250 | level: 'none', | 248 | 251 | }, | |
|