({
distDir,
config,
}: {
distDir: string
config: NextConfigComplete
})
| 10 | const glob = promisify(globOriginal) |
| 11 | |
| 12 | export async function inlineStaticEnv({ |
| 13 | distDir, |
| 14 | config, |
| 15 | }: { |
| 16 | distDir: string |
| 17 | config: NextConfigComplete |
| 18 | }) { |
| 19 | const nextConfigEnv = getNextConfigEnv(config) |
| 20 | const staticEnv = getStaticEnv(config, config.deploymentId) |
| 21 | |
| 22 | const serverDir = path.join(distDir, 'server') |
| 23 | const serverChunks = await glob('**/*.{js,json,js.map}', { |
| 24 | cwd: serverDir, |
| 25 | }) |
| 26 | const clientDir = path.join(distDir, 'static') |
| 27 | const clientChunks = await glob('**/*.{js,json,js.map}', { |
| 28 | cwd: clientDir, |
| 29 | }) |
| 30 | const manifestChunks = await glob('*.{js,json,js.map}', { |
| 31 | cwd: distDir, |
| 32 | }) |
| 33 | |
| 34 | const inlineSema = new Sema(8) |
| 35 | const nextConfigEnvKeys = Object.keys(nextConfigEnv).map((item) => |
| 36 | item.split('process.env.').pop() |
| 37 | ) |
| 38 | |
| 39 | const builtRegEx = new RegExp( |
| 40 | `[\\w]{1,}(\\.env)?\\.(?:NEXT_PUBLIC_[\\w]{1,}${nextConfigEnvKeys.length ? '|' + nextConfigEnvKeys.join('|') : ''})`, |
| 41 | 'g' |
| 42 | ) |
| 43 | const changedClientFiles: Array<{ file: string; content: string }> = [] |
| 44 | const filesToCheck = new Set<string>( |
| 45 | manifestChunks.map((f) => path.join(distDir, f)) |
| 46 | ) |
| 47 | |
| 48 | for (const [parentDir, files] of [ |
| 49 | [serverDir, serverChunks], |
| 50 | [clientDir, clientChunks], |
| 51 | ] as const) { |
| 52 | await Promise.all( |
| 53 | files.map(async (file) => { |
| 54 | await inlineSema.acquire() |
| 55 | const filepath = path.join(parentDir, file) |
| 56 | const content = await fs.promises.readFile(filepath, 'utf8') |
| 57 | const newContent = content.replace(builtRegEx, (match) => { |
| 58 | let normalizedMatch = `process.env.${match.split('.').pop()}` |
| 59 | |
| 60 | if (staticEnv[normalizedMatch]) { |
| 61 | return JSON.stringify(staticEnv[normalizedMatch]) |
| 62 | } |
| 63 | return match |
| 64 | }) |
| 65 | |
| 66 | await fs.promises.writeFile(filepath, newContent) |
| 67 | |
| 68 | if (content !== newContent && parentDir === clientDir) { |
| 69 | changedClientFiles.push({ file, content: newContent }) |
nothing calls this directly
no test coverage detected