{"version":3,"file":"buildPlugin.cjs","sources":["../../../../src/vite/plugins/buildPlugin.ts"],"sourcesContent":["import { performance } from \"node:perf_hooks\";\nimport { dirname, posix, resolve } from \"node:path\";\nimport { copyFile, mkdir } from \"node:fs/promises\";\nimport { existsSync } from \"node:fs\";\n\nimport type { Plugin } from \"vite\";\n\nimport type { ResolvedOptions } from \"../types\";\nimport { createAkteViteCache } from \"../createAkteViteCache\";\nimport { pkg } from \"../../lib/pkg\";\nimport { createDebugger } from \"../../lib/createDebugger\";\n\nlet isServerRestart = false;\n\nconst debug = createDebugger(\"akte:vite:build\", true);\n\nexport const buildPlugin = <TGlobalData>(\n\toptions: ResolvedOptions<TGlobalData>,\n): Plugin | null => {\n\tdebug(\"plugin registered\");\n\n\tlet cache = createAkteViteCache(resolve(options.cacheDir));\n\tlet relativeFilePaths: string[] = [];\n\tlet outDir = \"dist\";\n\n\treturn {\n\t\tname: \"akte:build\",\n\t\tenforce: \"post\",\n\t\tconfig: async (userConfig, env) => {\n\t\t\tif (env.mode === \"test\") {\n\t\t\t\tdebug(\"mode %o detected, skipping rollup config update\", env.mode);\n\n\t\t\t\treturn;\n\t\t\t} else if (env.mode === \"production\" && env.command === \"serve\") {\n\t\t\t\tdebug(\"mode %o detected, skipping rollup config update\", \"preview\");\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdebug(\"updating rollup config...\");\n\n\t\t\tuserConfig.build ||= {};\n\t\t\tuserConfig.build.rollupOptions ||= {};\n\n\t\t\tcache = createAkteViteCache(\n\t\t\t\tresolve(userConfig.root || \".\", options.cacheDir),\n\t\t\t);\n\n\t\t\t// Don't build full app directly in dev mode\n\t\t\tconst indexHTMLPath = resolve(cache.dir.render, \"index.html\");\n\t\t\tif (\n\t\t\t\tenv.mode === \"development\" &&\n\t\t\t\tenv.command === \"serve\" &&\n\t\t\t\texistsSync(indexHTMLPath) &&\n\t\t\t\tisServerRestart\n\t\t\t) {\n\t\t\t\tdebug(\"server restart detected, skipping full build\");\n\t\t\t\tuserConfig.build.rollupOptions.input = {\n\t\t\t\t\t\"index.html\": indexHTMLPath,\n\t\t\t\t};\n\n\t\t\t\tdebug(\"updated rollup config\");\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst then = performance.now();\n\t\t\tconst filePaths = await options.app.buildAll({\n\t\t\t\toutDir: cache.dir.render,\n\t\t\t});\n\t\t\tconst buildTime = Math.ceil(performance.now() - then);\n\n\t\t\tdebug.log(`akte/vite v${pkg.version} built in ${buildTime}ms`);\n\n\t\t\trelativeFilePaths = filePaths.map((filePath) =>\n\t\t\t\tfilePath.replace(/^\\.?\\//, \"\"),\n\t\t\t);\n\n\t\t\tconst input: Record<string, string> = {};\n\t\t\tfor (const filePath of relativeFilePaths) {\n\t\t\t\tif (filePath.endsWith(\".html\")) {\n\t\t\t\t\tinput[filePath] = resolve(cache.dir.render, filePath);\n\t\t\t\t\tdebug(\n\t\t\t\t\t\t\"registered %o as rollup input\",\n\t\t\t\t\t\tposix.join(\n\t\t\t\t\t\t\tuserConfig.root?.replaceAll(\"\\\\\", \"/\") || \".\",\n\t\t\t\t\t\t\toptions.cacheDir,\n\t\t\t\t\t\t\t\"render\",\n\t\t\t\t\t\t\tfilePath,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tuserConfig.build.rollupOptions.input = input;\n\n\t\t\tdebug(\"updated rollup config\");\n\n\t\t\tif (env.mode === \"development\") {\n\t\t\t\tdebug(\"caching globalData, bulkData, and data...\");\n\n\t\t\t\tconst globalData = await options.app.globalDataCache;\n\t\t\t\tawait cache.setAppGlobalData(globalData);\n\n\t\t\t\tconst cachingPromises: Promise<void>[] = [];\n\n\t\t\t\tfor (const file of options.app.files) {\n\t\t\t\t\tcachingPromises.push(cache.setFileDataMap(file));\n\t\t\t\t}\n\n\t\t\t\tawait Promise.all(cachingPromises);\n\n\t\t\t\tdebug(\"cached globalData, bulkData, and data\");\n\t\t\t}\n\n\t\t\tisServerRestart = true;\n\n\t\t\treturn userConfig;\n\t\t},\n\t\tconfigResolved(config) {\n\t\t\toutDir = resolve(config.root, config.build.outDir);\n\t\t},\n\t\tasync generateBundle(_, outputBundle) {\n\t\t\tdebug(\"updating akte bundle...\");\n\n\t\t\tconst operations = [\"fixed html file paths\"];\n\t\t\tif (options.minifyHTML) {\n\t\t\t\toperations.push(\"minified html\");\n\t\t\t}\n\n\t\t\tlet _minify = ((str: string) =>\n\t\t\t\tPromise.resolve(str)) as typeof import(\"html-minifier-terser\").minify;\n\t\t\tif (options.minifyHTML) {\n\t\t\t\ttry {\n\t\t\t\t\t_minify = (await import(\"html-minifier-terser\")).minify;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tdebug.error(\n\t\t\t\t\t\t\"\\nAkte → %o is required to minify HTML, install it or disable the %o option on the Vite plugin\\n\",\n\t\t\t\t\t\t\"html-minifier-terser\",\n\t\t\t\t\t\t\"minifyHTML\",\n\t\t\t\t\t);\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst minify = async (partialBundle: {\n\t\t\t\tsource: string | Uint8Array;\n\t\t\t}): Promise<void> => {\n\t\t\t\tpartialBundle.source = await _minify(\n\t\t\t\t\tpartialBundle.source as string,\n\t\t\t\t\toptions.minifyHTML || {},\n\t\t\t\t);\n\t\t\t};\n\n\t\t\tconst promises: Promise<void>[] = [];\n\t\t\tfor (const bundle of Object.values(outputBundle)) {\n\t\t\t\tif (\n\t\t\t\t\tbundle.type === \"asset\" &&\n\t\t\t\t\ttypeof bundle.source === \"string\" &&\n\t\t\t\t\trelativeFilePaths.find((relativeFilePath) =>\n\t\t\t\t\t\tbundle.fileName.endsWith(relativeFilePath),\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\t// Rewrite filename to be neither relative or absolute\n\t\t\t\t\tbundle.fileName = bundle.fileName.replace(\n\t\t\t\t\t\tnew RegExp(`^${options.cacheDir}\\\\/render\\\\/?`),\n\t\t\t\t\t\t\"\",\n\t\t\t\t\t);\n\n\t\t\t\t\tif (options.minifyHTML) {\n\t\t\t\t\t\tpromises.push(minify(bundle));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdebug(`updated akte bundle: ${operations.join(\", \")}`);\n\t\t},\n\t\tasync writeBundle() {\n\t\t\tconst filePaths = relativeFilePaths.filter(\n\t\t\t\t(filePath) => !filePath.endsWith(\".html\"),\n\t\t\t);\n\n\t\t\tif (!filePaths.length) {\n\t\t\t\tdebug(\"no non-html files to copy\");\n\t\t\t}\n\n\t\t\tdebug(\"copying non-html files to output directory...\");\n\n\t\t\tconst copy = async (filePath: string): Promise<void> => {\n\t\t\t\tconst src = resolve(cache.dir.render, filePath);\n\t\t\t\tconst dest = resolve(outDir, filePath);\n\t\t\t\tconst destDir = dirname(dest);\n\n\t\t\t\tawait mkdir(destDir, { recursive: true });\n\t\t\t\tawait copyFile(src, dest);\n\t\t\t\tdebug(\"copied %o to output directory\", filePath);\n\t\t\t};\n\n\t\t\tconst promises: Promise<void>[] = [];\n\t\t\tfor (const filePath of filePaths) {\n\t\t\t\tpromises.push(copy(filePath));\n\t\t\t}\n\n\t\t\tawait Promise.all(promises);\n\n\t\t\tdebug(\n\t\t\t\t`copied %o non-html ${\n\t\t\t\t\tpromises.length > 1 ? \"files\" : \"file\"\n\t\t\t\t} to output directory`,\n\t\t\t\tpromises.length,\n\t\t\t);\n\t\t},\n\t};\n};\n"],"names":["createDebugger","createAkteViteCache","resolve","existsSync","performance","pkg","posix","dirname","mkdir","copyFile"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,IAAI,kBAAkB;AAEtB,MAAM,QAAQA,eAAe,eAAA,mBAAmB,IAAI;AAEvC,MAAA,cAAc,CAC1B,YACkB;AAClB,QAAM,mBAAmB;AAEzB,MAAI,QAAQC,oBAAAA,oBAAoBC,KAAAA,QAAQ,QAAQ,QAAQ,CAAC;AACzD,MAAI,oBAA8B,CAAA;AAClC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