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

Minify and hash inject_styles.js #1524

Merged
merged 1 commit into from Sep 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/core/create_compilers/RollupCompiler.ts
Expand Up @@ -15,6 +15,7 @@ import { CompileResult } from './interfaces';
import RollupResult from './RollupResult';

const stderr = console.error.bind(console);
const INJECT_STYLES_ID = 'inject_styles';

let rollup: any;

Expand Down Expand Up @@ -123,6 +124,20 @@ export default class RollupCompiler {
}
mod.plugins.push({
name: 'sapper-internal',
buildStart(this: PluginContext): void {
this.emitFile({
type: 'chunk',
id: INJECT_STYLES_ID,
name: INJECT_STYLES_ID,
preserveSignature: 'strict'
});
},
load(id: string) {
return id === INJECT_STYLES_ID ? { code: inject_styles, moduleSideEffects: 'no-treeshake' } : null;
},
resolveId(importee: string, importer: string) {
return importee === INJECT_STYLES_ID ? INJECT_STYLES_ID : null;
},
renderChunk(code: string, chunk: RenderedChunk) {
that.chunks.push(chunk);
},
Expand Down Expand Up @@ -166,6 +181,8 @@ export default class RollupCompiler {
return Array.from(css_files);
};

const inject_styles_file = Object.keys(bundle).find(f => f.startsWith('inject_styles'));

let has_css = false;
for (const name in bundle) {
const chunk = <OutputChunk>bundle[name];
Expand All @@ -190,17 +207,13 @@ export default class RollupCompiler {

if (chunk_has_css) {
has_css = true;
chunk.code += `\nimport __inject_styles from './inject_styles.js';`;
chunk.code += `\nimport __inject_styles from './${inject_styles_file}';`;
}
}
}

if (has_css) {
this.emitFile({
type: 'asset',
fileName: 'inject_styles.js',
source: inject_styles
});
if (!has_css) {
delete bundle[inject_styles_file];
}

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