Skip to content

Commit 9061da3

Browse files
committed
sass - put vars analysis behind cache lookup
1 parent 24e136a commit 9061da3

File tree

2 files changed

+53
-50
lines changed

2 files changed

+53
-50
lines changed

src/core/sass.ts

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,36 +139,19 @@ export async function compileSass(
139139
...userRules,
140140
'// quarto-scss-analysis-annotation { "origin": null }',
141141
].join("\n\n");
142-
let failed = false;
143-
// deno-lint-ignore no-explicit-any
144-
let e: any = null;
145-
try {
146-
scssInput += "\n" + cssVarsBlock(scssInput);
147-
} catch (_e) {
148-
e = _e;
149-
failed = true;
150-
}
151142

152143
// Compile the scss
153144
const result = await compileWithCache(
154145
scssInput,
155146
loadPaths,
156147
temp,
157-
minified,
158-
await md5HashBytes(new TextEncoder().encode(scssInput)),
148+
{
149+
compressed: minified,
150+
cacheIdentifier: await md5HashBytes(new TextEncoder().encode(scssInput)),
151+
addVarsBlock: true,
152+
},
159153
);
160154

161-
if (failed) {
162-
console.warn("Error adding css vars block", e);
163-
console.warn(
164-
"The resulting CSS file will not have SCSS color variables exported as CSS.",
165-
);
166-
Deno.writeTextFileSync("_quarto_internal_scss_error.scss", scssInput);
167-
console.warn(
168-
"This is likely a Quarto bug.\nPlease consider reporting it at https://github.com/quarto-dev/quarto-cli,\nalong with the _quarto_internal_scss_error.scss file that can be found in the current working directory.",
169-
);
170-
}
171-
172155
if (!Deno.env.get("QUARTO_SAVE_SCSS")) {
173156
return result;
174157
}
@@ -353,13 +336,44 @@ export function sassLayerDir(
353336
};
354337
}
355338

339+
type CompileWithCacheOptions = {
340+
compressed?: boolean;
341+
cacheIdentifier?: string;
342+
addVarsBlock?: boolean;
343+
};
344+
356345
export async function compileWithCache(
357346
input: string,
358347
loadPaths: string[],
359348
temp: TempContext,
360-
compressed?: boolean,
361-
cacheIdentifier?: string,
349+
options?: CompileWithCacheOptions,
362350
) {
351+
const {
352+
compressed,
353+
cacheIdentifier,
354+
addVarsBlock,
355+
} = options || {};
356+
357+
const handleVarsBlock = (input: string) => {
358+
if (!addVarsBlock) {
359+
return input;
360+
}
361+
try {
362+
input += "\n" + cssVarsBlock(input);
363+
} catch (e) {
364+
console.warn("Error adding css vars block", e);
365+
console.warn(
366+
"The resulting CSS file will not have SCSS color variables exported as CSS.",
367+
);
368+
Deno.writeTextFileSync("_quarto_internal_scss_error.scss", input);
369+
console.warn(
370+
"This is likely a Quarto bug.\nPlease consider reporting it at https://github.com/quarto-dev/quarto-cli,\nalong with the _quarto_internal_scss_error.scss file that can be found in the current working directory.",
371+
);
372+
throw e;
373+
}
374+
return input;
375+
};
376+
363377
if (cacheIdentifier) {
364378
// If there are imports, the computed input Hash is incorrect
365379
// so we should be using a session cache which will cache
@@ -372,8 +386,16 @@ export async function compileWithCache(
372386
: quartoCacheDir("sass");
373387
// when using quarto session cache, we ensure to cleanup the cache files at TempContext cleanup
374388
const cache = await sassCache(cacheDir, useSessionCache ? temp : undefined);
375-
return cache.getOrSet(input, loadPaths, temp, cacheIdentifier, compressed);
389+
return cache.getOrSet(
390+
input,
391+
cacheIdentifier,
392+
async (outputFilePath: string) => {
393+
input = handleVarsBlock(input);
394+
await dartCompile(input, outputFilePath, temp, loadPaths, compressed);
395+
},
396+
);
376397
} else {
398+
input = handleVarsBlock(input);
377399
const outputFilePath = temp.createFile({ suffix: ".css" });
378400
// Skip the cache and just compile
379401
await dartCompile(

src/core/sass/cache.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,13 @@ class SassCache {
7777
async setFromHash(
7878
identifierHash: string,
7979
inputHash: string,
80-
input: string,
81-
loadPaths: string[],
82-
temp: TempContext,
8380
cacheIdentifier: string,
84-
compressed?: boolean,
81+
compilationThunk: (outputFilePath: string) => Promise<void>,
8582
): Promise<string> {
8683
log.debug(`SassCache.setFromHash(${identifierHash}, ${inputHash}), ...`);
8784
const outputFilePath = join(this.path, `${identifierHash}.css`);
8885
try {
89-
await dartCompile(
90-
input,
91-
outputFilePath,
92-
temp,
93-
loadPaths,
94-
compressed,
95-
);
86+
await compilationThunk(outputFilePath);
9687
} catch (error) {
9788
// Compilation failed, so clear out the output file (if exists)
9889
// which will be invalid CSS
@@ -112,30 +103,23 @@ class SassCache {
112103

113104
async set(
114105
input: string,
115-
loadPaths: string[],
116-
temp: TempContext,
117106
cacheIdentifier: string,
118-
compressed?: boolean,
107+
compilationThunk: (outputFilePath: string) => Promise<void>,
119108
): Promise<string> {
120109
const identifierHash = md5Hash(cacheIdentifier);
121110
const inputHash = md5Hash(input);
122111
return this.setFromHash(
123112
identifierHash,
124113
inputHash,
125-
input,
126-
loadPaths,
127-
temp,
128114
cacheIdentifier,
129-
compressed,
115+
compilationThunk,
130116
);
131117
}
132118

133119
async getOrSet(
134120
input: string,
135-
loadPaths: string[],
136-
temp: TempContext,
137121
cacheIdentifier: string,
138-
compressed?: boolean,
122+
compilationThunk: (outputFilePath: string) => Promise<void>,
139123
): Promise<string> {
140124
log.debug(`SassCache.getOrSet(...)`);
141125
const identifierHash = md5Hash(cacheIdentifier);
@@ -149,11 +133,8 @@ class SassCache {
149133
return this.setFromHash(
150134
identifierHash,
151135
inputHash,
152-
input,
153-
loadPaths,
154-
temp,
155136
cacheIdentifier,
156-
compressed,
137+
compilationThunk,
157138
);
158139
}
159140

0 commit comments

Comments
 (0)