@blinkk/root 1.0.0-beta.0 → 1.0.0-beta.2
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/client.d.ts +1 -0
- package/dist/chunk-LTSJAEBG.js +150 -0
- package/dist/chunk-LTSJAEBG.js.map +1 -0
- package/dist/cli.js +126 -231
- package/dist/cli.js.map +1 -1
- package/dist/core.js +4 -4
- package/dist/node.d.ts +30 -0
- package/dist/node.js +12 -0
- package/dist/node.js.map +1 -0
- package/dist/render.js +31 -6
- package/dist/render.js.map +1 -1
- package/package.json +10 -4
package/dist/cli.js
CHANGED
|
@@ -1,88 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
configureServerPlugins,
|
|
3
|
-
getVitePlugins
|
|
4
|
-
} from "./chunk-DTEQ2AIW.js";
|
|
5
1
|
import {
|
|
6
2
|
htmlMinify,
|
|
7
3
|
htmlPretty,
|
|
8
4
|
isValidTagName,
|
|
9
5
|
parseTagNames
|
|
10
6
|
} from "./chunk-GGQGZ7ZE.js";
|
|
7
|
+
import {
|
|
8
|
+
copyGlob,
|
|
9
|
+
createViteServer,
|
|
10
|
+
directoryContains,
|
|
11
|
+
fileExists,
|
|
12
|
+
isDirectory,
|
|
13
|
+
isJsFile,
|
|
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";
|
|
11
24
|
|
|
12
25
|
// src/cli/commands/dev.ts
|
|
13
|
-
import
|
|
26
|
+
import path3 from "node:path";
|
|
14
27
|
import { fileURLToPath } from "node:url";
|
|
15
28
|
import { default as express } from "express";
|
|
16
|
-
import { createServer as createViteServer } from "vite";
|
|
17
29
|
|
|
18
30
|
// src/render/asset-map/dev-asset-map.ts
|
|
19
|
-
import path2 from "node:path";
|
|
20
|
-
import { searchForWorkspaceRoot } from "vite";
|
|
21
|
-
|
|
22
|
-
// src/utils/fsutils.ts
|
|
23
|
-
import { promises as fs } from "node:fs";
|
|
24
31
|
import path from "node:path";
|
|
25
|
-
import
|
|
26
|
-
import glob from "tiny-glob";
|
|
27
|
-
function isJsFile(filename) {
|
|
28
|
-
return !!filename.match(/\.(j|t)sx?$/);
|
|
29
|
-
}
|
|
30
|
-
async function writeFile(filepath, content) {
|
|
31
|
-
const dirPath = path.dirname(filepath);
|
|
32
|
-
await makeDir(dirPath);
|
|
33
|
-
await fs.writeFile(filepath, content);
|
|
34
|
-
}
|
|
35
|
-
async function makeDir(dirpath) {
|
|
36
|
-
try {
|
|
37
|
-
await fs.access(dirpath);
|
|
38
|
-
} catch (e) {
|
|
39
|
-
await fs.mkdir(dirpath, { recursive: true });
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async function copyGlob(pattern, srcdir, dstdir) {
|
|
43
|
-
const files = await glob(pattern, { cwd: srcdir });
|
|
44
|
-
if (files.length > 0) {
|
|
45
|
-
await makeDir(dstdir);
|
|
46
|
-
}
|
|
47
|
-
files.forEach((file) => {
|
|
48
|
-
fsExtra.copySync(path.resolve(srcdir, file), path.resolve(dstdir, file));
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
async function rmDir(dirpath) {
|
|
52
|
-
await fs.rm(dirpath, { recursive: true, force: true });
|
|
53
|
-
}
|
|
54
|
-
async function loadJson(filepath) {
|
|
55
|
-
const content = await fs.readFile(filepath, "utf-8");
|
|
56
|
-
return JSON.parse(content);
|
|
57
|
-
}
|
|
58
|
-
async function isDirectory(dirpath) {
|
|
59
|
-
return fs.stat(dirpath).then((fsStat) => {
|
|
60
|
-
return fsStat.isDirectory();
|
|
61
|
-
}).catch((err) => {
|
|
62
|
-
if (err.code === "ENOENT") {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
throw err;
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
function fileExists(filepath) {
|
|
69
|
-
return fs.access(filepath).then(() => true).catch(() => false);
|
|
70
|
-
}
|
|
71
|
-
async function directoryContains(dirpath, subpath) {
|
|
72
|
-
const outer = await fs.realpath(dirpath);
|
|
73
|
-
const inner = await fs.realpath(subpath);
|
|
74
|
-
const rel = path.relative(outer, inner);
|
|
75
|
-
return !rel.startsWith("..");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// src/render/asset-map/dev-asset-map.ts
|
|
32
|
+
import { searchForWorkspaceRoot } from "vite";
|
|
79
33
|
var DevServerAssetMap = class {
|
|
80
34
|
constructor(rootConfig, moduleGraph) {
|
|
81
35
|
this.rootConfig = rootConfig;
|
|
82
36
|
this.moduleGraph = moduleGraph;
|
|
83
37
|
}
|
|
84
38
|
async get(src) {
|
|
85
|
-
const file =
|
|
39
|
+
const file = path.resolve(this.rootConfig.rootDir, src);
|
|
86
40
|
const viteModules = this.moduleGraph.getModulesByFile(file);
|
|
87
41
|
if (viteModules && viteModules.size > 0) {
|
|
88
42
|
const [viteModule] = viteModules;
|
|
@@ -114,7 +68,7 @@ var DevServerAssetMap = class {
|
|
|
114
68
|
return null;
|
|
115
69
|
}
|
|
116
70
|
filePathToSrc(file) {
|
|
117
|
-
return
|
|
71
|
+
return path.relative(this.rootConfig.rootDir, file);
|
|
118
72
|
}
|
|
119
73
|
};
|
|
120
74
|
var DevServerAsset = class {
|
|
@@ -151,7 +105,7 @@ var DevServerAsset = class {
|
|
|
151
105
|
return;
|
|
152
106
|
}
|
|
153
107
|
visited.add(asset.moduleId);
|
|
154
|
-
const parts =
|
|
108
|
+
const parts = path.parse(asset.assetUrl);
|
|
155
109
|
if ([".js", ".jsx", ".ts", ".tsx"].includes(parts.ext) && asset.moduleId.includes("/elements/")) {
|
|
156
110
|
urls.add(asset.assetUrl);
|
|
157
111
|
}
|
|
@@ -178,7 +132,7 @@ var DevServerAsset = class {
|
|
|
178
132
|
}
|
|
179
133
|
visited.add(asset.assetUrl);
|
|
180
134
|
if (asset.src.endsWith(".scss")) {
|
|
181
|
-
const parts =
|
|
135
|
+
const parts = path.parse(asset.src);
|
|
182
136
|
if (!parts.name.startsWith("_")) {
|
|
183
137
|
urls.add(asset.assetUrl);
|
|
184
138
|
}
|
|
@@ -196,39 +150,18 @@ var DevServerAsset = class {
|
|
|
196
150
|
}
|
|
197
151
|
};
|
|
198
152
|
|
|
199
|
-
// src/cli/load-config.ts
|
|
200
|
-
import { bundleRequire } from "bundle-require";
|
|
201
|
-
import JoyCon from "joycon";
|
|
202
|
-
async function loadRootConfig(rootDir) {
|
|
203
|
-
const joycon = new JoyCon();
|
|
204
|
-
const configPath = await joycon.resolve({
|
|
205
|
-
cwd: rootDir,
|
|
206
|
-
files: ["root.config.ts"]
|
|
207
|
-
});
|
|
208
|
-
if (configPath) {
|
|
209
|
-
const configBundle = await bundleRequire({
|
|
210
|
-
filepath: configPath
|
|
211
|
-
});
|
|
212
|
-
return Object.assign({}, configBundle.mod.default || {}, { rootDir });
|
|
213
|
-
}
|
|
214
|
-
return { rootDir };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
153
|
// src/cli/commands/dev.ts
|
|
218
|
-
import glob3 from "tiny-glob";
|
|
219
154
|
import { dim } from "kleur/colors";
|
|
220
155
|
|
|
221
156
|
// src/core/middleware.ts
|
|
222
157
|
function rootProjectMiddleware(options) {
|
|
223
158
|
return (req, _, next) => {
|
|
224
|
-
req.rootConfig =
|
|
225
|
-
rootDir: options.rootDir
|
|
226
|
-
});
|
|
159
|
+
req.rootConfig = options.rootConfig;
|
|
227
160
|
next();
|
|
228
161
|
};
|
|
229
162
|
}
|
|
230
163
|
|
|
231
|
-
// src/
|
|
164
|
+
// src/utils/ports.ts
|
|
232
165
|
import { createServer } from "node:net";
|
|
233
166
|
function isPortOpen(port) {
|
|
234
167
|
return new Promise((resolve, reject) => {
|
|
@@ -265,11 +198,11 @@ async function findOpenPort(min, max) {
|
|
|
265
198
|
throw new Error(`no ports open between ${min} and ${max}`);
|
|
266
199
|
}
|
|
267
200
|
|
|
268
|
-
// src/
|
|
269
|
-
import
|
|
270
|
-
import
|
|
201
|
+
// src/node/element-graph.ts
|
|
202
|
+
import fs from "node:fs";
|
|
203
|
+
import path2 from "node:path";
|
|
271
204
|
import { searchForWorkspaceRoot as searchForWorkspaceRoot2 } from "vite";
|
|
272
|
-
import
|
|
205
|
+
import glob from "tiny-glob";
|
|
273
206
|
var ElementGraph = class {
|
|
274
207
|
constructor(sourceFiles) {
|
|
275
208
|
this.sourceFiles = {};
|
|
@@ -296,7 +229,7 @@ var ElementGraph = class {
|
|
|
296
229
|
if (!srcFile) {
|
|
297
230
|
throw new Error(`could not find file path for tagName <${tagName}>`);
|
|
298
231
|
}
|
|
299
|
-
const src =
|
|
232
|
+
const src = fs.readFileSync(srcFile.filePath, "utf-8");
|
|
300
233
|
const tagNames = parseTagNames(src);
|
|
301
234
|
const deps = /* @__PURE__ */ new Set();
|
|
302
235
|
for (const depTagName of tagNames) {
|
|
@@ -311,14 +244,14 @@ async function getElements(rootConfig) {
|
|
|
311
244
|
var _a, _b;
|
|
312
245
|
const rootDir = rootConfig.rootDir;
|
|
313
246
|
const workspaceRoot = searchForWorkspaceRoot2(rootDir);
|
|
314
|
-
const elementsDirs = [
|
|
247
|
+
const elementsDirs = [path2.join(rootDir, "elements")];
|
|
315
248
|
const elementsInclude = ((_a = rootConfig.elements) == null ? void 0 : _a.include) || [];
|
|
316
249
|
const excludePatterns = ((_b = rootConfig.elements) == null ? void 0 : _b.exclude) || [];
|
|
317
250
|
const excludeElement = (moduleId) => {
|
|
318
251
|
return excludePatterns.some((pattern) => Boolean(moduleId.match(pattern)));
|
|
319
252
|
};
|
|
320
253
|
for (const dirPath of elementsInclude) {
|
|
321
|
-
const elementsDir =
|
|
254
|
+
const elementsDir = path2.resolve(rootDir, dirPath);
|
|
322
255
|
if (!directoryContains(rootDir, elementsDir)) {
|
|
323
256
|
throw new Error(
|
|
324
257
|
`the elements dir (${dirPath}) should be within the project's workspace (${workspaceRoot})`
|
|
@@ -329,13 +262,13 @@ async function getElements(rootConfig) {
|
|
|
329
262
|
const elementFilePaths = {};
|
|
330
263
|
for (const dirPath of elementsDirs) {
|
|
331
264
|
if (await isDirectory(dirPath)) {
|
|
332
|
-
const files = await
|
|
265
|
+
const files = await glob("**/*", { cwd: dirPath });
|
|
333
266
|
files.forEach((file) => {
|
|
334
|
-
const parts =
|
|
267
|
+
const parts = path2.parse(file);
|
|
335
268
|
if (isJsFile(parts.base) && isValidTagName(parts.name)) {
|
|
336
269
|
const tagName = parts.name;
|
|
337
|
-
const filePath =
|
|
338
|
-
const relPath =
|
|
270
|
+
const filePath = path2.join(dirPath, file);
|
|
271
|
+
const relPath = path2.relative(rootDir, filePath);
|
|
339
272
|
if (!excludeElement(relPath)) {
|
|
340
273
|
elementFilePaths[tagName] = { filePath, relPath };
|
|
341
274
|
}
|
|
@@ -348,10 +281,11 @@ async function getElements(rootConfig) {
|
|
|
348
281
|
}
|
|
349
282
|
|
|
350
283
|
// src/cli/commands/dev.ts
|
|
351
|
-
|
|
284
|
+
import glob2 from "tiny-glob";
|
|
285
|
+
var __dirname = path3.dirname(fileURLToPath(import.meta.url));
|
|
352
286
|
async function dev(rootProjectDir) {
|
|
353
287
|
process.env.NODE_ENV = "development";
|
|
354
|
-
const rootDir =
|
|
288
|
+
const rootDir = path3.resolve(rootProjectDir || process.cwd());
|
|
355
289
|
const defaultPort = parseInt(process.env.PORT || "4007");
|
|
356
290
|
const port = await findOpenPort(defaultPort, defaultPort + 10);
|
|
357
291
|
console.log();
|
|
@@ -363,13 +297,13 @@ async function dev(rootProjectDir) {
|
|
|
363
297
|
server.listen(port);
|
|
364
298
|
}
|
|
365
299
|
async function createServer2(options) {
|
|
366
|
-
const rootDir =
|
|
367
|
-
const rootConfig = await loadRootConfig(rootDir);
|
|
300
|
+
const rootDir = path3.resolve((options == null ? void 0 : options.rootDir) || process.cwd());
|
|
301
|
+
const rootConfig = await loadRootConfig(rootDir, { command: "dev" });
|
|
368
302
|
const port = options == null ? void 0 : options.port;
|
|
369
303
|
const server = express();
|
|
370
304
|
server.disable("x-powered-by");
|
|
371
|
-
server.use(rootProjectMiddleware({
|
|
372
|
-
server.use(await viteServerMiddleware({
|
|
305
|
+
server.use(rootProjectMiddleware({ rootConfig }));
|
|
306
|
+
server.use(await viteServerMiddleware({ rootConfig, port }));
|
|
373
307
|
const plugins = rootConfig.plugins || [];
|
|
374
308
|
await configureServerPlugins(
|
|
375
309
|
server,
|
|
@@ -389,76 +323,34 @@ async function createServer2(options) {
|
|
|
389
323
|
return server;
|
|
390
324
|
}
|
|
391
325
|
async function viteServerMiddleware(options) {
|
|
392
|
-
var _a, _b;
|
|
393
|
-
const rootDir = options.rootDir;
|
|
394
326
|
const rootConfig = options.rootConfig;
|
|
395
|
-
const
|
|
396
|
-
let hmrOptions = (_a = viteConfig.server) == null ? void 0 : _a.hmr;
|
|
397
|
-
if (typeof hmrOptions === "undefined" && options.port) {
|
|
398
|
-
hmrOptions = { port: options.port + 10 };
|
|
399
|
-
}
|
|
400
|
-
const routeFiles = [];
|
|
401
|
-
if (await isDirectory(path4.join(rootDir, "routes"))) {
|
|
402
|
-
const pageFiles = await glob3("routes/**/*", { cwd: rootDir });
|
|
403
|
-
pageFiles.forEach((file) => {
|
|
404
|
-
const parts = path4.parse(file);
|
|
405
|
-
if (!parts.name.startsWith("_") && isJsFile(parts.base)) {
|
|
406
|
-
routeFiles.push(file);
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
}
|
|
327
|
+
const rootDir = rootConfig.rootDir;
|
|
410
328
|
const elementGraph = await getElements(rootConfig);
|
|
411
329
|
const elements = Object.values(elementGraph.sourceFiles).map((sourceFile) => {
|
|
412
330
|
return sourceFile.relPath;
|
|
413
331
|
});
|
|
414
332
|
const bundleScripts = [];
|
|
415
|
-
if (await isDirectory(
|
|
416
|
-
const bundleFiles = await
|
|
333
|
+
if (await isDirectory(path3.join(rootDir, "bundles"))) {
|
|
334
|
+
const bundleFiles = await glob2("bundles/*", { cwd: rootDir });
|
|
417
335
|
bundleFiles.forEach((file) => {
|
|
418
|
-
const parts =
|
|
336
|
+
const parts = path3.parse(file);
|
|
419
337
|
if (isJsFile(parts.base)) {
|
|
420
338
|
bundleScripts.push(file);
|
|
421
339
|
}
|
|
422
340
|
});
|
|
423
341
|
}
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
publicDir: path4.join(rootDir, "public"),
|
|
429
|
-
server: {
|
|
430
|
-
...viteConfig.server || {},
|
|
431
|
-
middlewareMode: true,
|
|
432
|
-
hmr: hmrOptions
|
|
433
|
-
},
|
|
434
|
-
appType: "custom",
|
|
435
|
-
optimizeDeps: {
|
|
436
|
-
...viteConfig.optimizeDeps || {},
|
|
437
|
-
include: [
|
|
438
|
-
...elements,
|
|
439
|
-
...bundleScripts,
|
|
440
|
-
...((_b = viteConfig.optimizeDeps) == null ? void 0 : _b.include) || []
|
|
441
|
-
]
|
|
442
|
-
},
|
|
443
|
-
ssr: {
|
|
444
|
-
...viteConfig.ssr || {},
|
|
445
|
-
noExternal: ["@blinkk/root"]
|
|
446
|
-
},
|
|
447
|
-
esbuild: {
|
|
448
|
-
...viteConfig.esbuild || {},
|
|
449
|
-
jsx: "automatic",
|
|
450
|
-
jsxImportSource: "preact"
|
|
451
|
-
},
|
|
452
|
-
plugins: [
|
|
453
|
-
...viteConfig.plugins || [],
|
|
454
|
-
...getVitePlugins(rootConfig.plugins || [])
|
|
455
|
-
]
|
|
342
|
+
const optimizeDeps = [...elements, ...bundleScripts];
|
|
343
|
+
const viteServer = await createViteServer(rootConfig, {
|
|
344
|
+
port: options.port,
|
|
345
|
+
optimizeDeps
|
|
456
346
|
});
|
|
457
347
|
return async (req, res, next) => {
|
|
458
348
|
try {
|
|
459
349
|
req.viteServer = viteServer;
|
|
460
|
-
const renderModulePath =
|
|
461
|
-
const render = await viteServer.ssrLoadModule(
|
|
350
|
+
const renderModulePath = path3.resolve(__dirname, "./render.js");
|
|
351
|
+
const render = await viteServer.ssrLoadModule(
|
|
352
|
+
renderModulePath
|
|
353
|
+
);
|
|
462
354
|
const assetMap = new DevServerAssetMap(
|
|
463
355
|
rootConfig,
|
|
464
356
|
viteServer.moduleGraph
|
|
@@ -487,7 +379,7 @@ function rootDevServer404Middleware() {
|
|
|
487
379
|
console.error(`\u2753 404 ${req.originalUrl}`);
|
|
488
380
|
if (req.renderer) {
|
|
489
381
|
const url = req.path;
|
|
490
|
-
const ext =
|
|
382
|
+
const ext = path3.extname(url);
|
|
491
383
|
if (!ext) {
|
|
492
384
|
const renderer = req.renderer;
|
|
493
385
|
const data = await renderer.renderDevServer404(req);
|
|
@@ -505,7 +397,7 @@ function rootDevServer500Middleware() {
|
|
|
505
397
|
console.error(String(err.stack || err));
|
|
506
398
|
if (req.renderer) {
|
|
507
399
|
const url = req.path;
|
|
508
|
-
const ext =
|
|
400
|
+
const ext = path3.extname(url);
|
|
509
401
|
if (!ext) {
|
|
510
402
|
const renderer = req.renderer;
|
|
511
403
|
const data = await renderer.renderDevServer500(req, err);
|
|
@@ -519,15 +411,15 @@ function rootDevServer500Middleware() {
|
|
|
519
411
|
}
|
|
520
412
|
|
|
521
413
|
// src/cli/commands/build.ts
|
|
522
|
-
import
|
|
414
|
+
import path5 from "node:path";
|
|
523
415
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
524
|
-
import
|
|
525
|
-
import
|
|
416
|
+
import fsExtra from "fs-extra";
|
|
417
|
+
import glob3 from "tiny-glob";
|
|
526
418
|
import { build as viteBuild } from "vite";
|
|
527
419
|
|
|
528
420
|
// src/render/asset-map/build-asset-map.ts
|
|
529
|
-
import
|
|
530
|
-
import
|
|
421
|
+
import fs2 from "node:fs";
|
|
422
|
+
import path4 from "node:path";
|
|
531
423
|
var BuildAssetMap = class {
|
|
532
424
|
constructor(rootConfig) {
|
|
533
425
|
this.rootConfig = rootConfig;
|
|
@@ -575,7 +467,7 @@ var BuildAssetMap = class {
|
|
|
575
467
|
const src = manifestKey;
|
|
576
468
|
const manifestChunk = clientManifest[manifestKey];
|
|
577
469
|
const isElement = elementFiles.has(src);
|
|
578
|
-
const assetUrl = src.startsWith("routes/") ? "" : `/${manifestChunk.file}`;
|
|
470
|
+
const assetUrl = src.startsWith("routes/") && isJsFile(src) ? "" : `/${manifestChunk.file}`;
|
|
579
471
|
const assetData = {
|
|
580
472
|
src,
|
|
581
473
|
assetUrl,
|
|
@@ -597,12 +489,12 @@ var BuildAssetMap = class {
|
|
|
597
489
|
}
|
|
598
490
|
};
|
|
599
491
|
function realPathRelativeTo(rootDir, src) {
|
|
600
|
-
const fullPath =
|
|
601
|
-
if (!
|
|
492
|
+
const fullPath = path4.resolve(rootDir, src);
|
|
493
|
+
if (!fs2.existsSync(fullPath)) {
|
|
602
494
|
return src;
|
|
603
495
|
}
|
|
604
|
-
const realpath =
|
|
605
|
-
return
|
|
496
|
+
const realpath = fs2.realpathSync(path4.resolve(rootDir, src));
|
|
497
|
+
return path4.relative(rootDir, realpath);
|
|
606
498
|
}
|
|
607
499
|
var BuildAsset = class {
|
|
608
500
|
constructor(assetMap, assetData) {
|
|
@@ -680,14 +572,14 @@ var BuildAsset = class {
|
|
|
680
572
|
|
|
681
573
|
// src/cli/commands/build.ts
|
|
682
574
|
import { dim as dim2, cyan } from "kleur/colors";
|
|
683
|
-
var __dirname2 =
|
|
575
|
+
var __dirname2 = path5.dirname(fileURLToPath2(import.meta.url));
|
|
684
576
|
async function build(rootProjectDir, options) {
|
|
685
577
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
686
578
|
const mode = (options == null ? void 0 : options.mode) || "production";
|
|
687
579
|
process.env.NODE_ENV = mode;
|
|
688
|
-
const rootDir =
|
|
689
|
-
const rootConfig = await loadRootConfig(rootDir);
|
|
690
|
-
const distDir =
|
|
580
|
+
const rootDir = path5.resolve(rootProjectDir || process.cwd());
|
|
581
|
+
const rootConfig = await loadRootConfig(rootDir, { command: "build" });
|
|
582
|
+
const distDir = path5.join(rootDir, "dist");
|
|
691
583
|
const ssrOnly = (options == null ? void 0 : options.ssrOnly) || false;
|
|
692
584
|
console.log();
|
|
693
585
|
console.log(`${dim2("\u2503")} project: ${rootDir}`);
|
|
@@ -697,12 +589,12 @@ async function build(rootProjectDir, options) {
|
|
|
697
589
|
await rmDir(distDir);
|
|
698
590
|
await makeDir(distDir);
|
|
699
591
|
const routeFiles = [];
|
|
700
|
-
if (await isDirectory(
|
|
701
|
-
const pageFiles = await
|
|
592
|
+
if (await isDirectory(path5.join(rootDir, "routes"))) {
|
|
593
|
+
const pageFiles = await glob3("routes/**/*", { cwd: rootDir });
|
|
702
594
|
pageFiles.forEach((file) => {
|
|
703
|
-
const parts =
|
|
595
|
+
const parts = path5.parse(file);
|
|
704
596
|
if (!parts.name.startsWith("_") && isJsFile(parts.base)) {
|
|
705
|
-
routeFiles.push(
|
|
597
|
+
routeFiles.push(path5.resolve(rootDir, file));
|
|
706
598
|
}
|
|
707
599
|
});
|
|
708
600
|
}
|
|
@@ -711,12 +603,12 @@ async function build(rootProjectDir, options) {
|
|
|
711
603
|
return sourceFile.filePath;
|
|
712
604
|
});
|
|
713
605
|
const bundleScripts = [];
|
|
714
|
-
if (await isDirectory(
|
|
715
|
-
const bundleFiles = await
|
|
606
|
+
if (await isDirectory(path5.join(rootDir, "bundles"))) {
|
|
607
|
+
const bundleFiles = await glob3("bundles/*", { cwd: rootDir });
|
|
716
608
|
bundleFiles.forEach((file) => {
|
|
717
|
-
const parts =
|
|
609
|
+
const parts = path5.parse(file);
|
|
718
610
|
if (isJsFile(parts.base)) {
|
|
719
|
-
bundleScripts.push(
|
|
611
|
+
bundleScripts.push(path5.resolve(rootDir, file));
|
|
720
612
|
}
|
|
721
613
|
});
|
|
722
614
|
}
|
|
@@ -739,7 +631,7 @@ async function build(rootProjectDir, options) {
|
|
|
739
631
|
plugins: vitePlugins
|
|
740
632
|
};
|
|
741
633
|
const ssrInput = {
|
|
742
|
-
render:
|
|
634
|
+
render: path5.resolve(__dirname2, "./render.js")
|
|
743
635
|
};
|
|
744
636
|
rootPlugins.forEach((plugin) => {
|
|
745
637
|
if (plugin.ssrInput) {
|
|
@@ -769,7 +661,7 @@ async function build(rootProjectDir, options) {
|
|
|
769
661
|
assetFileNames: "assets/[name].[hash][extname]"
|
|
770
662
|
}
|
|
771
663
|
},
|
|
772
|
-
outDir:
|
|
664
|
+
outDir: path5.join(distDir, "server"),
|
|
773
665
|
ssr: true,
|
|
774
666
|
ssrManifest: false,
|
|
775
667
|
cssCodeSplit: true,
|
|
@@ -800,7 +692,7 @@ async function build(rootProjectDir, options) {
|
|
|
800
692
|
...(_e = (_d = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _d.rollupOptions) == null ? void 0 : _e.output
|
|
801
693
|
}
|
|
802
694
|
},
|
|
803
|
-
outDir:
|
|
695
|
+
outDir: path5.join(distDir, "routes"),
|
|
804
696
|
ssr: false,
|
|
805
697
|
ssrManifest: false,
|
|
806
698
|
manifest: true,
|
|
@@ -829,7 +721,7 @@ async function build(rootProjectDir, options) {
|
|
|
829
721
|
...(_h = (_g = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _g.rollupOptions) == null ? void 0 : _h.output
|
|
830
722
|
}
|
|
831
723
|
},
|
|
832
|
-
outDir:
|
|
724
|
+
outDir: path5.join(distDir, "client"),
|
|
833
725
|
ssr: false,
|
|
834
726
|
ssrManifest: false,
|
|
835
727
|
manifest: true,
|
|
@@ -841,18 +733,18 @@ async function build(rootProjectDir, options) {
|
|
|
841
733
|
}
|
|
842
734
|
});
|
|
843
735
|
} else {
|
|
844
|
-
await writeFile(
|
|
736
|
+
await writeFile(path5.join(distDir, "client/manifest.json"), "{}");
|
|
845
737
|
}
|
|
846
738
|
await copyGlob(
|
|
847
739
|
"*.css",
|
|
848
|
-
|
|
849
|
-
|
|
740
|
+
path5.join(distDir, "routes/assets"),
|
|
741
|
+
path5.join(distDir, "client/assets")
|
|
850
742
|
);
|
|
851
743
|
const routesManifest = await loadJson(
|
|
852
|
-
|
|
744
|
+
path5.join(distDir, "routes/manifest.json")
|
|
853
745
|
);
|
|
854
746
|
const clientManifest = await loadJson(
|
|
855
|
-
|
|
747
|
+
path5.join(distDir, "client/manifest.json")
|
|
856
748
|
);
|
|
857
749
|
function collectRouteCss(asset, cssDeps, visited) {
|
|
858
750
|
if (!asset || !asset.file || visited.has(asset.file)) {
|
|
@@ -888,38 +780,41 @@ async function build(rootProjectDir, options) {
|
|
|
888
780
|
);
|
|
889
781
|
const rootManifest = assetMap.toJson();
|
|
890
782
|
await writeFile(
|
|
891
|
-
|
|
783
|
+
path5.join(distDir, "client/root-manifest.json"),
|
|
892
784
|
JSON.stringify(rootManifest, null, 2)
|
|
893
785
|
);
|
|
894
|
-
const buildDir =
|
|
895
|
-
const publicDir =
|
|
896
|
-
if (
|
|
897
|
-
|
|
786
|
+
const buildDir = path5.join(distDir, "html");
|
|
787
|
+
const publicDir = path5.join(rootDir, "public");
|
|
788
|
+
if (fsExtra.existsSync(path5.join(rootDir, "public"))) {
|
|
789
|
+
fsExtra.copySync(publicDir, buildDir, { dereference: true });
|
|
898
790
|
} else {
|
|
899
|
-
makeDir(buildDir);
|
|
791
|
+
await makeDir(buildDir);
|
|
900
792
|
}
|
|
793
|
+
await makeDir(path5.join(buildDir, "assets"));
|
|
794
|
+
await makeDir(path5.join(buildDir, "chunks"));
|
|
901
795
|
console.log("\njs/css output:");
|
|
902
|
-
makeDir(path6.join(buildDir, "assets"));
|
|
903
|
-
makeDir(path6.join(buildDir, "chunks"));
|
|
904
796
|
await Promise.all(
|
|
905
797
|
Object.keys(rootManifest).map(async (src) => {
|
|
906
798
|
if (isRouteFile(src)) {
|
|
907
799
|
return;
|
|
908
800
|
}
|
|
909
801
|
const assetData = rootManifest[src];
|
|
802
|
+
if (!assetData.assetUrl) {
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
910
805
|
const assetRelPath = assetData.assetUrl.slice(1);
|
|
911
|
-
const assetFrom =
|
|
912
|
-
const assetTo =
|
|
806
|
+
const assetFrom = path5.join(distDir, "client", assetRelPath);
|
|
807
|
+
const assetTo = path5.join(buildDir, assetRelPath);
|
|
913
808
|
if (!await fileExists(assetFrom)) {
|
|
914
809
|
console.log(`${assetFrom} does not exist`);
|
|
915
810
|
return;
|
|
916
811
|
}
|
|
917
|
-
await
|
|
812
|
+
await fsExtra.copy(assetFrom, assetTo);
|
|
918
813
|
printFileOutput(fileSize(assetTo), "dist/html/", assetRelPath);
|
|
919
814
|
})
|
|
920
815
|
);
|
|
921
816
|
if (!ssrOnly) {
|
|
922
|
-
const render = await import(
|
|
817
|
+
const render = await import(path5.join(distDir, "server/render.js"));
|
|
923
818
|
const renderer = new render.Renderer(rootConfig, { assetMap, elementGraph });
|
|
924
819
|
const sitemap = await renderer.getSitemap();
|
|
925
820
|
console.log("\nhtml output:");
|
|
@@ -932,11 +827,11 @@ async function build(rootProjectDir, options) {
|
|
|
932
827
|
if (data.notFound) {
|
|
933
828
|
return;
|
|
934
829
|
}
|
|
935
|
-
let outFilePath =
|
|
830
|
+
let outFilePath = path5.join(urlPath.slice(1), "index.html");
|
|
936
831
|
if (outFilePath.endsWith("404/index.html")) {
|
|
937
832
|
outFilePath = outFilePath.replace("404/index.html", "404.html");
|
|
938
833
|
}
|
|
939
|
-
const outPath =
|
|
834
|
+
const outPath = path5.join(buildDir, outFilePath);
|
|
940
835
|
let html = data.html || "";
|
|
941
836
|
if (rootConfig.prettyHtml !== false) {
|
|
942
837
|
html = await htmlPretty(html, rootConfig.prettyHtmlOptions);
|
|
@@ -954,7 +849,7 @@ function isRouteFile(filepath) {
|
|
|
954
849
|
return filepath.startsWith("routes") && isJsFile(filepath);
|
|
955
850
|
}
|
|
956
851
|
function fileSize(filepath) {
|
|
957
|
-
const stats =
|
|
852
|
+
const stats = fsExtra.statSync(filepath);
|
|
958
853
|
const bytes = stats.size;
|
|
959
854
|
const k = 1024;
|
|
960
855
|
if (bytes < k) {
|
|
@@ -973,14 +868,14 @@ function printFileOutput(fileSize2, outputDir, outputFile) {
|
|
|
973
868
|
}
|
|
974
869
|
|
|
975
870
|
// src/cli/commands/preview.ts
|
|
976
|
-
import
|
|
871
|
+
import path6 from "node:path";
|
|
977
872
|
import { default as express2 } from "express";
|
|
978
873
|
import { dim as dim3 } from "kleur/colors";
|
|
979
874
|
import sirv from "sirv";
|
|
980
875
|
import compression from "compression";
|
|
981
876
|
async function preview(rootProjectDir) {
|
|
982
877
|
process.env.NODE_ENV = "development";
|
|
983
|
-
const rootDir =
|
|
878
|
+
const rootDir = path6.resolve(rootProjectDir || process.cwd());
|
|
984
879
|
const server = await createServer3({ rootDir });
|
|
985
880
|
const port = parseInt(process.env.PORT || "4007");
|
|
986
881
|
console.log();
|
|
@@ -992,12 +887,12 @@ async function preview(rootProjectDir) {
|
|
|
992
887
|
}
|
|
993
888
|
async function createServer3(options) {
|
|
994
889
|
const rootDir = options.rootDir;
|
|
995
|
-
const rootConfig = await loadRootConfig(rootDir);
|
|
996
|
-
const distDir =
|
|
890
|
+
const rootConfig = await loadRootConfig(rootDir, { command: "preview" });
|
|
891
|
+
const distDir = path6.join(rootDir, "dist");
|
|
997
892
|
const server = express2();
|
|
998
893
|
server.disable("x-powered-by");
|
|
999
894
|
server.use(compression());
|
|
1000
|
-
server.use(rootProjectMiddleware({
|
|
895
|
+
server.use(rootProjectMiddleware({ rootConfig }));
|
|
1001
896
|
server.use(await rootPreviewRendererMiddleware({ rootConfig, distDir }));
|
|
1002
897
|
const plugins = rootConfig.plugins || [];
|
|
1003
898
|
configureServerPlugins(
|
|
@@ -1008,7 +903,7 @@ async function createServer3(options) {
|
|
|
1008
903
|
userMiddlewares.forEach((middleware) => {
|
|
1009
904
|
server.use(middleware);
|
|
1010
905
|
});
|
|
1011
|
-
const publicDir =
|
|
906
|
+
const publicDir = path6.join(distDir, "html");
|
|
1012
907
|
server.use(sirv(publicDir, { dev: false }));
|
|
1013
908
|
server.use(rootPreviewServerMiddleware());
|
|
1014
909
|
server.use(rootPreviewServer404Middleware());
|
|
@@ -1021,8 +916,8 @@ async function createServer3(options) {
|
|
|
1021
916
|
}
|
|
1022
917
|
async function rootPreviewRendererMiddleware(options) {
|
|
1023
918
|
const { distDir, rootConfig } = options;
|
|
1024
|
-
const render = await import(
|
|
1025
|
-
const manifestPath =
|
|
919
|
+
const render = await import(path6.join(distDir, "server/render.js"));
|
|
920
|
+
const manifestPath = path6.join(distDir, "client/root-manifest.json");
|
|
1026
921
|
if (!await fileExists(manifestPath)) {
|
|
1027
922
|
throw new Error(
|
|
1028
923
|
`could not find ${manifestPath}. run \`root build\` before \`root preview\`.`
|
|
@@ -1058,7 +953,7 @@ function rootPreviewServer404Middleware() {
|
|
|
1058
953
|
return async (req, res) => {
|
|
1059
954
|
console.error(`\u2753 404 ${req.originalUrl}`);
|
|
1060
955
|
const url = req.path;
|
|
1061
|
-
const ext =
|
|
956
|
+
const ext = path6.extname(url);
|
|
1062
957
|
const renderer = req.renderer;
|
|
1063
958
|
if (!ext) {
|
|
1064
959
|
const data = await renderer.render404();
|
|
@@ -1074,7 +969,7 @@ function rootPreviewServer500Middleware() {
|
|
|
1074
969
|
console.error(`\u2757 500 ${req.originalUrl}`);
|
|
1075
970
|
console.error(String(err.stack || err));
|
|
1076
971
|
const url = req.path;
|
|
1077
|
-
const ext =
|
|
972
|
+
const ext = path6.extname(url);
|
|
1078
973
|
const renderer = req.renderer;
|
|
1079
974
|
if (!ext) {
|
|
1080
975
|
const data = await renderer.renderError(err);
|
|
@@ -1087,14 +982,14 @@ function rootPreviewServer500Middleware() {
|
|
|
1087
982
|
}
|
|
1088
983
|
|
|
1089
984
|
// src/cli/commands/start.ts
|
|
1090
|
-
import
|
|
985
|
+
import path7 from "node:path";
|
|
1091
986
|
import { default as express3 } from "express";
|
|
1092
987
|
import { dim as dim4 } from "kleur/colors";
|
|
1093
988
|
import sirv2 from "sirv";
|
|
1094
989
|
import compression2 from "compression";
|
|
1095
990
|
async function start(rootProjectDir) {
|
|
1096
991
|
process.env.NODE_ENV = "production";
|
|
1097
|
-
const rootDir =
|
|
992
|
+
const rootDir = path7.resolve(rootProjectDir || process.cwd());
|
|
1098
993
|
const server = await createServer4({ rootDir });
|
|
1099
994
|
const port = parseInt(process.env.PORT || "4007");
|
|
1100
995
|
console.log();
|
|
@@ -1106,12 +1001,12 @@ async function start(rootProjectDir) {
|
|
|
1106
1001
|
}
|
|
1107
1002
|
async function createServer4(options) {
|
|
1108
1003
|
const rootDir = options.rootDir;
|
|
1109
|
-
const rootConfig = await loadRootConfig(rootDir);
|
|
1110
|
-
const distDir =
|
|
1004
|
+
const rootConfig = await loadRootConfig(rootDir, { command: "start" });
|
|
1005
|
+
const distDir = path7.join(rootDir, "dist");
|
|
1111
1006
|
const server = express3();
|
|
1112
1007
|
server.disable("x-powered-by");
|
|
1113
1008
|
server.use(compression2());
|
|
1114
|
-
server.use(rootProjectMiddleware({
|
|
1009
|
+
server.use(rootProjectMiddleware({ rootConfig }));
|
|
1115
1010
|
server.use(await rootProdRendererMiddleware({ rootConfig, distDir }));
|
|
1116
1011
|
const plugins = rootConfig.plugins || [];
|
|
1117
1012
|
configureServerPlugins(
|
|
@@ -1122,7 +1017,7 @@ async function createServer4(options) {
|
|
|
1122
1017
|
userMiddlewares.forEach((middleware) => {
|
|
1123
1018
|
server.use(middleware);
|
|
1124
1019
|
});
|
|
1125
|
-
const publicDir =
|
|
1020
|
+
const publicDir = path7.join(distDir, "html");
|
|
1126
1021
|
server.use(sirv2(publicDir, { dev: false }));
|
|
1127
1022
|
server.use(rootProdServerMiddleware());
|
|
1128
1023
|
server.use(rootProdServer404Middleware());
|
|
@@ -1135,8 +1030,8 @@ async function createServer4(options) {
|
|
|
1135
1030
|
}
|
|
1136
1031
|
async function rootProdRendererMiddleware(options) {
|
|
1137
1032
|
const { distDir, rootConfig } = options;
|
|
1138
|
-
const render = await import(
|
|
1139
|
-
const manifestPath =
|
|
1033
|
+
const render = await import(path7.join(distDir, "server/render.js"));
|
|
1034
|
+
const manifestPath = path7.join(distDir, "client/root-manifest.json");
|
|
1140
1035
|
if (!await fileExists(manifestPath)) {
|
|
1141
1036
|
throw new Error(
|
|
1142
1037
|
`could not find ${manifestPath}. run \`root build\` before \`root start\`.`
|
|
@@ -1173,7 +1068,7 @@ function rootProdServer404Middleware() {
|
|
|
1173
1068
|
console.error(`\u2753 404 ${req.originalUrl}`);
|
|
1174
1069
|
if (req.renderer) {
|
|
1175
1070
|
const url = req.path;
|
|
1176
|
-
const ext =
|
|
1071
|
+
const ext = path7.extname(url);
|
|
1177
1072
|
if (!ext) {
|
|
1178
1073
|
const renderer = req.renderer;
|
|
1179
1074
|
const data = await renderer.render404();
|
|
@@ -1191,7 +1086,7 @@ function rootProdServer500Middleware() {
|
|
|
1191
1086
|
console.error(String(err.stack || err));
|
|
1192
1087
|
if (req.renderer) {
|
|
1193
1088
|
const url = req.path;
|
|
1194
|
-
const ext =
|
|
1089
|
+
const ext = path7.extname(url);
|
|
1195
1090
|
if (!ext) {
|
|
1196
1091
|
const renderer = req.renderer;
|
|
1197
1092
|
const data = await renderer.renderError(err);
|