|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as path from "node:path"; |
| 3 | + |
| 4 | +import { getExports } from "./get-exports.js"; |
| 5 | + |
| 6 | +function createUrl(publicPath, file) { |
| 7 | + return ( |
| 8 | + publicPath.split(path.win32.sep).join("/") + |
| 9 | + (file || "").split(path.win32.sep).join("/") |
| 10 | + ); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * @param {import("webpack").StatsCompilation} stats |
| 16 | + * @param {string} publicPath |
| 17 | + * @returns {(group: string) => string[]} |
| 18 | + */ |
| 19 | +function createNamedChunkGroupFactory(stats, publicPath) { |
| 20 | + const chunksById = new Map(stats.chunks?.map((chunk) => [chunk.id, chunk])); |
| 21 | + return (group) => { |
| 22 | + /** @type {Set<string>} */ |
| 23 | + const files = new Set(); |
| 24 | + stats.namedChunkGroups?.[group].chunks?.forEach((chunkId) => { |
| 25 | + const chunk = chunksById.get(chunkId); |
| 26 | + chunk?.files?.forEach((file) => files.add(createUrl(publicPath, file))); |
| 27 | + }); |
| 28 | + return [...files]; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * @param {webpack.StatsCompilation} param0 |
| 34 | + * @param {string} entrypointId |
| 35 | + */ |
| 36 | +const getAssets = ({ entrypoints }, entrypointId) => { |
| 37 | + if (entrypoints === undefined) throw Error("todo"); |
| 38 | + const { assets } = entrypoints[entrypointId]; |
| 39 | + if (assets === undefined) throw Error("todo"); |
| 40 | + return assets; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * @param {import("@remix-run/dev").ResolvedRemixConfig} remixConfig |
| 45 | + * @param {import("webpack").Stats} stats |
| 46 | + * @returns {import("@remix-run/dev").AssetsManifest} |
| 47 | + */ |
| 48 | +export async function toManifest(remixConfig, stats) { |
| 49 | + const compilationStats = stats.toJson({ |
| 50 | + modules: true, |
| 51 | + entrypoints: true, |
| 52 | + assets: true, |
| 53 | + groupAssetsByChunk: true, |
| 54 | + hash: true, |
| 55 | + }); |
| 56 | + const getByNamedChunkGroup = createNamedChunkGroupFactory( |
| 57 | + compilationStats, |
| 58 | + remixConfig.publicPath |
| 59 | + ); |
| 60 | + |
| 61 | + const entryImports = getByNamedChunkGroup("entry.client"); |
| 62 | + const entryModule = createUrl( |
| 63 | + remixConfig.publicPath, |
| 64 | + getAssets(compilationStats, "entry.client").slice(-1)[0].name |
| 65 | + ); |
| 66 | + const rootImports = getByNamedChunkGroup("root"); |
| 67 | + |
| 68 | + // TODO: what are runtime imports? dynamic imports? |
| 69 | + // let runtimeImports = compilationStats.assetsByChunkName["runtime"].map( |
| 70 | + // (asset) => createUrl(remixConfig.publicPath, asset) |
| 71 | + // ); |
| 72 | + |
| 73 | + const routes = Object.fromEntries( |
| 74 | + Object.entries(remixConfig.routes).map(([routeId, route]) => { |
| 75 | + const assets = getAssets(compilationStats, routeId); |
| 76 | + const routeImports = assets |
| 77 | + .slice(0, -1) |
| 78 | + .map((asset) => createUrl(remixConfig.publicPath, asset.name)); |
| 79 | + const routeModule = createUrl( |
| 80 | + remixConfig.publicPath, |
| 81 | + assets.slice(-1)[0].name |
| 82 | + ); |
| 83 | + const routePath = path.resolve(remixConfig.appDirectory, route.file); |
| 84 | + const routeExports = getExports(routePath, remixConfig); |
| 85 | + return [ |
| 86 | + routeId, |
| 87 | + { |
| 88 | + id: route.id, |
| 89 | + parentId: route.parentId, |
| 90 | + path: route.path, |
| 91 | + index: route.index, |
| 92 | + caseSensitive: route.caseSensitive, |
| 93 | + module: routeModule, |
| 94 | + imports: routeImports, |
| 95 | + hasAction: routeExports.includes("action"), |
| 96 | + hasLoader: routeExports.includes("loader"), |
| 97 | + hasCatchBoundary: routeExports.includes("CatchBoundary"), |
| 98 | + hasErrorBoundary: routeExports.includes("ErrorBoundary"), |
| 99 | + }, |
| 100 | + ]; |
| 101 | + }) |
| 102 | + ); |
| 103 | + |
| 104 | + const version = compilationStats.hash; |
| 105 | + if (version === undefined) throw Error("todo"); |
| 106 | + return { |
| 107 | + version, |
| 108 | + url: createUrl( |
| 109 | + remixConfig.publicPath, |
| 110 | + `manifest-${version.toUpperCase()}.js` |
| 111 | + ), |
| 112 | + entry: { |
| 113 | + imports: [ |
| 114 | + ...new Set([/* ...runtimeImports, */ ...entryImports, ...rootImports]), |
| 115 | + ], |
| 116 | + module: entryModule, |
| 117 | + }, |
| 118 | + routes, |
| 119 | + }; |
| 120 | +} |
| 121 | + |
| 122 | +export function writeManifest(config, manifest) { |
| 123 | + fs.mkdirSync("./.cache", { recursive: true }); |
| 124 | + fs.writeFileSync( |
| 125 | + "./.cache/manifest.json", |
| 126 | + JSON.stringify(manifest, null, 2), |
| 127 | + "utf8" |
| 128 | + ); |
| 129 | + |
| 130 | + fs.mkdirSync(config.assetsBuildDirectory, { recursive: true }); |
| 131 | + fs.writeFileSync( |
| 132 | + path.resolve(config.assetsBuildDirectory, path.basename(manifest.url)), |
| 133 | + `window.__remixManifest=${JSON.stringify(manifest)};` |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * @returns {import("@remix-run/dev").AssetsManifest} |
| 139 | + */ |
| 140 | +export function getManifest() { |
| 141 | + return JSON.parse(fs.readFileSync("./.cache/manifest.json", "utf8")); |
| 142 | +} |
0 commit comments