|
| 1 | +import { copy, pathExists } from "fs-extra"; |
| 2 | +import { readFile } from "fs/promises"; |
| 3 | +import { join } from "path"; |
| 4 | +import { lt } from "semver"; |
| 5 | +import { findDependency, FrameworkType, relativeRequire, SupportLevel } from ".."; |
| 6 | + |
| 7 | +import { NuxtDependency } from "../nuxt/interfaces"; |
| 8 | +import { nuxtConfigFilesExist } from "../nuxt/utils"; |
| 9 | + |
| 10 | +export const name = "Nuxt"; |
| 11 | +export const support = SupportLevel.Experimental; |
| 12 | +export const type = FrameworkType.MetaFramework; |
| 13 | + |
| 14 | +/** |
| 15 | + * |
| 16 | + * @param dir current directory |
| 17 | + * @return undefined if project is not Nuxt 2, {mayWantBackend: true } otherwise |
| 18 | + */ |
| 19 | +export async function discover(dir: string): Promise<{ mayWantBackend: true } | undefined> { |
| 20 | + if (!(await pathExists(join(dir, "package.json")))) return; |
| 21 | + const nuxtDependency = <NuxtDependency>findDependency("nuxt", { |
| 22 | + cwd: dir, |
| 23 | + depth: 0, |
| 24 | + omitDev: false, |
| 25 | + }); |
| 26 | + |
| 27 | + const version = nuxtDependency?.version; |
| 28 | + const anyConfigFileExists = await nuxtConfigFilesExist(dir); |
| 29 | + |
| 30 | + if (!anyConfigFileExists && !nuxtDependency) return; |
| 31 | + if (version && lt(version, "3.0.0-0")) return { mayWantBackend: true }; |
| 32 | + |
| 33 | + return; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Get the Nuxt app |
| 38 | + * @param cwd |
| 39 | + * @return Nuxt app object |
| 40 | + */ |
| 41 | +async function getNuxtApp(cwd: string): Promise<any> { |
| 42 | + return await relativeRequire(cwd, "nuxt/dist/nuxt.js"); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * |
| 47 | + * @param root nuxt project root |
| 48 | + * @return whether backend is needed or not |
| 49 | + */ |
| 50 | +export async function build(root: string): Promise<{ wantsBackend: boolean }> { |
| 51 | + const nuxt = await getNuxtApp(root); |
| 52 | + |
| 53 | + const nuxtApp = await nuxt.loadNuxt({ |
| 54 | + for: "build", |
| 55 | + rootDir: root, |
| 56 | + }); |
| 57 | + |
| 58 | + const { |
| 59 | + options: { ssr, target }, |
| 60 | + } = await nuxt.build(nuxtApp); |
| 61 | + |
| 62 | + if (ssr === true && target === "server") { |
| 63 | + return { wantsBackend: true }; |
| 64 | + } else { |
| 65 | + // Inform the user that static target is not supported with `ssr: false`, |
| 66 | + // and continue with building for client side as per current Nuxt 2. |
| 67 | + if (ssr === false && target === "static") { |
| 68 | + console.log( |
| 69 | + "Firebase: Nuxt 2: Static target is not supported with `ssr: false`. Please use `target: 'server'` in your `nuxt.config.js` file." |
| 70 | + ); |
| 71 | + console.log("Firebase: Nuxt 2: Bundling only for client side.\n"); |
| 72 | + } |
| 73 | + |
| 74 | + await buildAndGenerate(nuxt, root); |
| 75 | + return { wantsBackend: false }; |
| 76 | + } |
| 77 | +} |
| 78 | +/** |
| 79 | + * Build and generate the Nuxt app |
| 80 | + * |
| 81 | + * @param nuxt nuxt object |
| 82 | + * @param root root directory |
| 83 | + * @return void |
| 84 | + */ |
| 85 | +async function buildAndGenerate(nuxt: any, root: string): Promise<void> { |
| 86 | + const nuxtApp = await nuxt.loadNuxt({ |
| 87 | + for: "start", |
| 88 | + rootDir: root, |
| 89 | + }); |
| 90 | + |
| 91 | + const builder = await nuxt.getBuilder(nuxtApp); |
| 92 | + const generator = new nuxt.Generator(nuxtApp, builder); |
| 93 | + await generator.generate({ build: false, init: true }); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Copy the static files to the destination directory whether it's a static build or server build. |
| 98 | + * @param root |
| 99 | + * @param dest |
| 100 | + */ |
| 101 | +export async function ɵcodegenPublicDirectory(root: string, dest: string) { |
| 102 | + const nuxt = await getNuxtApp(root); |
| 103 | + const nuxtConfig = await nuxt.loadNuxtConfig(); |
| 104 | + const { ssr, target } = nuxtConfig; |
| 105 | + |
| 106 | + // If `target` is set to `static`, copy the generated files |
| 107 | + // to the destination directory (i.e. `/hosting`). |
| 108 | + if (!(ssr === true && target === "server")) { |
| 109 | + const source = |
| 110 | + nuxtConfig?.generate?.dir !== undefined |
| 111 | + ? join(root, nuxtConfig?.generate?.dir) |
| 112 | + : join(root, "dist"); |
| 113 | + |
| 114 | + await copy(source, dest); |
| 115 | + } |
| 116 | + |
| 117 | + // Copy static assets if they exist. |
| 118 | + const staticPath = join(root, "static"); |
| 119 | + if (await pathExists(staticPath)) { |
| 120 | + await copy(staticPath, dest); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +export async function ɵcodegenFunctionsDirectory(sourceDir: string, destDir: string) { |
| 125 | + const packageJsonBuffer = await readFile(join(sourceDir, "package.json")); |
| 126 | + const packageJson = JSON.parse(packageJsonBuffer.toString()); |
| 127 | + |
| 128 | + // Get the nuxt config into an object so we can check the `target` and `ssr` properties. |
| 129 | + const nuxt = await getNuxtApp(sourceDir); |
| 130 | + const nuxtConfig = await nuxt.loadNuxtConfig(); |
| 131 | + |
| 132 | + // When starting the Nuxt 2 server, we need to copy the `.nuxt` to the destination directory (`functions`) |
| 133 | + // with the same folder name (.firebase/<project-name>/functions/.nuxt). |
| 134 | + // This is because `loadNuxt` (called from `firebase-frameworks`) will only look |
| 135 | + // for the `.nuxt` directory in the destination directory. |
| 136 | + await copy(join(sourceDir, ".nuxt"), join(destDir, ".nuxt")); |
| 137 | + |
| 138 | + // When using `SSR: false`, we need to copy the `nuxt.config.js` to the destination directory (`functions`) |
| 139 | + // This is because `loadNuxt` (called from `firebase-frameworks`) will look |
| 140 | + // for the `nuxt.config.js` file in the destination directory. |
| 141 | + if (!nuxtConfig.ssr) { |
| 142 | + const nuxtConfigFile = nuxtConfig._nuxtConfigFile.split("/").pop(); |
| 143 | + await copy(nuxtConfig._nuxtConfigFile, join(destDir, nuxtConfigFile)); |
| 144 | + } |
| 145 | + |
| 146 | + return { packageJson: { ...packageJson }, frameworksEntry: "nuxt" }; |
| 147 | +} |
0 commit comments