main
js 62 lines 1.61 KB
Raw
1 'use strict';
2
3 const path = require('path');
4 const url = require('url');
5 const fs = require('fs');
6
7 const clientModules = {};
8 const clientManifest = {};
9 const ssrModuleMap = {};
10 let moduleIdx = 0;
11
12 function registerClientModule(modulePath) {
13 const id = String(moduleIdx++);
14 const chunkId = 'chunk-' + id;
15 const absPath = path.resolve(__dirname, modulePath);
16 const actualExports = require(absPath);
17 clientModules[id] = actualExports;
18
19 const href = url.pathToFileURL(absPath).href;
20 clientManifest[href] = {id, chunks: [chunkId, absPath], name: '*'};
21 ssrModuleMap[id] = {'*': {id, chunks: [chunkId, absPath], name: '*'}};
22 }
23
24 // Auto-register all 'use client' components by scanning src/
25 const srcDirs = [
26 path.resolve(__dirname, 'src'),
27 path.resolve(__dirname, 'src/components'),
28 ];
29 for (const dir of srcDirs) {
30 if (!fs.existsSync(dir)) continue;
31 for (const file of fs.readdirSync(dir)) {
32 if (!file.endsWith('.js')) continue;
33 const filePath = path.join(dir, file);
34 const source = fs.readFileSync(filePath, 'utf-8');
35 if (
36 source.trimStart().startsWith("'use client'") ||
37 source.trimStart().startsWith('"use client"')
38 ) {
39 registerClientModule(filePath);
40 }
41 }
42 }
43
44 global.__webpack_require__ = function (id) {
45 if (clientModules[id]) {
46 return clientModules[id];
47 }
48 throw new Error('Unknown module: ' + id);
49 };
50 global.__webpack_chunk_load__ = function () {
51 return new Promise(function (resolve) {
52 setImmediate(resolve);
53 });
54 };
55
56 const ssrManifest = {
57 moduleMap: ssrModuleMap,
58 moduleLoading: null,
59 serverModuleMap: null,
60 };
61
62 module.exports = {clientManifest, ssrManifest};