From e0b4319c7d7097951cddd465c948c4b033093d35 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sat, 26 May 2018 12:31:43 -0400 Subject: [PATCH] preserve webpack stats, write client assets elsewhere --- src/api/build.ts | 6 ++--- src/api/dev.ts | 46 +++++++++++++++++++++++++++--------- src/core/create_manifests.ts | 1 - src/middleware.ts | 5 ++-- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/api/build.ts b/src/api/build.ts index 2560fee..3bc87c1 100644 --- a/src/api/build.ts +++ b/src/api/build.ts @@ -61,9 +61,9 @@ async function execute(emitter: EventEmitter, { webpack_stats: client_stats }); - fs.writeFileSync(path.join(dest, 'client_info.json'), JSON.stringify({ - assets: client_stats.toJson().assetsByChunkName - })); + const client_info = client_stats.toJson(); + fs.writeFileSync(path.join(dest, 'client_info.json'), JSON.stringify(client_info)); + fs.writeFileSync(path.join(dest, 'client_assets.json'), JSON.stringify(client_info.assetsByChunkName)); const server_stats = await compile(server); emitter.emit('build', { diff --git a/src/api/dev.ts b/src/api/dev.ts index 4a9cb2a..5c6742e 100644 --- a/src/api/dev.ts +++ b/src/api/dev.ts @@ -189,9 +189,8 @@ class Watcher extends EventEmitter { }, result: info => { - fs.writeFileSync(path.join(dest, 'client_info.json'), JSON.stringify({ - assets: info.assetsByChunkName - }, null, ' ')); + fs.writeFileSync(path.join(dest, 'client_info.json'), JSON.stringify(info)); + fs.writeFileSync(path.join(dest, 'client_info.json'), JSON.stringify(info.assetsByChunkName, null, ' ')); this.deferreds.client.fulfil(); const client_files = info.assets.map((chunk: { name: string }) => `client/${chunk.name}`); @@ -291,20 +290,14 @@ class Watcher extends EventEmitter { const duplicate = this.current_build.unique_errors.has(message); this.current_build.unique_errors.add(message); - return { - message, - duplicate - } + return mungeWebpackError(message, duplicate); }), warnings: messages.warnings.map((message: string) => { const duplicate = this.current_build.unique_warnings.has(message); this.current_build.unique_warnings.add(message); - return { - message, - duplicate - } + return mungeWebpackError(message, duplicate); }), }); @@ -314,6 +307,37 @@ class Watcher extends EventEmitter { } } +const locPattern = /\((\d+):(\d+)\)$/; + +function mungeWebpackError(message: string, duplicate: boolean) { + // TODO this is all a bit rube goldberg... + const lines = message.split('\n'); + + const file = lines.shift() + .replace('', '') // careful — there is a special character at the beginning of this string + .replace('', '') + .replace('./', ''); + + let line = null; + let column = null; + + const match = locPattern.exec(lines[0]); + if (match) { + lines[0] = lines[0].replace(locPattern, ''); + line = +match[1]; + column = +match[2]; + } + + return { + file, + line, + column, + message: lines.join('\n'), + originalMessage: message, + duplicate + }; +} + class Deferred { promise: Promise; fulfil: (value?: any) => void; diff --git a/src/core/create_manifests.ts b/src/core/create_manifests.ts index 2a31220..1135a8c 100644 --- a/src/core/create_manifests.ts +++ b/src/core/create_manifests.ts @@ -1,7 +1,6 @@ import * as fs from 'fs'; import * as path from 'path'; import * as glob from 'glob'; -import create_routes from './create_routes'; import { posixify, write_if_changed } from './utils'; import { dev, locations } from '../config'; import { Route } from '../interfaces'; diff --git a/src/middleware.ts b/src/middleware.ts index 735ad96..bf9d3bd 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -8,7 +8,6 @@ import rimraf from 'rimraf'; import devalue from 'devalue'; import fetch from 'node-fetch'; import { lookup } from './middleware/mime'; -import { create_routes, create_compilers } from './core'; import { locations, dev } from './config'; import { Route, Template } from './interfaces'; import sourceMapSupport from 'source-map-support'; @@ -60,7 +59,7 @@ export default function middleware({ App, routes, store }: { const output = locations.dest(); - const client_info = JSON.parse(fs.readFileSync(path.join(output, 'client_info.json'), 'utf-8')); + const client_assets = JSON.parse(fs.readFileSync(path.join(output, 'client_assets.json'), 'utf-8')); const middleware = compose_handlers([ (req: Req, res: ServerResponse, next: () => void) => { @@ -97,7 +96,7 @@ export default function middleware({ App, routes, store }: { cache_control: 'max-age=31536000' }), - get_route_handler(client_info.assets, App, routes, store) + get_route_handler(client_assets, App, routes, store) ].filter(Boolean)); return middleware;