@crossworks/app-build 0.300.143 → 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/src/index.ts
CHANGED
|
@@ -10,11 +10,15 @@
|
|
|
10
10
|
* Build inputs are agent-authored TSX; build errors are returned (not thrown)
|
|
11
11
|
* with file/line locations so Appsmith can read them and self-correct.
|
|
12
12
|
*/
|
|
13
|
+
import * as fs from 'node:fs';
|
|
14
|
+
import { createRequire } from 'node:module';
|
|
13
15
|
import * as path from 'node:path';
|
|
14
16
|
import { fileURLToPath } from 'node:url';
|
|
17
|
+
import { parse, type ParserPlugin } from '@babel/parser';
|
|
15
18
|
import esbuild, { type BuildOptions, type Message, type Plugin } from 'esbuild';
|
|
16
19
|
import { buildAppCss } from './css';
|
|
17
20
|
import { KIT } from './kit';
|
|
21
|
+
import { APP_LIBRARIES_LEFT_OUT, APP_LIBRARY_SPECIFIERS } from './app-libraries';
|
|
18
22
|
// Local copy of @mantle/std's errorMessage. This package is published as a
|
|
19
23
|
// contract package; it must not depend on private workspace packages, which
|
|
20
24
|
// do not exist on npm (the v0.232.122-153 tarballs shipped an unresolvable
|
|
@@ -60,11 +64,74 @@ export type BuildResult = {
|
|
|
60
64
|
export const ESBUILD_VERSION = esbuild.version;
|
|
61
65
|
|
|
62
66
|
const APP_NS = 'app';
|
|
67
|
+
/** Marks the bundler's own nested resolve, so the gate does not re-enter. */
|
|
68
|
+
const RESOLVING = Symbol('app-build-resolving');
|
|
63
69
|
const STDIN = '<stdin>';
|
|
64
70
|
|
|
65
|
-
/** Bare npm packages a mini app may import. Everything else is
|
|
71
|
+
/** Bare npm packages a mini app may import and bundle. Everything else is
|
|
72
|
+
* rejected, except the exact library specifiers below. */
|
|
66
73
|
const BARE_ALLOW = /^(react($|\/)|react-dom($|\/)|lucide-react($|\/))/;
|
|
67
74
|
|
|
75
|
+
/** The vendored libraries (app-libraries.ts): EXACT specifiers, left bare for
|
|
76
|
+
* the frame's import map, never bundled, so the build never reads them.
|
|
77
|
+
* `echarts/core` is not `echarts`. */
|
|
78
|
+
const LIBRARY_EXTERNALS = new Set(APP_LIBRARY_SPECIFIERS);
|
|
79
|
+
|
|
80
|
+
const ALLOWED_TEXT =
|
|
81
|
+
`react, react-dom, lucide-react, and the libraries ${APP_LIBRARY_SPECIFIERS.join(', ')} ` +
|
|
82
|
+
`(call canvas_library first)`;
|
|
83
|
+
|
|
84
|
+
/** A library import stays bare for the import map. It takes no import
|
|
85
|
+
* attribute: none means anything for a library module, and the @-scoped
|
|
86
|
+
* ones resolve before the attribute gate (audit 2026-10-24). */
|
|
87
|
+
function libraryImport(args: { path: string; with?: Record<string, string> }) {
|
|
88
|
+
if (args.with && Object.keys(args.with).length) {
|
|
89
|
+
return {
|
|
90
|
+
errors: [{ text: `Import '${args.path}' takes no import attribute in a mini app.` }],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return { path: args.path, external: true };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The refusal for a bare import, naming the fix when it is a known one. */
|
|
97
|
+
function refusal(spec: string): string {
|
|
98
|
+
const why = APP_LIBRARIES_LEFT_OUT[spec];
|
|
99
|
+
if (why)
|
|
100
|
+
return `Import '${spec}' is not available in a mini app: ${why}. Allowed: ${ALLOWED_TEXT}.`;
|
|
101
|
+
return `Import '${spec}' is not allowed in a mini app. Allowed packages: ${ALLOWED_TEXT}. Use @/components/ui/*, @host, or relative files for everything else.`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The installed directories of the allowed packages: whatever an allowed
|
|
105
|
+
* bare import resolves to must lie inside one of them. */
|
|
106
|
+
const BUNDLED_PACKAGES = ['react', 'react-dom', 'lucide-react'];
|
|
107
|
+
|
|
108
|
+
/** A specifier that steps out of its package (`lucide-react/../x`, a `.`
|
|
109
|
+
* segment, an empty segment, a backslash) or carries a query or hash. The
|
|
110
|
+
* prefix match above passed `lucide-react/../../package.json` to node
|
|
111
|
+
* resolution, which read a file of the server's disk into the app bundle
|
|
112
|
+
* (security audit 2026-10-24). */
|
|
113
|
+
function escapesPackage(spec: string): boolean {
|
|
114
|
+
if (/[\\?#]/.test(spec)) return true;
|
|
115
|
+
return spec.split('/').some((seg) => seg === '.' || seg === '..' || seg === '');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** An import attribute other than JSON (`with { type: 'text' }` and the
|
|
119
|
+
* like) makes esbuild read any file as text or bytes: user code may not
|
|
120
|
+
* ask for one. */
|
|
121
|
+
function refusedAttributes(attrs: Record<string, string> | undefined): string | null {
|
|
122
|
+
if (!attrs) return null;
|
|
123
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
124
|
+
if (!(k === 'type' && v === 'json')) return `${k}: '${v}'`;
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function fromUserSource(args: { namespace: string; importer: string }): boolean {
|
|
130
|
+
return (
|
|
131
|
+
args.namespace === APP_NS || args.importer === STDIN || args.importer.endsWith(`/${STDIN}`)
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
68
135
|
function loaderFor(p: string): 'tsx' | 'ts' | 'js' | 'jsx' | 'json' | 'css' {
|
|
69
136
|
if (p.endsWith('.ts')) return 'ts';
|
|
70
137
|
if (p.endsWith('.js')) return 'js';
|
|
@@ -74,6 +141,200 @@ function loaderFor(p: string): 'tsx' | 'ts' | 'js' | 'jsx' | 'json' | 'css' {
|
|
|
74
141
|
return 'tsx';
|
|
75
142
|
}
|
|
76
143
|
|
|
144
|
+
/** The one package a build reads from disk (react and react-dom are
|
|
145
|
+
* external), by its real directory, so a folder that is merely NAMED
|
|
146
|
+
* `node_modules/lucide-react` elsewhere does not pass (audit 2026-10-24). */
|
|
147
|
+
const LUCIDE_ROOT = fs.realpathSync(
|
|
148
|
+
path.dirname(createRequire(import.meta.url).resolve('lucide-react/package.json')),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
/** Code an app may hold: what esbuild parses as a module. */
|
|
152
|
+
const CODE_LOADERS = new Set(['tsx', 'ts', 'jsx', 'js']);
|
|
153
|
+
|
|
154
|
+
/** Wrappers esbuild strips before it reads a call: TypeScript casts,
|
|
155
|
+
* non-null marks and type arguments (`(require as any)(…)`, `require!(…)`,
|
|
156
|
+
* `(require<any>)(…)`), and parentheses. */
|
|
157
|
+
const TRANSPARENT = new Set([
|
|
158
|
+
'TSInstantiationExpression',
|
|
159
|
+
'TSAsExpression',
|
|
160
|
+
'TSSatisfiesExpression',
|
|
161
|
+
'TSNonNullExpression',
|
|
162
|
+
'TSTypeAssertion',
|
|
163
|
+
'ParenthesizedExpression',
|
|
164
|
+
]);
|
|
165
|
+
|
|
166
|
+
function unwrap(node: unknown): Record<string, unknown> | undefined {
|
|
167
|
+
let n = node as Record<string, unknown> | undefined;
|
|
168
|
+
while (n && typeof n.type === 'string' && TRANSPARENT.has(n.type)) {
|
|
169
|
+
n = n.expression as Record<string, unknown> | undefined;
|
|
170
|
+
}
|
|
171
|
+
return n;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** What a call's argument must be for esbuild to resolve it through the
|
|
175
|
+
* plugin gates: one plain string, or a template with no substitution. */
|
|
176
|
+
function isPlainPath(arg: unknown): boolean {
|
|
177
|
+
const a = unwrap(arg);
|
|
178
|
+
if (!a) return false;
|
|
179
|
+
if (a.type === 'StringLiteral') return true;
|
|
180
|
+
if (a.type === 'TemplateLiteral') return ((a.expressions as unknown[]) ?? []).length === 0;
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** `require` as a callee, also when wrapped: `(0, require)`, `(require)`, a cast. */
|
|
185
|
+
function isRequireCallee(callee: unknown): boolean {
|
|
186
|
+
const c = unwrap(callee);
|
|
187
|
+
if (!c) return false;
|
|
188
|
+
if (c.type === 'Identifier') return c.name === 'require';
|
|
189
|
+
if (c.type === 'SequenceExpression') {
|
|
190
|
+
const exprs = (c.expressions as unknown[]) ?? [];
|
|
191
|
+
return isRequireCallee(exprs[exprs.length - 1]);
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Keys of a Babel node that hold no code. */
|
|
197
|
+
const NOT_CODE = new Set([
|
|
198
|
+
'type',
|
|
199
|
+
'start',
|
|
200
|
+
'end',
|
|
201
|
+
'loc',
|
|
202
|
+
'range',
|
|
203
|
+
'extra',
|
|
204
|
+
'leadingComments',
|
|
205
|
+
'trailingComments',
|
|
206
|
+
'innerComments',
|
|
207
|
+
'comments',
|
|
208
|
+
'tokens',
|
|
209
|
+
]);
|
|
210
|
+
|
|
211
|
+
/** The first `import()` or `require()` in a parsed module whose argument is
|
|
212
|
+
* not a plain path, or null. The tree is the app's own source as written,
|
|
213
|
+
* so neither quote style, regular expressions, comments, template nesting
|
|
214
|
+
* nor dead code (`if (false) require(…)`, which the bundler still expands)
|
|
215
|
+
* can hide a call (the M2's probe and the audit of 2026-10-24). */
|
|
216
|
+
function computedCall(ast: unknown): 'import' | 'require' | null {
|
|
217
|
+
const stack: unknown[] = [ast];
|
|
218
|
+
while (stack.length) {
|
|
219
|
+
const node = stack.pop() as Record<string, unknown> | null;
|
|
220
|
+
if (!node || typeof node !== 'object') continue;
|
|
221
|
+
if (node.type === 'ImportExpression' && !isPlainPath(node.source)) return 'import';
|
|
222
|
+
// An optional call (`require?.(…)`) is not expanded by esbuild today; it
|
|
223
|
+
// is checked the same way, so a later esbuild cannot open a gap.
|
|
224
|
+
if (node.type === 'CallExpression' || node.type === 'OptionalCallExpression') {
|
|
225
|
+
const callee = unwrap(node.callee);
|
|
226
|
+
const args = (node.arguments as unknown[]) ?? [];
|
|
227
|
+
if (callee?.type === 'Import' && !isPlainPath(args[0])) return 'import';
|
|
228
|
+
if (isRequireCallee(callee) && (args.length === 0 || !isPlainPath(args[0]))) return 'require';
|
|
229
|
+
}
|
|
230
|
+
for (const key of Object.keys(node)) {
|
|
231
|
+
if (NOT_CODE.has(key)) continue;
|
|
232
|
+
const v = node[key];
|
|
233
|
+
// One at a time: a spread of a huge array (10^5 statements) throws.
|
|
234
|
+
if (Array.isArray(v)) for (const item of v) stack.push(item);
|
|
235
|
+
else if (v && typeof v === 'object') stack.push(v);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** The Babel syntax plugins for an app file's loader: the language as
|
|
242
|
+
* written, TypeScript and JSX included, with no transform in between. */
|
|
243
|
+
function parserPlugins(loader: string): ParserPlugin[] {
|
|
244
|
+
if (loader === 'tsx') return ['typescript', 'jsx', 'decorators-legacy'];
|
|
245
|
+
if (loader === 'ts') return ['typescript', 'decorators-legacy'];
|
|
246
|
+
return ['jsx', ['decorators', {}]];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Refuse an `import()` or `require()` with a computed path before esbuild
|
|
251
|
+
* runs. esbuild expands one (`require(\`./${x}\`)`, `'./' + x`) by reading the
|
|
252
|
+
* matching files on the real disk itself, with no onResolve call, so the
|
|
253
|
+
* plugin gates never see them, and a file that fails to parse is quoted in
|
|
254
|
+
* the error (audit 2026-10-24). Each app file is parsed AS WRITTEN (Babel,
|
|
255
|
+
* TypeScript and JSX), not through a transform, which folds constants and
|
|
256
|
+
* drops dead code the bundler still expands. A file the parser cannot read
|
|
257
|
+
* is refused with the parser's own message: the check fails closed.
|
|
258
|
+
*/
|
|
259
|
+
async function computedImportErrors(source: AppSource): Promise<BuildMessage[]> {
|
|
260
|
+
const out: BuildMessage[] = [];
|
|
261
|
+
for (const [file, text] of Object.entries(source.files)) {
|
|
262
|
+
const loader = loaderFor(file);
|
|
263
|
+
if (!CODE_LOADERS.has(loader)) continue;
|
|
264
|
+
let ast: unknown;
|
|
265
|
+
try {
|
|
266
|
+
ast = parse(text, {
|
|
267
|
+
sourceType: 'module',
|
|
268
|
+
sourceFilename: file,
|
|
269
|
+
plugins: parserPlugins(loader),
|
|
270
|
+
createImportExpressions: true,
|
|
271
|
+
errorRecovery: false,
|
|
272
|
+
});
|
|
273
|
+
} catch (err) {
|
|
274
|
+
// About the app's own source, so it goes back as it is.
|
|
275
|
+
const e = err as { message?: string; loc?: { line: number; column: number } };
|
|
276
|
+
out.push({
|
|
277
|
+
text: `This file could not be checked for computed imports, so the build is refused: ${e.message ?? 'unreadable'}`,
|
|
278
|
+
location: { file, line: e.loc?.line ?? 0, column: e.loc?.column ?? 0 },
|
|
279
|
+
});
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
const kind = computedCall(ast);
|
|
283
|
+
if (kind) {
|
|
284
|
+
out.push({
|
|
285
|
+
text: `${kind}() in a mini app takes one quoted path, like ${kind}('./Chart'). A computed path (a variable, a template string or a joined string) is not allowed.`,
|
|
286
|
+
location: { file, line: 0, column: 0 },
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return out;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Every file a build read that is neither an app file (the virtual
|
|
294
|
+
* namespace), the stdin entry, nor inside lucide-react's real directory.
|
|
295
|
+
* The backstop behind computedImportErrors. */
|
|
296
|
+
function inputsOutsideTheApp(inputs: Record<string, unknown>): string[] {
|
|
297
|
+
return Object.keys(inputs).filter(
|
|
298
|
+
(p) =>
|
|
299
|
+
!p.startsWith(`${APP_NS}:`) &&
|
|
300
|
+
p !== STDIN &&
|
|
301
|
+
!p.endsWith(`/${STDIN}`) &&
|
|
302
|
+
!path.resolve(p).startsWith(LUCIDE_ROOT + path.sep),
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** esbuild's messages about reading a file off the disk. One can sit at an
|
|
307
|
+
* app file (a glob's read error is located at the call), so these are
|
|
308
|
+
* generic wherever they are located (the audits of 2026-10-24). */
|
|
309
|
+
const DISK_READ_RE = /Cannot read (file|directory)|No loader is configured|Could not read/;
|
|
310
|
+
/** A path that climbs out or is absolute, quoted or not: `../x`, `/a/b`,
|
|
311
|
+
* `C:\\x`. Not `"/"` alone, which a plain syntax error quotes. */
|
|
312
|
+
const OUTSIDE_PATH_RE = /(?:^|[\s"'(:])(?:\.\.[\\/]|\/[^\s"'/]+\/|[A-Za-z]:[\\/])/;
|
|
313
|
+
const GENERIC_MSG = 'The build failed on a file that is not part of this app.';
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* A build message the author may see. One about the app's own code (located
|
|
317
|
+
* at `app:<key>`, or the stdin entry) keeps its text, so a warning such as a
|
|
318
|
+
* duplicate key still helps; a disk-read message never does. Any other
|
|
319
|
+
* message carries no text or path of a file that is not the app's: a failed
|
|
320
|
+
* parse quotes the file (audit 2026-10-24).
|
|
321
|
+
*/
|
|
322
|
+
function appOnlyMsg(source: AppSource, m: Message): BuildMessage {
|
|
323
|
+
if (DISK_READ_RE.test(m.text)) return { text: GENERIC_MSG, location: null };
|
|
324
|
+
const file = m.location?.file;
|
|
325
|
+
if (!file) {
|
|
326
|
+
return OUTSIDE_PATH_RE.test(m.text) ? { text: GENERIC_MSG, location: null } : toMsg(m);
|
|
327
|
+
}
|
|
328
|
+
if (file === STDIN || file.endsWith(`/${STDIN}`)) return toMsg(m);
|
|
329
|
+
// An app file is located as `app:<key>` (its namespace). A bare path that
|
|
330
|
+
// merely equals a file key is a disk file, not the app's (the M2's C2).
|
|
331
|
+
if (file.startsWith(`${APP_NS}:`) && Object.hasOwn(source.files, file.slice(APP_NS.length + 1))) {
|
|
332
|
+
return toMsg(m);
|
|
333
|
+
}
|
|
334
|
+
if (path.resolve(file).startsWith(LUCIDE_ROOT + path.sep)) return toMsg(m);
|
|
335
|
+
return { text: GENERIC_MSG, location: null };
|
|
336
|
+
}
|
|
337
|
+
|
|
77
338
|
/** Normalize a key as it appears in `files` (posix, no leading ./). */
|
|
78
339
|
function normKey(k: string): string {
|
|
79
340
|
return path.posix.normalize(k.replace(/^\.\//, '')).replace(/^\/+/, '');
|
|
@@ -112,15 +373,35 @@ function virtualPlugin(source: AppSource): Plugin {
|
|
|
112
373
|
// map at load time instead of being bundled into every app.
|
|
113
374
|
build.onResolve({ filter: /^@/ }, (args) => {
|
|
114
375
|
if (args.path in KIT) return { path: args.path, external: true };
|
|
376
|
+
if (LIBRARY_EXTERNALS.has(args.path)) return libraryImport(args);
|
|
115
377
|
return {
|
|
116
378
|
errors: [
|
|
117
379
|
{
|
|
118
|
-
text: `Unknown import '${args.path}'. Allowed:
|
|
380
|
+
text: `Unknown import '${args.path}'. Allowed: ${Object.keys(KIT).join(', ')}, ${ALLOWED_TEXT}, and relative files.`,
|
|
119
381
|
},
|
|
120
382
|
],
|
|
121
383
|
};
|
|
122
384
|
});
|
|
123
385
|
|
|
386
|
+
// Every import written in user or kit source: no import attribute but
|
|
387
|
+
// JSON, and never the bare `.` or `..` (they resolve against this
|
|
388
|
+
// package's own directory on disk).
|
|
389
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
390
|
+
if (args.pluginData === RESOLVING || !fromUserSource(args)) return undefined;
|
|
391
|
+
const attr = refusedAttributes(args.with);
|
|
392
|
+
if (attr) {
|
|
393
|
+
return {
|
|
394
|
+
errors: [
|
|
395
|
+
{ text: `Import '${args.path}' with { ${attr} } is not allowed in a mini app.` },
|
|
396
|
+
],
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
if (/^\.\.?([?#]|$)/.test(args.path)) {
|
|
400
|
+
return { errors: [{ text: `Import '${args.path}' is not allowed in a mini app.` }] };
|
|
401
|
+
}
|
|
402
|
+
return undefined;
|
|
403
|
+
});
|
|
404
|
+
|
|
124
405
|
// Relative imports — resolve against the virtual source tree. The stdin
|
|
125
406
|
// entry's `import App from './<entry>'` also lands here (its importer is
|
|
126
407
|
// the resolved stdin path, e.g. '/.../app-build/src/<stdin>').
|
|
@@ -140,22 +421,32 @@ function virtualPlugin(source: AppSource): Plugin {
|
|
|
140
421
|
// is rejected with a clear message so Appsmith self-corrects. The iframe
|
|
141
422
|
// sandbox + CSP is the real boundary, but failing the build is cleaner
|
|
142
423
|
// feedback than a silently dead import.
|
|
143
|
-
build.onResolve({ filter: /^[^.@]/ }, (args) => {
|
|
424
|
+
build.onResolve({ filter: /^[^.@]/ }, async (args) => {
|
|
425
|
+
// The backstop's own resolve call below comes back through here.
|
|
426
|
+
if (args.pluginData === RESOLVING) return null;
|
|
144
427
|
// Only gate imports written in USER/KIT source — let transitive
|
|
145
428
|
// node_modules imports (react-dom → scheduler, etc.) resolve freely.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (!
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
};
|
|
429
|
+
if (!fromUserSource(args)) return null;
|
|
430
|
+
const refused = { errors: [{ text: refusal(args.path) }] };
|
|
431
|
+
// A vendored library: bare in the output, the import map serves it.
|
|
432
|
+
if (LIBRARY_EXTERNALS.has(args.path)) return libraryImport(args);
|
|
433
|
+
if (!BARE_ALLOW.test(args.path) || escapesPackage(args.path)) return refused;
|
|
434
|
+
// Backstop: resolve it here, and keep it only when it lands inside
|
|
435
|
+
// the package it names (a symlink or an odd `exports` map aside).
|
|
436
|
+
const pkg = BUNDLED_PACKAGES.find((p) => args.path === p || args.path.startsWith(`${p}/`))!;
|
|
437
|
+
const resolved = await build.resolve(args.path, {
|
|
438
|
+
kind: args.kind,
|
|
439
|
+
resolveDir: args.resolveDir,
|
|
440
|
+
pluginData: RESOLVING,
|
|
441
|
+
});
|
|
442
|
+
if (resolved.errors.length) return { errors: resolved.errors };
|
|
443
|
+
if (
|
|
444
|
+
!resolved.external &&
|
|
445
|
+
!resolved.path.includes(`${path.sep}node_modules${path.sep}${pkg}${path.sep}`)
|
|
446
|
+
) {
|
|
447
|
+
return refused;
|
|
448
|
+
}
|
|
449
|
+
return resolved;
|
|
159
450
|
});
|
|
160
451
|
|
|
161
452
|
build.onLoad({ filter: /.*/, namespace: APP_NS }, (args) => {
|
|
@@ -219,6 +510,14 @@ export function lintToolRefs(source: AppSource, declaredSlugs: string[]): BuildM
|
|
|
219
510
|
* and named (including a default+named combination). */
|
|
220
511
|
const IMPORT_RE = /import\s+([^'";]+?)\s+from\s*['"]([^'"]+)['"]/g;
|
|
221
512
|
|
|
513
|
+
/** A re-export links like an import: `export { a } from 'm'`,
|
|
514
|
+
* `export type { A } from 'm'`, `export * as ns from 'm'`. Only these exact
|
|
515
|
+
* shapes (the clause runs to `from` with nothing between), so an `export`
|
|
516
|
+
* statement without a `from` never reaches over to a later import.
|
|
517
|
+
* `export * from 'm'` names nothing and is left alone. */
|
|
518
|
+
const REEXPORT_RE =
|
|
519
|
+
/export\s+((?:type\s*)?\{[^}]*\}|\*\s+as\s+[A-Za-z_$][\w$]*)\s*from\s*['"]([^'"]+)['"]/g;
|
|
520
|
+
|
|
222
521
|
/** `import type …` — the whole clause is type-only. Lookahead keeps
|
|
223
522
|
* `import type from 'm'` and `import type, { a } from 'm'` (a default import
|
|
224
523
|
* that happens to be NAMED `type`) as value imports. */
|
|
@@ -286,29 +585,31 @@ export function lintRuntimeImports(
|
|
|
286
585
|
): BuildMessage[] {
|
|
287
586
|
const out: BuildMessage[] = [];
|
|
288
587
|
for (const [file, text] of Object.entries(source.files)) {
|
|
289
|
-
IMPORT_RE
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
588
|
+
for (const re of [IMPORT_RE, REEXPORT_RE]) {
|
|
589
|
+
re.lastIndex = 0;
|
|
590
|
+
let m: RegExpExecArray | null;
|
|
591
|
+
while ((m = re.exec(text)) !== null) {
|
|
592
|
+
const spec = m[2]!;
|
|
593
|
+
if (spec.startsWith('.') || spec.startsWith('/')) continue;
|
|
594
|
+
const available = runtimeExports[spec];
|
|
595
|
+
if (!available) continue; // not a shared-runtime specifier; esbuild will rule on it
|
|
596
|
+
const { names, namespace } = importedNames(m[1]!);
|
|
597
|
+
if (namespace) continue;
|
|
598
|
+
const have = new Set(available);
|
|
599
|
+
for (const name of names) {
|
|
600
|
+
if (have.has(name)) continue;
|
|
601
|
+
const pre = text.slice(0, m.index);
|
|
602
|
+
const line = pre.length - pre.replace(/\n/g, '').length + 1; // 1-based
|
|
603
|
+
const column = m.index - (pre.lastIndexOf('\n') + 1); // 0-based within line
|
|
604
|
+
const hint =
|
|
605
|
+
name === 'default'
|
|
606
|
+
? `'${spec}' has no default export. Use a named import, e.g. import { ${available[0]} } from '${spec}'.`
|
|
607
|
+
: `'${spec}' does not export '${name}'.`;
|
|
608
|
+
out.push({
|
|
609
|
+
text: `${hint} It exports: ${available.join(', ')}. Left as-is the app builds but fails to load in the browser, with no error the app can catch.`,
|
|
610
|
+
location: { file, line, column },
|
|
611
|
+
});
|
|
612
|
+
}
|
|
312
613
|
}
|
|
313
614
|
}
|
|
314
615
|
}
|
|
@@ -319,6 +620,14 @@ export function lintRuntimeImports(
|
|
|
319
620
|
* `declaredToolSlugs` is supplied, undeclared `host.tools.call` slugs are
|
|
320
621
|
* appended to the build's warnings (see lintToolRefs). */
|
|
321
622
|
export { loadRuntimeExports } from './runtime-exports';
|
|
623
|
+
export { buildCanvasCss } from './css';
|
|
624
|
+
export { CANVAS_MODULE_SPECIFIERS } from './canvas-runtime';
|
|
625
|
+
export {
|
|
626
|
+
APP_CANVAS_LIBRARIES,
|
|
627
|
+
APP_LIBRARIES_LEFT_OUT,
|
|
628
|
+
APP_LIBRARY_SPECIFIERS,
|
|
629
|
+
APP_RUNTIME_LIBRARIES,
|
|
630
|
+
} from './app-libraries';
|
|
322
631
|
|
|
323
632
|
export async function buildApp(
|
|
324
633
|
source: AppSource,
|
|
@@ -337,6 +646,11 @@ export async function buildApp(
|
|
|
337
646
|
if (importErrors.length) {
|
|
338
647
|
return { ok: false, errors: importErrors, warnings: [], esbuildVersion: ESBUILD_VERSION };
|
|
339
648
|
}
|
|
649
|
+
// Before esbuild, which would read the disk for a computed import.
|
|
650
|
+
const computedErrors = await computedImportErrors(source);
|
|
651
|
+
if (computedErrors.length) {
|
|
652
|
+
return { ok: false, errors: computedErrors, warnings: [], esbuildVersion: ESBUILD_VERSION };
|
|
653
|
+
}
|
|
340
654
|
const toolWarnings = opts.declaredToolSlugs ? lintToolRefs(source, opts.declaredToolSlugs) : [];
|
|
341
655
|
const withToolWarnings = (r: BuildResult): BuildResult =>
|
|
342
656
|
toolWarnings.length ? { ...r, warnings: [...r.warnings, ...toolWarnings] } : r;
|
|
@@ -389,17 +703,33 @@ async function buildAppInner(source: AppSource): Promise<BuildResult> {
|
|
|
389
703
|
legalComments: 'none',
|
|
390
704
|
logLevel: 'silent',
|
|
391
705
|
define: { 'process.env.NODE_ENV': '"production"' },
|
|
706
|
+
metafile: true,
|
|
392
707
|
plugins: [virtualPlugin(source)],
|
|
393
708
|
};
|
|
394
709
|
|
|
710
|
+
const appMsg = (m: Message) => appOnlyMsg(source, m);
|
|
395
711
|
try {
|
|
396
712
|
const res = await esbuild.build(options);
|
|
713
|
+
if (inputsOutsideTheApp(res.metafile?.inputs ?? {}).length) {
|
|
714
|
+
return {
|
|
715
|
+
ok: false,
|
|
716
|
+
// No path: it would tell the author which files exist off the app.
|
|
717
|
+
errors: [
|
|
718
|
+
{
|
|
719
|
+
text: 'The build read a file that is not part of this app. An import() or require() in a mini app takes one quoted path: a computed path (a template string or a joined string) is not allowed.',
|
|
720
|
+
location: null,
|
|
721
|
+
},
|
|
722
|
+
],
|
|
723
|
+
warnings: res.warnings.map(appMsg),
|
|
724
|
+
esbuildVersion: ESBUILD_VERSION,
|
|
725
|
+
};
|
|
726
|
+
}
|
|
397
727
|
const code = res.outputFiles?.[0]?.text;
|
|
398
728
|
return {
|
|
399
729
|
ok: !!code,
|
|
400
730
|
...(code ? { code } : {}),
|
|
401
|
-
errors: res.errors.map(
|
|
402
|
-
warnings: res.warnings.map(
|
|
731
|
+
errors: res.errors.map(appMsg),
|
|
732
|
+
warnings: res.warnings.map(appMsg),
|
|
403
733
|
esbuildVersion: ESBUILD_VERSION,
|
|
404
734
|
};
|
|
405
735
|
} catch (err) {
|
|
@@ -408,8 +738,8 @@ async function buildAppInner(source: AppSource): Promise<BuildResult> {
|
|
|
408
738
|
if (bf && Array.isArray(bf.errors)) {
|
|
409
739
|
return {
|
|
410
740
|
ok: false,
|
|
411
|
-
errors: bf.errors.map(
|
|
412
|
-
warnings: (bf.warnings ?? []).map(
|
|
741
|
+
errors: bf.errors.map(appMsg),
|
|
742
|
+
warnings: (bf.warnings ?? []).map(appMsg),
|
|
413
743
|
esbuildVersion: ESBUILD_VERSION,
|
|
414
744
|
};
|
|
415
745
|
}
|
|
@@ -28,4 +28,5 @@ export const RUNTIME_EXPORTS_FIXTURE: Record<string, string[]> = {
|
|
|
28
28
|
'@/components/ui/badge': ['Badge'],
|
|
29
29
|
'@/components/ui/separator': ['Separator'],
|
|
30
30
|
'@host': ['__mount', 'host'],
|
|
31
|
+
'motion/react': ['motion', 'AnimatePresence', 'animate', 'useAnimate', 'useReducedMotion'],
|
|
31
32
|
};
|
|
@@ -172,3 +172,28 @@ describe('buildApp — runtime import check', () => {
|
|
|
172
172
|
expect(res.errors).toEqual([]);
|
|
173
173
|
});
|
|
174
174
|
});
|
|
175
|
+
|
|
176
|
+
describe('lintRuntimeImports: re-exports link like imports', () => {
|
|
177
|
+
it('checks export { a } from a runtime module', () => {
|
|
178
|
+
const msgs = lintRuntimeImports(src({ 'a.ts': "export { nope } from '@host';\n" }), RT);
|
|
179
|
+
expect(msgs).toHaveLength(1);
|
|
180
|
+
expect(msgs[0]!.text).toContain("'@host' does not export 'nope'");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('accepts a correct re-export, export * and export * as', () => {
|
|
184
|
+
for (const line of [
|
|
185
|
+
"export { host } from '@host';",
|
|
186
|
+
"export { host as h } from '@host';",
|
|
187
|
+
"export * from '@host';",
|
|
188
|
+
"export * as h from '@host';",
|
|
189
|
+
"export type { Nope } from '@host';",
|
|
190
|
+
]) {
|
|
191
|
+
expect(lintRuntimeImports(src({ 'a.ts': `${line}\n` }), RT), line).toEqual([]);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('an export without from does not reach over to a later import', () => {
|
|
196
|
+
const text = "export const o = { nope: 1 }\nexport { o as p }\nimport { host } from '@host'\n";
|
|
197
|
+
expect(lintRuntimeImports(src({ 'a.ts': text }), RT)).toEqual([]);
|
|
198
|
+
});
|
|
199
|
+
});
|