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

Commit 3fb05ed

Browse files
committed
Minify and hash inject_styles.js
1 parent cfa5a42 commit 3fb05ed

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/core/create_compilers/RollupCompiler.ts

Lines changed: 25 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,25 @@ 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+
},
141+
transform(code: string, id: string) {
142+
if (id === INJECT_STYLES_ID) {
143+
return { code, moduleSideEffects: 'no-treeshake' };
144+
}
145+
},
126146
renderChunk(code: string, chunk: RenderedChunk) {
127147
that.chunks.push(chunk);
128148
},
@@ -166,6 +186,8 @@ export default class RollupCompiler {
166186
return Array.from(css_files);
167187
};
168188

189+
const inject_styles_file = Object.keys(bundle).find(f => f.startsWith('inject_styles'));
190+
169191
let has_css = false;
170192
for (const name in bundle) {
171193
const chunk = <OutputChunk>bundle[name];
@@ -190,17 +212,13 @@ export default class RollupCompiler {
190212

191213
if (chunk_has_css) {
192214
has_css = true;
193-
chunk.code += `\nimport __inject_styles from './inject_styles.js';`;
215+
chunk.code += `\nimport __inject_styles from './${inject_styles_file}';`;
194216
}
195217
}
196218
}
197219

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

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

0 commit comments

Comments
 (0)