|
1 | 1 | import type { PluginContext } from './types'; |
2 | 2 |
|
3 | | -export function createBuildCode(ctx: PluginContext, inlineModules: boolean) { |
4 | | - const c = []; |
| 3 | +export function createDynamicImportedCode(ctx: PluginContext) { |
| 4 | + const c: string[] = []; |
5 | 5 |
|
6 | 6 | c.push(`export const LAYOUTS = {`); |
7 | 7 | Object.entries(ctx.opts.layouts).forEach(([layoutName, layoutPath]) => { |
8 | | - let importPath = layoutPath; |
9 | | - if (importPath.endsWith('.tsx') || importPath.endsWith('.jsx')) { |
10 | | - importPath = importPath.substring(0, importPath.length - 4); |
11 | | - } else if (importPath.endsWith('.ts') || importPath.endsWith('.js')) { |
12 | | - importPath = importPath.substring(0, importPath.length - 3); |
13 | | - } |
14 | | - |
| 8 | + const importPath = getImportPath(layoutPath); |
15 | 9 | c.push(` ${JSON.stringify(layoutName)}: () => import(${JSON.stringify(importPath)}),`); |
16 | 10 | }); |
17 | 11 | c.push(`};`); |
18 | 12 |
|
19 | | - c.push(`export const INDEXES = {`); |
20 | | - for (const i of ctx.indexes) { |
21 | | - c.push(` ${JSON.stringify(i.pathname)}: ${JSON.stringify(i)},`); |
| 13 | + c.push(`export const PAGES = {`); |
| 14 | + for (const p of ctx.pages) { |
| 15 | + c.push(` ${JSON.stringify(p.pathname)}: () => import(${JSON.stringify(p.filePath)}),`); |
22 | 16 | } |
23 | 17 | c.push(`};`); |
24 | 18 |
|
| 19 | + c.push(`export const INLINED_MODULES = false`); |
| 20 | + |
| 21 | + c.push(...createPageIndex(ctx)); |
| 22 | + |
| 23 | + c.push(createBuildId()); |
| 24 | + |
| 25 | + return c.join('\n'); |
| 26 | +} |
| 27 | + |
| 28 | +export function createEsmImportedCode(ctx: PluginContext) { |
| 29 | + const esmImports: string[] = []; |
| 30 | + const c: string[] = []; |
| 31 | + |
| 32 | + c.push(`export const LAYOUTS = {`); |
| 33 | + Object.entries(ctx.opts.layouts).forEach(([layoutName, layoutPath], index) => { |
| 34 | + const importPath = getImportPath(layoutPath); |
| 35 | + |
| 36 | + const importName = `layout_${index}`; |
| 37 | + esmImports.push(`import ${importName} from ${JSON.stringify(importPath)};`); |
| 38 | + c.push(` ${JSON.stringify(layoutName)}: () => ${importName},`); |
| 39 | + }); |
| 40 | + c.push(`};`); |
| 41 | + |
25 | 42 | c.push(`export const PAGES = {`); |
26 | | - if (inlineModules) { |
27 | | - for (const p of ctx.pages) { |
28 | | - c.push(` ${JSON.stringify(p.pathname)}: () => import(${JSON.stringify(p.filePath)}),`); |
29 | | - } |
| 43 | + let importPageIndex = 0; |
| 44 | + for (const p of ctx.pages) { |
| 45 | + const importName = `page_${importPageIndex++}`; |
| 46 | + esmImports.push(`import ${importName} from ${JSON.stringify(p.filePath)};`); |
| 47 | + c.push(` ${JSON.stringify(p.pathname)}: () => import(${JSON.stringify(p.filePath)}),`); |
30 | 48 | } |
31 | 49 | c.push(`};`); |
32 | 50 |
|
33 | | - c.push(`export const INLINED_MODULES = ${JSON.stringify(inlineModules)};`); |
| 51 | + c.push(`export const INLINED_MODULES = true`); |
34 | 52 |
|
35 | | - c.push( |
36 | | - `export const BUILD_ID = ${JSON.stringify( |
37 | | - Math.round(Math.random() * Number.MAX_SAFE_INTEGER) |
38 | | - .toString(36) |
39 | | - .toLowerCase() |
40 | | - )};` |
41 | | - ); |
| 53 | + c.push(...createPageIndex(ctx)); |
42 | 54 |
|
43 | | - return c.join('\n'); |
| 55 | + c.push(createBuildId()); |
| 56 | + |
| 57 | + return esmImports.join('\n') + c.join('\n'); |
| 58 | +} |
| 59 | + |
| 60 | +function createPageIndex(ctx: PluginContext) { |
| 61 | + const c: string[] = []; |
| 62 | + c.push(`export const INDEXES = {`); |
| 63 | + for (const i of ctx.indexes) { |
| 64 | + c.push(` ${JSON.stringify(i.pathname)}: ${JSON.stringify(i)},`); |
| 65 | + } |
| 66 | + c.push(`};`); |
| 67 | + return c; |
| 68 | +} |
| 69 | + |
| 70 | +function createBuildId() { |
| 71 | + return `export const BUILD_ID = ${JSON.stringify( |
| 72 | + Math.round(Math.random() * Number.MAX_SAFE_INTEGER) |
| 73 | + .toString(36) |
| 74 | + .toLowerCase() |
| 75 | + )};`; |
| 76 | +} |
| 77 | + |
| 78 | +function getImportPath(importPath: string) { |
| 79 | + if (importPath.endsWith('.tsx') || importPath.endsWith('.jsx')) { |
| 80 | + return importPath.substring(0, importPath.length - 4); |
| 81 | + } |
| 82 | + if (importPath.endsWith('.ts') || importPath.endsWith('.js')) { |
| 83 | + return importPath.substring(0, importPath.length - 3); |
| 84 | + } |
| 85 | + return importPath; |
44 | 86 | } |
0 commit comments