|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import { dirname, join, relative } from 'path'; |
| 3 | +import { |
| 4 | + debug, |
| 5 | + download, |
| 6 | + EdgeFunction, |
| 7 | + execCommand, |
| 8 | + getEnvForPackageManager, |
| 9 | + getNodeVersion, |
| 10 | + getSpawnOptions, |
| 11 | + glob, |
| 12 | + readConfigFile, |
| 13 | + runNpmInstall, |
| 14 | + runPackageJsonScript, |
| 15 | + scanParentDirs, |
| 16 | +} from '@vercel/build-utils'; |
| 17 | +import type { BuildV2, PackageJson } from '@vercel/build-utils'; |
| 18 | + |
| 19 | +export const build: BuildV2 = async ({ |
| 20 | + entrypoint, |
| 21 | + files, |
| 22 | + workPath, |
| 23 | + config, |
| 24 | + meta = {}, |
| 25 | +}) => { |
| 26 | + const { installCommand, buildCommand } = config; |
| 27 | + |
| 28 | + await download(files, workPath, meta); |
| 29 | + |
| 30 | + const mountpoint = dirname(entrypoint); |
| 31 | + const entrypointDir = join(workPath, mountpoint); |
| 32 | + |
| 33 | + // Run "Install Command" |
| 34 | + const nodeVersion = await getNodeVersion( |
| 35 | + entrypointDir, |
| 36 | + undefined, |
| 37 | + config, |
| 38 | + meta |
| 39 | + ); |
| 40 | + |
| 41 | + const spawnOpts = getSpawnOptions(meta, nodeVersion); |
| 42 | + const { cliType, lockfileVersion } = await scanParentDirs(entrypointDir); |
| 43 | + |
| 44 | + spawnOpts.env = getEnvForPackageManager({ |
| 45 | + cliType, |
| 46 | + lockfileVersion, |
| 47 | + nodeVersion, |
| 48 | + env: spawnOpts.env || {}, |
| 49 | + }); |
| 50 | + |
| 51 | + if (typeof installCommand === 'string') { |
| 52 | + if (installCommand.trim()) { |
| 53 | + console.log(`Running "install" command: \`${installCommand}\`...`); |
| 54 | + await execCommand(installCommand, { |
| 55 | + ...spawnOpts, |
| 56 | + cwd: entrypointDir, |
| 57 | + }); |
| 58 | + } else { |
| 59 | + console.log(`Skipping "install" command...`); |
| 60 | + } |
| 61 | + } else { |
| 62 | + await runNpmInstall(entrypointDir, [], spawnOpts, meta, nodeVersion); |
| 63 | + } |
| 64 | + |
| 65 | + // Copy the edge entrypoint file into `.vercel/cache` |
| 66 | + const edgeEntryDir = join(workPath, '.vercel/cache/hydrogen'); |
| 67 | + const edgeEntryRelative = relative(edgeEntryDir, workPath); |
| 68 | + const edgeEntryDest = join(edgeEntryDir, 'edge-entry.js'); |
| 69 | + let edgeEntryContents = await fs.readFile( |
| 70 | + join(__dirname, '..', 'edge-entry.js'), |
| 71 | + 'utf8' |
| 72 | + ); |
| 73 | + edgeEntryContents = edgeEntryContents.replace( |
| 74 | + /__RELATIVE__/g, |
| 75 | + edgeEntryRelative |
| 76 | + ); |
| 77 | + await fs.mkdir(edgeEntryDir, { recursive: true }); |
| 78 | + await fs.writeFile(edgeEntryDest, edgeEntryContents); |
| 79 | + |
| 80 | + // Make `shopify hydrogen build` output a Edge Function compatible bundle |
| 81 | + spawnOpts.env.SHOPIFY_FLAG_BUILD_TARGET = 'worker'; |
| 82 | + |
| 83 | + // Use this file as the entrypoint for the Edge Function bundle build |
| 84 | + spawnOpts.env.SHOPIFY_FLAG_BUILD_SSR_ENTRY = edgeEntryDest; |
| 85 | + |
| 86 | + // Run "Build Command" |
| 87 | + if (buildCommand) { |
| 88 | + debug(`Executing build command "${buildCommand}"`); |
| 89 | + await execCommand(buildCommand, { |
| 90 | + ...spawnOpts, |
| 91 | + cwd: entrypointDir, |
| 92 | + }); |
| 93 | + } else { |
| 94 | + const pkg = await readConfigFile<PackageJson>( |
| 95 | + join(entrypointDir, 'package.json') |
| 96 | + ); |
| 97 | + if (hasScript('vercel-build', pkg)) { |
| 98 | + debug(`Executing "yarn vercel-build"`); |
| 99 | + await runPackageJsonScript(entrypointDir, 'vercel-build', spawnOpts); |
| 100 | + } else if (hasScript('build', pkg)) { |
| 101 | + debug(`Executing "yarn build"`); |
| 102 | + await runPackageJsonScript(entrypointDir, 'build', spawnOpts); |
| 103 | + } else { |
| 104 | + await execCommand('shopify hydrogen build', { |
| 105 | + ...spawnOpts, |
| 106 | + cwd: entrypointDir, |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + const [staticFiles, edgeFunctionFiles] = await Promise.all([ |
| 112 | + glob('**', join(entrypointDir, 'dist/client')), |
| 113 | + glob('**', join(entrypointDir, 'dist/worker')), |
| 114 | + ]); |
| 115 | + |
| 116 | + const edgeFunction = new EdgeFunction({ |
| 117 | + name: 'hydrogen', |
| 118 | + deploymentTarget: 'v8-worker', |
| 119 | + entrypoint: 'index.js', |
| 120 | + files: edgeFunctionFiles, |
| 121 | + }); |
| 122 | + |
| 123 | + // The `index.html` file is a template, but we want to serve the |
| 124 | + // SSR version instead, so omit this static file from the output |
| 125 | + delete staticFiles['index.html']; |
| 126 | + |
| 127 | + return { |
| 128 | + routes: [ |
| 129 | + { |
| 130 | + handle: 'filesystem', |
| 131 | + }, |
| 132 | + { |
| 133 | + src: '/(.*)', |
| 134 | + dest: '/hydrogen', |
| 135 | + }, |
| 136 | + ], |
| 137 | + output: { |
| 138 | + hydrogen: edgeFunction, |
| 139 | + ...staticFiles, |
| 140 | + }, |
| 141 | + }; |
| 142 | +}; |
| 143 | + |
| 144 | +function hasScript(scriptName: string, pkg: PackageJson | null) { |
| 145 | + const scripts = pkg?.scripts || {}; |
| 146 | + return typeof scripts[scriptName] === 'string'; |
| 147 | +} |
0 commit comments