@@ -49,70 +49,150 @@ import {
4949 TypstCompileOptions ,
5050 validateRequiredTypstVersion ,
5151} from "../../core/typst.ts" ;
52+ import { runAnalyze , toTomlPath } from "../../core/typst-gather.ts" ;
5253import { asArray } from "../../core/array.ts" ;
5354import { ProjectContext } from "../../project/types.ts" ;
5455import { validatePdfStandards } from "../../core/verapdf.ts" ;
5556
56- // Stage typst packages to .quarto/typst-packages/
57- // First stages built-in packages, then extension packages (which can override)
58- async function stageTypstPackages (
59- input : string ,
60- projectDir ?: string ,
61- ) : Promise < string | undefined > {
62- if ( ! projectDir ) {
63- return undefined ;
64- }
57+ export interface NeededPackage {
58+ namespace : string ;
59+ name : string ;
60+ version : string ;
61+ }
6562
66- const packageSources : string [ ] = [ ] ;
63+ // Collect all package source directories (built-in + extensions)
64+ async function collectPackageSources (
65+ input : string ,
66+ projectDir : string ,
67+ ) : Promise < string [ ] > {
68+ const sources : string [ ] = [ ] ;
6769
68- // 1. Add built -in packages from quarto resources
70+ // 1. Built -in packages
6971 const builtinPackages = resourcePath ( "formats/typst/packages" ) ;
7072 if ( existsSync ( builtinPackages ) ) {
71- packageSources . push ( builtinPackages ) ;
73+ sources . push ( builtinPackages ) ;
7274 }
7375
74- // 2. Add packages from extensions (can override built-in)
76+ // 2. Extension packages
7577 const extensionDirs = inputExtensionDirs ( input , projectDir ) ;
7678 const subtreePath = builtinSubtreeExtensions ( ) ;
7779 for ( const extDir of extensionDirs ) {
78- // Use readSubtreeExtensions for subtree directory, readExtensions for others
7980 const extensions = extDir === subtreePath
8081 ? await readSubtreeExtensions ( extDir )
8182 : await readExtensions ( extDir ) ;
8283 for ( const ext of extensions ) {
8384 const packagesDir = join ( ext . path , "typst/packages" ) ;
8485 if ( existsSync ( packagesDir ) ) {
85- packageSources . push ( packagesDir ) ;
86+ sources . push ( packagesDir ) ;
8687 }
8788 }
8889 }
8990
90- if ( packageSources . length === 0 ) {
91- return undefined ;
91+ return sources ;
92+ }
93+
94+ // Build the TOML config string for typst-gather analyze
95+ export function buildAnalyzeToml (
96+ typstInput : string ,
97+ packageSources : string [ ] ,
98+ ) : string {
99+ const discoverPath = toTomlPath ( typstInput ) ;
100+ const cachePaths = packageSources . map ( ( p ) => `"${ toTomlPath ( p ) } "` ) . join ( ", " ) ;
101+
102+ return [
103+ `discover = ["${ discoverPath } "]` ,
104+ `package-cache = [${ cachePaths } ]` ,
105+ ] . join ( "\n" ) ;
106+ }
107+
108+ // Run typst-gather analyze on the .typ file to determine needed packages
109+ async function analyzeNeededPackages (
110+ typstInput : string ,
111+ packageSources : string [ ] ,
112+ ) : Promise < NeededPackage [ ] | null > {
113+ const tomlConfig = buildAnalyzeToml ( typstInput , packageSources ) ;
114+
115+ try {
116+ const result = await runAnalyze ( tomlConfig ) ;
117+ return result . imports . map ( ( { namespace, name, version } ) => ( {
118+ namespace,
119+ name,
120+ version,
121+ } ) ) ;
122+ } catch {
123+ // Fallback: if analyze fails, stage everything (current behavior)
124+ warning ( "typst-gather analyze failed; staging all packages as fallback" ) ;
125+ return null ;
92126 }
127+ }
93128
94- // Stage to .quarto/typst/packages/
95- const cacheDir = projectScratchPath ( projectDir , "typst/packages" ) ;
129+ // Stage only the needed packages from source dirs into the cache dir.
130+ // Last write wins — extensions (listed after built-in) override built-in packages.
131+ export function stageSelectedPackages (
132+ sources : string [ ] ,
133+ cacheDir : string ,
134+ needed : NeededPackage [ ] | null ,
135+ ) : void {
136+ if ( needed === null ) {
137+ stageAllPackages ( sources , cacheDir ) ;
138+ return ;
139+ }
96140
97- // Copy contents of each source directory (merging namespaces like "preview", "local")
98- for ( const source of packageSources ) {
99- for ( const entry of Deno . readDirSync ( source ) ) {
100- const srcPath = join ( source , entry . name ) ;
101- const destPath = join ( cacheDir , entry . name ) ;
102- if ( ! existsSync ( destPath ) ) {
103- copySync ( srcPath , destPath ) ;
104- } else if ( entry . isDirectory ) {
105- // Merge directory contents (e.g., merge packages within "preview" namespace)
106- for ( const subEntry of Deno . readDirSync ( srcPath ) ) {
107- const subSrcPath = join ( srcPath , subEntry . name ) ;
108- const subDestPath = join ( destPath , subEntry . name ) ;
109- if ( ! existsSync ( subDestPath ) ) {
110- copySync ( subSrcPath , subDestPath ) ;
111- }
112- }
141+ for ( const pkg of needed ) {
142+ const relPath = join ( pkg . namespace , pkg . name , pkg . version ) ;
143+ const destPath = join ( cacheDir , relPath ) ;
144+
145+ for ( const source of sources ) {
146+ const srcPath = join ( source , relPath ) ;
147+ if ( existsSync ( srcPath ) ) {
148+ ensureDirSync ( dirname ( destPath ) ) ;
149+ copySync ( srcPath , destPath , { overwrite : true } ) ;
113150 }
114151 }
115152 }
153+ }
154+
155+ // Fallback: copy all packages from all sources. Last write wins at the
156+ // package directory level. Built-in listed first, extensions after.
157+ export function stageAllPackages ( sources : string [ ] , cacheDir : string ) : void {
158+ for ( const source of sources ) {
159+ for ( const nsEntry of Deno . readDirSync ( source ) ) {
160+ if ( ! nsEntry . isDirectory ) continue ;
161+ const nsSrc = join ( source , nsEntry . name ) ;
162+ const nsDest = join ( cacheDir , nsEntry . name ) ;
163+ ensureDirSync ( nsDest ) ;
164+ for ( const pkgEntry of Deno . readDirSync ( nsSrc ) ) {
165+ const pkgSrc = join ( nsSrc , pkgEntry . name ) ;
166+ const pkgDest = join ( nsDest , pkgEntry . name ) ;
167+ copySync ( pkgSrc , pkgDest , { overwrite : true } ) ;
168+ }
169+ }
170+ }
171+ }
172+
173+ // Stage typst packages to .quarto/typst-packages/
174+ // First stages built-in packages, then extension packages (which can override)
175+ async function stageTypstPackages (
176+ input : string ,
177+ typstInput : string ,
178+ projectDir ?: string ,
179+ ) : Promise < string | undefined > {
180+ if ( ! projectDir ) {
181+ return undefined ;
182+ }
183+
184+ const packageSources = await collectPackageSources ( input , projectDir ) ;
185+ if ( packageSources . length === 0 ) {
186+ return undefined ;
187+ }
188+
189+ const neededPackages = await analyzeNeededPackages (
190+ typstInput ,
191+ packageSources ,
192+ ) ;
193+
194+ const cacheDir = projectScratchPath ( projectDir , "typst/packages" ) ;
195+ stageSelectedPackages ( packageSources , cacheDir , neededPackages ) ;
116196
117197 return cacheDir ;
118198}
@@ -168,7 +248,11 @@ export function typstPdfOutputRecipe(
168248 typstOptions . rootDir = project . dir ;
169249
170250 // Stage extension typst packages
171- const packagePath = await stageTypstPackages ( input , project . dir ) ;
251+ const packagePath = await stageTypstPackages (
252+ input ,
253+ typstInput ,
254+ project . dir ,
255+ ) ;
172256 if ( packagePath ) {
173257 typstOptions . packagePath = packagePath ;
174258 }
0 commit comments