Skip to content

Commit ce0bbd2

Browse files
committed
partial commit
1 parent b8d3803 commit ce0bbd2

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

src/command/render/render-contexts.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ import {
8989
import { ExtensionContext } from "../../extension/types.ts";
9090
import { NotebookContext } from "../../render/notebook/notebook-types.ts";
9191

92+
// we can't naively ld.cloneDeep everything
93+
// because that destroys class instances
94+
// with private members
95+
//
96+
// Currently, that's ProjectContext.
97+
//
98+
// TODO: Ideally, we shouldn't be copying the RenderContext at all.
99+
export function copyRenderContext(
100+
context: RenderContext,
101+
): RenderContext {
102+
return {
103+
...ld.cloneDeep(context),
104+
project: context.project,
105+
};
106+
}
92107
export async function resolveFormatsFromMetadata(
93108
metadata: Metadata,
94109
input: string,

src/command/render/render-files.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { outputRecipe } from "./output.ts";
4848

4949
import { renderPandoc } from "./render.ts";
5050
import { PandocRenderCompletion, RenderServices } from "./types.ts";
51-
import { renderContexts } from "./render-contexts.ts";
51+
import { copyRenderContext, renderContexts } from "./render-contexts.ts";
5252
import { renderProgress } from "./render-info.ts";
5353
import {
5454
ExecutedFile,
@@ -503,7 +503,7 @@ async function renderFileInternal(
503503

504504
for (const format of Object.keys(contexts)) {
505505
pushTiming("render-context");
506-
const context = ld.cloneDeep(contexts[format]) as RenderContext; // since we're going to mutate it...
506+
const context = copyRenderContext(contexts[format]); // since we're going to mutate it...
507507

508508
// disquality some documents from server: shiny
509509
if (isServerShiny(context.format) && context.project) {

src/command/render/render-shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export async function render(
9797

9898
// NB: singleFileProjectContext is currently not fully-featured
9999
context = await singleFileProjectContext(path, nbContext, options.flags);
100+
debugger;
100101

101102
// otherwise it's just a file render
102103
const result = await renderFiles(

src/core/cache/cache.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { md5HashBytes } from "../hash.ts";
1313
import { satisfies } from "semver/mod.ts";
1414
import { quartoConfig } from "../quarto.ts";
1515
import { CacheIndexEntry, ProjectCache } from "./cache-types.ts";
16+
import { ProjectContext } from "../../project/types.ts";
1617

1718
export { type ProjectCache } from "./cache-types.ts";
1819

@@ -95,6 +96,7 @@ class ProjectCacheImpl {
9596
join(this.projectScratchDir, "project-cache", hash),
9697
value,
9798
);
99+
console.log("Adding to cache", key, hash);
98100
const result = await this.index.set(key, {
99101
hash,
100102
type: "string",
@@ -147,6 +149,23 @@ class ProjectCacheImpl {
147149
join(this.projectScratchDir, "project-cache", hash),
148150
));
149151
}
152+
153+
memoizeStringFunction(
154+
key: string[],
155+
fn: (param: string) => Promise<string>,
156+
): (param: string) => Promise<string> {
157+
return async (param: string) => {
158+
const paramHash = await md5HashBytes(new TextEncoder().encode(param));
159+
const memoizationKey = [...key, paramHash];
160+
const cached = await this.getString(memoizationKey);
161+
if (cached !== null) {
162+
return cached;
163+
}
164+
const result = await fn(param);
165+
await this.addString(memoizationKey, result);
166+
return result;
167+
};
168+
}
150169
}
151170

152171
export const createProjectCache = async (
@@ -156,3 +175,15 @@ export const createProjectCache = async (
156175
await result.init();
157176
return result as ProjectCache;
158177
};
178+
179+
export const memoizeStringFunction = (
180+
key: string[],
181+
fn: (param: string) => Promise<string>,
182+
): (project: ProjectContext, param: string) => Promise<string> => {
183+
return async (project: ProjectContext, param: string) => {
184+
return (project.diskCache as ProjectCacheImpl).memoizeStringFunction(
185+
key,
186+
fn,
187+
)(param);
188+
};
189+
};

src/core/sass.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { kSourceMappingRegexes } from "../config/constants.ts";
2222
import { quartoConfig } from "../core/quarto.ts";
2323
import { safeModeFromFile } from "../deno_ral/fs.ts";
2424
import { ProjectContext } from "../project/types.ts";
25+
import { memoizeStringFunction } from "./cache/cache.ts";
2526

2627
export interface SassVariable {
2728
name: string;
@@ -50,10 +51,9 @@ export function outputVariable(
5051
let counter: number = 1;
5152
export async function compileSass(
5253
bundles: SassBundleLayers[],
53-
projectContext: ProjectContext,
54+
project: ProjectContext,
5455
minified = true,
5556
) {
56-
const { temp } = projectContext;
5757
// Gather the inputs for the framework
5858
const frameWorkUses = bundles.map(
5959
(bundle) => bundle.framework?.uses || "",
@@ -161,7 +161,7 @@ export async function compileSass(
161161
const result = await compileWithCache(
162162
scssInput,
163163
loadPaths,
164-
temp,
164+
project,
165165
{
166166
compressed: minified,
167167
cacheIdentifier: await md5HashBytes(new TextEncoder().encode(scssInput)),
@@ -357,24 +357,37 @@ type CompileWithCacheOptions = {
357357
addVarsBlock?: boolean;
358358
};
359359

360+
const memoizedGetVarsBlock = memoizeStringFunction([
361+
"core",
362+
"sass",
363+
"cssVarsBlock",
364+
], (input: string) => Promise.resolve(cssVarsBlock(input)));
365+
360366
export async function compileWithCache(
361367
input: string,
362368
loadPaths: string[],
363-
temp: TempContext,
369+
project: ProjectContext,
364370
options?: CompileWithCacheOptions,
365371
) {
372+
const { temp } = project;
366373
const {
367374
compressed,
368375
cacheIdentifier,
369376
addVarsBlock,
370377
} = options || {};
371378

372-
const handleVarsBlock = (input: string) => {
379+
const handleVarsBlock = async (input: string) => {
373380
if (!addVarsBlock) {
374381
return input;
375382
}
376383
try {
377-
input += "\n" + cssVarsBlock(input);
384+
const result = await memoizedGetVarsBlock(project, input);
385+
console.log("<<<<");
386+
console.log(input);
387+
console.log("----");
388+
console.log(result);
389+
console.log(">>>>");
390+
return input + result;
378391
} catch (e) {
379392
console.warn("Error adding css vars block", e);
380393
console.warn(
@@ -404,12 +417,12 @@ export async function compileWithCache(
404417
input,
405418
cacheIdentifier,
406419
async (outputFilePath: string) => {
407-
input = handleVarsBlock(input);
420+
input = await handleVarsBlock(input);
408421
await dartCompile(input, outputFilePath, temp, loadPaths, compressed);
409422
},
410423
);
411424
} else {
412-
input = handleVarsBlock(input);
425+
input = await handleVarsBlock(input);
413426
const outputFilePath = temp.createFile({ suffix: ".css" });
414427
// Skip the cache and just compile
415428
await dartCompile(

0 commit comments

Comments
 (0)