@crossworks/app-build 0.300.140 → 0.400.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -3
- package/src/app-libraries.metafile.test.ts +60 -0
- package/src/app-libraries.test.ts +128 -0
- package/src/app-libraries.ts +81 -0
- package/src/build-runtime.ts +27 -0
- package/src/build.test.ts +259 -0
- package/src/canvas-runtime.ts +248 -0
- package/src/css.ts +10 -0
- package/src/index.ts +373 -43
- package/src/runtime-exports.fixture.ts +1 -0
- package/src/runtime-imports.test.ts +25 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crossworks/app-build",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.400.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -11,13 +11,23 @@
|
|
|
11
11
|
"src"
|
|
12
12
|
],
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@
|
|
14
|
+
"@babel/parser": "7.29.9",
|
|
15
|
+
"@mantle/share-ui": "npm:@crossworks/share-ui@0.400.0",
|
|
15
16
|
"@tailwindcss/oxide": "^4.3.3",
|
|
17
|
+
"alpinejs": "3.17.5",
|
|
18
|
+
"d3": "7.9.0",
|
|
19
|
+
"echarts": "6.1.0",
|
|
16
20
|
"esbuild": "^0.28.2",
|
|
21
|
+
"katex": "0.19.1",
|
|
22
|
+
"konva": "10.9.0",
|
|
17
23
|
"lucide-react": "^1.52.0",
|
|
24
|
+
"motion": "14.6.0",
|
|
25
|
+
"plotly.js-dist-min": "4.1.2",
|
|
18
26
|
"react": "^19.3.0",
|
|
19
27
|
"react-dom": "^19.3.0",
|
|
20
|
-
"
|
|
28
|
+
"reveal.js": "6.0.2",
|
|
29
|
+
"tailwindcss": "^4.3.3",
|
|
30
|
+
"three": "0.186.1"
|
|
21
31
|
},
|
|
22
32
|
"devDependencies": {
|
|
23
33
|
"@types/node": "^26.6.4",
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The libraries an app imports are external: the bundler never opens their
|
|
3
|
+
* files. Main's metafile backstop (inputsOutsideTheApp, index.ts) refuses a
|
|
4
|
+
* build that read any file off the app, so a green build already says so;
|
|
5
|
+
* this checks the metafile itself. esbuild is wrapped (not changed) to keep
|
|
6
|
+
* each build's inputs.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
9
|
+
import { RUNTIME_EXPORTS_FIXTURE } from './runtime-exports.fixture';
|
|
10
|
+
|
|
11
|
+
const seen = vi.hoisted(() => [] as string[][]);
|
|
12
|
+
|
|
13
|
+
vi.mock('esbuild', async (importOriginal) => {
|
|
14
|
+
const real = (await importOriginal()) as { default: typeof import('esbuild') };
|
|
15
|
+
const build = (async (options: Parameters<typeof real.default.build>[0]) => {
|
|
16
|
+
const res = await real.default.build(options);
|
|
17
|
+
seen.push(Object.keys(res.metafile?.inputs ?? {}));
|
|
18
|
+
return res;
|
|
19
|
+
}) as typeof real.default.build;
|
|
20
|
+
return { ...real, default: { ...real.default, build }, build };
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const { buildApp, APP_LIBRARY_SPECIFIERS } = await import('./index');
|
|
24
|
+
|
|
25
|
+
/** The runtime shapes of the libraries (subsets), as app-libraries.test.ts. */
|
|
26
|
+
const RT: Record<string, string[]> = {
|
|
27
|
+
...RUNTIME_EXPORTS_FIXTURE,
|
|
28
|
+
echarts: ['init'],
|
|
29
|
+
d3: ['select'],
|
|
30
|
+
three: ['Scene'],
|
|
31
|
+
'three/addons/controls/OrbitControls.js': ['OrbitControls'],
|
|
32
|
+
konva: ['default'],
|
|
33
|
+
katex: ['default'],
|
|
34
|
+
'@mantle/diagram-templates/browser': ['render'],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
describe('library imports read no file', () => {
|
|
38
|
+
for (const spec of APP_LIBRARY_SPECIFIERS) {
|
|
39
|
+
it(`'${spec}': the metafile holds the app's own files only`, async () => {
|
|
40
|
+
seen.length = 0;
|
|
41
|
+
const res = await buildApp(
|
|
42
|
+
{
|
|
43
|
+
entry: 'App.tsx',
|
|
44
|
+
files: {
|
|
45
|
+
'App.tsx': `import * as lib from '${spec}';\nconsole.log(lib);\nexport default function App() { return <div />; }\n`,
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{ runtimeExports: RT },
|
|
49
|
+
);
|
|
50
|
+
expect(res.errors).toEqual([]);
|
|
51
|
+
expect(seen).toHaveLength(1);
|
|
52
|
+
const inputs = seen[0]!;
|
|
53
|
+
expect(inputs).toContain('app:App.tsx');
|
|
54
|
+
for (const input of inputs) {
|
|
55
|
+
expect(input).not.toMatch(/node_modules|canvas-runtime|app-runtime|diagram-templates\//);
|
|
56
|
+
expect(input === 'app:App.tsx' || input.endsWith('<stdin>'), input).toBe(true);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vendored libraries a mini app may import (app-libraries.ts): each exact
|
|
3
|
+
* specifier stays a bare import for the frame's import map (never bundled),
|
|
4
|
+
* a near miss or a left-out library is refused with a message that names the
|
|
5
|
+
* fix, and the import check knows their export shapes.
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
import { buildApp, type AppSource } from './index';
|
|
9
|
+
import { APP_CANVAS_LIBRARIES, APP_LIBRARY_SPECIFIERS, appCanvasLibraries } from './app-libraries';
|
|
10
|
+
import { CANVAS_MODULE_SPECIFIERS } from './canvas-runtime';
|
|
11
|
+
import { RUNTIME_EXPORTS_FIXTURE } from './runtime-exports.fixture';
|
|
12
|
+
|
|
13
|
+
/** The fixture plus the library shapes the canvas runtime manifest records
|
|
14
|
+
* (subsets; the browser test checks the real build). */
|
|
15
|
+
const RT: Record<string, string[]> = {
|
|
16
|
+
...RUNTIME_EXPORTS_FIXTURE,
|
|
17
|
+
echarts: ['init', 'graphic', 'use'],
|
|
18
|
+
d3: ['select', 'scaleLinear', 'axisBottom'],
|
|
19
|
+
three: ['Scene', 'WebGLRenderer', 'PerspectiveCamera', 'Mesh'],
|
|
20
|
+
'three/addons/controls/OrbitControls.js': ['OrbitControls'],
|
|
21
|
+
konva: ['default'],
|
|
22
|
+
katex: ['default'],
|
|
23
|
+
'@mantle/diagram-templates/browser': ['domMeasurer', 'mount', 'render'],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const app = (body: string): AppSource => ({
|
|
27
|
+
entry: 'App.tsx',
|
|
28
|
+
files: { 'App.tsx': `${body}\nexport default function App() { return <div />; }\n` },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('mini-app library imports', () => {
|
|
32
|
+
it('every canvas library an app reads is one the canvas runtime serves', () => {
|
|
33
|
+
for (const { specifier } of APP_CANVAS_LIBRARIES) {
|
|
34
|
+
expect(CANVAS_MODULE_SPECIFIERS).toContain(specifier);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
for (const spec of APP_LIBRARY_SPECIFIERS) {
|
|
39
|
+
it(`'${spec}' stays a bare import (served by the import map, not bundled)`, async () => {
|
|
40
|
+
const res = await buildApp(app(`import * as lib from '${spec}';\nconsole.log(lib);`), {
|
|
41
|
+
runtimeExports: RT,
|
|
42
|
+
});
|
|
43
|
+
expect(res.errors).toEqual([]);
|
|
44
|
+
expect(res.ok).toBe(true);
|
|
45
|
+
expect(res.code).toContain(JSON.stringify(spec));
|
|
46
|
+
// Bundling any of them would run to tens of kilobytes at least.
|
|
47
|
+
expect(res.code!.length).toBeLessThan(5000);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
it('refuses an import attribute on a library import', async () => {
|
|
52
|
+
for (const spec of ['echarts', '@mantle/diagram-templates/browser']) {
|
|
53
|
+
for (const attr of ["{ type: 'text' }", "{ type: 'json' }"]) {
|
|
54
|
+
const res = await buildApp(
|
|
55
|
+
app(`import * as x from '${spec}' with ${attr};\nconsole.log(x);`),
|
|
56
|
+
{ runtimeExports: RT },
|
|
57
|
+
);
|
|
58
|
+
expect(res.ok, `${spec} ${attr}`).toBe(false);
|
|
59
|
+
expect(res.errors[0]!.text, `${spec} ${attr}`).toMatch(/import attribute|not allowed/);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('refuses a subpath of an allowed library', async () => {
|
|
65
|
+
const res = await buildApp(app("import { use } from 'echarts/core';\nuse([]);"), {
|
|
66
|
+
runtimeExports: RT,
|
|
67
|
+
});
|
|
68
|
+
expect(res.ok).toBe(false);
|
|
69
|
+
expect(res.errors[0]!.text).toContain("Import 'echarts/core' is not allowed");
|
|
70
|
+
expect(res.errors[0]!.text).toContain('canvas_library');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('refuses the libraries left out, with the reason', async () => {
|
|
74
|
+
for (const [spec, why] of [
|
|
75
|
+
['reveal.js', 'slide-deck'],
|
|
76
|
+
['alpinejs', 'unsafe-eval'],
|
|
77
|
+
['motion', "'motion/react'"],
|
|
78
|
+
] as const) {
|
|
79
|
+
const res = await buildApp(app(`import x from '${spec}';\nconsole.log(x);`), {
|
|
80
|
+
runtimeExports: RT,
|
|
81
|
+
});
|
|
82
|
+
expect(res.ok, spec).toBe(false);
|
|
83
|
+
expect(res.errors[0]!.text).toContain(`'${spec}' is not available in a mini app`);
|
|
84
|
+
expect(res.errors[0]!.text).toContain(why);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('refuses an unlisted @-scoped package', async () => {
|
|
89
|
+
const res = await buildApp(app("import x from '@mantle/diagram-templates';\nconsole.log(x);"), {
|
|
90
|
+
runtimeExports: RT,
|
|
91
|
+
});
|
|
92
|
+
expect(res.ok).toBe(false);
|
|
93
|
+
expect(res.errors[0]!.text).toContain("Unknown import '@mantle/diagram-templates'");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('the import check catches the usual wrong shape: a default import of echarts', async () => {
|
|
97
|
+
const res = await buildApp(app("import echarts from 'echarts';\nconsole.log(echarts);"), {
|
|
98
|
+
runtimeExports: RT,
|
|
99
|
+
});
|
|
100
|
+
expect(res.ok).toBe(false);
|
|
101
|
+
expect(res.errors[0]!.text).toContain("'echarts' has no default export");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('accepts the default import of konva and katex', async () => {
|
|
105
|
+
const res = await buildApp(
|
|
106
|
+
app("import Konva from 'konva';\nimport katex from 'katex';\nconsole.log(Konva, katex);"),
|
|
107
|
+
{ runtimeExports: RT },
|
|
108
|
+
);
|
|
109
|
+
expect(res.errors).toEqual([]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('appCanvasLibraries throws when the canvas manifest lacks a library', () => {
|
|
113
|
+
expect(() => appCanvasLibraries({ imports: {}, exports: {} })).toThrow(/echarts/);
|
|
114
|
+
const imports = Object.fromEntries(
|
|
115
|
+
APP_CANVAS_LIBRARIES.map((l) => [l.specifier, `/canvas-runtime/${l.guide}.js`]),
|
|
116
|
+
);
|
|
117
|
+
const exports = Object.fromEntries(APP_CANVAS_LIBRARIES.map((l) => [l.specifier, ['x']]));
|
|
118
|
+
const out = appCanvasLibraries({
|
|
119
|
+
imports: { ...imports, 'reveal.js': '/canvas-runtime/reveal.js' },
|
|
120
|
+
exports,
|
|
121
|
+
});
|
|
122
|
+
// Only the app subset: a left-out library never reaches the app map.
|
|
123
|
+
expect(Object.keys(out.imports).sort()).toEqual(
|
|
124
|
+
APP_CANVAS_LIBRARIES.map((l) => l.specifier).sort(),
|
|
125
|
+
);
|
|
126
|
+
expect(out.imports['reveal.js']).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The canvas libraries a MINI APP may import, and the ones it may not.
|
|
3
|
+
*
|
|
4
|
+
* One vendored copy: an app imports the same content-hashed files a Present
|
|
5
|
+
* canvas does (`/canvas-runtime/`, canvas-runtime.ts), never a copy bundled
|
|
6
|
+
* into the app. The app runtime build (build-runtime.ts) copies each
|
|
7
|
+
* specifier's URL into the app import map and its export names into the app
|
|
8
|
+
* import check, so the bundler leaves the import bare (index.ts) and the
|
|
9
|
+
* frame resolves it, as it resolves React.
|
|
10
|
+
*
|
|
11
|
+
* The one exception is `motion/react`: Motion's React API imports React, so
|
|
12
|
+
* it is built into the APP runtime against the one shared React
|
|
13
|
+
* (build-runtime.ts), not taken from the canvas set, which has no React.
|
|
14
|
+
*
|
|
15
|
+
* Every entry is an exact specifier. A subpath (`echarts/core`,
|
|
16
|
+
* `three/examples/jsm/...`) is refused like any other unlisted import.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** A canvas-runtime module an app may import: the specifier as written in
|
|
20
|
+
* app source, and the canvas_library guide that covers it. */
|
|
21
|
+
export type AppLibrary = Readonly<{ specifier: string; guide: string }>;
|
|
22
|
+
|
|
23
|
+
/** Taken from the canvas runtime's manifest (must be in CANVAS_MODULES). */
|
|
24
|
+
export const APP_CANVAS_LIBRARIES: readonly AppLibrary[] = [
|
|
25
|
+
{ specifier: 'echarts', guide: 'echarts' },
|
|
26
|
+
{ specifier: 'd3', guide: 'd3' },
|
|
27
|
+
{ specifier: 'three', guide: 'three' },
|
|
28
|
+
// The addon imports `three` through the import map, so a scene and its
|
|
29
|
+
// controls share one Three.
|
|
30
|
+
{ specifier: 'three/addons/controls/OrbitControls.js', guide: 'three' },
|
|
31
|
+
{ specifier: 'konva', guide: 'konva' },
|
|
32
|
+
{ specifier: 'katex', guide: 'katex' },
|
|
33
|
+
// The diagram templates (flow, pid): `render` + `domMeasurer` draw a
|
|
34
|
+
// template's data to an SVG string in the app's own document.
|
|
35
|
+
{ specifier: '@mantle/diagram-templates/browser', guide: 'diagram-templates' },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
/** Built into the app runtime itself (build-runtime.ts ENTRY_SRC). */
|
|
39
|
+
export const APP_RUNTIME_LIBRARIES: readonly AppLibrary[] = [
|
|
40
|
+
{ specifier: 'motion/react', guide: 'motion' },
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/** Canvas libraries an app may NOT import, and why. The error text names
|
|
44
|
+
* them so the builder does not keep trying. */
|
|
45
|
+
export const APP_LIBRARIES_LEFT_OUT: Readonly<Record<string, string>> = {
|
|
46
|
+
'reveal.js':
|
|
47
|
+
'a slide-deck framework that owns the whole page (its own layout, scaling and keys); a deck is a Present canvas',
|
|
48
|
+
alpinejs:
|
|
49
|
+
'a second reactive DOM framework that fights React for the same nodes, and it needs unsafe-eval, which the app frame does not allow',
|
|
50
|
+
motion: "use 'motion/react' in an app (the vanilla build is for canvases)",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** Every library specifier an app may import. */
|
|
54
|
+
export const APP_LIBRARY_SPECIFIERS: readonly string[] = [
|
|
55
|
+
...APP_CANVAS_LIBRARIES,
|
|
56
|
+
...APP_RUNTIME_LIBRARIES,
|
|
57
|
+
].map((l) => l.specifier);
|
|
58
|
+
|
|
59
|
+
/** The canvas-runtime subset an app reads, from that runtime's manifest:
|
|
60
|
+
* its URLs (for the app import map) and export names (for the import
|
|
61
|
+
* check). Throws when the canvas runtime lacks one: an app import map that
|
|
62
|
+
* silently missed a library would ship apps that fail to link. */
|
|
63
|
+
export function appCanvasLibraries(canvas: {
|
|
64
|
+
imports: Record<string, string>;
|
|
65
|
+
exports?: Record<string, string[]>;
|
|
66
|
+
}): { imports: Record<string, string>; exports: Record<string, string[]> } {
|
|
67
|
+
const imports: Record<string, string> = {};
|
|
68
|
+
const exports: Record<string, string[]> = {};
|
|
69
|
+
for (const { specifier } of APP_CANVAS_LIBRARIES) {
|
|
70
|
+
const url = canvas.imports[specifier];
|
|
71
|
+
const names = canvas.exports?.[specifier];
|
|
72
|
+
if (!url || !names) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`canvas runtime manifest has no '${specifier}' (url or exports); rebuild the canvas runtime before the app runtime`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
imports[specifier] = url;
|
|
78
|
+
exports[specifier] = names;
|
|
79
|
+
}
|
|
80
|
+
return { imports, exports };
|
|
81
|
+
}
|
package/src/build-runtime.ts
CHANGED
|
@@ -16,6 +16,11 @@
|
|
|
16
16
|
* Output: `<outDir>/<name>-<hash>.js` files + `manifest.json`, a map from the
|
|
17
17
|
* specifier as written in app source (`react`, `@/components/ui/button`, `@host`,
|
|
18
18
|
* …) to its hashed URL. The sandbox reads the manifest to build the import map.
|
|
19
|
+
*
|
|
20
|
+
* The canvas libraries an app may import (app-libraries.ts) join the same
|
|
21
|
+
* manifest: their URLs point at the canvas runtime's files (`/canvas-runtime/`),
|
|
22
|
+
* so the app import map and the app import check carry them with no second
|
|
23
|
+
* copy and no change to the frame route or the export loader.
|
|
19
24
|
*/
|
|
20
25
|
import * as fs from 'node:fs';
|
|
21
26
|
import * as path from 'node:path';
|
|
@@ -23,6 +28,7 @@ import { createRequire } from 'node:module';
|
|
|
23
28
|
import { fileURLToPath } from 'node:url';
|
|
24
29
|
import esbuild from 'esbuild';
|
|
25
30
|
import { KIT } from './kit.ts';
|
|
31
|
+
import { appCanvasLibraries } from './app-libraries.ts';
|
|
26
32
|
|
|
27
33
|
const REAL_DIR = path.dirname(fileURLToPath(import.meta.url));
|
|
28
34
|
const ENTRY_NS = 'rt-entry';
|
|
@@ -61,6 +67,10 @@ const ENTRY_SRC: Record<string, string> = {
|
|
|
61
67
|
badge: KIT['@/components/ui/badge']!,
|
|
62
68
|
separator: KIT['@/components/ui/separator']!,
|
|
63
69
|
host: KIT['@host']!,
|
|
70
|
+
// Motion's React API (app-libraries.ts): it imports React, so it is built
|
|
71
|
+
// here, where splitting gives it the one shared React, not in the canvas
|
|
72
|
+
// runtime. ESM, so `export *` carries its names across the boundary.
|
|
73
|
+
'motion-react': "export * from 'motion/react';",
|
|
64
74
|
};
|
|
65
75
|
|
|
66
76
|
/** Specifier as written in app source → entry name. This is the contract the
|
|
@@ -78,6 +88,7 @@ export const RUNTIME_SPECIFIERS: Record<string, string> = {
|
|
|
78
88
|
'@/components/ui/badge': 'badge',
|
|
79
89
|
'@/components/ui/separator': 'separator',
|
|
80
90
|
'@host': 'host',
|
|
91
|
+
'motion/react': 'motion-react',
|
|
81
92
|
};
|
|
82
93
|
|
|
83
94
|
/** esbuild plugin: serve the virtual entry sources + bind the kit's own
|
|
@@ -132,7 +143,13 @@ export type RuntimeManifest = {
|
|
|
132
143
|
export async function buildRuntime(
|
|
133
144
|
outDir: string,
|
|
134
145
|
urlPrefix = '/app-runtime/',
|
|
146
|
+
/** The canvas runtime's manifest, whose app-library subset joins this
|
|
147
|
+
* one. The build script always passes it; a test may leave it out. */
|
|
148
|
+
canvas?: { imports: Record<string, string>; exports?: Record<string, string[]> },
|
|
135
149
|
): Promise<RuntimeManifest> {
|
|
150
|
+
// Before anything is written: a canvas manifest that lacks a library fails
|
|
151
|
+
// here, not after the runtime directory was emptied.
|
|
152
|
+
const libraries = canvas ? appCanvasLibraries(canvas) : null;
|
|
136
153
|
fs.rmSync(outDir, { recursive: true, force: true });
|
|
137
154
|
fs.mkdirSync(outDir, { recursive: true });
|
|
138
155
|
|
|
@@ -189,6 +206,16 @@ export async function buildRuntime(
|
|
|
189
206
|
exports[specifier] = names;
|
|
190
207
|
}
|
|
191
208
|
|
|
209
|
+
if (libraries) {
|
|
210
|
+
for (const spec of Object.keys(libraries.imports)) {
|
|
211
|
+
// A library named like a runtime module would replace it silently.
|
|
212
|
+
if (spec in imports)
|
|
213
|
+
throw new Error(`app runtime: '${spec}' is both a runtime module and a library`);
|
|
214
|
+
}
|
|
215
|
+
Object.assign(imports, libraries.imports);
|
|
216
|
+
Object.assign(exports, libraries.exports);
|
|
217
|
+
}
|
|
218
|
+
|
|
192
219
|
const manifest: RuntimeManifest = {
|
|
193
220
|
imports,
|
|
194
221
|
exports,
|
package/src/build.test.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as os from 'node:os';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
1
5
|
import { describe, expect, it } from 'vitest';
|
|
2
6
|
import { buildApp, type AppSource } from './index';
|
|
3
7
|
import { RUNTIME_EXPORTS_FIXTURE as RT } from './runtime-exports.fixture';
|
|
@@ -90,6 +94,208 @@ describe('buildApp — the import allowlist (security boundary)', () => {
|
|
|
90
94
|
expect(res.ok).toBe(false);
|
|
91
95
|
expect(res.errors.some((e) => /Unknown import/i.test(e.text))).toBe(true);
|
|
92
96
|
});
|
|
97
|
+
|
|
98
|
+
it('still bundles lucide-react icons, tree-shaken', async () => {
|
|
99
|
+
const res = await buildApp(
|
|
100
|
+
app({
|
|
101
|
+
'App.tsx':
|
|
102
|
+
"import { Home } from 'lucide-react';\nexport default function App() { return <Home />; }\n",
|
|
103
|
+
}),
|
|
104
|
+
{ runtimeExports: RT },
|
|
105
|
+
);
|
|
106
|
+
expect(res.errors).toEqual([]);
|
|
107
|
+
expect(res.code!.length).toBeLessThan(8000);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('refuses a computed require() that reaches a readable file on disk', async () => {
|
|
111
|
+
// A file outside the app that parses: on the old code its text landed in
|
|
112
|
+
// the bundle. The path is relative to this package's src, the bundler's
|
|
113
|
+
// resolveDir, as an author would write it.
|
|
114
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'app-build-escape-'));
|
|
115
|
+
try {
|
|
116
|
+
fs.writeFileSync(path.join(dir, 'secret.js'), "export default 'SECRET-7f3a';\n");
|
|
117
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
118
|
+
const rel = path.relative(here, dir).split(path.sep).join('/');
|
|
119
|
+
for (const call of [`require(\`${rel}/\${n}.js\`)`, `import(\`${rel}/\${n}.js\`)`]) {
|
|
120
|
+
const res = await buildApp(
|
|
121
|
+
app({
|
|
122
|
+
'App.tsx': `const n = 'secret';\nconst m = ${call};\nexport default function App() { return <div>{String(m)}</div>; }\n`,
|
|
123
|
+
}),
|
|
124
|
+
{ runtimeExports: RT },
|
|
125
|
+
);
|
|
126
|
+
expect(res.ok, call).toBe(false);
|
|
127
|
+
expect(res.code, call).toBeUndefined();
|
|
128
|
+
expect(res.errors[0]!.text, call).toContain('one quoted path');
|
|
129
|
+
}
|
|
130
|
+
} finally {
|
|
131
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('refuses a computed call in the FIRST layer, whatever the quotes, regexes or comments around it', async () => {
|
|
136
|
+
// The M2's probe (2026-10-24): a string holding a double quote printed in
|
|
137
|
+
// single quotes, and the string-emptying regex swallowed the call. Each
|
|
138
|
+
// case must be refused before the build (the first layer's own text),
|
|
139
|
+
// not by a backstop, and no canary text may come back.
|
|
140
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'app-build-first-layer-'));
|
|
141
|
+
try {
|
|
142
|
+
fs.writeFileSync(path.join(dir, 'x.json'), '{ "CANARY_JSON": 1 }\n');
|
|
143
|
+
fs.writeFileSync(path.join(dir, 'x.js'), "export default 'CANARY_JS';\n");
|
|
144
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
145
|
+
const R = path.relative(here, dir).split(path.sep).join('/');
|
|
146
|
+
const cases = [
|
|
147
|
+
`const t = ['q"', require(\`${R}/\${n}.json\`), "z"];`,
|
|
148
|
+
`const a = 'he said "hi"'; const m = import(\`${R}/\${n}.js\`); const b = "q";`,
|
|
149
|
+
`const r = /"/; const m = require(\`${R}/\${n}.js\`); const z = "x";`,
|
|
150
|
+
`const r = /['"\`]/g; const m = require(\`${R}/\${n}.js\`); const z = '"';`,
|
|
151
|
+
`/* " */ const m = require(\`${R}/\${n}.js\`); // "`,
|
|
152
|
+
`const m = \`\${require(\`${R}/\${n}.js\`)}\`;`,
|
|
153
|
+
`const m = [1].map(() => require(\`${R}/\${n}.js\`));`,
|
|
154
|
+
`const m = (0, require)(\`${R}/\${n}.js\`);`,
|
|
155
|
+
// Dead code: the bundler still expands it (the audit of 2026-10-24).
|
|
156
|
+
`if (false) { require(\`${R}/\${n}.json\`); }`,
|
|
157
|
+
`const m = false && require(\`${R}/\${n}.json\`);`,
|
|
158
|
+
`function f() { return 1; require(\`${R}/\${n}.json\`); }\nvoid f;`,
|
|
159
|
+
// Syntax a lower transform target refused, so the file went unchecked.
|
|
160
|
+
`import { "a b" as q } from './m';\nconst m = require(\`${R}/\${n}.json\`);\nvoid q;`,
|
|
161
|
+
`await 0;\nconst m = require(\`${R}/\${n}.json\`);`,
|
|
162
|
+
// TypeScript wrappers esbuild strips before it reads the call.
|
|
163
|
+
`const m = (require as any)(\`${R}/\${n}.json\`);`,
|
|
164
|
+
`const m = require!(\`${R}/\${n}.json\`);`,
|
|
165
|
+
`const m = import((\`${R}/\${n}.json\`) as string);`,
|
|
166
|
+
`const m = (require<any>)(\`${R}/\${n}.json\`);`,
|
|
167
|
+
`const m = (require!<any>)(\`${R}/\${n}.json\`);`,
|
|
168
|
+
`const m = require?.(\`${R}/\${n}.json\`);`,
|
|
169
|
+
];
|
|
170
|
+
for (const line of cases) {
|
|
171
|
+
const res = await buildApp(
|
|
172
|
+
app({
|
|
173
|
+
'App.tsx': `const n = String(Date.now());\n${line}\nexport default function App() { return <div>{String(n)}</div>; }\n`,
|
|
174
|
+
'm.ts': 'const v = 1;\nexport { v as "a b" };\n',
|
|
175
|
+
}),
|
|
176
|
+
{ runtimeExports: RT },
|
|
177
|
+
);
|
|
178
|
+
expect(res.ok, line).toBe(false);
|
|
179
|
+
expect(res.code, line).toBeUndefined();
|
|
180
|
+
// The first layer's own text; a backstop's refusal reads differently.
|
|
181
|
+
expect(res.errors[0]!.text, line).toMatch(
|
|
182
|
+
/^(require|import)\(\) in a mini app takes one quoted path/,
|
|
183
|
+
);
|
|
184
|
+
expect(JSON.stringify(res.errors), line).not.toContain('CANARY');
|
|
185
|
+
}
|
|
186
|
+
} finally {
|
|
187
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("keeps a warning about the app's own code, even when it quotes a path-like key", async () => {
|
|
192
|
+
const res = await buildApp(
|
|
193
|
+
app({
|
|
194
|
+
'App.tsx':
|
|
195
|
+
'const o = { "/api/v1/": 1, "/api/v1/": 2 };\nexport default function App() { return <p>{String(o["/api/v1/"])}</p>; }\n',
|
|
196
|
+
}),
|
|
197
|
+
{ runtimeExports: RT },
|
|
198
|
+
);
|
|
199
|
+
expect(res.ok).toBe(true);
|
|
200
|
+
expect(
|
|
201
|
+
res.warnings.some((w) => /Duplicate key/.test(w.text) && w.location?.file === 'app:App.tsx'),
|
|
202
|
+
).toBe(true);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('still builds a typed app: generics, casts, enums and a quoted lazy import', async () => {
|
|
206
|
+
const res = await buildApp(
|
|
207
|
+
app({
|
|
208
|
+
'App.tsx':
|
|
209
|
+
"enum Mode { A, B }\ntype Row<T> = { v: T };\nconst rows: Row<number>[] = [{ v: 1 }];\nconst load = () => import('./B' as string);\nexport default function App() { void load; return <p>{String(rows[0]!.v as number)}{Mode.B}</p>; }\n",
|
|
210
|
+
'B.tsx': 'export default function B() { return <span>b</span>; }\n',
|
|
211
|
+
}),
|
|
212
|
+
{ runtimeExports: RT },
|
|
213
|
+
);
|
|
214
|
+
expect(res.errors).toEqual([]);
|
|
215
|
+
expect(res.ok).toBe(true);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('still builds an app whose strings and regexes only mention require( or import(', async () => {
|
|
219
|
+
const res = await buildApp(
|
|
220
|
+
app({
|
|
221
|
+
'App.tsx':
|
|
222
|
+
"const s = 'say \"require(x)\" or import(`./${y}`) now';\nconst r = /require\\(|import\\(/;\nconst load = () => import('./B');\nexport default function App() { void load; return <p title={String(r)}>{s}</p>; }\n",
|
|
223
|
+
'B.tsx': 'export default function B() { return <span>b</span>; }\n',
|
|
224
|
+
}),
|
|
225
|
+
{ runtimeExports: RT },
|
|
226
|
+
);
|
|
227
|
+
expect(res.errors).toEqual([]);
|
|
228
|
+
expect(res.ok).toBe(true);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('refuses a computed call however it is spelled, and quotes nothing off the app', async () => {
|
|
232
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'app-build-escape-'));
|
|
233
|
+
try {
|
|
234
|
+
// A file that fails to parse (its text was quoted in the error) and a
|
|
235
|
+
// folder only NAMED like the allowed package.
|
|
236
|
+
fs.writeFileSync(path.join(dir, 'bad.json'), '{ MARKER_BADJSON: 1 }\n');
|
|
237
|
+
fs.mkdirSync(path.join(dir, 'node_modules', 'lucide-react'), { recursive: true });
|
|
238
|
+
fs.writeFileSync(
|
|
239
|
+
path.join(dir, 'node_modules', 'lucide-react', 'x.js'),
|
|
240
|
+
"export default 'MARKER_FAKE';\n",
|
|
241
|
+
);
|
|
242
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
243
|
+
const rel = path.relative(here, dir).split(path.sep).join('/');
|
|
244
|
+
for (const call of [
|
|
245
|
+
`require(\`${rel}/\${n}.json\`)`,
|
|
246
|
+
`requir\\u0065(\`${rel}/\${n}.json\`)`,
|
|
247
|
+
`require(\`${rel}/node_modules/lucide-react/\${n}.js\`)`,
|
|
248
|
+
`require /* c */ (\`${rel}/\${n}.json\`)`,
|
|
249
|
+
]) {
|
|
250
|
+
const res = await buildApp(
|
|
251
|
+
app({
|
|
252
|
+
'App.tsx': `const n = String(Date.now());\nconst m = ${call};\nexport default function App() { return <div>{String(m)}</div>; }\n`,
|
|
253
|
+
}),
|
|
254
|
+
{ runtimeExports: RT },
|
|
255
|
+
);
|
|
256
|
+
expect(res.ok, call).toBe(false);
|
|
257
|
+
expect(res.code, call).toBeUndefined();
|
|
258
|
+
const all = JSON.stringify(res.errors);
|
|
259
|
+
expect(all, call).not.toContain('MARKER');
|
|
260
|
+
expect(all, call).not.toContain(dir);
|
|
261
|
+
}
|
|
262
|
+
} finally {
|
|
263
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('refuses a computed import() or require() (esbuild read the real disk for it)', async () => {
|
|
268
|
+
for (const call of [
|
|
269
|
+
'require(`./${n}`)',
|
|
270
|
+
'require(`../${n}`)',
|
|
271
|
+
'require(`../../${n}`)',
|
|
272
|
+
'import(`./${n}`)',
|
|
273
|
+
"require('./' + n)",
|
|
274
|
+
"require('../' + n)",
|
|
275
|
+
]) {
|
|
276
|
+
const res = await buildApp(
|
|
277
|
+
app({
|
|
278
|
+
'App.tsx': `const n = String(Date.now());\nconst m = ${call};\nexport default function App() { return <div>{String(m)}</div>; }\n`,
|
|
279
|
+
}),
|
|
280
|
+
{ runtimeExports: RT },
|
|
281
|
+
);
|
|
282
|
+
expect(res.ok, call).toBe(false);
|
|
283
|
+
expect(res.code, call).toBeUndefined();
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it('still builds a quoted import(), and text that only mentions require(', async () => {
|
|
288
|
+
const res = await buildApp(
|
|
289
|
+
app({
|
|
290
|
+
'App.tsx':
|
|
291
|
+
"const load = () => import('./Chart');\nexport default function App() { void load; return <div>{'call require(x) or import(`./${y}`)'}</div>; }\n",
|
|
292
|
+
'Chart.tsx': 'export default function Chart() { return <span>c</span>; }\n',
|
|
293
|
+
}),
|
|
294
|
+
{ runtimeExports: RT },
|
|
295
|
+
);
|
|
296
|
+
expect(res.errors).toEqual([]);
|
|
297
|
+
expect(res.ok).toBe(true);
|
|
298
|
+
});
|
|
93
299
|
});
|
|
94
300
|
|
|
95
301
|
describe('buildApp — error reporting', () => {
|
|
@@ -180,3 +386,56 @@ describe('lintToolRefs — undeclared host.tools.call', () => {
|
|
|
180
386
|
expect(res.warnings.length).toBe(0);
|
|
181
387
|
});
|
|
182
388
|
});
|
|
389
|
+
|
|
390
|
+
describe('buildApp: no read of the server disk through an import', () => {
|
|
391
|
+
// Security audit 2026-10-24: the bare allowlist was a prefix match, so
|
|
392
|
+
// `lucide-react/../../package.json` resolved to a file outside the package,
|
|
393
|
+
// and `with { type: 'text' }` made esbuild read any file as text.
|
|
394
|
+
const MARKER = '"private"';
|
|
395
|
+
const tries: Array<[string, string]> = [
|
|
396
|
+
['parent segments', "import p from 'lucide-react/../../package.json';"],
|
|
397
|
+
['deep parent segments', "import p from 'react/../../../../package.json';"],
|
|
398
|
+
['a dot segment', "import p from 'lucide-react/./../../package.json';"],
|
|
399
|
+
['an empty segment', "import p from 'lucide-react//../../package.json';"],
|
|
400
|
+
['a backslash', "import p from 'lucide-react\\\\..\\\\..\\\\package.json';"],
|
|
401
|
+
['a query', "import p from 'lucide-react?raw';"],
|
|
402
|
+
['a hash', "import p from 'lucide-react#x';"],
|
|
403
|
+
['text attribute', "import p from 'lucide-react/../../package.json' with { type: 'text' };"],
|
|
404
|
+
['text attribute on a relative file', "import p from './App.tsx' with { type: 'text' };"],
|
|
405
|
+
['bytes attribute', "import p from 'lucide-react' with { type: 'bytes' };"],
|
|
406
|
+
['the bare dot', "import p from '.' with { type: 'json' };"],
|
|
407
|
+
['the bare double dot', "import p from '..';"],
|
|
408
|
+
['an absolute path', "import p from '/etc/hosts';"],
|
|
409
|
+
];
|
|
410
|
+
for (const [name, line] of tries) {
|
|
411
|
+
it(`refuses ${name}`, async () => {
|
|
412
|
+
const src = app({
|
|
413
|
+
'App.tsx': `${line}\nexport default function App() {\n return <div>{String(p)}</div>;\n}\n`,
|
|
414
|
+
});
|
|
415
|
+
const res = await buildApp(src, { runtimeExports: RT });
|
|
416
|
+
expect(res.ok).toBe(false);
|
|
417
|
+
expect(res.code ?? '').not.toContain(MARKER);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
it('still builds a package file inside an allowed package', async () => {
|
|
422
|
+
const src = app({
|
|
423
|
+
'App.tsx':
|
|
424
|
+
"import pkg from 'lucide-react/package.json';\nexport default function App() {\n return <div>{pkg.name}</div>;\n}\n",
|
|
425
|
+
});
|
|
426
|
+
const res = await buildApp(src, { runtimeExports: RT });
|
|
427
|
+
expect(res.errors).toEqual([]);
|
|
428
|
+
expect(res.ok).toBe(true);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('still allows a JSON attribute on a relative JSON file', async () => {
|
|
432
|
+
const src = app({
|
|
433
|
+
'App.tsx':
|
|
434
|
+
"import d from './data.json' with { type: 'json' };\nexport default function App() {\n return <div>{d.n}</div>;\n}\n",
|
|
435
|
+
'data.json': '{"n": 1}',
|
|
436
|
+
});
|
|
437
|
+
const res = await buildApp(src, { runtimeExports: RT });
|
|
438
|
+
expect(res.errors).toEqual([]);
|
|
439
|
+
expect(res.ok).toBe(true);
|
|
440
|
+
});
|
|
441
|
+
});
|