UNPKG

5 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.exportSite = void 0;
7const path_1 = __importDefault(require("path"));
8const fs_extra_1 = __importDefault(require("fs-extra"));
9const consola_1 = __importDefault(require("consola"));
10const chalk_1 = __importDefault(require("chalk"));
11const server_1 = require("@ream/server");
12const promise_queue_1 = require("@egoist/promise-queue");
13const flatten_routes_1 = require("./utils/flatten-routes");
14function getHref(attrs) {
15 const match = /href\s*=\s*(?:"(.*?)"|'(.*?)'|([^\s>]*))/.exec(attrs);
16 return match && (match[1] || match[2] || match[3]);
17}
18const exportSite = async (dotReamDir, fullyExport) => {
19 const ssrManifest = require(path_1.default.join(dotReamDir, 'manifest/ssr-manifest.json'));
20 const serverEntry = require(path_1.default.join(dotReamDir, 'server/server-entry.js')).default;
21 const globalPreload = await serverEntry.getGlobalPreload();
22 // Adding a global `preload` function in `_app.vue` will disable automatic static generation
23 if (globalPreload && !fullyExport)
24 return;
25 const { scripts, styles } = server_1.extractClientManifest(dotReamDir) || {
26 scripts: '',
27 styles: '',
28 };
29 const assets = { cssLinkTags: styles, scriptTags: scripts };
30 const clientRoutes = await flatten_routes_1.flattenRoutes(serverEntry.clientRoutes);
31 const staticPaths = [];
32 const exportInfo = { staticPaths, fallbackPathsRaw: [] };
33 await Promise.all(clientRoutes.map(async (route) => {
34 const shouldExport = route.matched.every((m) => !m.preload);
35 if (!shouldExport) {
36 return;
37 }
38 if (route.path === '/:404(.*)') {
39 staticPaths.push('/404.html');
40 }
41 else if (route.path.includes(':')) {
42 const paths = [];
43 let fallback = false;
44 for (const m of route.matched) {
45 if (m.getStaticPaths) {
46 const res = await m.getStaticPaths();
47 paths.push(...res.paths);
48 if (res.fallback != null) {
49 fallback = res.fallback;
50 }
51 }
52 }
53 if (paths.length === 0) {
54 consola_1.default.warn(`No static paths provided for ${route.path}, skipped exporting`);
55 }
56 for (const path of paths) {
57 const staticPath = route.path
58 .replace('(.*)', '')
59 .replace(/:([^\/]+)/g, (_, p1) => path.params[p1]);
60 staticPaths.push(staticPath);
61 }
62 if (fallback) {
63 exportInfo.fallbackPathsRaw.push(route.path);
64 }
65 }
66 else {
67 staticPaths.push(route.path);
68 }
69 }));
70 await fs_extra_1.default.outputFile(path_1.default.join(dotReamDir, 'meta/export-info.json'), JSON.stringify(exportInfo), 'utf8');
71 const queue = new promise_queue_1.PromiseQueue(async (jobId, url) => {
72 consola_1.default.info(chalk_1.default.dim(jobId));
73 const result = await server_1.render({
74 url,
75 dotReamDir,
76 ssrManifest,
77 serverEntry,
78 assets,
79 exportInfo: {
80 staticPaths: [],
81 // Skip fallback check, force all static paths to render
82 fallbackPathsRaw: clientRoutes.map((r) => r.path),
83 },
84 });
85 // Crawl pages
86 if (fullyExport) {
87 for (const file of result.cacheFiles.keys()) {
88 if (file.endsWith('.html')) {
89 const html = result.cacheFiles.get(file);
90 if (html) {
91 // find all `<a>` tags in exported html files and export links that are not yet exported
92 let match = null;
93 const LINK_RE = /<a ([\s\S]+?)>/gm;
94 while ((match = LINK_RE.exec(html))) {
95 const href = getHref(match[1]);
96 if (href) {
97 const url = new URL(href, 'http://self');
98 if (url.host === 'self') {
99 queue.add(`Exporting ${url.pathname}`, url.pathname);
100 }
101 }
102 }
103 }
104 }
105 }
106 }
107 await server_1.writeCacheFiles(result.cacheFiles);
108 }, { maxConcurrent: 100 });
109 for (const path of staticPaths) {
110 queue.add(`Exporting ${path}`, path);
111 }
112 await queue.run();
113 consola_1.default.success(`Your app has been exported to ${path_1.default.relative(process.cwd(), path_1.default.join(dotReamDir, 'client'))}`);
114};
115exports.exportSite = exportSite;