Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit b162c63

Browse files
authored
Minify and hash inject_styles.js (#1524)
1 parent cfa5a42 commit b162c63

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/core/create_compilers/RollupCompiler.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { CompileResult } from './interfaces';
1515
import RollupResult from './RollupResult';
1616

1717
const stderr = console.error.bind(console);
18+
const INJECT_STYLES_ID = 'inject_styles';
1819

1920
let rollup: any;
2021

@@ -123,6 +124,20 @@ export default class RollupCompiler {
123124
}
124125
mod.plugins.push({
125126
name: 'sapper-internal',
127+
buildStart(this: PluginContext): void {
128+
this.emitFile({
129+
type: 'chunk',
130+
id: INJECT_STYLES_ID,
131+
name: INJECT_STYLES_ID,
132+
preserveSignature: 'strict'
133+
});
134+
},
135+
load(id: string) {
136+
return id === INJECT_STYLES_ID ? { code: inject_styles, moduleSideEffects: 'no-treeshake' } : null;
137+
},
138+
resolveId(importee: string, importer: string) {
139+
return importee === INJECT_STYLES_ID ? INJECT_STYLES_ID : null;
140+
},
126141
renderChunk(code: string, chunk: RenderedChunk) {
127142
that.chunks.push(chunk);
128143
},
@@ -166,6 +181,8 @@ export default class RollupCompiler {
166181
return Array.from(css_files);
167182
};
168183

184+
const inject_styles_file = Object.keys(bundle).find(f => f.startsWith('inject_styles'));
185+
169186
let has_css = false;
170187
for (const name in bundle) {
171188
const chunk = <OutputChunk>bundle[name];
@@ -190,17 +207,13 @@ export default class RollupCompiler {
190207

191208
if (chunk_has_css) {
192209
has_css = true;
193-
chunk.code += `\nimport __inject_styles from './inject_styles.js';`;
210+
chunk.code += `\nimport __inject_styles from './${inject_styles_file}';`;
194211
}
195212
}
196213
}
197214

198-
if (has_css) {
199-
this.emitFile({
200-
type: 'asset',
201-
fileName: 'inject_styles.js',
202-
source: inject_styles
203-
});
215+
if (!has_css) {
216+
delete bundle[inject_styles_file];
204217
}
205218

206219
// Store the build dependencies so that we can create build.json

0 commit comments

Comments
 (0)