| import fs from 'fs/promises' |
| import path from 'path' |
|
|
| |
| const PACKAGE_ROOT = path.normalize(path.join(__dirname, '../../..')) |
| const TEMPLATE_FOLDER = path.join(__dirname, 'templates') |
| const TEMPLATES_ESM_FOLDER = path.normalize( |
| path.join(__dirname, '../../dist/esm/build/templates') |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function loadEntrypoint( |
| entrypoint: |
| | 'app-page' |
| | 'app-route' |
| | 'edge-app-route' |
| | 'edge-ssr' |
| | 'edge-ssr-app' |
| | 'middleware' |
| | 'pages' |
| | 'pages-api', |
| replacements: Record<`VAR_${string}`, string>, |
| injections?: Record<string, string>, |
| imports?: Record<string, string | null> |
| ): Promise<string> { |
| const filepath = path.resolve( |
| path.join(TEMPLATES_ESM_FOLDER, `${entrypoint}.js`) |
| ) |
|
|
| let file = await fs.readFile(filepath, 'utf8') |
|
|
| |
| |
| let count = 0 |
| file = file.replaceAll( |
| /from '(\..*)'|import '(\..*)'/g, |
| function (_, fromRequest, importRequest) { |
| count++ |
|
|
| const relative = path |
| .relative( |
| PACKAGE_ROOT, |
| path.resolve(TEMPLATE_FOLDER, fromRequest ?? importRequest) |
| ) |
| |
| .replace(/\\/g, '/') |
|
|
| |
| |
| |
| if (!relative.startsWith('next/')) { |
| throw new Error( |
| `Invariant: Expected relative import to start with "next/", found "${relative}"` |
| ) |
| } |
|
|
| return fromRequest |
| ? `from ${JSON.stringify(relative)}` |
| : `import ${JSON.stringify(relative)}` |
| } |
| ) |
|
|
| |
| |
| |
| |
| if (count === 0) { |
| throw new Error('Invariant: Expected to replace at least one import') |
| } |
|
|
| const replaced = new Set<string>() |
|
|
| |
| |
| file = file.replaceAll( |
| new RegExp( |
| `${Object.keys(replacements) |
| .map((k) => `'${k}'`) |
| .join('|')}`, |
| 'g' |
| ), |
| (match) => { |
| const key = JSON.parse(match.replace(/'/g, `"`)) |
|
|
| if (!(key in replacements)) { |
| throw new Error(`Invariant: Unexpected template variable ${key}`) |
| } |
|
|
| replaced.add(key) |
|
|
| return JSON.stringify(replacements[key]) |
| } |
| ) |
|
|
| |
| let matches = file.match(/VAR_[A-Z_]+/g) |
| if (matches) { |
| throw new Error( |
| `Invariant: Expected to replace all template variables, found ${matches.join( |
| ', ' |
| )}` |
| ) |
| } |
|
|
| |
| if (replaced.size !== Object.keys(replacements).length) { |
| |
| |
| |
| const difference = Object.keys(replacements).filter( |
| (key) => !replaced.has(key) |
| ) |
|
|
| throw new Error( |
| `Invariant: Expected to replace all template variables, missing ${difference.join( |
| ', ' |
| )} in template` |
| ) |
| } |
|
|
| |
| const injected = new Set<string>() |
| if (injections) { |
| |
| file = file.replaceAll( |
| new RegExp(`// INJECT:(${Object.keys(injections).join('|')})`, 'g'), |
| (_, key) => { |
| if (!(key in injections)) { |
| throw new Error(`Invariant: Unexpected injection ${key}`) |
| } |
|
|
| injected.add(key) |
|
|
| return `const ${key} = ${injections[key]}` |
| } |
| ) |
| } |
|
|
| |
| matches = file.match(/\/\/ INJECT:[A-Za-z0-9_]+/g) |
| if (matches) { |
| throw new Error( |
| `Invariant: Expected to inject all injections, found ${matches.join( |
| ', ' |
| )}` |
| ) |
| } |
|
|
| |
| if (injected.size !== Object.keys(injections ?? {}).length) { |
| |
| |
| |
| const difference = Object.keys(injections ?? {}).filter( |
| (key) => !injected.has(key) |
| ) |
|
|
| throw new Error( |
| `Invariant: Expected to inject all injections, missing ${difference.join( |
| ', ' |
| )} in template` |
| ) |
| } |
|
|
| |
| const importsAdded = new Set<string>() |
| if (imports) { |
| |
| file = file.replaceAll( |
| new RegExp( |
| `// OPTIONAL_IMPORT:(\\* as )?(${Object.keys(imports).join('|')})`, |
| 'g' |
| ), |
| (_, asNamespace = '', key) => { |
| if (!(key in imports)) { |
| throw new Error(`Invariant: Unexpected optional import ${key}`) |
| } |
|
|
| importsAdded.add(key) |
|
|
| if (imports[key]) { |
| return `import ${asNamespace}${key} from ${JSON.stringify( |
| imports[key] |
| )}` |
| } else { |
| return `const ${key} = null` |
| } |
| } |
| ) |
| } |
|
|
| |
| matches = file.match(/\/\/ OPTIONAL_IMPORT:(\* as )?[A-Za-z0-9_]+/g) |
| if (matches) { |
| throw new Error( |
| `Invariant: Expected to inject all imports, found ${matches.join(', ')}` |
| ) |
| } |
|
|
| |
| if (importsAdded.size !== Object.keys(imports ?? {}).length) { |
| |
| |
| |
| const difference = Object.keys(imports ?? {}).filter( |
| (key) => !importsAdded.has(key) |
| ) |
|
|
| throw new Error( |
| `Invariant: Expected to inject all imports, missing ${difference.join( |
| ', ' |
| )} in template` |
| ) |
| } |
|
|
| return file |
| } |
|
|