1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
22 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
23 | };
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | exports.prepareFiles = void 0;
|
26 | const fast_glob_1 = __importDefault(require("fast-glob"));
|
27 | const fs_extra_1 = require("fs-extra");
|
28 | const store_1 = require("./store");
|
29 | const path_1 = __importDefault(require("path"));
|
30 | const load_routes_1 = require("./utils/load-routes");
|
31 | const normalize_path_1 = require("./utils/normalize-path");
|
32 | const isAbsolutPath = (p) => /^\/|[a-zA-Z]:/.test(p);
|
33 | async function prepareFiles(api) {
|
34 | const pagesDir = api.resolveSrcDir('pages');
|
35 | const routesFilePattern = '**/*.{vue,ts,tsx,js,jsx}';
|
36 | if (!(await fs_extra_1.pathExists(pagesDir))) {
|
37 | throw new Error(`${pagesDir} doesn't exist`);
|
38 | }
|
39 | const files = await fast_glob_1.default(routesFilePattern, {
|
40 | cwd: pagesDir,
|
41 | onlyFiles: true,
|
42 | ignore: ['node_modules', 'dist'],
|
43 | });
|
44 | const writeRoutes = async () => {
|
45 | const routesInfo = load_routes_1.filesToRoutes(files, pagesDir);
|
46 | const getRelativePathToTemplatesDir = (p) => {
|
47 | if (!isAbsolutPath(p)) {
|
48 | return p;
|
49 | }
|
50 | return normalize_path_1.normalizePath(path_1.default.relative(api.resolveDotReam('templates'), p));
|
51 | };
|
52 | const stringifyClientRoutes = (routes) => {
|
53 | const clientRoutes = routes.filter((route) => !route.isServerRoute);
|
54 | return `[
|
55 | ${clientRoutes
|
56 | .map((route) => {
|
57 | return `{
|
58 | path: "${route.path}",
|
59 | ${route.routeName ? `name: "${route.routeName}",` : ``}
|
60 | component: function() {
|
61 | return import("${getRelativePathToTemplatesDir(route.file)}")
|
62 | .then(wrapPage)
|
63 | },
|
64 | ${route.children
|
65 | ? `children: ${stringifyClientRoutes(route.children)}`
|
66 | : ``}
|
67 | }`;
|
68 | })
|
69 | .join(',')}${clientRoutes.length === 0 ? '' : ','}
|
70 | // Adding a 404 route to suppress vue-router warning
|
71 | {
|
72 | name: '404',
|
73 | path: '/:404(.*)',
|
74 | component: import.meta.env.DEV ? {
|
75 | render() {
|
76 | return h('h1','error: this component should not be rendered')
|
77 | }
|
78 | } : {}
|
79 | }
|
80 | ]`;
|
81 | };
|
82 | const stringifyServerRoutes = (routes) => {
|
83 | const serverRoutes = routes.filter((route) => route.isServerRoute);
|
84 | return `[
|
85 | ${serverRoutes
|
86 | .map((route) => {
|
87 | return `{
|
88 | path: "${route.path}",
|
89 | meta: {load: () => import("${getRelativePathToTemplatesDir(route.file)}")},
|
90 | component: {}
|
91 | }`;
|
92 | })
|
93 | .join(',')}${serverRoutes.length === 0 ? '' : ','}
|
94 | {
|
95 | name: '404',
|
96 | path: '/:404(.*)',
|
97 | component: {}
|
98 | }
|
99 | ]`;
|
100 | };
|
101 |
|
102 | const sharedExportsContent = `
|
103 | import { h, defineAsyncComponent } from 'vue'
|
104 |
|
105 | var ErrorComponent = defineAsyncComponent(function() {
|
106 | return import("${getRelativePathToTemplatesDir(routesInfo.errorFile)}")
|
107 | })
|
108 |
|
109 | var AppComponent = defineAsyncComponent(function() {
|
110 | return import("${getRelativePathToTemplatesDir(routesInfo.appFile)}")
|
111 | })
|
112 |
|
113 | var NotFoundComponent = defineAsyncComponent(function() {
|
114 | return import("${getRelativePathToTemplatesDir(routesInfo.notFoundFile)}")
|
115 | })
|
116 |
|
117 | var wrapPage = function(page) {
|
118 | return {
|
119 | name: 'PageWrapper',
|
120 | $$preload: page.preload,
|
121 | $$staticPreload: page.staticPreload,
|
122 | $$getStaticPaths: page.getStaticPaths,
|
123 | setup: function () {
|
124 | return function() {
|
125 | var Component = page.default
|
126 | return h(Component)
|
127 | }
|
128 | }
|
129 | }
|
130 | }
|
131 |
|
132 | var clientRoutes = ${stringifyClientRoutes(routesInfo.routes)}
|
133 |
|
134 | export {
|
135 | clientRoutes,
|
136 | ErrorComponent,
|
137 | AppComponent,
|
138 | NotFoundComponent
|
139 | }
|
140 | `;
|
141 | await fs_extra_1.outputFile(api.resolveDotReam('templates/shared-exports.js'), sharedExportsContent, 'utf8');
|
142 | const serverExportsContent = `
|
143 | export const serverRoutes = ${stringifyServerRoutes(routesInfo.routes)}
|
144 |
|
145 | export const _document = () => import("${routesInfo.documentFile}")
|
146 | `;
|
147 | await fs_extra_1.outputFile(api.resolveDotReam('templates/server-exports.js'), serverExportsContent, 'utf8');
|
148 | await fs_extra_1.outputFile(api.resolveDotReam('templates/global-imports.js'), `
|
149 | ${api.config.imports
|
150 | .map((file) => `import ${JSON.stringify(file)}`)
|
151 | .join('\n')}
|
152 | `, 'utf8');
|
153 | const enhanceAppFiles = [...store_1.store.state.pluginsFiles['enhance-app']];
|
154 | await fs_extra_1.outputFile(api.resolveDotReam('templates/enhance-app.js'), `
|
155 | ${enhanceAppFiles
|
156 | .map((file, index) => {
|
157 | return `import * as enhanceApp_${index} from "${file}"`;
|
158 | })
|
159 | .join('\n')}
|
160 |
|
161 | var files = [
|
162 | ${enhanceAppFiles.map((_, i) => `enhanceApp_${i}`).join(',')}
|
163 | ]
|
164 |
|
165 | var exec = function(name, context) {
|
166 | for (var i = 0; i < files.length; i++) {
|
167 | var mod = files[i]
|
168 | if (mod[name]) {
|
169 | mod[name](context)
|
170 | }
|
171 | }
|
172 | }
|
173 |
|
174 | export function onCreatedApp(context) {
|
175 | exec('onCreatedApp', context)
|
176 | }
|
177 | `, 'utf8');
|
178 | };
|
179 | await writeRoutes();
|
180 | if (!api.isDev) {
|
181 | const writeConfig = async () => {
|
182 | const config = {
|
183 | port: api.config.server.port,
|
184 | };
|
185 | await fs_extra_1.outputFile(api.resolveDotReam('meta/config.json'), JSON.stringify(config), 'utf8');
|
186 | };
|
187 | await writeConfig();
|
188 | }
|
189 | if (api.isDev) {
|
190 | const { watch } = await Promise.resolve().then(() => __importStar(require('chokidar')));
|
191 | watch(routesFilePattern, {
|
192 | cwd: pagesDir,
|
193 | ignoreInitial: true,
|
194 | })
|
195 | .on('add', async (file) => {
|
196 | files.push(file);
|
197 | await writeRoutes();
|
198 | })
|
199 | .on('unlink', async (file) => {
|
200 | files.splice(files.indexOf(file), 1);
|
201 | await writeRoutes();
|
202 | });
|
203 | }
|
204 | }
|
205 | exports.prepareFiles = prepareFiles;
|