-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import * as path from "node:path"; | ||
import esbuild from "esbuild"; | ||
|
||
import type { Context } from "../context"; | ||
import { externalPlugin } from "../plugins/external"; | ||
import { emptyModulesPlugin } from "../plugins/emptyModules"; | ||
import { createMatchPath } from "../utils/tsconfig"; | ||
import invariant from "../../invariant"; | ||
import { getRouteModuleExports } from "../utils/routeExports"; | ||
|
||
function isBareModuleId(id: string): boolean { | ||
return !id.startsWith("node:") && !id.startsWith(".") && !path.isAbsolute(id); | ||
} | ||
|
||
type Route = Context["config"]["routes"][string]; | ||
|
||
export let create = async (ctx: Context) => { | ||
let entryPoints: Record<string, string> = {}; | ||
for (let id of Object.keys(ctx.config.routes)) { | ||
entryPoints[id] = ctx.config.routes[id].file + "?loader"; | ||
} | ||
let compiler = await esbuild.context({ | ||
bundle: true, | ||
entryPoints: entryPoints, | ||
treeShaking: true, | ||
metafile: true, | ||
outdir: "blah", | ||
write: false, | ||
entryNames: "[dir]/[name]-[hash]", | ||
plugins: [ | ||
{ | ||
name: "hmr-loader", | ||
setup(build) { | ||
let routesByFile: Map<string, Route> = Object.keys( | ||
ctx.config.routes | ||
).reduce((map, key) => { | ||
let route = ctx.config.routes[key]; | ||
map.set(route.file, route); | ||
return map; | ||
}, new Map()); | ||
let filter = /\?loader$/; | ||
build.onResolve({ filter }, (args) => { | ||
return { path: args.path, namespace: "hmr-loader" }; | ||
}); | ||
build.onLoad({ filter, namespace: "hmr-loader" }, async (args) => { | ||
let file = args.path.replace(filter, ""); | ||
let route = routesByFile.get(file); | ||
invariant(route, `Cannot get route by path: ${args.path}`); | ||
let theExports = await getRouteModuleExports(ctx.config, route.id); | ||
let contents = "module.exports = {};"; | ||
if (theExports.includes("loader")) { | ||
contents = `export { loader } from ${JSON.stringify( | ||
`./${file}` | ||
)};`; | ||
} | ||
return { | ||
contents, | ||
resolveDir: ctx.config.appDirectory, | ||
loader: "js", | ||
}; | ||
}); | ||
}, | ||
}, | ||
externalPlugin(/^node:.*/, { sideEffects: false }), | ||
externalPlugin(/\.css$/, { sideEffects: false }), | ||
externalPlugin(/^https?:\/\//, { sideEffects: false }), | ||
emptyModulesPlugin(ctx, /\.client(\.[jt]sx?)?$/), | ||
{ | ||
name: "hmr-bare-modules", | ||
setup(build) { | ||
let matchPath = ctx.config.tsconfigPath | ||
? createMatchPath(ctx.config.tsconfigPath) | ||
: undefined; | ||
function resolvePath(id: string) { | ||
if (!matchPath) return id; | ||
return ( | ||
matchPath(id, undefined, undefined, [ | ||
".ts", | ||
".tsx", | ||
".js", | ||
".jsx", | ||
]) || id | ||
); | ||
} | ||
build.onResolve({ filter: /.*/ }, (args) => { | ||
if (!isBareModuleId(resolvePath(args.path))) { | ||
return undefined; | ||
} | ||
return { path: args.path, external: true }; | ||
}); | ||
}, | ||
}, | ||
], | ||
}); | ||
return compiler; | ||
// let result = await compiler.rebuild(); | ||
// console.log(result.metafile.outputs); | ||
}; |