Skip to content

Commit 0eafbb9

Browse files
claude: Fix extension-build import-map to work in both dev and dist modes
Problem: import-map.json pointed to ./quarto-types.d.ts which doesn't exist in dev mode (src/resources/extension-build/). Solution: Follow Lua filter transformation pattern: - Source file in src/resources/ uses dev-mode path - Transform during packaging for distribution Changes: - import-map.json now points to ../../../packages/quarto-types/dist/index.d.ts (works in dev mode from src/resources/extension-build/) - Added updateImportMap() function in prepare-dist.ts that: - Runs after supportingFiles() copies all resources - Transforms @quarto/types path to ./quarto-types.d.ts for distribution - Updates all Deno std versions from src/import_map.json - Loops over all imports dynamically (not hardcoded list) - Updated README.md to document dev vs distribution behavior Result: Default config now works in dev mode without committing generated artifacts to src/resources/. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 09f2f95 commit 0eafbb9

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

package/src/common/prepare-dist.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ export async function prepareDist(
118118
supportingFiles(config);
119119
info("");
120120

121+
// Update extension-build import map for distribution
122+
info("Updating extension-build import map");
123+
updateImportMap(config);
124+
info("");
125+
121126
// Create the deno bundle
122127
// const input = join(config.directoryInfo.src, "quarto.ts");
123128
const output = join(config.directoryInfo.pkgWorking.bin, "quarto.js");
@@ -172,8 +177,8 @@ export async function prepareDist(
172177
);
173178

174179
// Copy quarto-types to extension-build directory
175-
// Note: src/resources/extension-build/ already has deno.json and import-map.json
176-
// which are copied automatically by supportingFiles()
180+
// Note: deno.json and import-map.json are copied by supportingFiles() and
181+
// import-map.json is then updated by updateImportMap() for distribution
177182
info("Copying quarto-types.d.ts to extension-build directory");
178183
const extensionBuildDir = join(
179184
config.directoryInfo.pkgWorking.share,
@@ -231,6 +236,49 @@ function supportingFiles(config: Configuration) {
231236
pathsToClean.forEach((path) => Deno.removeSync(path, { recursive: true }));
232237
}
233238

239+
function updateImportMap(config: Configuration) {
240+
// Read the import map that was copied from src/resources/extension-build/
241+
const importMapPath = join(
242+
config.directoryInfo.pkgWorking.share,
243+
"extension-build",
244+
"import-map.json",
245+
);
246+
const importMapContent = JSON.parse(Deno.readTextFileSync(importMapPath));
247+
248+
// Read the source import map to get current Deno std versions
249+
const sourceImportMapPath = join(config.directoryInfo.src, "import_map.json");
250+
const sourceImportMap = JSON.parse(Deno.readTextFileSync(sourceImportMapPath));
251+
const sourceImports = sourceImportMap.imports as Record<string, string>;
252+
253+
// Update the import map for distribution:
254+
// 1. Change @quarto/types path from dev (../../../packages/...) to dist (./quarto-types.d.ts)
255+
// 2. Update all other imports (Deno std versions) from source import map
256+
const updatedImports: Record<string, string> = {
257+
"@quarto/types": "./quarto-types.d.ts",
258+
};
259+
260+
// Copy all other imports from source, updating versions
261+
for (const key in importMapContent.imports) {
262+
if (key !== "@quarto/types") {
263+
const sourceValue = sourceImports[key];
264+
if (!sourceValue) {
265+
throw new Error(
266+
`Import map key "${key}" not found in source import_map.json`,
267+
);
268+
}
269+
updatedImports[key] = sourceValue;
270+
}
271+
}
272+
273+
importMapContent.imports = updatedImports;
274+
275+
// Write back the updated import map
276+
Deno.writeTextFileSync(
277+
importMapPath,
278+
JSON.stringify(importMapContent, null, 2) + "\n",
279+
);
280+
}
281+
234282
interface Filter {
235283
// The name of the filter (the LUA file and perhaps the directory)
236284
name: string;

src/resources/extension-build/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ This directory contains default configuration files for TypeScript execution eng
66

77
- **deno.json** - Default TypeScript compiler configuration
88
- **import-map.json** - Import mappings for @quarto/types and Deno standard library
9-
- **quarto-types.d.ts** - Type definitions for Quarto API (copied from packages/quarto-types/dist/ during build)
9+
- **quarto-types.d.ts** - Type definitions (copied from packages/quarto-types/dist/ during packaging)
1010

1111
## Usage
1212

1313
These files are used by `quarto dev-call build-ts-extension` when a user project doesn't have its own `deno.json`.
1414

15-
In dev mode: accessed via `resourcePath("extension-build/")`
16-
In distribution: copied to `share/extension-build/` during packaging
15+
**Dev mode:** Accessed via `resourcePath("extension-build/")`
16+
- `import-map.json` points to `../../../packages/quarto-types/dist/index.d.ts`
17+
- This relative path works from `src/resources/extension-build/`
1718

18-
## Updating Versions
19+
**Distribution mode:** Copied to `share/extension-build/` during packaging
20+
- `import-map.json` is transformed by `updateImportMap()` in prepare-dist.ts
21+
- `@quarto/types` path changed to `./quarto-types.d.ts`
22+
- Deno std versions updated from `src/import_map.json`
1923

20-
When updating Deno standard library versions:
21-
1. Update `src/import_map.json` (main Quarto CLI import map)
22-
2. Update `import-map.json` in this directory to match
24+
## Updating Versions
2325

24-
The versions should stay in sync with Quarto CLI's dependencies.
26+
Deno std library versions are automatically synced from `src/import_map.json` during packaging.
27+
No manual updates needed to this directory's import-map.json versions.

src/resources/extension-build/import-map.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"imports": {
3-
"@quarto/types": "./quarto-types.d.ts",
3+
"@quarto/types": "../../../packages/quarto-types/dist/index.d.ts",
44
"path": "jsr:@std/path@1.0.8",
55
"path/posix": "jsr:@std/path@1.0.8/posix",
66
"log": "jsr:/@std/log@0.224.0",

0 commit comments

Comments
 (0)