From f8d742bdd006c525d2dd0d5d35280bca394cbb19 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 29 Aug 2018 10:55:52 -0400 Subject: [PATCH] hashing --- src/core/create_compilers.ts | 41 ++++++++++++++++++++++++++---------- src/rollup.ts | 2 ++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/core/create_compilers.ts b/src/core/create_compilers.ts index 0013a9a..5b7fe38 100644 --- a/src/core/create_compilers.ts +++ b/src/core/create_compilers.ts @@ -22,7 +22,7 @@ export class CompileResult { } class RollupResult extends CompileResult { - constructor(duration: number, stats: any) { + constructor(duration: number, { input, chunks }: { input: string, chunks: any[] }) { super(); this.duration = duration; @@ -30,14 +30,17 @@ class RollupResult extends CompileResult { this.errors = []; this.warnings = []; - // TODO - this.assets = []; + this.assets = chunks.map(chunk => chunk.fileName); - this.assetsByChunkName = { - // TODO need to hash these filenames and - // expose the info in the Rollup output - main: `client.js` - }; + // TODO populate this properly. We don't have named chunks, as in + // webpack, but we can have a route -> [chunk] map or something + this.assetsByChunkName = {}; + + chunks.forEach(chunk => { + if (input in chunk.modules) { + this.assetsByChunkName.main = chunk.fileName; + } + }); } print() { @@ -77,9 +80,13 @@ export class RollupCompiler { _: Promise; _oninvalid: (filename: string) => void; _start: number; + input: string; + chunks: any[]; // TODO types constructor(config: any) { this._ = this.get_config(path.resolve(config)); + this.input = null; + this.chunks = []; } async get_config(input: string) { @@ -107,8 +114,13 @@ export class RollupCompiler { (mod.plugins || (mod.plugins = [])).push({ name: 'sapper-internal', - watchChange: (file: string) => { - this._oninvalid(file); + options: (opts: any) => { + this.input = opts.input; + }, + renderChunk: (code: string, chunk: any) => { + if (chunk.isEntry) { + this.chunks.push(chunk); + } } }); @@ -135,6 +147,10 @@ export class RollupCompiler { const watcher = r.watch(config); + watcher.on('change', (id: string) => { + this._oninvalid(id); + }); + watcher.on('event', (event: any) => { switch (event.code) { case 'FATAL': @@ -157,7 +173,10 @@ export class RollupCompiler { break; case 'BUNDLE_END': - cb(null, new RollupResult(Date.now() - this._start, event.result)); + cb(null, new RollupResult(Date.now() - this._start, { + input: this.input, + chunks: this.chunks + })); break; default: diff --git a/src/rollup.ts b/src/rollup.ts index 77d41fd..5ef280a 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -11,6 +11,8 @@ export default { output: () => { return { dir: `${locations.dest()}/client`, + entryFileNames: '[name].[hash].js', + chunkFileNames: '[name].[hash].js', format: 'esm' }; }