Skip to content
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

Update zen to work with webpack-dev-server 4 #69

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ async function runIfCodeChanged() {
// HMR can't recover from compile errors. Once the build is good again, we need to reload
if (compile.status == 'error') return (compileHasFailed = true)

if (compileHasFailed)
if (compileHasFailed) {
// after recovering, we need to reload
return window.location.reload()
}

// If we loaded before the first compile, reload once the code is ready
// ZenWebpackClient can be undefined while errored or still bundling
Expand Down
6 changes: 5 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = class Server {
this.results = [] // results of all tests run
this.passedFocus = [] // all tests that passed after running

this.start()
}

async start () {
// start up the local webserver for `head` to connect to
let app = connect()
let server = http.createServer(app).listen(Zen.config.port)
Expand All @@ -29,7 +33,7 @@ module.exports = class Server {
app.use('/icons', Util.serveIcons)

if (Zen.webpack) {
Zen.webpack.startDevServer(app)
await Zen.webpack.startDevServer(app)
Zen.webpack.on('status', (stats) => {
if (Zen.webpack.status == 'done') {
this.workers.forEach(
Expand Down
68 changes: 37 additions & 31 deletions lib/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,34 @@ module.exports = class WebpackAdapter extends EventEmitter {
})
}

startDevServer(server: Server) {
async startDevServer(server: Server) {
const zenConfig = this.zenConfig
if (zenConfig?.setDevelopmentHeaders) {
WebpackDevServer.prototype.setContentHeaders = function (req, res, next) {
if (this.headers) {
for (var name in this.headers) {
res.setHeader(name, this.headers[name])
}
const devServer = new WebpackDevServer({
hot: false,
client: false,
headers: (req, res) => {
const headers = zenConfig.getDevelopmentHeaders(req.originalUrl)
return headers
},
devMiddleware: {
publicPath: '/',
stats: {
preset: 'minimal', // Only output when errors or new compilation
moduleTrace: false,
errorDetails: false
}

zenConfig.setDevelopmentHeaders(req, res)
next()
}
}

const devServer = new WebpackDevServer(this.compiler, {
stats: { errorDetails: true },
hot: true,
inline: false,
})

// @ts-expect-error app does exist in this version of dev server
},
// Silence server startup logs.
setupMiddlewares: (middlewares, devServer) => {
devServer.logger.info = () => {};
return middlewares;
},
static: {
directory: zenConfig.devServerOptions.directory,
},
port: 9000
}, this.compiler)
await devServer.start()
server.use('/webpack', devServer.app)
}

Expand All @@ -126,18 +132,18 @@ module.exports = class WebpackAdapter extends EventEmitter {
return e.module ? `${e.module.id}: ${e.message}` : e.message
})

const state = Object.assign(stats, {
files: Object.keys(stats.compilation.assets).map((name) => {
const source = stats.compilation.assets[name].source()
return { path: `webpack/${name}`, body: source }
}),

entrypoints:
stats.compilation.entrypoints
.get('bundle')
?.chunks.map((chunk: Chunk) => chunk.files.values().next().value) ||
[],
const files = Object.keys(stats.compilation.assets).map((name) => {
const source = stats.compilation.assets[name].source()
return { path: `webpack/${name}`, body: source }
})
const entrypoints = stats.compilation.entrypoints
.get('bundle')
?.chunks.map((chunk: Chunk) => chunk.files.values().next().value) ||
[]

const state = Object.assign(stats, {
files,
entrypoints,
errors,
status: errors.length ? ('error' as const) : ('done' as const),
})
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@superhuman/zen",
"version": "0.3.32",
"version": "0.3.33",
"description": "Karma replacement that runs your tests in seconds",
"main": "lib/cli.js",
"scripts": {
Expand Down Expand Up @@ -68,6 +68,6 @@
"ts-node": "^10.4.0",
"typescript": "^4.4.3",
"webpack": "4.47.0",
"webpack-dev-server": "^3.4"
"webpack-dev-server": "4.15.2"
}
}
Loading