feat: implement mock data support with API interceptor and update documentation

This commit is contained in:
2026-02-26 02:37:01 +00:00
parent 30501f5f4c
commit 20eaa50935
9 changed files with 410 additions and 5 deletions

View File

@@ -44,6 +44,25 @@ const resolvePlugin = {
},
}
// When MOCK is disabled, replace the mock module with a no-op stub so
// esbuild can tree-shake all mock data out of the production bundle.
const mockPlugin = {
name: "mockPlugin",
setup(build) {
if (process.env.MOCK !== "1") {
build.onResolve({ filter: /\/mock$/ }, (args) => {
if (args.importer.includes("api.ts") || args.importer.includes("api.js")) {
return { path: "mock-noop", namespace: "mock-noop" }
}
})
build.onLoad({ filter: /.*/, namespace: "mock-noop" }, () => ({
contents: "export function mockApiRequest() { return null }",
loader: "ts",
}))
}
},
}
////////////////////////// esbuild-svelte
const sveltePlugin = require("esbuild-svelte")
@@ -84,7 +103,10 @@ const options = {
minify: process.argv[2] == "build",
bundle: true,
splitting: false,
plugins: [esbuildSvelte, postcssPlugin(), resolvePlugin],
define: {
__MOCK__: process.env.MOCK === "1" ? "true" : "false",
},
plugins: [esbuildSvelte, postcssPlugin(), resolvePlugin, mockPlugin],
loader: {
".woff2": "file",
".woff": "file",