Skip to content

Commit a884fa0

Browse files
mhuisiclaudeVtec234
authored
chore: enable infoview debugging (#732)
This PR ensures that we can set breakpoints in the InfoView again. Specifically: - We fix a deadlock in the `watch` task which meant changes in the infoview would not propagate to the extension build. - We ensure that all stages use development rather than production builds of the infoview and the infoview loader. - We use standalone sourcemap files instead of inline sourcemaps. - We add a bunch of hacks to make sure both VSCode and Chrome DevTools have access to these files. - The terser plugin we use was stripping inline source maps. This PR ensures that this doesn't happen. (Though we are not using them anymore.) Also: - Bump `es-module-shims` and `rehype-mathjax`. Pin `mathjax-full` to the latest working version that `rehype-mathjax` is compatible with. --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Wojciech Nawrocki <wjnawrocki@protonmail.com>
1 parent 705168a commit a884fa0

File tree

13 files changed

+424
-564
lines changed

13 files changed

+424
-564
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"args": ["--extensionDevelopmentPath=${workspaceRoot}/vscode-lean4"],
1111
"stopOnEntry": false,
1212
"sourceMaps": true,
13-
"outFiles": ["${workspaceRoot}/vscode-lean4/dist/**/*.js"],
13+
"outFiles": ["${workspaceRoot}/*/dist/**/*.js"],
1414
"preLaunchTask": "watch",
1515
"debugWebviews": true,
1616
"rendererDebugOptions": {

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
{
7777
"label": "build",
7878
"type": "npm",
79-
"script": "build",
79+
"script": "build:dev",
8080
"problemMatcher": {
8181
"owner": "typescript",
8282
"source": "ts",

lean4-infoview-api/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"scripts": {
1010
"watch": "tsc --watch",
1111
"watchTest": "tsc --watch",
12-
"build": "tsc"
12+
"build": "tsc",
13+
"build:dev": "tsc"
1314
},
1415
"main": "dist/index",
1516
"types": "dist/index",

lean4-infoview/package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
"scripts": {
1010
"watch": "rollup --config --environment NODE_ENV:development --watch",
1111
"build": "rollup --config --environment NODE_ENV:production",
12+
"build:dev": "rollup --config --environment NODE_ENV:development",
1213
"watchTest": "npm run watch",
1314
"test": "tsc -p test/tsconfig.json"
1415
},
1516
"exports": {
1617
".": {
17-
"default": "./dist/index.production.min.js",
18-
"types": "./dist/index.d.ts"
18+
"types": "./dist/index.d.ts",
19+
"development": "./dist/index.development.js",
20+
"default": "./dist/index.production.min.js"
1921
},
2022
"./loader": {
21-
"default": "./dist/loader.production.min.js",
22-
"types": "./dist/loader.d.ts"
23+
"types": "./dist/loader.d.ts",
24+
"development": "./dist/loader.development.js",
25+
"default": "./dist/loader.production.min.js"
2326
},
2427
"./package.json": "./package.json"
2528
},
@@ -30,11 +33,11 @@
3033
"license": "Apache-2.0",
3134
"devDependencies": {
3235
"@floating-ui/react": "^0.26.25",
33-
"@rollup/plugin-commonjs": "^28.0.0",
36+
"@rollup/plugin-commonjs": "^29.0.2",
3437
"@rollup/plugin-json": "^6.1.0",
35-
"@rollup/plugin-node-resolve": "^15.1.0",
38+
"@rollup/plugin-node-resolve": "^16.0.3",
3639
"@rollup/plugin-replace": "^6.0.1",
37-
"@rollup/plugin-terser": "^0.4.4",
40+
"@rollup/plugin-terser": "^1.0.0",
3841
"@rollup/plugin-typescript": "^12.1.0",
3942
"@rollup/plugin-url": "^8.0.1",
4043
"@types/react": "^18.2.15",
@@ -47,7 +50,8 @@
4750
"react-markdown": "^9.0.1",
4851
"react-syntax-highlighter": "^15.5.0",
4952
"remark-math": "^6.0.0",
50-
"rehype-mathjax": "^6.0.0",
53+
"rehype-mathjax": "^7.1.0",
54+
"mathjax-full": "~3.2.2",
5155
"rollup": "^4.24.0",
5256
"rollup-plugin-css-only": "^4.3.0",
5357
"typescript": "^5.4.5"
@@ -57,7 +61,7 @@
5761
"@vscode/codicons": "^0.0.40",
5862
"@vscode-elements/react-elements": "^0.5.0",
5963
"es-module-lexer": "^1.5.4",
60-
"es-module-shims": "^1.7.3",
64+
"es-module-shims": "^2.8.0",
6165
"react-fast-compare": "^3.2.2",
6266
"tachyons": "^4.12.0",
6367
"vscode-languageserver-protocol": "^3.17.3"

lean4-infoview/rollup.config.js

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,34 @@ import replace from '@rollup/plugin-replace'
55
import terser from '@rollup/plugin-terser'
66
import typescript from '@rollup/plugin-typescript'
77
import url from '@rollup/plugin-url'
8+
import fs from 'fs'
9+
import path from 'path'
810
import css from 'rollup-plugin-css-only'
911

12+
/**
13+
* A minimal Rollup plugin that loads existing source maps for `.js` files.
14+
* When Rollup imports an already-compiled `.js` file (e.g. from `lean4-infoview-api/dist`),
15+
* it does not read the accompanying `.js.map` by default.
16+
* This plugin checks for a sibling `.map` file and, if found,
17+
* returns its contents from the `load` hook so that Rollup chains the maps together.
18+
* This allows the final bundle's source map to point to the original `.ts` sources
19+
* rather than the intermediate `.js` files.
20+
*
21+
* @returns {import('rollup').Plugin} */
22+
const inputSourcemaps = () => ({
23+
name: 'input-sourcemaps',
24+
async load(id) {
25+
if (!id.endsWith('.js')) return null
26+
try {
27+
const code = await fs.promises.readFile(id, 'utf8')
28+
const mapJson = await fs.promises.readFile(id + '.map', 'utf8')
29+
return { code, map: JSON.parse(mapJson) }
30+
} catch {
31+
return null
32+
}
33+
},
34+
})
35+
1036
/** @type {import('rollup').OutputOptions} */
1137
const output =
1238
process.env.NODE_ENV && process.env.NODE_ENV === 'production'
@@ -21,42 +47,61 @@ const output =
2147
}
2248
: {
2349
dir: 'dist',
24-
sourcemap: 'inline',
50+
sourcemap: true,
51+
/* By default, `sourceMappingURL` in the infoview bundle (`dist/index.development.js`)
52+
* points to the source map as a relative path.
53+
* When we debug the webview,
54+
* the infoview bundle is fetched from VSCode's builtin `vscode-resource` server,
55+
* and Chrome DevTools tries to fetch the source map that way as well.
56+
* This fails,
57+
* despite `WebviewOptions.localResourceRoots` supposedly including the entire workspace by default.
58+
* Using a `file://` URI instead allows DevTools to fetch the source map,
59+
* as well as all the source files it refers to. */
60+
sourcemapBaseUrl: 'file://' + path.resolve('dist'),
61+
/* Source maps refer to relative paths by default,
62+
* but these paths get broken when lean4-infoview/dist is copied into vscode-lean4/dist.
63+
* Make them absolute instead. */
64+
sourcemapPathTransform: (relativeSourcePath, sourcemapPath) =>
65+
path.resolve(path.dirname(sourcemapPath), relativeSourcePath),
2566
format: 'esm',
2667
entryFileNames: '[name].development.js',
2768
chunkFileNames: '[name]-[hash].development.js',
2869
}
2970

3071
/** @type {import('rollup').InputPluginOption} */
3172
const plugins = [
73+
inputSourcemaps(),
3274
url({
3375
include: ['**/*.ttf'],
3476
fileName: '[name][extname]',
3577
}),
3678
typescript({
3779
tsconfig: './tsconfig.json',
3880
outputToFilesystem: false,
39-
// https://stackoverflow.com/a/63235210
40-
sourceMap: false,
4181
}),
4282
nodeResolve({
4383
browser: true,
4484
}),
4585
replace({
4686
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
47-
preventAssignment: true, // TODO delete when `true` becomes the default
87+
preventAssignment: true,
4888
}),
4989
commonjs(),
5090
json(),
5191
]
5292

5393
/**
54-
* Note that besides building the infoview single-page application, we build a loader and a bunch
55-
* of esm-shims. This is a way of compiling our dependencies into single-file ES modules which can
56-
* be shared as imports between the infoview app and dynamically loaded widget modules. Although
57-
* projects such * as jspm.io do exist, they tend to chunk modules into a bunch of files which are
58-
* not easy to * bundle, and requiring them dynamically would make the infoview depend on an internet
59-
* connection. See also `README.md`.
94+
* Besides building the infoview single-page application,
95+
* we build a loader and a bunch of esm-shims.
96+
* This is a way of compiling our runtime dependencies into single-file ES modules
97+
* which can be shared as imports between the infoview app and dynamically loaded widget modules.
98+
* Due to limitations in Rollup,
99+
* we must use an array of configs rather than a single config to do this.
100+
* Although projects do exist (e.g. jspm.io)
101+
* that could in principle produce the esm-shims for us,
102+
* they tend to chunk modules into many files rather than producing a single file.
103+
* Requiring them dynamically would make the infoview depend on an internet connection.
104+
* See also `README.md`.
60105
*
61106
* @type {import('rollup').RollupOptions[]}
62107
*/

lean4-infoview/src/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ export function loadRenderInfoview(
2727
importShim.addImportMap({ imports })
2828
importShim('@leanprover/infoview')
2929
.then((mod: any) => next(mod.renderInfoview(...args)))
30-
.catch(ex => console.error(`Error importing '@leanprover/infoview': ${JSON.stringify(ex)}`))
30+
.catch(ex => console.error(`Error importing '@leanprover/infoview': ${ex}`))
3131
}

lean4-unicode-input-component/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"scripts": {
1010
"watch": "tsc --watch",
1111
"watchTest": "tsc --watch",
12-
"build": "tsc"
12+
"build": "tsc",
13+
"build:dev": "tsc"
1314
},
1415
"main": "dist/index",
1516
"types": "dist/index",

lean4-unicode-input/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"scripts": {
1010
"watch": "tsc --watch",
1111
"watchTest": "tsc --watch",
12-
"build": "tsc"
12+
"build": "tsc",
13+
"build:dev": "tsc"
1314
},
1415
"main": "dist/index",
1516
"types": "dist/index",

0 commit comments

Comments
 (0)