@blinkk/root 1.0.0-beta.6 → 1.0.0-beta.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/root.js +4 -0
- package/dist/chunk-AJF5RXXV.js +182 -0
- package/dist/chunk-AJF5RXXV.js.map +1 -0
- package/dist/{chunk-WVRC46JG.js → chunk-DXD5LKU3.js} +18 -18
- package/dist/chunk-DXD5LKU3.js.map +1 -0
- package/dist/{chunk-WTSNW7BB.js → chunk-HQXIHYDN.js} +1 -1
- package/dist/chunk-HQXIHYDN.js.map +1 -0
- package/dist/{chunk-DTEQ2AIW.js → chunk-QKBMWK5B.js} +1 -1
- package/dist/chunk-QKBMWK5B.js.map +1 -0
- package/dist/{chunk-LTSJAEBG.js → chunk-SDT4J6VY.js} +18 -6
- package/dist/chunk-SDT4J6VY.js.map +1 -0
- package/dist/chunk-XG7BJNHU.js +1289 -0
- package/dist/chunk-XG7BJNHU.js.map +1 -0
- package/dist/cli.d.ts +14 -4
- package/dist/cli.js +12 -1101
- package/dist/cli.js.map +1 -1
- package/dist/core.d.ts +10 -7
- package/dist/core.js +14 -13
- package/dist/core.js.map +1 -1
- package/dist/functions.d.ts +14 -0
- package/dist/functions.js +30 -0
- package/dist/functions.js.map +1 -0
- package/dist/middleware.d.ts +34 -0
- package/dist/middleware.js +17 -0
- package/dist/middleware.js.map +1 -0
- package/dist/node.d.ts +1 -1
- package/dist/node.js +2 -2
- package/dist/render.d.ts +1 -1
- package/dist/render.js +427 -226
- package/dist/render.js.map +1 -1
- package/dist/{types-47b9b530.d.ts → types-b5510320.d.ts} +158 -34
- package/package.json +40 -4
- package/dist/chunk-DTEQ2AIW.js.map +0 -1
- package/dist/chunk-LTSJAEBG.js.map +0 -1
- package/dist/chunk-WTSNW7BB.js.map +0 -1
- package/dist/chunk-WVRC46JG.js.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -1,1105 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
loadJson,
|
|
15
|
-
loadRootConfig,
|
|
16
|
-
makeDir,
|
|
17
|
-
rmDir,
|
|
18
|
-
writeFile
|
|
19
|
-
} from "./chunk-LTSJAEBG.js";
|
|
20
|
-
import {
|
|
21
|
-
configureServerPlugins,
|
|
22
|
-
getVitePlugins
|
|
23
|
-
} from "./chunk-DTEQ2AIW.js";
|
|
24
|
-
|
|
25
|
-
// src/cli/commands/build.ts
|
|
26
|
-
import path3 from "node:path";
|
|
27
|
-
import { fileURLToPath } from "node:url";
|
|
28
|
-
import fsExtra from "fs-extra";
|
|
29
|
-
import glob2 from "tiny-glob";
|
|
30
|
-
import { build as viteBuild } from "vite";
|
|
31
|
-
|
|
32
|
-
// src/render/asset-map/build-asset-map.ts
|
|
33
|
-
import fs from "node:fs";
|
|
34
|
-
import path from "node:path";
|
|
35
|
-
var BuildAssetMap = class {
|
|
36
|
-
constructor(rootConfig) {
|
|
37
|
-
this.rootConfig = rootConfig;
|
|
38
|
-
this.srcToAsset = /* @__PURE__ */ new Map();
|
|
39
|
-
}
|
|
40
|
-
async get(src) {
|
|
41
|
-
const asset = this.srcToAsset.get(src);
|
|
42
|
-
if (asset) {
|
|
43
|
-
return asset;
|
|
44
|
-
}
|
|
45
|
-
const realSrc = realPathRelativeTo(this.rootConfig.rootDir, src);
|
|
46
|
-
if (realSrc !== src) {
|
|
47
|
-
const asset2 = this.srcToAsset.get(realSrc);
|
|
48
|
-
if (asset2) {
|
|
49
|
-
return asset2;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
console.log(`could not find build asset: ${src}`);
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
add(asset) {
|
|
56
|
-
this.srcToAsset.set(asset.src, asset);
|
|
57
|
-
}
|
|
58
|
-
toJson() {
|
|
59
|
-
const result = {};
|
|
60
|
-
for (const src of this.srcToAsset.keys()) {
|
|
61
|
-
result[src] = this.srcToAsset.get(src).toJson();
|
|
62
|
-
}
|
|
63
|
-
return result;
|
|
64
|
-
}
|
|
65
|
-
static fromViteManifest(rootConfig, clientManifest, elementsGraph) {
|
|
66
|
-
const assetMap = new BuildAssetMap(rootConfig);
|
|
67
|
-
const elementFiles = /* @__PURE__ */ new Set();
|
|
68
|
-
Object.values(elementsGraph.sourceFiles).forEach((elementSource) => {
|
|
69
|
-
elementFiles.add(elementSource.relPath);
|
|
70
|
-
const realSrc = realPathRelativeTo(
|
|
71
|
-
rootConfig.rootDir,
|
|
72
|
-
elementSource.relPath
|
|
73
|
-
);
|
|
74
|
-
if (realSrc !== elementSource.filePath) {
|
|
75
|
-
elementFiles.add(realSrc);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
Object.keys(clientManifest).forEach((manifestKey) => {
|
|
79
|
-
const src = manifestKey;
|
|
80
|
-
const manifestChunk = clientManifest[manifestKey];
|
|
81
|
-
const isElement = elementFiles.has(src);
|
|
82
|
-
const assetUrl = src.startsWith("routes/") && isJsFile(src) ? "" : `/${manifestChunk.file}`;
|
|
83
|
-
const assetData = {
|
|
84
|
-
src,
|
|
85
|
-
assetUrl,
|
|
86
|
-
importedModules: manifestChunk.imports || [],
|
|
87
|
-
importedCss: (manifestChunk.css || []).map((relPath) => `/${relPath}`),
|
|
88
|
-
isElement
|
|
89
|
-
};
|
|
90
|
-
assetMap.add(new BuildAsset(assetMap, assetData));
|
|
91
|
-
});
|
|
92
|
-
return assetMap;
|
|
93
|
-
}
|
|
94
|
-
static fromRootManifest(rootConfig, rootManifest) {
|
|
95
|
-
const assetMap = new BuildAssetMap(rootConfig);
|
|
96
|
-
Object.keys(rootManifest).forEach((moduleId) => {
|
|
97
|
-
const assetData = rootManifest[moduleId];
|
|
98
|
-
assetMap.add(new BuildAsset(assetMap, assetData));
|
|
99
|
-
});
|
|
100
|
-
return assetMap;
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
function realPathRelativeTo(rootDir, src) {
|
|
104
|
-
const fullPath = path.resolve(rootDir, src);
|
|
105
|
-
if (!fs.existsSync(fullPath)) {
|
|
106
|
-
return src;
|
|
107
|
-
}
|
|
108
|
-
const realpath = fs.realpathSync(path.resolve(rootDir, src));
|
|
109
|
-
return path.relative(rootDir, realpath);
|
|
110
|
-
}
|
|
111
|
-
var BuildAsset = class {
|
|
112
|
-
constructor(assetMap, assetData) {
|
|
113
|
-
this.assetMap = assetMap;
|
|
114
|
-
this.src = assetData.src;
|
|
115
|
-
this.assetUrl = assetData.assetUrl;
|
|
116
|
-
this.importedModules = assetData.importedModules;
|
|
117
|
-
this.importedCss = assetData.importedCss;
|
|
118
|
-
this.isElement = assetData.isElement;
|
|
119
|
-
}
|
|
120
|
-
async getCssDeps() {
|
|
121
|
-
const visited = /* @__PURE__ */ new Set();
|
|
122
|
-
const deps = /* @__PURE__ */ new Set();
|
|
123
|
-
await this.collectCss(this, deps, visited);
|
|
124
|
-
return Array.from(deps);
|
|
125
|
-
}
|
|
126
|
-
async getJsDeps() {
|
|
127
|
-
const visited = /* @__PURE__ */ new Set();
|
|
128
|
-
const deps = /* @__PURE__ */ new Set();
|
|
129
|
-
await this.collectJs(this, deps, visited);
|
|
130
|
-
return Array.from(deps);
|
|
131
|
-
}
|
|
132
|
-
async collectJs(asset, urls, visited) {
|
|
133
|
-
if (!asset) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (!asset.src) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (visited.has(asset.src)) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
visited.add(asset.src);
|
|
143
|
-
if (asset.isElement) {
|
|
144
|
-
urls.add(asset.assetUrl);
|
|
145
|
-
}
|
|
146
|
-
await Promise.all(
|
|
147
|
-
asset.importedModules.map(async (src) => {
|
|
148
|
-
const importedAsset = await this.assetMap.get(src);
|
|
149
|
-
this.collectJs(importedAsset, urls, visited);
|
|
150
|
-
})
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
async collectCss(asset, urls, visited) {
|
|
154
|
-
if (!asset) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (!asset.src) {
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
if (visited.has(asset.src)) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
visited.add(asset.src);
|
|
164
|
-
if (asset.importedCss) {
|
|
165
|
-
asset.importedCss.forEach((cssUrl) => urls.add(cssUrl));
|
|
166
|
-
}
|
|
167
|
-
await Promise.all(
|
|
168
|
-
asset.importedModules.map(async (moduleId) => {
|
|
169
|
-
const importedAsset = await this.assetMap.get(moduleId);
|
|
170
|
-
this.collectCss(importedAsset, urls, visited);
|
|
171
|
-
})
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
toJson() {
|
|
175
|
-
return {
|
|
176
|
-
src: this.src,
|
|
177
|
-
assetUrl: this.assetUrl,
|
|
178
|
-
importedModules: this.importedModules,
|
|
179
|
-
importedCss: this.importedCss,
|
|
180
|
-
isElement: this.isElement
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// src/cli/commands/build.ts
|
|
186
|
-
import { dim, cyan } from "kleur/colors";
|
|
187
|
-
|
|
188
|
-
// src/node/element-graph.ts
|
|
189
|
-
import fs2 from "node:fs";
|
|
190
|
-
import path2 from "node:path";
|
|
191
|
-
import { searchForWorkspaceRoot } from "vite";
|
|
192
|
-
import glob from "tiny-glob";
|
|
193
|
-
var ElementGraph = class {
|
|
194
|
-
constructor(sourceFiles) {
|
|
195
|
-
this.sourceFiles = {};
|
|
196
|
-
this.deps = {};
|
|
197
|
-
this.sourceFiles = sourceFiles;
|
|
198
|
-
}
|
|
199
|
-
getDeps(tagName, visited) {
|
|
200
|
-
visited ??= /* @__PURE__ */ new Set();
|
|
201
|
-
if (visited.has(tagName)) {
|
|
202
|
-
return [];
|
|
203
|
-
}
|
|
204
|
-
visited.add(tagName);
|
|
205
|
-
this.deps[tagName] ??= this.parseDepsFromSource(tagName);
|
|
206
|
-
const deps = new Set(this.deps[tagName]);
|
|
207
|
-
for (const depTagName of this.deps[tagName]) {
|
|
208
|
-
for (const childDep of this.getDeps(depTagName, visited)) {
|
|
209
|
-
deps.add(childDep);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
return Array.from(deps);
|
|
213
|
-
}
|
|
214
|
-
parseDepsFromSource(tagName) {
|
|
215
|
-
const srcFile = this.sourceFiles[tagName];
|
|
216
|
-
if (!srcFile) {
|
|
217
|
-
throw new Error(`could not find file path for tagName <${tagName}>`);
|
|
218
|
-
}
|
|
219
|
-
const src = fs2.readFileSync(srcFile.filePath, "utf-8");
|
|
220
|
-
const tagNames = parseTagNames(src);
|
|
221
|
-
const deps = /* @__PURE__ */ new Set();
|
|
222
|
-
for (const depTagName of tagNames) {
|
|
223
|
-
if (depTagName !== tagName && depTagName in this.sourceFiles) {
|
|
224
|
-
deps.add(depTagName);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return Array.from(deps);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
async function getElements(rootConfig) {
|
|
231
|
-
var _a, _b;
|
|
232
|
-
const rootDir = rootConfig.rootDir;
|
|
233
|
-
const workspaceRoot = searchForWorkspaceRoot(rootDir);
|
|
234
|
-
const elementsDirs = [path2.join(rootDir, "elements")];
|
|
235
|
-
const elementsInclude = ((_a = rootConfig.elements) == null ? void 0 : _a.include) || [];
|
|
236
|
-
const excludePatterns = ((_b = rootConfig.elements) == null ? void 0 : _b.exclude) || [];
|
|
237
|
-
const excludeElement = (moduleId) => {
|
|
238
|
-
return excludePatterns.some((pattern) => Boolean(moduleId.match(pattern)));
|
|
239
|
-
};
|
|
240
|
-
for (const dirPath of elementsInclude) {
|
|
241
|
-
const elementsDir = path2.resolve(rootDir, dirPath);
|
|
242
|
-
if (!directoryContains(rootDir, elementsDir)) {
|
|
243
|
-
throw new Error(
|
|
244
|
-
`the elements dir (${dirPath}) should be within the project's workspace (${workspaceRoot})`
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
elementsDirs.push(elementsDir);
|
|
248
|
-
}
|
|
249
|
-
const elementFilePaths = {};
|
|
250
|
-
for (const dirPath of elementsDirs) {
|
|
251
|
-
if (await isDirectory(dirPath)) {
|
|
252
|
-
const files = await glob("**/*", { cwd: dirPath });
|
|
253
|
-
files.forEach((file) => {
|
|
254
|
-
const parts = path2.parse(file);
|
|
255
|
-
if (isJsFile(parts.base) && isValidTagName(parts.name)) {
|
|
256
|
-
const tagName = parts.name;
|
|
257
|
-
const filePath = path2.join(dirPath, file);
|
|
258
|
-
const relPath = path2.relative(rootDir, filePath);
|
|
259
|
-
if (!excludeElement(relPath)) {
|
|
260
|
-
elementFilePaths[tagName] = { filePath, relPath };
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
const graph = new ElementGraph(elementFilePaths);
|
|
267
|
-
return graph;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// src/cli/commands/build.ts
|
|
271
|
-
var __dirname = path3.dirname(fileURLToPath(import.meta.url));
|
|
272
|
-
async function build(rootProjectDir, options) {
|
|
273
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
274
|
-
const mode = (options == null ? void 0 : options.mode) || "production";
|
|
275
|
-
process.env.NODE_ENV = mode;
|
|
276
|
-
const rootDir = path3.resolve(rootProjectDir || process.cwd());
|
|
277
|
-
const rootConfig = await loadRootConfig(rootDir, { command: "build" });
|
|
278
|
-
const distDir = path3.join(rootDir, "dist");
|
|
279
|
-
const ssrOnly = (options == null ? void 0 : options.ssrOnly) || false;
|
|
280
|
-
console.log();
|
|
281
|
-
console.log(`${dim("\u2503")} project: ${rootDir}`);
|
|
282
|
-
console.log(`${dim("\u2503")} output: ${distDir}/html`);
|
|
283
|
-
console.log(`${dim("\u2503")} mode: ${mode}`);
|
|
284
|
-
console.log();
|
|
285
|
-
await rmDir(distDir);
|
|
286
|
-
await makeDir(distDir);
|
|
287
|
-
const routeFiles = [];
|
|
288
|
-
if (await isDirectory(path3.join(rootDir, "routes"))) {
|
|
289
|
-
const pageFiles = await glob2("routes/**/*", { cwd: rootDir });
|
|
290
|
-
pageFiles.forEach((file) => {
|
|
291
|
-
const parts = path3.parse(file);
|
|
292
|
-
if (!parts.name.startsWith("_") && isJsFile(parts.base)) {
|
|
293
|
-
routeFiles.push(path3.resolve(rootDir, file));
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
const elementGraph = await getElements(rootConfig);
|
|
298
|
-
const elements = Object.values(elementGraph.sourceFiles).map((sourceFile) => {
|
|
299
|
-
return sourceFile.filePath;
|
|
300
|
-
});
|
|
301
|
-
const bundleScripts = [];
|
|
302
|
-
if (await isDirectory(path3.join(rootDir, "bundles"))) {
|
|
303
|
-
const bundleFiles = await glob2("bundles/*", { cwd: rootDir });
|
|
304
|
-
bundleFiles.forEach((file) => {
|
|
305
|
-
const parts = path3.parse(file);
|
|
306
|
-
if (isJsFile(parts.base)) {
|
|
307
|
-
bundleScripts.push(path3.resolve(rootDir, file));
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
const rootPlugins = rootConfig.plugins || [];
|
|
312
|
-
const viteConfig = rootConfig.vite || {};
|
|
313
|
-
const vitePlugins = [
|
|
314
|
-
...viteConfig.plugins || [],
|
|
315
|
-
...getVitePlugins(rootPlugins)
|
|
316
|
-
];
|
|
317
|
-
const baseConfig = {
|
|
318
|
-
...viteConfig,
|
|
319
|
-
root: rootDir,
|
|
320
|
-
mode,
|
|
321
|
-
esbuild: {
|
|
322
|
-
...viteConfig.esbuild,
|
|
323
|
-
jsx: "automatic",
|
|
324
|
-
jsxImportSource: "preact",
|
|
325
|
-
treeShaking: true
|
|
326
|
-
},
|
|
327
|
-
plugins: vitePlugins
|
|
328
|
-
};
|
|
329
|
-
const ssrInput = {
|
|
330
|
-
render: path3.resolve(__dirname, "./render.js")
|
|
331
|
-
};
|
|
332
|
-
rootPlugins.forEach((plugin) => {
|
|
333
|
-
if (plugin.ssrInput) {
|
|
334
|
-
Object.assign(ssrInput, plugin.ssrInput());
|
|
335
|
-
}
|
|
336
|
-
});
|
|
337
|
-
const noExternalConfig = (_a = viteConfig.ssr) == null ? void 0 : _a.noExternal;
|
|
338
|
-
const noExternal = [];
|
|
339
|
-
if (noExternalConfig) {
|
|
340
|
-
if (Array.isArray(noExternalConfig)) {
|
|
341
|
-
noExternal.push(...noExternalConfig);
|
|
342
|
-
} else {
|
|
343
|
-
noExternal.push(noExternalConfig);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
await viteBuild({
|
|
347
|
-
...baseConfig,
|
|
348
|
-
publicDir: false,
|
|
349
|
-
build: {
|
|
350
|
-
...viteConfig == null ? void 0 : viteConfig.build,
|
|
351
|
-
rollupOptions: {
|
|
352
|
-
...(_b = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _b.rollupOptions,
|
|
353
|
-
input: ssrInput,
|
|
354
|
-
output: {
|
|
355
|
-
format: "esm",
|
|
356
|
-
chunkFileNames: "chunks/[name].[hash].min.js",
|
|
357
|
-
assetFileNames: "assets/[name].[hash][extname]"
|
|
358
|
-
}
|
|
359
|
-
},
|
|
360
|
-
outDir: path3.join(distDir, "server"),
|
|
361
|
-
ssr: true,
|
|
362
|
-
ssrManifest: false,
|
|
363
|
-
cssCodeSplit: true,
|
|
364
|
-
target: "esnext",
|
|
365
|
-
minify: false,
|
|
366
|
-
modulePreload: { polyfill: false },
|
|
367
|
-
reportCompressedSize: false
|
|
368
|
-
},
|
|
369
|
-
ssr: {
|
|
370
|
-
...viteConfig.ssr,
|
|
371
|
-
target: "node",
|
|
372
|
-
noExternal: ["@blinkk/root", ...noExternal]
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
await viteBuild({
|
|
376
|
-
...baseConfig,
|
|
377
|
-
publicDir: false,
|
|
378
|
-
build: {
|
|
379
|
-
...viteConfig == null ? void 0 : viteConfig.build,
|
|
380
|
-
rollupOptions: {
|
|
381
|
-
...(_c = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _c.rollupOptions,
|
|
382
|
-
input: [...routeFiles],
|
|
383
|
-
output: {
|
|
384
|
-
format: "esm",
|
|
385
|
-
entryFileNames: "assets/[name].[hash].min.js",
|
|
386
|
-
chunkFileNames: "chunks/[name].[hash].min.js",
|
|
387
|
-
assetFileNames: "assets/[name].[hash][extname]",
|
|
388
|
-
...(_e = (_d = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _d.rollupOptions) == null ? void 0 : _e.output
|
|
389
|
-
}
|
|
390
|
-
},
|
|
391
|
-
outDir: path3.join(distDir, "routes"),
|
|
392
|
-
ssr: false,
|
|
393
|
-
ssrManifest: false,
|
|
394
|
-
manifest: true,
|
|
395
|
-
cssCodeSplit: true,
|
|
396
|
-
target: "esnext",
|
|
397
|
-
minify: true,
|
|
398
|
-
modulePreload: { polyfill: false },
|
|
399
|
-
reportCompressedSize: false
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
const clientInput = [...elements, ...bundleScripts];
|
|
403
|
-
if (clientInput.length > 0) {
|
|
404
|
-
await viteBuild({
|
|
405
|
-
...baseConfig,
|
|
406
|
-
publicDir: false,
|
|
407
|
-
build: {
|
|
408
|
-
...viteConfig == null ? void 0 : viteConfig.build,
|
|
409
|
-
rollupOptions: {
|
|
410
|
-
...(_f = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _f.rollupOptions,
|
|
411
|
-
input: [...elements, ...bundleScripts],
|
|
412
|
-
output: {
|
|
413
|
-
format: "esm",
|
|
414
|
-
entryFileNames: "assets/[name].[hash].min.js",
|
|
415
|
-
chunkFileNames: "chunks/[name].[hash].min.js",
|
|
416
|
-
assetFileNames: "assets/[name].[hash][extname]",
|
|
417
|
-
...(_h = (_g = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _g.rollupOptions) == null ? void 0 : _h.output
|
|
418
|
-
}
|
|
419
|
-
},
|
|
420
|
-
outDir: path3.join(distDir, "client"),
|
|
421
|
-
ssr: false,
|
|
422
|
-
ssrManifest: false,
|
|
423
|
-
manifest: true,
|
|
424
|
-
cssCodeSplit: true,
|
|
425
|
-
target: "esnext",
|
|
426
|
-
minify: true,
|
|
427
|
-
modulePreload: { polyfill: false },
|
|
428
|
-
reportCompressedSize: false
|
|
429
|
-
}
|
|
430
|
-
});
|
|
431
|
-
} else {
|
|
432
|
-
await writeFile(path3.join(distDir, "client/manifest.json"), "{}");
|
|
433
|
-
}
|
|
434
|
-
await copyGlob(
|
|
435
|
-
"*.css",
|
|
436
|
-
path3.join(distDir, "routes/assets"),
|
|
437
|
-
path3.join(distDir, "client/assets")
|
|
438
|
-
);
|
|
439
|
-
const routesManifest = await loadJson(
|
|
440
|
-
path3.join(distDir, "routes/manifest.json")
|
|
441
|
-
);
|
|
442
|
-
const clientManifest = await loadJson(
|
|
443
|
-
path3.join(distDir, "client/manifest.json")
|
|
444
|
-
);
|
|
445
|
-
function collectRouteCss(asset, cssDeps, visited) {
|
|
446
|
-
if (!asset || !asset.file || visited.has(asset.file)) {
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
visited.add(asset.file);
|
|
450
|
-
if (asset.css) {
|
|
451
|
-
asset.css.forEach((dep) => cssDeps.add(dep));
|
|
452
|
-
}
|
|
453
|
-
if (asset.imports) {
|
|
454
|
-
asset.imports.forEach((manifestKey) => {
|
|
455
|
-
collectRouteCss(routesManifest[manifestKey], cssDeps, visited);
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
Object.keys(routesManifest).forEach((manifestKey) => {
|
|
460
|
-
const asset = routesManifest[manifestKey];
|
|
461
|
-
if (asset.isEntry) {
|
|
462
|
-
const visited = /* @__PURE__ */ new Set();
|
|
463
|
-
const cssDeps = /* @__PURE__ */ new Set();
|
|
464
|
-
collectRouteCss(asset, cssDeps, visited);
|
|
465
|
-
asset.css = Array.from(cssDeps);
|
|
466
|
-
asset.imports = [];
|
|
467
|
-
clientManifest[manifestKey] = asset;
|
|
468
|
-
} else if (asset.file.endsWith(".css")) {
|
|
469
|
-
clientManifest[manifestKey] = asset;
|
|
470
|
-
}
|
|
471
|
-
});
|
|
472
|
-
const assetMap = BuildAssetMap.fromViteManifest(
|
|
473
|
-
rootConfig,
|
|
474
|
-
clientManifest,
|
|
475
|
-
elementGraph
|
|
476
|
-
);
|
|
477
|
-
const rootManifest = assetMap.toJson();
|
|
478
|
-
await writeFile(
|
|
479
|
-
path3.join(distDir, "client/root-manifest.json"),
|
|
480
|
-
JSON.stringify(rootManifest, null, 2)
|
|
481
|
-
);
|
|
482
|
-
const buildDir = path3.join(distDir, "html");
|
|
483
|
-
const publicDir = path3.join(rootDir, "public");
|
|
484
|
-
if (fsExtra.existsSync(path3.join(rootDir, "public"))) {
|
|
485
|
-
fsExtra.copySync(publicDir, buildDir, { dereference: true });
|
|
486
|
-
} else {
|
|
487
|
-
await makeDir(buildDir);
|
|
488
|
-
}
|
|
489
|
-
await makeDir(path3.join(buildDir, "assets"));
|
|
490
|
-
await makeDir(path3.join(buildDir, "chunks"));
|
|
491
|
-
console.log("\njs/css output:");
|
|
492
|
-
await Promise.all(
|
|
493
|
-
Object.keys(rootManifest).map(async (src) => {
|
|
494
|
-
if (isRouteFile(src)) {
|
|
495
|
-
return;
|
|
496
|
-
}
|
|
497
|
-
const assetData = rootManifest[src];
|
|
498
|
-
if (!assetData.assetUrl) {
|
|
499
|
-
return;
|
|
500
|
-
}
|
|
501
|
-
const assetRelPath = assetData.assetUrl.slice(1);
|
|
502
|
-
const assetFrom = path3.join(distDir, "client", assetRelPath);
|
|
503
|
-
const assetTo = path3.join(buildDir, assetRelPath);
|
|
504
|
-
if (!await fileExists(assetFrom)) {
|
|
505
|
-
console.log(`${assetFrom} does not exist`);
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
await fsExtra.copy(assetFrom, assetTo);
|
|
509
|
-
printFileOutput(fileSize(assetTo), "dist/html/", assetRelPath);
|
|
510
|
-
})
|
|
511
|
-
);
|
|
512
|
-
if (!ssrOnly) {
|
|
513
|
-
const render = await import(path3.join(distDir, "server/render.js"));
|
|
514
|
-
const renderer = new render.Renderer(rootConfig, { assetMap, elementGraph });
|
|
515
|
-
const sitemap = await renderer.getSitemap();
|
|
516
|
-
console.log("\nhtml output:");
|
|
517
|
-
await Promise.all(
|
|
518
|
-
Object.keys(sitemap).map(async (urlPath) => {
|
|
519
|
-
const { route, params } = sitemap[urlPath];
|
|
520
|
-
const data = await renderer.renderRoute(route, {
|
|
521
|
-
routeParams: params
|
|
522
|
-
});
|
|
523
|
-
if (data.notFound) {
|
|
524
|
-
return;
|
|
525
|
-
}
|
|
526
|
-
let outFilePath = path3.join(urlPath.slice(1), "index.html");
|
|
527
|
-
if (outFilePath.endsWith("404/index.html")) {
|
|
528
|
-
outFilePath = outFilePath.replace("404/index.html", "404.html");
|
|
529
|
-
}
|
|
530
|
-
const outPath = path3.join(buildDir, outFilePath);
|
|
531
|
-
let html = data.html || "";
|
|
532
|
-
if (rootConfig.prettyHtml !== false) {
|
|
533
|
-
html = await htmlPretty(html, rootConfig.prettyHtmlOptions);
|
|
534
|
-
}
|
|
535
|
-
if (rootConfig.minifyHtml !== false) {
|
|
536
|
-
html = await htmlMinify(html, rootConfig.minifyHtmlOptions);
|
|
537
|
-
}
|
|
538
|
-
await writeFile(outPath, html);
|
|
539
|
-
printFileOutput(fileSize(outPath), "dist/html/", outFilePath);
|
|
540
|
-
})
|
|
541
|
-
);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
function isRouteFile(filepath) {
|
|
545
|
-
return filepath.startsWith("routes") && isJsFile(filepath);
|
|
546
|
-
}
|
|
547
|
-
function fileSize(filepath) {
|
|
548
|
-
const stats = fsExtra.statSync(filepath);
|
|
549
|
-
const bytes = stats.size;
|
|
550
|
-
const k = 1024;
|
|
551
|
-
if (bytes < k) {
|
|
552
|
-
return (bytes / k).toFixed(2) + " kB";
|
|
553
|
-
}
|
|
554
|
-
const units = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
555
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
556
|
-
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + units[i];
|
|
557
|
-
}
|
|
558
|
-
function printFileOutput(fileSize2, outputDir, outputFile) {
|
|
559
|
-
const indent = " ".repeat(2);
|
|
560
|
-
const paddedSize = fileSize2.padStart(9, " ");
|
|
561
|
-
console.log(
|
|
562
|
-
`${indent}${dim(paddedSize)} ${dim(outputDir)}${cyan(outputFile)}`
|
|
563
|
-
);
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
// src/cli/commands/dev.ts
|
|
567
|
-
import path5 from "node:path";
|
|
568
|
-
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
569
|
-
import { default as express } from "express";
|
|
570
|
-
|
|
571
|
-
// src/render/asset-map/dev-asset-map.ts
|
|
572
|
-
import path4 from "node:path";
|
|
573
|
-
import { searchForWorkspaceRoot as searchForWorkspaceRoot2 } from "vite";
|
|
574
|
-
var DevServerAssetMap = class {
|
|
575
|
-
constructor(rootConfig, moduleGraph) {
|
|
576
|
-
this.rootConfig = rootConfig;
|
|
577
|
-
this.moduleGraph = moduleGraph;
|
|
578
|
-
}
|
|
579
|
-
async get(src) {
|
|
580
|
-
const file = path4.resolve(this.rootConfig.rootDir, src);
|
|
581
|
-
const viteModules = this.moduleGraph.getModulesByFile(file);
|
|
582
|
-
if (viteModules && viteModules.size > 0) {
|
|
583
|
-
const [viteModule] = viteModules;
|
|
584
|
-
return new DevServerAsset(src, {
|
|
585
|
-
assetMap: this,
|
|
586
|
-
viteModule
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
if (file.startsWith(this.rootConfig.rootDir)) {
|
|
590
|
-
const assetUrl = file.slice(this.rootConfig.rootDir.length);
|
|
591
|
-
return {
|
|
592
|
-
src,
|
|
593
|
-
assetUrl,
|
|
594
|
-
getCssDeps: async () => [],
|
|
595
|
-
getJsDeps: async () => [assetUrl]
|
|
596
|
-
};
|
|
597
|
-
}
|
|
598
|
-
const workspaceRoot = searchForWorkspaceRoot2(this.rootConfig.rootDir);
|
|
599
|
-
if (await directoryContains(workspaceRoot, file)) {
|
|
600
|
-
const assetUrl = `/@fs/${file}`;
|
|
601
|
-
return {
|
|
602
|
-
src,
|
|
603
|
-
assetUrl,
|
|
604
|
-
getCssDeps: async () => [],
|
|
605
|
-
getJsDeps: async () => [assetUrl]
|
|
606
|
-
};
|
|
607
|
-
}
|
|
608
|
-
console.log(`could not find asset in asset map: ${src}`);
|
|
609
|
-
return null;
|
|
610
|
-
}
|
|
611
|
-
filePathToSrc(file) {
|
|
612
|
-
return path4.relative(this.rootConfig.rootDir, file);
|
|
613
|
-
}
|
|
614
|
-
};
|
|
615
|
-
var DevServerAsset = class {
|
|
616
|
-
constructor(src, options) {
|
|
617
|
-
this.src = src;
|
|
618
|
-
this.assetMap = options.assetMap;
|
|
619
|
-
this.viteModule = options.viteModule;
|
|
620
|
-
this.moduleId = this.viteModule.id;
|
|
621
|
-
this.assetUrl = this.viteModule.url;
|
|
622
|
-
}
|
|
623
|
-
async getCssDeps() {
|
|
624
|
-
const visited = /* @__PURE__ */ new Set();
|
|
625
|
-
const deps = /* @__PURE__ */ new Set();
|
|
626
|
-
this.collectCss(this, deps, visited);
|
|
627
|
-
return Array.from(deps);
|
|
628
|
-
}
|
|
629
|
-
async getJsDeps() {
|
|
630
|
-
const visited = /* @__PURE__ */ new Set();
|
|
631
|
-
const deps = /* @__PURE__ */ new Set();
|
|
632
|
-
this.collectJs(this, deps, visited);
|
|
633
|
-
return Array.from(deps);
|
|
634
|
-
}
|
|
635
|
-
getImportedModules() {
|
|
636
|
-
return this.viteModule.importedModules;
|
|
637
|
-
}
|
|
638
|
-
collectJs(asset, urls, visited) {
|
|
639
|
-
if (!asset) {
|
|
640
|
-
return;
|
|
641
|
-
}
|
|
642
|
-
if (!asset.moduleId) {
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
if (visited.has(asset.moduleId)) {
|
|
646
|
-
return;
|
|
647
|
-
}
|
|
648
|
-
visited.add(asset.moduleId);
|
|
649
|
-
const parts = path4.parse(asset.assetUrl);
|
|
650
|
-
if ([".js", ".jsx", ".ts", ".tsx"].includes(parts.ext) && asset.moduleId.includes("/elements/")) {
|
|
651
|
-
urls.add(asset.assetUrl);
|
|
652
|
-
}
|
|
653
|
-
asset.getImportedModules().forEach((viteModule) => {
|
|
654
|
-
if (viteModule.file) {
|
|
655
|
-
const src = this.assetMap.filePathToSrc(viteModule.file);
|
|
656
|
-
const importedAsset = new DevServerAsset(src, {
|
|
657
|
-
assetMap: this.assetMap,
|
|
658
|
-
viteModule
|
|
659
|
-
});
|
|
660
|
-
this.collectJs(importedAsset, urls, visited);
|
|
661
|
-
}
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
collectCss(asset, urls, visited) {
|
|
665
|
-
if (!asset) {
|
|
666
|
-
return;
|
|
667
|
-
}
|
|
668
|
-
if (!asset.assetUrl) {
|
|
669
|
-
return;
|
|
670
|
-
}
|
|
671
|
-
if (visited.has(asset.assetUrl)) {
|
|
672
|
-
return;
|
|
673
|
-
}
|
|
674
|
-
visited.add(asset.assetUrl);
|
|
675
|
-
if (asset.src.endsWith(".scss")) {
|
|
676
|
-
const parts = path4.parse(asset.src);
|
|
677
|
-
if (!parts.name.startsWith("_")) {
|
|
678
|
-
urls.add(asset.assetUrl);
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
asset.getImportedModules().forEach((viteModule) => {
|
|
682
|
-
if (viteModule.file) {
|
|
683
|
-
const src = this.assetMap.filePathToSrc(viteModule.file);
|
|
684
|
-
const importedAsset = new DevServerAsset(src, {
|
|
685
|
-
assetMap: this.assetMap,
|
|
686
|
-
viteModule
|
|
687
|
-
});
|
|
688
|
-
this.collectCss(importedAsset, urls, visited);
|
|
689
|
-
}
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
|
-
};
|
|
693
|
-
|
|
694
|
-
// src/cli/commands/dev.ts
|
|
695
|
-
import { dim as dim2 } from "kleur/colors";
|
|
696
|
-
|
|
697
|
-
// src/core/middleware.ts
|
|
698
|
-
function rootProjectMiddleware(options) {
|
|
699
|
-
return (req, _, next) => {
|
|
700
|
-
req.rootConfig = options.rootConfig;
|
|
701
|
-
next();
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
|
|
705
|
-
// src/utils/ports.ts
|
|
706
|
-
import { createServer } from "node:net";
|
|
707
|
-
function isPortOpen(port) {
|
|
708
|
-
return new Promise((resolve, reject) => {
|
|
709
|
-
const server = createServer();
|
|
710
|
-
server.on("error", (err) => {
|
|
711
|
-
if (err.code === "EADDRINUSE") {
|
|
712
|
-
resolve(false);
|
|
713
|
-
return;
|
|
714
|
-
}
|
|
715
|
-
reject(err);
|
|
716
|
-
});
|
|
717
|
-
server.on("close", () => {
|
|
718
|
-
resolve(true);
|
|
719
|
-
});
|
|
720
|
-
server.listen(port, () => {
|
|
721
|
-
server.close((err) => {
|
|
722
|
-
if (err) {
|
|
723
|
-
console.log(`error closing server: ${err}`);
|
|
724
|
-
reject(err);
|
|
725
|
-
}
|
|
726
|
-
});
|
|
727
|
-
});
|
|
728
|
-
});
|
|
729
|
-
}
|
|
730
|
-
async function findOpenPort(min, max) {
|
|
731
|
-
let port = min;
|
|
732
|
-
while (port <= max) {
|
|
733
|
-
const isOpen = await isPortOpen(port);
|
|
734
|
-
if (isOpen) {
|
|
735
|
-
return port;
|
|
736
|
-
}
|
|
737
|
-
port += 1;
|
|
738
|
-
}
|
|
739
|
-
throw new Error(`no ports open between ${min} and ${max}`);
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
// src/cli/commands/dev.ts
|
|
743
|
-
import glob3 from "tiny-glob";
|
|
744
|
-
var __dirname2 = path5.dirname(fileURLToPath2(import.meta.url));
|
|
745
|
-
async function dev(rootProjectDir) {
|
|
746
|
-
process.env.NODE_ENV = "development";
|
|
747
|
-
const rootDir = path5.resolve(rootProjectDir || process.cwd());
|
|
748
|
-
const defaultPort = parseInt(process.env.PORT || "4007");
|
|
749
|
-
const port = await findOpenPort(defaultPort, defaultPort + 10);
|
|
750
|
-
console.log();
|
|
751
|
-
console.log(`${dim2("\u2503")} project: ${rootDir}`);
|
|
752
|
-
console.log(`${dim2("\u2503")} server: http://localhost:${port}`);
|
|
753
|
-
console.log(`${dim2("\u2503")} mode: development`);
|
|
754
|
-
console.log();
|
|
755
|
-
const server = await createDevServer({ rootDir, port });
|
|
756
|
-
server.listen(port);
|
|
757
|
-
}
|
|
758
|
-
async function createDevServer(options) {
|
|
759
|
-
const rootDir = path5.resolve((options == null ? void 0 : options.rootDir) || process.cwd());
|
|
760
|
-
const rootConfig = await loadRootConfig(rootDir, { command: "dev" });
|
|
761
|
-
const port = options == null ? void 0 : options.port;
|
|
762
|
-
const server = express();
|
|
763
|
-
server.disable("x-powered-by");
|
|
764
|
-
server.use(rootProjectMiddleware({ rootConfig }));
|
|
765
|
-
server.use(await viteServerMiddleware({ rootConfig, port }));
|
|
766
|
-
const plugins = rootConfig.plugins || [];
|
|
767
|
-
await configureServerPlugins(
|
|
768
|
-
server,
|
|
769
|
-
async () => {
|
|
770
|
-
var _a;
|
|
771
|
-
const userMiddlewares = ((_a = rootConfig.server) == null ? void 0 : _a.middlewares) || [];
|
|
772
|
-
for (const middleware of userMiddlewares) {
|
|
773
|
-
server.use(middleware);
|
|
774
|
-
}
|
|
775
|
-
server.use(rootDevServerMiddleware());
|
|
776
|
-
server.use(rootDevServer404Middleware());
|
|
777
|
-
server.use(rootDevServer500Middleware());
|
|
778
|
-
},
|
|
779
|
-
plugins,
|
|
780
|
-
{ type: "dev", rootConfig }
|
|
781
|
-
);
|
|
782
|
-
return server;
|
|
783
|
-
}
|
|
784
|
-
async function viteServerMiddleware(options) {
|
|
785
|
-
const rootConfig = options.rootConfig;
|
|
786
|
-
const rootDir = rootConfig.rootDir;
|
|
787
|
-
const elementGraph = await getElements(rootConfig);
|
|
788
|
-
const elements = Object.values(elementGraph.sourceFiles).map((sourceFile) => {
|
|
789
|
-
return sourceFile.relPath;
|
|
790
|
-
});
|
|
791
|
-
const bundleScripts = [];
|
|
792
|
-
if (await isDirectory(path5.join(rootDir, "bundles"))) {
|
|
793
|
-
const bundleFiles = await glob3("bundles/*", { cwd: rootDir });
|
|
794
|
-
bundleFiles.forEach((file) => {
|
|
795
|
-
const parts = path5.parse(file);
|
|
796
|
-
if (isJsFile(parts.base)) {
|
|
797
|
-
bundleScripts.push(file);
|
|
798
|
-
}
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
const optimizeDeps = [...elements, ...bundleScripts];
|
|
802
|
-
const viteServer = await createViteServer(rootConfig, {
|
|
803
|
-
port: options.port,
|
|
804
|
-
optimizeDeps
|
|
805
|
-
});
|
|
806
|
-
return async (req, res, next) => {
|
|
807
|
-
try {
|
|
808
|
-
req.viteServer = viteServer;
|
|
809
|
-
const renderModulePath = path5.resolve(__dirname2, "./render.js");
|
|
810
|
-
const render = await viteServer.ssrLoadModule(
|
|
811
|
-
renderModulePath
|
|
812
|
-
);
|
|
813
|
-
const assetMap = new DevServerAssetMap(
|
|
814
|
-
rootConfig,
|
|
815
|
-
viteServer.moduleGraph
|
|
816
|
-
);
|
|
817
|
-
req.renderer = new render.Renderer(rootConfig, { assetMap, elementGraph });
|
|
818
|
-
viteServer.middlewares(req, res, next);
|
|
819
|
-
} catch (e) {
|
|
820
|
-
next(e);
|
|
821
|
-
}
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
function rootDevServerMiddleware() {
|
|
825
|
-
return async (req, res, next) => {
|
|
826
|
-
const renderer = req.renderer;
|
|
827
|
-
const viteServer = req.viteServer;
|
|
828
|
-
try {
|
|
829
|
-
await renderer.handle(req, res, next);
|
|
830
|
-
} catch (err) {
|
|
831
|
-
viteServer.ssrFixStacktrace(err);
|
|
832
|
-
next(err);
|
|
833
|
-
}
|
|
834
|
-
};
|
|
835
|
-
}
|
|
836
|
-
function rootDevServer404Middleware() {
|
|
837
|
-
return async (req, res) => {
|
|
838
|
-
console.error(`\u2753 404 ${req.originalUrl}`);
|
|
839
|
-
if (req.renderer) {
|
|
840
|
-
const url = req.path;
|
|
841
|
-
const ext = path5.extname(url);
|
|
842
|
-
if (!ext) {
|
|
843
|
-
const renderer = req.renderer;
|
|
844
|
-
const data = await renderer.renderDevServer404(req);
|
|
845
|
-
const html = data.html || "";
|
|
846
|
-
res.status(404).set({ "Content-Type": "text/html" }).end(html);
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
res.status(404).set({ "Content-Type": "text/plain" }).end("404");
|
|
851
|
-
};
|
|
852
|
-
}
|
|
853
|
-
function rootDevServer500Middleware() {
|
|
854
|
-
return async (err, req, res, next) => {
|
|
855
|
-
console.error(`\u2757 500 ${req.originalUrl}`);
|
|
856
|
-
console.error(String(err.stack || err));
|
|
857
|
-
if (req.renderer) {
|
|
858
|
-
const url = req.path;
|
|
859
|
-
const ext = path5.extname(url);
|
|
860
|
-
if (!ext) {
|
|
861
|
-
const renderer = req.renderer;
|
|
862
|
-
const data = await renderer.renderDevServer500(req, err);
|
|
863
|
-
const html = data.html || "";
|
|
864
|
-
res.status(500).set({ "Content-Type": "text/html" }).end(html);
|
|
865
|
-
return;
|
|
866
|
-
}
|
|
867
|
-
}
|
|
868
|
-
next(err);
|
|
869
|
-
};
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
// src/cli/commands/preview.ts
|
|
873
|
-
import path6 from "node:path";
|
|
874
|
-
import { default as express2 } from "express";
|
|
875
|
-
import { dim as dim3 } from "kleur/colors";
|
|
876
|
-
import sirv from "sirv";
|
|
877
|
-
import compression from "compression";
|
|
878
|
-
async function preview(rootProjectDir) {
|
|
879
|
-
process.env.NODE_ENV = "development";
|
|
880
|
-
const rootDir = path6.resolve(rootProjectDir || process.cwd());
|
|
881
|
-
const server = await createPreviewServer({ rootDir });
|
|
882
|
-
const port = parseInt(process.env.PORT || "4007");
|
|
883
|
-
console.log();
|
|
884
|
-
console.log(`${dim3("\u2503")} project: ${rootDir}`);
|
|
885
|
-
console.log(`${dim3("\u2503")} server: http://localhost:${port}`);
|
|
886
|
-
console.log(`${dim3("\u2503")} mode: preview`);
|
|
887
|
-
console.log();
|
|
888
|
-
server.listen(port);
|
|
889
|
-
}
|
|
890
|
-
async function createPreviewServer(options) {
|
|
891
|
-
const rootDir = options.rootDir;
|
|
892
|
-
const rootConfig = await loadRootConfig(rootDir, { command: "preview" });
|
|
893
|
-
const distDir = path6.join(rootDir, "dist");
|
|
894
|
-
const server = express2();
|
|
895
|
-
server.disable("x-powered-by");
|
|
896
|
-
server.use(compression());
|
|
897
|
-
server.use(rootProjectMiddleware({ rootConfig }));
|
|
898
|
-
server.use(await rootPreviewRendererMiddleware({ rootConfig, distDir }));
|
|
899
|
-
const plugins = rootConfig.plugins || [];
|
|
900
|
-
configureServerPlugins(
|
|
901
|
-
server,
|
|
902
|
-
async () => {
|
|
903
|
-
var _a;
|
|
904
|
-
const userMiddlewares = ((_a = rootConfig.server) == null ? void 0 : _a.middlewares) || [];
|
|
905
|
-
userMiddlewares.forEach((middleware) => {
|
|
906
|
-
server.use(middleware);
|
|
907
|
-
});
|
|
908
|
-
const publicDir = path6.join(distDir, "html");
|
|
909
|
-
server.use(sirv(publicDir, { dev: false }));
|
|
910
|
-
server.use(rootPreviewServerMiddleware());
|
|
911
|
-
server.use(rootPreviewServer404Middleware());
|
|
912
|
-
server.use(rootPreviewServer500Middleware());
|
|
913
|
-
},
|
|
914
|
-
plugins,
|
|
915
|
-
{ type: "preview", rootConfig }
|
|
916
|
-
);
|
|
917
|
-
return server;
|
|
918
|
-
}
|
|
919
|
-
async function rootPreviewRendererMiddleware(options) {
|
|
920
|
-
const { distDir, rootConfig } = options;
|
|
921
|
-
const render = await import(path6.join(distDir, "server/render.js"));
|
|
922
|
-
const manifestPath = path6.join(distDir, "client/root-manifest.json");
|
|
923
|
-
if (!await fileExists(manifestPath)) {
|
|
924
|
-
throw new Error(
|
|
925
|
-
`could not find ${manifestPath}. run \`root build\` before \`root preview\`.`
|
|
926
|
-
);
|
|
927
|
-
}
|
|
928
|
-
const rootManifest = await loadJson(manifestPath);
|
|
929
|
-
const assetMap = BuildAssetMap.fromRootManifest(rootConfig, rootManifest);
|
|
930
|
-
const elementGraph = await getElements(rootConfig);
|
|
931
|
-
const renderer = new render.Renderer(rootConfig, { assetMap, elementGraph });
|
|
932
|
-
return async (req, _, next) => {
|
|
933
|
-
req.renderer = renderer;
|
|
934
|
-
next();
|
|
935
|
-
};
|
|
936
|
-
}
|
|
937
|
-
function rootPreviewServerMiddleware() {
|
|
938
|
-
return async (req, res, next) => {
|
|
939
|
-
const renderer = req.renderer;
|
|
940
|
-
try {
|
|
941
|
-
await renderer.handle(req, res, next);
|
|
942
|
-
} catch (e) {
|
|
943
|
-
try {
|
|
944
|
-
const { html } = await renderer.renderError(e);
|
|
945
|
-
res.status(500).set({ "Content-Type": "text/html" }).end(html);
|
|
946
|
-
} catch (e2) {
|
|
947
|
-
console.error("failed to render custom error");
|
|
948
|
-
console.error(e2);
|
|
949
|
-
next(e);
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
};
|
|
953
|
-
}
|
|
954
|
-
function rootPreviewServer404Middleware() {
|
|
955
|
-
return async (req, res) => {
|
|
956
|
-
console.error(`\u2753 404 ${req.originalUrl}`);
|
|
957
|
-
const url = req.path;
|
|
958
|
-
const ext = path6.extname(url);
|
|
959
|
-
const renderer = req.renderer;
|
|
960
|
-
if (!ext) {
|
|
961
|
-
const data = await renderer.render404();
|
|
962
|
-
const html = data.html || "";
|
|
963
|
-
res.status(404).set({ "Content-Type": "text/html" }).end(html);
|
|
964
|
-
return;
|
|
965
|
-
}
|
|
966
|
-
res.status(404).set({ "Content-Type": "text/plain" }).end("404");
|
|
967
|
-
};
|
|
968
|
-
}
|
|
969
|
-
function rootPreviewServer500Middleware() {
|
|
970
|
-
return async (err, req, res, next) => {
|
|
971
|
-
console.error(`\u2757 500 ${req.originalUrl}`);
|
|
972
|
-
console.error(String(err.stack || err));
|
|
973
|
-
const url = req.path;
|
|
974
|
-
const ext = path6.extname(url);
|
|
975
|
-
const renderer = req.renderer;
|
|
976
|
-
if (!ext) {
|
|
977
|
-
const data = await renderer.renderError(err);
|
|
978
|
-
const html = data.html || "";
|
|
979
|
-
res.status(500).set({ "Content-Type": "text/html" }).end(html);
|
|
980
|
-
return;
|
|
981
|
-
}
|
|
982
|
-
next(err);
|
|
983
|
-
};
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
// src/cli/commands/start.ts
|
|
987
|
-
import path7 from "node:path";
|
|
988
|
-
import { default as express3 } from "express";
|
|
989
|
-
import { dim as dim4 } from "kleur/colors";
|
|
990
|
-
import sirv2 from "sirv";
|
|
991
|
-
import compression2 from "compression";
|
|
992
|
-
async function start(rootProjectDir) {
|
|
993
|
-
process.env.NODE_ENV = "production";
|
|
994
|
-
const rootDir = path7.resolve(rootProjectDir || process.cwd());
|
|
995
|
-
const server = await createProdServer({ rootDir });
|
|
996
|
-
const port = parseInt(process.env.PORT || "4007");
|
|
997
|
-
console.log();
|
|
998
|
-
console.log(`${dim4("\u2503")} project: ${rootDir}`);
|
|
999
|
-
console.log(`${dim4("\u2503")} server: http://localhost:${port}`);
|
|
1000
|
-
console.log(`${dim4("\u2503")} mode: production`);
|
|
1001
|
-
console.log();
|
|
1002
|
-
server.listen(port);
|
|
1003
|
-
}
|
|
1004
|
-
async function createProdServer(options) {
|
|
1005
|
-
const rootDir = options.rootDir;
|
|
1006
|
-
const rootConfig = await loadRootConfig(rootDir, { command: "start" });
|
|
1007
|
-
const distDir = path7.join(rootDir, "dist");
|
|
1008
|
-
const server = express3();
|
|
1009
|
-
server.disable("x-powered-by");
|
|
1010
|
-
server.use(compression2());
|
|
1011
|
-
server.use(rootProjectMiddleware({ rootConfig }));
|
|
1012
|
-
server.use(await rootProdRendererMiddleware({ rootConfig, distDir }));
|
|
1013
|
-
const plugins = rootConfig.plugins || [];
|
|
1014
|
-
configureServerPlugins(
|
|
1015
|
-
server,
|
|
1016
|
-
async () => {
|
|
1017
|
-
var _a;
|
|
1018
|
-
const userMiddlewares = ((_a = rootConfig.server) == null ? void 0 : _a.middlewares) || [];
|
|
1019
|
-
userMiddlewares.forEach((middleware) => {
|
|
1020
|
-
server.use(middleware);
|
|
1021
|
-
});
|
|
1022
|
-
const publicDir = path7.join(distDir, "html");
|
|
1023
|
-
server.use(sirv2(publicDir, { dev: false }));
|
|
1024
|
-
server.use(rootProdServerMiddleware());
|
|
1025
|
-
server.use(rootProdServer404Middleware());
|
|
1026
|
-
server.use(rootProdServer500Middleware());
|
|
1027
|
-
},
|
|
1028
|
-
plugins,
|
|
1029
|
-
{ type: "prod", rootConfig }
|
|
1030
|
-
);
|
|
1031
|
-
return server;
|
|
1032
|
-
}
|
|
1033
|
-
async function rootProdRendererMiddleware(options) {
|
|
1034
|
-
const { distDir, rootConfig } = options;
|
|
1035
|
-
const render = await import(path7.join(distDir, "server/render.js"));
|
|
1036
|
-
const manifestPath = path7.join(distDir, "client/root-manifest.json");
|
|
1037
|
-
if (!await fileExists(manifestPath)) {
|
|
1038
|
-
throw new Error(
|
|
1039
|
-
`could not find ${manifestPath}. run \`root build\` before \`root start\`.`
|
|
1040
|
-
);
|
|
1041
|
-
}
|
|
1042
|
-
const rootManifest = await loadJson(manifestPath);
|
|
1043
|
-
const assetMap = BuildAssetMap.fromRootManifest(rootConfig, rootManifest);
|
|
1044
|
-
const elementGraph = await getElements(rootConfig);
|
|
1045
|
-
const renderer = new render.Renderer(rootConfig, { assetMap, elementGraph });
|
|
1046
|
-
return async (req, _, next) => {
|
|
1047
|
-
req.renderer = renderer;
|
|
1048
|
-
next();
|
|
1049
|
-
};
|
|
1050
|
-
}
|
|
1051
|
-
function rootProdServerMiddleware() {
|
|
1052
|
-
return async (req, res, next) => {
|
|
1053
|
-
const renderer = req.renderer;
|
|
1054
|
-
try {
|
|
1055
|
-
await renderer.handle(req, res, next);
|
|
1056
|
-
} catch (e) {
|
|
1057
|
-
try {
|
|
1058
|
-
const { html } = await renderer.renderError(e);
|
|
1059
|
-
res.status(500).set({ "Content-Type": "text/html" }).end(html);
|
|
1060
|
-
} catch (e2) {
|
|
1061
|
-
console.error("failed to render custom error");
|
|
1062
|
-
console.error(e2);
|
|
1063
|
-
next(e);
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
};
|
|
1067
|
-
}
|
|
1068
|
-
function rootProdServer404Middleware() {
|
|
1069
|
-
return async (req, res) => {
|
|
1070
|
-
console.error(`\u2753 404 ${req.originalUrl}`);
|
|
1071
|
-
if (req.renderer) {
|
|
1072
|
-
const url = req.path;
|
|
1073
|
-
const ext = path7.extname(url);
|
|
1074
|
-
if (!ext) {
|
|
1075
|
-
const renderer = req.renderer;
|
|
1076
|
-
const data = await renderer.render404();
|
|
1077
|
-
const html = data.html || "";
|
|
1078
|
-
res.status(404).set({ "Content-Type": "text/html" }).end(html);
|
|
1079
|
-
return;
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
res.status(404).set({ "Content-Type": "text/plain" }).end("404");
|
|
1083
|
-
};
|
|
1084
|
-
}
|
|
1085
|
-
function rootProdServer500Middleware() {
|
|
1086
|
-
return async (err, req, res, next) => {
|
|
1087
|
-
console.error(`\u2757 500 ${req.originalUrl}`);
|
|
1088
|
-
console.error(String(err.stack || err));
|
|
1089
|
-
if (req.renderer) {
|
|
1090
|
-
const url = req.path;
|
|
1091
|
-
const ext = path7.extname(url);
|
|
1092
|
-
if (!ext) {
|
|
1093
|
-
const renderer = req.renderer;
|
|
1094
|
-
const data = await renderer.renderError(err);
|
|
1095
|
-
const html = data.html || "";
|
|
1096
|
-
res.status(500).set({ "Content-Type": "text/html" }).end(html);
|
|
1097
|
-
return;
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
next(err);
|
|
1101
|
-
};
|
|
1102
|
-
}
|
|
2
|
+
build,
|
|
3
|
+
createDevServer,
|
|
4
|
+
createPreviewServer,
|
|
5
|
+
createProdServer,
|
|
6
|
+
dev,
|
|
7
|
+
preview,
|
|
8
|
+
start
|
|
9
|
+
} from "./chunk-XG7BJNHU.js";
|
|
10
|
+
import "./chunk-AJF5RXXV.js";
|
|
11
|
+
import "./chunk-SDT4J6VY.js";
|
|
12
|
+
import "./chunk-QKBMWK5B.js";
|
|
13
|
+
import "./chunk-DXD5LKU3.js";
|
|
1103
14
|
export {
|
|
1104
15
|
build,
|
|
1105
16
|
createDevServer,
|