102 lines
3.8 KiB
JavaScript
102 lines
3.8 KiB
JavaScript
import "./libs/unplugin.mjs";
|
|
import { readFileSync } from "node:fs";
|
|
import { relative, resolve } from "pathe";
|
|
import "@nuxt/kit";
|
|
import { withTrailingSlash, withoutLeadingSlash } from "ufo";
|
|
import { hash } from "ohash";
|
|
import "node:process";
|
|
import escapeStringRegexp from "escape-string-regexp";
|
|
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
import { normalizeViteManifest, precomputeDependencies } from "vue-bundle-renderer";
|
|
import { serialize } from "seroval";
|
|
function parseModuleId(id) {
|
|
const qIndex = id.indexOf("?");
|
|
if (qIndex === -1) return {
|
|
pathname: id,
|
|
search: ""
|
|
};
|
|
return {
|
|
pathname: id.slice(0, qIndex),
|
|
search: id.slice(qIndex)
|
|
};
|
|
}
|
|
const NUXT_COMPONENT_RE = /[?&]nuxt_component=/;
|
|
const MACRO_RE = /[?&]macro=/;
|
|
const VUE_QUERY_RE = /[?&]vue(?:&|$)/;
|
|
const SETUP_QUERY_RE = /[?&]setup(?:=|&|$)/;
|
|
const TYPE_QUERY_RE = /[?&]type=([^&]*)/;
|
|
function isVue(id, opts = {}) {
|
|
const { search } = parseModuleId(id);
|
|
if (id.endsWith(".vue") && !search) return true;
|
|
if (!search) return false;
|
|
if (NUXT_COMPONENT_RE.test(search)) return false;
|
|
if (MACRO_RE.test(search) && (search === "?macro=true" || !opts.type || opts.type.includes("script"))) return true;
|
|
if (!VUE_QUERY_RE.test(search)) return false;
|
|
if (opts.type) {
|
|
const type = SETUP_QUERY_RE.test(search) ? "script" : TYPE_QUERY_RE.exec(search)?.[1];
|
|
if (!type || !opts.type.includes(type)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
const PREFIX = "virtual:nuxt:";
|
|
function toVirtualId(absolutePath, nuxt) {
|
|
return PREFIX + encodeURIComponent(relative(nuxt.options.rootDir, absolutePath));
|
|
}
|
|
function uniq(arr) {
|
|
return Array.from(new Set(arr));
|
|
}
|
|
const IS_CSS_RE = /\.(?:css|scss|sass|postcss|pcss|less|stylus|styl)(?:\?[^.]+)?$/;
|
|
function isCSS(file) {
|
|
return IS_CSS_RE.test(file);
|
|
}
|
|
function hashId(id) {
|
|
return "$id_" + hash(id);
|
|
}
|
|
function toArray(value) {
|
|
return Array.isArray(value) ? value : [value];
|
|
}
|
|
async function writeManifest(ctx, css = []) {
|
|
const { nuxt } = ctx;
|
|
const devClientManifest = {
|
|
"@vite/client": {
|
|
isEntry: true,
|
|
file: "@vite/client",
|
|
css,
|
|
module: true,
|
|
resourceType: "script"
|
|
},
|
|
...nuxt.options.features.noScripts === "all" ? {} : { [ctx.entry]: {
|
|
isEntry: true,
|
|
file: ctx.entry,
|
|
module: true,
|
|
resourceType: "script"
|
|
} }
|
|
};
|
|
const clientDist = resolve(nuxt.options.buildDir, "dist/client");
|
|
const serverDist = resolve(nuxt.options.buildDir, "dist/server");
|
|
const manifestFile = resolve(clientDist, "manifest.json");
|
|
const clientManifest = nuxt.options.dev ? devClientManifest : JSON.parse(readFileSync(manifestFile, "utf-8"));
|
|
const manifestEntries = Object.values(clientManifest);
|
|
const buildAssetsDir = withTrailingSlash(withoutLeadingSlash(nuxt.options.app.buildAssetsDir));
|
|
const BASE_RE = new RegExp(`^${escapeStringRegexp(buildAssetsDir)}`);
|
|
for (const entry of manifestEntries) {
|
|
entry.file &&= entry.file.replace(BASE_RE, "");
|
|
for (const item of ["css", "assets"]) entry[item] &&= entry[item].map((i) => i.replace(BASE_RE, ""));
|
|
}
|
|
await mkdir(serverDist, { recursive: true });
|
|
if (ctx.config.build?.cssCodeSplit === false) {
|
|
for (const entry of manifestEntries) if (entry.file?.endsWith(".css")) {
|
|
const key = relative(ctx.config.root, ctx.entry);
|
|
clientManifest[key].css ||= [];
|
|
clientManifest[key].css.push(entry.file);
|
|
break;
|
|
}
|
|
}
|
|
const manifest = normalizeViteManifest(clientManifest);
|
|
await nuxt.callHook("build:manifest", manifest);
|
|
const precomputed = precomputeDependencies(manifest);
|
|
await writeFile(resolve(serverDist, "client.manifest.mjs"), "export default " + serialize(manifest), "utf8");
|
|
await writeFile(resolve(serverDist, "client.precomputed.mjs"), "export default " + serialize(precomputed), "utf8");
|
|
if (!nuxt.options.dev) await rm(manifestFile, { force: true });
|
|
}
|
|
export { IS_CSS_RE, hashId, isCSS, isVue, parseModuleId, toArray, toVirtualId, uniq, writeManifest };
|