UNPKG

@mini-umi/preset-umi

Version:
192 lines (190 loc) 6.56 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/commands/ssg.ts var ssg_exports = {}; __export(ssg_exports, { default: () => ssg_default }); module.exports = __toCommonJS(ssg_exports); var import_utils = require("@umijs/utils"); var import_vite = require("vite"); var import_path = require("path"); var import_plugin_vue = __toESM(require("@vitejs/plugin-vue")); var import_plugin_vue_jsx = __toESM(require("@vitejs/plugin-vue-jsx")); var isTest = process.env.VITEST; var virtualFile = "@virtual-file"; var virtualId = "\0" + virtualFile; var nestedVirtualFile = "@nested-virtual-file"; var nestedVirtualId = "\0" + nestedVirtualFile; var base = "./"; globalThis.__vite_test_filename = __filename; globalThis.__vite_test_dirname = __dirname; var ssg_default = (api) => { const cwd = process.cwd(); api.registerCommand({ name: "ssg", async fn() { const viteConfig = { base, plugins: [ (0, import_plugin_vue.default)(), (0, import_plugin_vue_jsx.default)(), { name: "virtual", resolveId(id) { if (id === "@foo") { return id; } }, load(id, options) { const ssrFromOptions = (options == null ? void 0 : options.ssr) ?? false; if (id === "@foo") { return `export default { msg: '${command === "build" && !!ssrBuild !== ssrFromOptions ? `defineConfig ssrBuild !== ssr from load options` : "hi"}' }`; } } }, { name: "virtual-module", resolveId(id) { if (id === virtualFile) { return virtualId; } else if (id === nestedVirtualFile) { return nestedVirtualId; } }, load(id) { if (id === virtualId) { return `export { msg } from "@nested-virtual-file";`; } else if (id === nestedVirtualId) { return `export const msg = "[success] from conventional virtual file"`; } } }, function() { const queryRE = /\?.*$/s; const hashRE = /#.*$/s; const cleanUrl = (url) => url.replace(hashRE, "").replace(queryRE, ""); let config; const virtualId2 = "\0virtual:ssr-vue-built-url"; return { name: "built-url", enforce: "post", configResolved(_config) { config = _config; }, resolveId(id) { if (id === virtualId2) { return id; } }, load(id) { if (id === virtualId2) { return { code: `export const __ssr_vue_processAssetPath = (url) => '${base}' + url`, moduleSideEffects: "no-treeshake" }; } }, transform(code, id) { const cleanId = cleanUrl(id); if (config.build.ssr && (cleanId.endsWith(".js") || cleanId.endsWith(".vue")) && !code.includes("__ssr_vue_processAssetPath")) { return { code: `import { __ssr_vue_processAssetPath } from '${virtualId2}';__ssr_vue_processAssetPath;` + code, sourcemap: null }; } } }; }() ], experimental: { renderBuiltUrl(filename, { hostType, type, ssr }) { if (ssr && type === "asset" && hostType === "js") { return { runtime: `__ssr_vue_processAssetPath(${JSON.stringify(filename)})` }; } } }, build: { minify: false }, ssr: { noExternal: [ "example-external-component" ] }, optimizeDeps: { exclude: ["example-external-component"] }, resolve: { alias: { "@": "../" } } }; await (0, import_vite.build)({ ...viteConfig, configFile: false, base: "./", build: { ssrManifest: true, rollupOptions: { input: (0, import_path.resolve)(cwd, "./.mini-umi-ssr/index.html"), output: { dir: (0, import_path.resolve)(cwd, "./.mini-umi-ssr/static") } } } }); await (0, import_vite.build)({ ...viteConfig, configFile: false, base: "./", build: { rollupOptions: { input: (0, import_path.resolve)(cwd, "./.mini-umi-ssr/entry-server.js") }, outDir: (0, import_path.resolve)(cwd, "./.mini-umi-ssr/server") } }); console.log(); console.log(); console.log(); console.log( import_utils.chalk.greenBright("----------\u6784\u5EFA\u4EA7\u7269\u6210\u529F-----------") ); console.log(); console.log(); console.log( `${import_utils.chalk.yellowBright("\u8BF7\u4F7F\u7528")} ${import_utils.chalk.blueBright("npm run preview")} ${import_utils.chalk.yellowBright("\u9884\u89C8")}` ); console.log(); console.log(); } }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = {}); //# sourceMappingURL=ssg.js.map