what-compiler 0.13.11 → 0.15.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/LICENSE +21 -0
- package/README.md +181 -2
- package/babel.d.ts +78 -2
- package/dist/babel/auto-selector.min.js +94 -0
- package/dist/babel/common.min.js +2053 -0
- package/dist/babel/dev-metadata.min.js +106 -0
- package/dist/babel/dom.min.js +1669 -0
- package/dist/babel/jsx-root.min.js +13 -0
- package/dist/babel/lists.min.js +492 -0
- package/dist/babel/nesting.min.js +328 -0
- package/dist/babel/reactivity.min.js +1218 -0
- package/dist/babel/server-actions.min.js +85 -0
- package/dist/babel/ssr.min.js +496 -0
- package/dist/babel/types.min.js +9 -0
- package/dist/babel-plugin.min.js +101 -1
- package/dist/file-router.min.js +368 -12
- package/dist/framework-files.min.js +62 -0
- package/dist/hot.min.js +315 -0
- package/dist/index.min.js +13 -414
- package/dist/release.min.js +78 -0
- package/dist/runtime.min.js +9 -1
- package/dist/tables/import-changes.min.js +272 -0
- package/dist/tables/markup-rules.min.js +213 -0
- package/dist/vite-plugin.min.js +432 -399
- package/file-router.d.ts +3 -1
- package/index.d.ts +1 -1
- package/package.json +11 -2
- package/runtime.d.ts +2 -1
- package/src/babel/auto-selector.js +248 -0
- package/src/babel/common.js +3800 -0
- package/src/babel/dev-metadata.js +167 -0
- package/src/babel/dom.js +3386 -0
- package/src/babel/jsx-root.js +27 -0
- package/src/babel/lists.js +704 -0
- package/src/babel/nesting.js +260 -0
- package/src/babel/reactivity.js +1998 -0
- package/src/babel/server-actions.js +144 -0
- package/src/babel/ssr.js +870 -0
- package/src/babel/types.js +10 -0
- package/src/babel-plugin.js +185 -3598
- package/src/file-router.js +58 -38
- package/src/framework-files.js +106 -0
- package/src/hot.js +523 -0
- package/src/release.js +127 -0
- package/src/runtime.js +7 -7
- package/src/tables/import-changes.js +157 -0
- package/src/tables/markup-rules.js +388 -0
- package/src/vite-plugin.js +460 -175
- package/vite.d.ts +47 -3
- package/src/error-overlay.js +0 -428
package/src/file-router.js
CHANGED
|
@@ -9,7 +9,9 @@
|
|
|
9
9
|
* src/pages/blog/index.jsx → /blog
|
|
10
10
|
* src/pages/blog/[slug].jsx → /blog/:slug
|
|
11
11
|
* src/pages/[...path].jsx → catch-all
|
|
12
|
-
* src/pages/_layout.jsx → layout for that directory
|
|
12
|
+
* src/pages/_layout.jsx → layout for that directory and every
|
|
13
|
+
* directory below it, around the layouts
|
|
14
|
+
* those declare (Next.js's nested layouts)
|
|
13
15
|
* src/pages/(auth)/login.jsx → /login (group doesn't affect URL)
|
|
14
16
|
* src/pages/api/users.js → API route: /api/users
|
|
15
17
|
*
|
|
@@ -182,10 +184,10 @@ const PAGE_EXPORT_RE = /export\s+const\s+page\s*=\s*\{/;
|
|
|
182
184
|
/**
|
|
183
185
|
* Extract `export const page = { ... }` from a file's source code.
|
|
184
186
|
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
187
|
+
* Chained regexes over the matched text (swap ' for ", quote bare keys, drop
|
|
188
|
+
* trailing commas, strip comments) would each be blind to string boundaries, so
|
|
189
|
+
* any config whose VALUES contained a parser-significant character would be
|
|
190
|
+
* corrupted into invalid JSON and thrown away:
|
|
189
191
|
*
|
|
190
192
|
* vary: ['cookie:theme'] → the key regex rewrote it to ["cookie":theme"]
|
|
191
193
|
* canonical: 'https://x.com/a' → the comment strip ate the rest of the line
|
|
@@ -199,7 +201,7 @@ const PAGE_EXPORT_RE = /export\s+const\s+page\s*=\s*\{/;
|
|
|
199
201
|
* anything else looks at the text, and brace matching finds the real end of the
|
|
200
202
|
* literal. It is still not a JS parser: only JSON-shaped values are supported
|
|
201
203
|
* (strings, numbers, booleans, null, arrays, nested objects). Anything else is
|
|
202
|
-
* a parse failure, which is
|
|
204
|
+
* a parse failure, which is REPORTED rather than swallowed.
|
|
203
205
|
*
|
|
204
206
|
* @param {string} source Page module source.
|
|
205
207
|
* @param {{ filePath?: string }} [options] filePath is used in the warning.
|
|
@@ -431,6 +433,13 @@ function readStringLiteral(source, start) {
|
|
|
431
433
|
// Quoted strings cannot span lines; treat it as unterminated rather than
|
|
432
434
|
// running off into the rest of the module.
|
|
433
435
|
if (quote !== '`' && (ch === '\n' || ch === '\r')) break;
|
|
436
|
+
// A template's line break is a \n whatever the file's line endings, as
|
|
437
|
+
// in JavaScript.
|
|
438
|
+
if (ch === '\r') {
|
|
439
|
+
value += '\n';
|
|
440
|
+
i += source[i + 1] === '\n' ? 2 : 1;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
434
443
|
|
|
435
444
|
value += ch;
|
|
436
445
|
i++;
|
|
@@ -442,14 +451,28 @@ function readStringLiteral(source, start) {
|
|
|
442
451
|
/**
|
|
443
452
|
* Detect which named exports a page module declares. Functions (loader,
|
|
444
453
|
* getStaticPaths) cannot live in `export const page` (JSON-only), so the codegen
|
|
445
|
-
* imports them as live bindings instead.
|
|
446
|
-
*
|
|
454
|
+
* imports them as live bindings instead. A page's `preload` and `middleware`
|
|
455
|
+
* are its route's (what-router), in the browser's routes module too. \b
|
|
456
|
+
* anchors avoid false positives like `loaderState` or `getStaticPathsHelper`.
|
|
447
457
|
*/
|
|
448
458
|
export function detectPageExports(source) {
|
|
449
459
|
return {
|
|
450
460
|
hasLoader: /export\s+(?:async\s+)?(?:const|let|var|function)\s+loader\b/.test(source),
|
|
451
461
|
hasGetStaticPaths: /export\s+(?:async\s+)?(?:const|let|var|function)\s+getStaticPaths\b/.test(source),
|
|
452
462
|
hasPageConfig: /export\s+const\s+page\b/.test(source),
|
|
463
|
+
hasPreload: /export\s+(?:async\s+)?(?:const|let|var|function)\s+preload\b/.test(source),
|
|
464
|
+
hasMiddleware: /export\s+(?:const|let|var)\s+middleware\b/.test(source),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** A page's route fields that come from its own exports (preload, middleware), and their imports. */
|
|
469
|
+
function routeExports(detected, varName) {
|
|
470
|
+
const names = [];
|
|
471
|
+
if (detected.hasPreload) names.push('preload');
|
|
472
|
+
if (detected.hasMiddleware) names.push('middleware');
|
|
473
|
+
return {
|
|
474
|
+
named: names.map((n) => `${n} as ${varName}_${n}`).join(', '),
|
|
475
|
+
fields: names.map((n) => `, ${n}: ${varName}_${n}`).join(''),
|
|
453
476
|
};
|
|
454
477
|
}
|
|
455
478
|
|
|
@@ -464,37 +487,35 @@ export function generateRoutesModule(pagesDir, rootDir) {
|
|
|
464
487
|
const routeEntries = [];
|
|
465
488
|
|
|
466
489
|
// Generate layout imports
|
|
467
|
-
const
|
|
468
|
-
layouts.forEach((layout, i) => {
|
|
490
|
+
const layoutVars = layouts.map((layout, i) => {
|
|
469
491
|
const varName = `_layout${i}`;
|
|
470
492
|
const relPath = toImportPath(layout.filePath, rootDir);
|
|
471
493
|
imports.push(`import ${varName} from '${relPath}';`);
|
|
472
|
-
|
|
494
|
+
return { dir: path.dirname(layout.filePath), name: varName };
|
|
473
495
|
});
|
|
474
496
|
|
|
475
497
|
// Generate page imports and route entries
|
|
476
498
|
pages.forEach((page, i) => {
|
|
477
499
|
const varName = `_page${i}`;
|
|
478
500
|
const relPath = toImportPath(page.filePath, rootDir);
|
|
479
|
-
imports.push(`import ${varName} from '${relPath}';`);
|
|
480
501
|
|
|
481
502
|
// Read file to extract page config + detect loader/getStaticPaths exports
|
|
482
503
|
let pageConfig = { mode: 'client' };
|
|
483
|
-
let detected = { hasLoader: false, hasGetStaticPaths: false, hasPageConfig: false };
|
|
504
|
+
let detected = { hasLoader: false, hasGetStaticPaths: false, hasPageConfig: false, hasPreload: false, hasMiddleware: false };
|
|
484
505
|
try {
|
|
485
506
|
const source = fs.readFileSync(page.filePath, 'utf-8');
|
|
486
507
|
pageConfig = extractPageConfig(source, { filePath: page.filePath });
|
|
487
508
|
detected = detectPageExports(source);
|
|
488
509
|
} catch {}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
const layoutVar = findLayout(page.routePath, layoutMap);
|
|
510
|
+
const own = routeExports(detected, varName);
|
|
511
|
+
imports.push(own.named ? `import ${varName}, { ${own.named} } from '${relPath}';` : `import ${varName} from '${relPath}';`);
|
|
492
512
|
|
|
493
513
|
const entry = {
|
|
494
514
|
path: page.routePath,
|
|
495
515
|
component: varName,
|
|
496
516
|
mode: pageConfig.mode || 'client',
|
|
497
|
-
layout:
|
|
517
|
+
layout: findLayouts(page.filePath, layoutVars),
|
|
518
|
+
own: own.fields,
|
|
498
519
|
hasLoader: detected.hasLoader,
|
|
499
520
|
};
|
|
500
521
|
|
|
@@ -522,7 +543,7 @@ export function generateRoutesModule(pagesDir, rootDir) {
|
|
|
522
543
|
'',
|
|
523
544
|
'export const routes = [',
|
|
524
545
|
...routeEntries.map(r =>
|
|
525
|
-
` { path: '${r.path}', component: ${r.component}, mode: '${r.mode}'${r.layout ? `, layout: ${r.layout}` : ''}${r.hasLoader ? ', hasLoader: true' : ''} },`
|
|
546
|
+
` { path: '${r.path}', component: ${r.component}, mode: '${r.mode}'${r.layout ? `, layout: ${r.layout}` : ''}${r.own}${r.hasLoader ? ', hasLoader: true' : ''} },`
|
|
526
547
|
),
|
|
527
548
|
'];',
|
|
528
549
|
'',
|
|
@@ -555,11 +576,10 @@ export function generateServerRoutesModule(pagesDir, rootDir) {
|
|
|
555
576
|
const imports = [];
|
|
556
577
|
const routeEntries = [];
|
|
557
578
|
|
|
558
|
-
const
|
|
559
|
-
layouts.forEach((layout, i) => {
|
|
579
|
+
const layoutVars = layouts.map((layout, i) => {
|
|
560
580
|
const varName = `_layout${i}`;
|
|
561
581
|
imports.push(`import ${varName} from '${toImportPath(layout.filePath, rootDir)}';`);
|
|
562
|
-
|
|
582
|
+
return { dir: path.dirname(layout.filePath), name: varName };
|
|
563
583
|
});
|
|
564
584
|
|
|
565
585
|
pages.forEach((page, i) => {
|
|
@@ -568,14 +588,14 @@ export function generateServerRoutesModule(pagesDir, rootDir) {
|
|
|
568
588
|
const relPath = toImportPath(page.filePath, rootDir);
|
|
569
589
|
|
|
570
590
|
let pageConfig = { mode: 'client' };
|
|
571
|
-
let detected = { hasLoader: false, hasGetStaticPaths: false, hasPageConfig: false };
|
|
591
|
+
let detected = { hasLoader: false, hasGetStaticPaths: false, hasPageConfig: false, hasPreload: false, hasMiddleware: false };
|
|
572
592
|
try {
|
|
573
593
|
const source = fs.readFileSync(page.filePath, 'utf-8');
|
|
574
594
|
pageConfig = extractPageConfig(source, { filePath: page.filePath });
|
|
575
595
|
detected = detectPageExports(source);
|
|
576
596
|
} catch {}
|
|
577
597
|
|
|
578
|
-
const needsNs = detected.hasLoader || detected.hasGetStaticPaths || detected.hasPageConfig;
|
|
598
|
+
const needsNs = detected.hasLoader || detected.hasGetStaticPaths || detected.hasPageConfig || detected.hasPreload || detected.hasMiddleware;
|
|
579
599
|
if (needsNs) {
|
|
580
600
|
imports.push(`import ${def}, * as ${ns} from '${relPath}';`);
|
|
581
601
|
} else {
|
|
@@ -587,7 +607,7 @@ export function generateServerRoutesModule(pagesDir, rootDir) {
|
|
|
587
607
|
component: def,
|
|
588
608
|
ns,
|
|
589
609
|
mode: pageConfig.mode || 'client',
|
|
590
|
-
layout:
|
|
610
|
+
layout: findLayouts(page.filePath, layoutVars),
|
|
591
611
|
...detected,
|
|
592
612
|
});
|
|
593
613
|
});
|
|
@@ -602,6 +622,8 @@ export function generateServerRoutesModule(pagesDir, rootDir) {
|
|
|
602
622
|
const routeLine = (r) =>
|
|
603
623
|
` { path: '${r.path}', component: ${r.component}, mode: '${r.mode}'` +
|
|
604
624
|
`${r.layout ? `, layout: ${r.layout}` : ''}` +
|
|
625
|
+
`${r.hasPreload ? `, preload: ${r.ns}.preload` : ''}` +
|
|
626
|
+
`${r.hasMiddleware ? `, middleware: ${r.ns}.middleware` : ''}` +
|
|
605
627
|
`${r.hasLoader ? `, loader: ${r.ns}.loader` : ''}` +
|
|
606
628
|
`${r.hasGetStaticPaths ? `, getStaticPaths: ${r.ns}.getStaticPaths` : ''}` +
|
|
607
629
|
`${r.hasPageConfig ? `, page: ${r.ns}.page` : ''} },`;
|
|
@@ -638,19 +660,17 @@ function toImportPath(filePath, rootDir) {
|
|
|
638
660
|
}
|
|
639
661
|
|
|
640
662
|
/**
|
|
641
|
-
*
|
|
663
|
+
* The layouts of the page in `pageFile`, as its route's `layout`: the
|
|
664
|
+
* `_layout` of every directory that holds the page, from the pages
|
|
665
|
+
* directory's down to the closest (a route group's `(auth)/_layout` too,
|
|
666
|
+
* around the group's pages only), as one name, a list of names (outermost
|
|
667
|
+
* first), or null without one. The router nests them in that order and keeps
|
|
668
|
+
* those two routes share.
|
|
642
669
|
*/
|
|
643
|
-
function
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
if (layoutMap.has(prefix)) return layoutMap.get(prefix);
|
|
650
|
-
segments.pop();
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
// Check root layout
|
|
654
|
-
if (layoutMap.has('/')) return layoutMap.get('/');
|
|
655
|
-
return null;
|
|
670
|
+
function findLayouts(pageFile, layoutVars) {
|
|
671
|
+
const found = layoutVars
|
|
672
|
+
.filter((layout) => pageFile.startsWith(layout.dir + path.sep))
|
|
673
|
+
.sort((a, b) => a.dir.length - b.dir.length)
|
|
674
|
+
.map((layout) => layout.name);
|
|
675
|
+
return found.length === 0 ? null : found.length === 1 ? found[0] : `[${found.join(', ')}]`;
|
|
656
676
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// What's own packages are the framework, never the app.
|
|
2
|
+
//
|
|
3
|
+
// The compiler's rewrites are written for an app's source: a destructured
|
|
4
|
+
// prop read where it is used, a rest element kept live (_$omit), development
|
|
5
|
+
// names on signal() and h() calls, server action ids. A package of What's
|
|
6
|
+
// that ships its source (what-react, what-router, what-core in development)
|
|
7
|
+
// is not that source: what-react's forwardRef() destructures `ref` from props
|
|
8
|
+
// to call a render function, and compiled as an app's component it handed
|
|
9
|
+
// that function a props object Headless UI could not call hasOwnProperty on.
|
|
10
|
+
// So a file is What's own when the nearest package.json with a name above it
|
|
11
|
+
// names one of these packages and the file is one the package publishes (its
|
|
12
|
+
// `files`): its tests and fixtures, which no install carries, are compiled as
|
|
13
|
+
// any app's files are.
|
|
14
|
+
//
|
|
15
|
+
// It is decided from the package, not the path: an npm or pnpm install puts
|
|
16
|
+
// the files under node_modules, but a workspace link (npm, pnpm or yarn
|
|
17
|
+
// workspaces, `file:`, `npm link`) resolves to the package's own directory,
|
|
18
|
+
// where no path pattern can tell it from the app's source.
|
|
19
|
+
//
|
|
20
|
+
// Only Node can look: with no `fs` (the compiler running in a browser) no
|
|
21
|
+
// file is What's own. Lookups are cached per directory.
|
|
22
|
+
|
|
23
|
+
const WHAT_PACKAGES = new Set([
|
|
24
|
+
'what-framework', 'what-core', 'what-compiler', 'what-react', 'what-router', 'what-server',
|
|
25
|
+
'what-isr', 'what-devtools', 'what-devtools-mcp', 'what-text', 'what-mcp',
|
|
26
|
+
'eslint-plugin-what', 'what-framework-cli', 'create-what',
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
/** @type {Map<string, { name: string, root: string, files: RegExp[] | null } | null>} directory → the What package it is in */
|
|
30
|
+
const packages = new Map();
|
|
31
|
+
|
|
32
|
+
function parentOf(dir) {
|
|
33
|
+
const parent = dir.replace(/[\\/][^\\/]*$/, '');
|
|
34
|
+
return parent === dir || parent === '' ? null : parent;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// npm's `files` entries as patterns of a path relative to the package: a name
|
|
38
|
+
// is the file or the directory of that name, `*` any run of a path segment,
|
|
39
|
+
// `**` any number of segments.
|
|
40
|
+
function filePattern(entry) {
|
|
41
|
+
const clean = String(entry).replace(/^\.\//, '').replace(/\/+$/, '');
|
|
42
|
+
const body = clean.split(/(\*\*\/|\*\*|\*)/).map((part) => (
|
|
43
|
+
part === '**/' ? '(?:.*/)?' : part === '**' ? '.*' : part === '*' ? '[^/]*' : part.replace(/[.+?^${}()|[\]\\]/g, '\\$&')
|
|
44
|
+
)).join('');
|
|
45
|
+
return new RegExp(`^${body}(?:/.*)?$`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function packageAt(dir, fs) {
|
|
49
|
+
if (packages.has(dir)) return packages.get(dir);
|
|
50
|
+
/** @type {{ name: string, root: string, files: RegExp[] | null } | null} */
|
|
51
|
+
let found;
|
|
52
|
+
/** @type {any} */
|
|
53
|
+
let pkg = null;
|
|
54
|
+
try {
|
|
55
|
+
const file = `${dir}/package.json`;
|
|
56
|
+
if (fs.existsSync(file)) pkg = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
57
|
+
} catch {
|
|
58
|
+
pkg = null;
|
|
59
|
+
}
|
|
60
|
+
if (pkg !== null && typeof pkg.name === 'string') {
|
|
61
|
+
// The nearest package with a name is the file's package: What's, or not.
|
|
62
|
+
found = WHAT_PACKAGES.has(pkg.name)
|
|
63
|
+
? { name: pkg.name, root: dir, files: Array.isArray(pkg.files) ? pkg.files.map(filePattern) : null }
|
|
64
|
+
: null;
|
|
65
|
+
} else {
|
|
66
|
+
// A package.json with no name (`{ "type": "module" }` in a directory)
|
|
67
|
+
// belongs to the package around it.
|
|
68
|
+
const parent = parentOf(dir);
|
|
69
|
+
found = parent === null ? null : packageAt(parent, fs);
|
|
70
|
+
}
|
|
71
|
+
packages.set(dir, found);
|
|
72
|
+
return found;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether `file` is one of the files a package of What's publishes (the
|
|
77
|
+
* package named `name`, when given). `fs` is node:fs; without it, false.
|
|
78
|
+
* @param {string | null | undefined} file
|
|
79
|
+
* @param {any} fs
|
|
80
|
+
* @param {string} [name]
|
|
81
|
+
* @returns {boolean}
|
|
82
|
+
*/
|
|
83
|
+
export function isFrameworkFile(file, fs, name) {
|
|
84
|
+
if (!fs || !file) return false;
|
|
85
|
+
const clean = String(file).replace(/[?#].*$/, '').replace(/\\/g, '/');
|
|
86
|
+
if (clean.startsWith('\0') || !clean.includes('/')) return false;
|
|
87
|
+
const dir = parentOf(clean);
|
|
88
|
+
const found = dir === null ? null : packageAt(dir, fs);
|
|
89
|
+
if (found === null || (name !== undefined && found.name !== name)) return false;
|
|
90
|
+
const rel = clean.slice(found.root.length + 1);
|
|
91
|
+
if (rel.startsWith('node_modules/') || rel.includes('/node_modules/')) return false;
|
|
92
|
+
return found.files === null || found.files.some((pattern) => pattern.test(rel));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* isFrameworkFile() for the module Babel is compiling, with node:fs where the
|
|
97
|
+
* compiler runs in Node, else false.
|
|
98
|
+
* @param {string | null | undefined} filename
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
export function isFrameworkModule(filename) {
|
|
102
|
+
const host = /** @type {any} */ (globalThis).process;
|
|
103
|
+
// node-floor: feature-detected; through what-compiler/vite every Node 20 release is checked
|
|
104
|
+
const fs = typeof host?.getBuiltinModule === 'function' ? host.getBuiltinModule('node:fs') : null;
|
|
105
|
+
return isFrameworkFile(filename, fs);
|
|
106
|
+
}
|