@tanstack/router-plugin 1.58.3 → 1.58.10
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/dist/cjs/core/router-code-splitter-plugin.cjs +2 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-generator-plugin.cjs +16 -2
- package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +3 -2
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-generator-plugin.js +16 -2
- package/dist/esm/core/router-generator-plugin.js.map +1 -1
- package/package.json +1 -1
- package/src/core/router-code-splitter-plugin.ts +8 -3
- package/src/core/router-generator-plugin.ts +26 -6
|
@@ -10,7 +10,8 @@ function capitalizeFirst(str) {
|
|
|
10
10
|
}
|
|
11
11
|
function fileIsInRoutesDirectory(filePath, routesDirectory) {
|
|
12
12
|
const routesDirectoryPath = node_path.isAbsolute(routesDirectory) ? routesDirectory : node_path.join(process.cwd(), routesDirectory);
|
|
13
|
-
|
|
13
|
+
const path = node_path.normalize(filePath);
|
|
14
|
+
return path.startsWith(routesDirectoryPath);
|
|
14
15
|
}
|
|
15
16
|
const bannedBeforeExternalPlugins = [
|
|
16
17
|
{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-code-splitter-plugin.cjs","sources":["../../../src/core/router-code-splitter-plugin.ts"],"sourcesContent":["import { isAbsolute, join } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\n\nimport { getConfig } from './config'\nimport {\n compileCodeSplitReferenceRoute,\n compileCodeSplitVirtualRoute,\n} from './code-splitter/compilers'\nimport { splitPrefix } from './constants'\n\nimport type { Config } from './config'\nimport type { UnpluginContextMeta, UnpluginFactory } from 'unplugin'\n\nfunction capitalizeFirst(str: string): string {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nfunction fileIsInRoutesDirectory(filePath: string, routesDirectory: string) {\n const routesDirectoryPath = isAbsolute(routesDirectory)\n ? routesDirectory\n : join(process.cwd(), routesDirectory)\n\n return filePath.startsWith(routesDirectoryPath)\n}\n\ntype BannedBeforeExternalPlugin = {\n identifier: string\n pkg: string\n usage: string\n frameworks: Array<UnpluginContextMeta['framework']>\n}\n\nconst bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n frameworks: ['vite'],\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(externalPlugin: BannedBeforeExternalPlugin, framework: string) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nconst PLUGIN_NAME = 'unplugin:router-code-splitter'\nconst JoinedSplitPrefix = splitPrefix + ':'\n\nexport const unpluginRouterCodeSplitterFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, { framework }) => {\n const debug = Boolean(process.env.TSR_VITE_DEBUG)\n\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const handleSplittingFile = (code: string, id: string) => {\n if (debug) console.info('Splitting route: ', id)\n\n const compiledVirtualRoute = compileCodeSplitVirtualRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Split Output')\n if (debug) console.info('')\n if (debug) console.info(compiledVirtualRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledVirtualRoute\n }\n\n const handleCompilingFile = (code: string, id: string) => {\n if (debug) console.info('Handling createRoute: ', id)\n\n const compiledReferenceRoute = compileCodeSplitReferenceRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Compiled Output')\n if (debug) console.info('')\n if (debug) console.info(compiledReferenceRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledReferenceRoute\n }\n\n return {\n name: 'router-code-splitter-plugin',\n enforce: 'pre',\n\n resolveId(source) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n if (source.startsWith(splitPrefix + ':')) {\n return source.replace(splitPrefix + ':', '')\n }\n return null\n },\n\n transform(code, id) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (id.includes(splitPrefix)) {\n return handleSplittingFile(code, id)\n } else if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&\n (code.includes('createRoute(') || code.includes('createFileRoute('))\n ) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (!externalPlugin.frameworks.includes(framework)) {\n continue\n }\n\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return handleCompilingFile(code, id)\n }\n\n return null\n },\n\n transformInclude(transformId) {\n if (!userConfig.autoCodeSplitting) {\n return undefined\n }\n\n let id = transformId\n\n if (id.startsWith(JoinedSplitPrefix)) {\n id = id.replace(JoinedSplitPrefix, '')\n }\n\n if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||\n id.includes(splitPrefix)\n ) {\n return true\n }\n return false\n },\n\n vite: {\n configResolved(config) {\n ROOT = config.root\n\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n },\n\n webpack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n\n if (\n userConfig.autoCodeSplitting &&\n compiler.options.mode === 'production'\n ) {\n compiler.hooks.done.tap(PLUGIN_NAME, () => {\n console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":["isAbsolute","join","splitPrefix","compileCodeSplitVirtualRoute","compileCodeSplitReferenceRoute","pathToFileURL","fileURLToPath","config","getConfig"],"mappings":";;;;;;;AAaA,SAAS,gBAAgB,KAAqB;AACrC,SAAA,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC;AAClD;AAEA,SAAS,wBAAwB,UAAkB,iBAAyB;AACpE,QAAA,sBAAsBA,qBAAW,eAAe,IAClD,kBACAC,UAAK,KAAA,QAAQ,OAAO,eAAe;AAEhC,SAAA,SAAS,WAAW,mBAAmB;AAChD;AASA,MAAM,8BAAiE;AAAA,EACrE;AAAA,IACE,YAAY;AAAA,IACZ,KAAK;AAAA,IACL,OAAO;AAAA,IACP,YAAY,CAAC,MAAM;AAAA,EACrB;AACF;AAEA,MAAM,gCAAgC,MAAM;AAAA,EAC1C,YAAY,gBAA4C,WAAmB;AACzE,UAAM,yBAAyB,eAAe,GAAG,oHAAoH,eAAe,GAAG;AAAA;AAAA;AAAA,kBAGzK,gBAAgB,SAAS,CAAC,4BAA4B,eAAe,KAAK;AAAA,IACxF,eAAe,KAAK;AAAA;AAAA,CAEvB;AAAA,EACC;AACF;AAEA,MAAM,cAAc;AACpB,MAAM,oBAAo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AAAA,IAEA,OAAO,UAAU;AACf,aAAO,QAAQ;AAEf,eAAS,MAAM,cAAc,IAAI,aAAa,CAAC,SAAS;AACjD,aAAA,oBAAoB,MAAM,cAAc;AAAA,UAC3C;AAAA,UACA,CAAC,gBAAqC;AACpC,gBAAI,YAAY,QAAQ,SAAS,iBAAiB,GAAG;AACvC,0BAAA,UAAU,YAAY,QAAQ;AAAA,gBACxC;AAAA,gBACA;AAAA,cAAA;AAAA,YAEJ;AAAA,UACF;AAAA,QAAA;AAAA,MACF,CACD;AAEY,mBAAAA,OAAA,UAAU,SAAS,IAAI;AAAA,IACtC;AAAA,IAEA,QAAQ,UAAU;AAChB,aAAO,QAAQ;AAEf,eAAS,MAAM,cAAc,IAAI,aAAa,CAAC,SAAS;AACjD,aAAA,oBAAoB,MAAM,cAAc;AAAA,UAC3C;AAAA,UACA,CAAC,gBAAqC;AACpC,gBAAI,YAAY,QAAQ,SAAS,iBAAiB,GAAG;AACvC,0BAAA,UAAU,YAAY,QAAQ;AAAA,gBACxC;AAAA,gBACA;AAAA,cAAA;AAAA,YAEJ;AAAA,UACF;AAAA,QAAA;AAAA,MACF,CACD;AAEY,mBAAAA,OAAA,UAAU,SAAS,IAAI;AAEpC,UACE,WAAW,qBACX,SAAS,QAAQ,SAAS,cAC1B;AACA,iBAAS,MAAM,KAAK,IAAI,aAAa,MAAM;AACjC,kBAAA,KAAK,OAAO,cAAc,wBAAwB;AAC1D,qBAAW,MAAM;AACf,oBAAQ,KAAK,CAAC;AAAA,UAAA,CACf;AAAA,QAAA,CACF;AAAA,MACH;AAAA,IACF;AAAA,EAAA;AAEJ;;"}
|
|
1
|
+
{"version":3,"file":"router-code-splitter-plugin.cjs","sources":["../../../src/core/router-code-splitter-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\n\nimport { getConfig } from './config'\nimport {\n compileCodeSplitReferenceRoute,\n compileCodeSplitVirtualRoute,\n} from './code-splitter/compilers'\nimport { splitPrefix } from './constants'\n\nimport type { Config } from './config'\nimport type { UnpluginContextMeta, UnpluginFactory } from 'unplugin'\n\nfunction capitalizeFirst(str: string): string {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nfunction fileIsInRoutesDirectory(\n filePath: string,\n routesDirectory: string,\n): boolean {\n const routesDirectoryPath = isAbsolute(routesDirectory)\n ? routesDirectory\n : join(process.cwd(), routesDirectory)\n\n const path = normalize(filePath)\n\n return path.startsWith(routesDirectoryPath)\n}\n\ntype BannedBeforeExternalPlugin = {\n identifier: string\n pkg: string\n usage: string\n frameworks: Array<UnpluginContextMeta['framework']>\n}\n\nconst bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n frameworks: ['vite'],\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(externalPlugin: BannedBeforeExternalPlugin, framework: string) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nconst PLUGIN_NAME = 'unplugin:router-code-splitter'\nconst JoinedSplitPrefix = splitPrefix + ':'\n\nexport const unpluginRouterCodeSplitterFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, { framework }) => {\n const debug = Boolean(process.env.TSR_VITE_DEBUG)\n\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const handleSplittingFile = (code: string, id: string) => {\n if (debug) console.info('Splitting route: ', id)\n\n const compiledVirtualRoute = compileCodeSplitVirtualRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Split Output')\n if (debug) console.info('')\n if (debug) console.info(compiledVirtualRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledVirtualRoute\n }\n\n const handleCompilingFile = (code: string, id: string) => {\n if (debug) console.info('Handling createRoute: ', id)\n\n const compiledReferenceRoute = compileCodeSplitReferenceRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Compiled Output')\n if (debug) console.info('')\n if (debug) console.info(compiledReferenceRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledReferenceRoute\n }\n\n return {\n name: 'router-code-splitter-plugin',\n enforce: 'pre',\n\n resolveId(source) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n if (source.startsWith(splitPrefix + ':')) {\n return source.replace(splitPrefix + ':', '')\n }\n return null\n },\n\n transform(code, id) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (id.includes(splitPrefix)) {\n return handleSplittingFile(code, id)\n } else if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&\n (code.includes('createRoute(') || code.includes('createFileRoute('))\n ) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (!externalPlugin.frameworks.includes(framework)) {\n continue\n }\n\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return handleCompilingFile(code, id)\n }\n\n return null\n },\n\n transformInclude(transformId) {\n if (!userConfig.autoCodeSplitting) {\n return undefined\n }\n\n let id = transformId\n\n if (id.startsWith(JoinedSplitPrefix)) {\n id = id.replace(JoinedSplitPrefix, '')\n }\n\n if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||\n id.includes(splitPrefix)\n ) {\n return true\n }\n return false\n },\n\n vite: {\n configResolved(config) {\n ROOT = config.root\n\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n },\n\n webpack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n\n if (\n userConfig.autoCodeSplitting &&\n compiler.options.mode === 'production'\n ) {\n compiler.hooks.done.tap(PLUGIN_NAME, () => {\n console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":["isAbsolute","join","normalize","splitPrefix","compileCodeSplitVirtualRoute","compileCodeSplitReferenceRoute","pathToFileURL","fileURLToPath","config","getConfig"],"mappings":";;;;;;;AAaA,SAAS,gBAAgB,KAAqB;AACrC,SAAA,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC;AAClD;AAEA,SAAS,wBACP,UACA,iBACS;AACH,QAAA,sBAAsBA,qBAAW,eAAe,IAClD,kBACAC,UAAK,KAAA,QAAQ,OAAO,eAAe;AAEjC,QAAA,OAAOC,oBAAU,QAAQ;AAExB,SAAA,KAAK,WAAW,mBAAmB;AAC5C;AASA,MAAM,8BAAiE;AAAA,EACrE;AAAA,IACE,YAAY;AAAA,IACZ,KAAK;AAAA,IACL,OAAO;AAAA,IACP,YAAY,CAAC,MAAM;AAAA,EACrB;AACF;AAEA,MAAM,gCAAgC,MAAM;AAAA,EAC1C,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;AAEJ;;"}
|
|
@@ -92,9 +92,16 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
92
92
|
} else {
|
|
93
93
|
const routesDirectoryPath = getRoutesDirectoryPath();
|
|
94
94
|
const chokidar = await import("chokidar");
|
|
95
|
-
chokidar.watch(routesDirectoryPath).on("add", async () => {
|
|
95
|
+
chokidar.watch(routesDirectoryPath, { ignoreInitial: true }).on("add", async () => {
|
|
96
96
|
await run(generate);
|
|
97
97
|
});
|
|
98
|
+
let generated = false;
|
|
99
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
100
|
+
if (!generated) {
|
|
101
|
+
generated = true;
|
|
102
|
+
return run(generate);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
98
105
|
}
|
|
99
106
|
},
|
|
100
107
|
async webpack(compiler) {
|
|
@@ -104,9 +111,16 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
104
111
|
} else {
|
|
105
112
|
const routesDirectoryPath = getRoutesDirectoryPath();
|
|
106
113
|
const chokidar = await import("chokidar");
|
|
107
|
-
chokidar.watch(routesDirectoryPath).on("add", async () => {
|
|
114
|
+
chokidar.watch(routesDirectoryPath, { ignoreInitial: true }).on("add", async () => {
|
|
108
115
|
await run(generate);
|
|
109
116
|
});
|
|
117
|
+
let generated = false;
|
|
118
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
119
|
+
if (!generated) {
|
|
120
|
+
generated = true;
|
|
121
|
+
return run(generate);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
110
124
|
}
|
|
111
125
|
if (compiler.options.mode === "production") {
|
|
112
126
|
compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-generator-plugin.cjs","sources":["../../../src/core/router-generator-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize, resolve } from 'node:path'\nimport { generator } from '@tanstack/router-generator'\n\nimport { getConfig } from './config'\nimport { CONFIG_FILE_NAME } from './constants'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\n\nlet lock = false\nconst checkLock = () => lock\nconst setLock = (bool: boolean) => {\n lock = bool\n}\n\nconst PLUGIN_NAME = 'unplugin:router-generator'\n\nexport const unpluginRouterGeneratorFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}) => {\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const getRoutesDirectoryPath = () => {\n return isAbsolute(userConfig.routesDirectory)\n ? userConfig.routesDirectory\n : join(ROOT, userConfig.routesDirectory)\n }\n\n const generate = async () => {\n if (checkLock()) {\n return\n }\n\n setLock(true)\n\n try {\n await generator(userConfig)\n } catch (err) {\n console.error(err)\n console.info()\n } finally {\n setLock(false)\n }\n }\n\n const handleFile = async (\n file: string,\n event: 'create' | 'update' | 'delete',\n ) => {\n const filePath = normalize(file)\n\n if (filePath === join(ROOT, CONFIG_FILE_NAME)) {\n userConfig = getConfig(options, ROOT)\n return\n }\n\n if (\n event === 'update' &&\n filePath === resolve(userConfig.generatedRouteTree)\n ) {\n // skip generating routes if the generated route tree is updated\n return\n }\n\n const routesDirectoryPath = getRoutesDirectoryPath()\n if (filePath.startsWith(routesDirectoryPath)) {\n await generate()\n }\n }\n\n const run: (cb: () => Promise<void> | void) => Promise<void> = async (cb) => {\n if (userConfig.enableRouteGeneration ?? true) {\n await cb()\n }\n }\n\n return {\n name: 'router-generator-plugin',\n async watchChange(id, { event }) {\n await run(async () => {\n await handleFile(id, event)\n })\n },\n vite: {\n async configResolved(config) {\n ROOT = config.root\n userConfig = getConfig(options, ROOT)\n\n await run(generate)\n },\n },\n async rspack(compiler) {\n userConfig = getConfig(options, ROOT)\n\n
|
|
1
|
+
{"version":3,"file":"router-generator-plugin.cjs","sources":["../../../src/core/router-generator-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize, resolve } from 'node:path'\nimport { generator } from '@tanstack/router-generator'\n\nimport { getConfig } from './config'\nimport { CONFIG_FILE_NAME } from './constants'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\n\nlet lock = false\nconst checkLock = () => lock\nconst setLock = (bool: boolean) => {\n lock = bool\n}\n\nconst PLUGIN_NAME = 'unplugin:router-generator'\n\nexport const unpluginRouterGeneratorFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}) => {\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const getRoutesDirectoryPath = () => {\n return isAbsolute(userConfig.routesDirectory)\n ? userConfig.routesDirectory\n : join(ROOT, userConfig.routesDirectory)\n }\n\n const generate = async () => {\n if (checkLock()) {\n return\n }\n\n setLock(true)\n\n try {\n await generator(userConfig)\n } catch (err) {\n console.error(err)\n console.info()\n } finally {\n setLock(false)\n }\n }\n\n const handleFile = async (\n file: string,\n event: 'create' | 'update' | 'delete',\n ) => {\n const filePath = normalize(file)\n\n if (filePath === join(ROOT, CONFIG_FILE_NAME)) {\n userConfig = getConfig(options, ROOT)\n return\n }\n\n if (\n event === 'update' &&\n filePath === resolve(userConfig.generatedRouteTree)\n ) {\n // skip generating routes if the generated route tree is updated\n return\n }\n\n const routesDirectoryPath = getRoutesDirectoryPath()\n if (filePath.startsWith(routesDirectoryPath)) {\n await generate()\n }\n }\n\n const run: (cb: () => Promise<void> | void) => Promise<void> = async (cb) => {\n if (userConfig.enableRouteGeneration ?? true) {\n await cb()\n }\n }\n\n return {\n name: 'router-generator-plugin',\n async watchChange(id, { event }) {\n await run(async () => {\n await handleFile(id, event)\n })\n },\n vite: {\n async configResolved(config) {\n ROOT = config.root\n userConfig = getConfig(options, ROOT)\n\n await run(generate)\n },\n },\n async rspack(compiler) {\n userConfig = getConfig(options, ROOT)\n\n if (compiler.options.mode === 'production') {\n await run(generate)\n } else {\n // rspack watcher doesn't register newly created files\n const routesDirectoryPath = getRoutesDirectoryPath()\n const chokidar = await import('chokidar')\n chokidar\n .watch(routesDirectoryPath, { ignoreInitial: true })\n .on('add', async () => {\n await run(generate)\n })\n\n let generated = false\n compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n if (!generated) {\n generated = true\n return run(generate)\n }\n })\n }\n },\n async webpack(compiler) {\n userConfig = getConfig(options, ROOT)\n\n if (compiler.options.mode === 'production') {\n await run(generate)\n } else {\n // webpack watcher doesn't register newly created files\n const routesDirectoryPath = getRoutesDirectoryPath()\n const chokidar = await import('chokidar')\n chokidar\n .watch(routesDirectoryPath, { ignoreInitial: true })\n .on('add', async () => {\n await run(generate)\n })\n\n let generated = false\n compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n if (!generated) {\n generated = true\n return run(generate)\n }\n })\n }\n\n if (compiler.options.mode === 'production') {\n compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {\n console.info('✅ ' + PLUGIN_NAME + ': route-tree generation done')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":["isAbsolute","join","generator","normalize","CONFIG_FILE_NAME","getConfig","resolve","config"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA,IAAI,OAAO;AACX,MAAM,YAAY,MAAM;AACxB,MAAM,UAAU,CAAC,SAAkB;AAC1B,SAAA;AACT;AAEA,MAAM,cAAc;AAEb,MAAM,iCAET,CAAC,UAAU,OAAO;AAChB,MAAA,OAAe,QAAQ;AAC3B,MAAI,aAAa;AAEjB,QAAM,yBAAyB,MAAM;AAC5B,WAAAA,qBAAW,WAAW,eAAe,IACxC,WAAW,kBACXC,UAAAA,KAAK,MAAM,WAAW,eAAe;AAAA,EAAA;AAG3C,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf;AAAA,IACF;AAEA,YAAQ,IAAI;AAER,QAAA;AACF,YAAMC,gBAAAA,UAAU,UAAU;AAAA,aACnB,KAAK;AACZ,cAAQ,MAAM,GAAG;AACjB,cAAQ,KAAK;AAAA,IAAA,UACb;AACA,cAAQ,KAAK;AAAA,IACf;AAAA,EAAA;AAGI,QAAA,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AAA,CACD;AAAA,MACH;AAAA,IACF;AAAA,IACA,MAAM,QAAQ,UAAU;AACT,mBAAAA,OAAA,UAAU,SAAS,IAAI;AAEhC,UAAA,SAAS,QAAQ,SAAS,cAAc;AAC1C,cAAM,IAAI,QAAQ;AAAA,MAAA,OACb;AAEL,cAAM,sBAAsB;AACtB,cAAA,WAAW,MAAM,OAAO,UAAU;AAErC,iBAAA,MAAM,qBAAqB,EAAE,eAAe,MAAM,EAClD,GAAG,OAAO,YAAY;AACrB,gBAAM,IAAI,QAAQ;AAAA,QAAA,CACnB;AAEH,YAAI,YAAY;AAChB,iBAAS,MAAM,SAAS,WAAW,aAAa,YAAY;AAC1D,cAAI,CAAC,WAAW;AACF,wBAAA;AACZ,mBAAO,IAAI,QAAQ;AAAA,UACrB;AAAA,QAAA,CACD;AAAA,MACH;AAEI,UAAA,SAAS,QAAQ,SAAS,cAAc;AAC1C,iBAAS,MAAM,KAAK,IAAI,aAAa,CAAC,UAAU;AACtC,kBAAA,KAAK,OAAO,cAAc,8BAA8B;AAChE,qBAAW,MAAM;AACf,oBAAQ,KAAK,CAAC;AAAA,UAAA,CACf;AAAA,QAAA,CACF;AAAA,MACH;AAAA,IACF;AAAA,EAAA;AAEJ;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isAbsolute, join } from "node:path";
|
|
1
|
+
import { isAbsolute, join, normalize } from "node:path";
|
|
2
2
|
import { pathToFileURL, fileURLToPath } from "node:url";
|
|
3
3
|
import { getConfig } from "./config.js";
|
|
4
4
|
import { compileCodeSplitVirtualRoute, compileCodeSplitReferenceRoute } from "./code-splitter/compilers.js";
|
|
@@ -8,7 +8,8 @@ function capitalizeFirst(str) {
|
|
|
8
8
|
}
|
|
9
9
|
function fileIsInRoutesDirectory(filePath, routesDirectory) {
|
|
10
10
|
const routesDirectoryPath = isAbsolute(routesDirectory) ? routesDirectory : join(process.cwd(), routesDirectory);
|
|
11
|
-
|
|
11
|
+
const path = normalize(filePath);
|
|
12
|
+
return path.startsWith(routesDirectoryPath);
|
|
12
13
|
}
|
|
13
14
|
const bannedBeforeExternalPlugins = [
|
|
14
15
|
{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-code-splitter-plugin.js","sources":["../../../src/core/router-code-splitter-plugin.ts"],"sourcesContent":["import { isAbsolute, join } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\n\nimport { getConfig } from './config'\nimport {\n compileCodeSplitReferenceRoute,\n compileCodeSplitVirtualRoute,\n} from './code-splitter/compilers'\nimport { splitPrefix } from './constants'\n\nimport type { Config } from './config'\nimport type { UnpluginContextMeta, UnpluginFactory } from 'unplugin'\n\nfunction capitalizeFirst(str: string): string {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nfunction fileIsInRoutesDirectory(filePath: string, routesDirectory: string) {\n const routesDirectoryPath = isAbsolute(routesDirectory)\n ? routesDirectory\n : join(process.cwd(), routesDirectory)\n\n return filePath.startsWith(routesDirectoryPath)\n}\n\ntype BannedBeforeExternalPlugin = {\n identifier: string\n pkg: string\n usage: string\n frameworks: Array<UnpluginContextMeta['framework']>\n}\n\nconst bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n frameworks: ['vite'],\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(externalPlugin: BannedBeforeExternalPlugin, framework: string) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nconst PLUGIN_NAME = 'unplugin:router-code-splitter'\nconst JoinedSplitPrefix = splitPrefix + ':'\n\nexport const unpluginRouterCodeSplitterFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, { framework }) => {\n const debug = Boolean(process.env.TSR_VITE_DEBUG)\n\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const handleSplittingFile = (code: string, id: string) => {\n if (debug) console.info('Splitting route: ', id)\n\n const compiledVirtualRoute = compileCodeSplitVirtualRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Split Output')\n if (debug) console.info('')\n if (debug) console.info(compiledVirtualRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledVirtualRoute\n }\n\n const handleCompilingFile = (code: string, id: string) => {\n if (debug) console.info('Handling createRoute: ', id)\n\n const compiledReferenceRoute = compileCodeSplitReferenceRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Compiled Output')\n if (debug) console.info('')\n if (debug) console.info(compiledReferenceRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledReferenceRoute\n }\n\n return {\n name: 'router-code-splitter-plugin',\n enforce: 'pre',\n\n resolveId(source) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n if (source.startsWith(splitPrefix + ':')) {\n return source.replace(splitPrefix + ':', '')\n }\n return null\n },\n\n transform(code, id) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (id.includes(splitPrefix)) {\n return handleSplittingFile(code, id)\n } else if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&\n (code.includes('createRoute(') || code.includes('createFileRoute('))\n ) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (!externalPlugin.frameworks.includes(framework)) {\n continue\n }\n\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return handleCompilingFile(code, id)\n }\n\n return null\n },\n\n transformInclude(transformId) {\n if (!userConfig.autoCodeSplitting) {\n return undefined\n }\n\n let id = transformId\n\n if (id.startsWith(JoinedSplitPrefix)) {\n id = id.replace(JoinedSplitPrefix, '')\n }\n\n if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||\n id.includes(splitPrefix)\n ) {\n return true\n }\n return false\n },\n\n vite: {\n configResolved(config) {\n ROOT = config.root\n\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n },\n\n webpack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n\n if (\n userConfig.autoCodeSplitting &&\n compiler.options.mode === 'production'\n ) {\n compiler.hooks.done.tap(PLUGIN_NAME, () => {\n console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":[],"mappings":";;;;;AAaA,SAAS,gBAAgB,KAAqB;AACrC,SAAA,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC;AAClD;AAEA,SAAS,wBAAwB,UAAkB,iBAAyB;AACpE,QAAA,sBAAsB,WAAW,eAAe,IAClD,kBACA,KAAK,QAAQ,OAAO,eAAe;AAEhC,SAAA,SAAS,WAAW,mBAAmB;A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oBAAQ,KAAK,CAAC;AAAA,UAAA,CACf;AAAA,QAAA,CACF;AAAA,MACH;AAAA,IACF;AAAA,EAAA;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"router-code-splitter-plugin.js","sources":["../../../src/core/router-code-splitter-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize } from 'node:path'\nimport { fileURLToPath, pathToFileURL } from 'node:url'\n\nimport { getConfig } from './config'\nimport {\n compileCodeSplitReferenceRoute,\n compileCodeSplitVirtualRoute,\n} from './code-splitter/compilers'\nimport { splitPrefix } from './constants'\n\nimport type { Config } from './config'\nimport type { UnpluginContextMeta, UnpluginFactory } from 'unplugin'\n\nfunction capitalizeFirst(str: string): string {\n return str.charAt(0).toUpperCase() + str.slice(1)\n}\n\nfunction fileIsInRoutesDirectory(\n filePath: string,\n routesDirectory: string,\n): boolean {\n const routesDirectoryPath = isAbsolute(routesDirectory)\n ? routesDirectory\n : join(process.cwd(), routesDirectory)\n\n const path = normalize(filePath)\n\n return path.startsWith(routesDirectoryPath)\n}\n\ntype BannedBeforeExternalPlugin = {\n identifier: string\n pkg: string\n usage: string\n frameworks: Array<UnpluginContextMeta['framework']>\n}\n\nconst bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [\n {\n identifier: '@react-refresh',\n pkg: '@vitejs/plugin-react',\n usage: 'viteReact()',\n frameworks: ['vite'],\n },\n]\n\nclass FoundPluginInBeforeCode extends Error {\n constructor(externalPlugin: BannedBeforeExternalPlugin, framework: string) {\n super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again: \ne.g.\nplugins: [\n TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}\n ${externalPlugin.usage},\n]\n`)\n }\n}\n\nconst PLUGIN_NAME = 'unplugin:router-code-splitter'\nconst JoinedSplitPrefix = splitPrefix + ':'\n\nexport const unpluginRouterCodeSplitterFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, { framework }) => {\n const debug = Boolean(process.env.TSR_VITE_DEBUG)\n\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const handleSplittingFile = (code: string, id: string) => {\n if (debug) console.info('Splitting route: ', id)\n\n const compiledVirtualRoute = compileCodeSplitVirtualRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Split Output')\n if (debug) console.info('')\n if (debug) console.info(compiledVirtualRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledVirtualRoute\n }\n\n const handleCompilingFile = (code: string, id: string) => {\n if (debug) console.info('Handling createRoute: ', id)\n\n const compiledReferenceRoute = compileCodeSplitReferenceRoute({\n code,\n root: ROOT,\n filename: id,\n })\n\n if (debug) console.info('')\n if (debug) console.info('Compiled Output')\n if (debug) console.info('')\n if (debug) console.info(compiledReferenceRoute.code)\n if (debug) console.info('')\n if (debug) console.info('')\n if (debug) console.info('')\n\n return compiledReferenceRoute\n }\n\n return {\n name: 'router-code-splitter-plugin',\n enforce: 'pre',\n\n resolveId(source) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n if (source.startsWith(splitPrefix + ':')) {\n return source.replace(splitPrefix + ':', '')\n }\n return null\n },\n\n transform(code, id) {\n if (!userConfig.autoCodeSplitting) {\n return null\n }\n\n const url = pathToFileURL(id)\n url.searchParams.delete('v')\n id = fileURLToPath(url).replace(/\\\\/g, '/')\n\n if (id.includes(splitPrefix)) {\n return handleSplittingFile(code, id)\n } else if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&\n (code.includes('createRoute(') || code.includes('createFileRoute('))\n ) {\n for (const externalPlugin of bannedBeforeExternalPlugins) {\n if (!externalPlugin.frameworks.includes(framework)) {\n continue\n }\n\n if (code.includes(externalPlugin.identifier)) {\n throw new FoundPluginInBeforeCode(externalPlugin, framework)\n }\n }\n\n return handleCompilingFile(code, id)\n }\n\n return null\n },\n\n transformInclude(transformId) {\n if (!userConfig.autoCodeSplitting) {\n return undefined\n }\n\n let id = transformId\n\n if (id.startsWith(JoinedSplitPrefix)) {\n id = id.replace(JoinedSplitPrefix, '')\n }\n\n if (\n fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||\n id.includes(splitPrefix)\n ) {\n return true\n }\n return false\n },\n\n vite: {\n configResolved(config) {\n ROOT = config.root\n\n userConfig = getConfig(options, ROOT)\n },\n },\n\n rspack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n },\n\n webpack(compiler) {\n ROOT = process.cwd()\n\n compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {\n self.normalModuleFactory.hooks.beforeResolve.tap(\n PLUGIN_NAME,\n (resolveData: { request: string }) => {\n if (resolveData.request.includes(JoinedSplitPrefix)) {\n resolveData.request = resolveData.request.replace(\n JoinedSplitPrefix,\n '',\n )\n }\n },\n )\n })\n\n userConfig = getConfig(options, ROOT)\n\n if (\n userConfig.autoCodeSplitting &&\n compiler.options.mode === 'production'\n ) {\n compiler.hooks.done.tap(PLUGIN_NAME, () => {\n console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":[],"mappings":";;;;;AAaA,SAAS,gBAAgB,KAAqB;AACrC,SAAA,IAAI,OAAO,CAAC,EAAE,gBAAgB,IAAI,MAAM,CAAC;AAClD;AAEA,SAAS,wBACP,UACA,iBACS;AACH,QAAA,sBAAsB,WAAW,eAAe,IAClD,kBACA,KAAK,QAAQ,OAAO,eAAe;AAEjC,QAAA,OAAO,UAAU,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O,cAAc,wBAAwB;AAC1D,qBAAW,MAAM;AACf,oBAAQ,KAAK,CAAC;AAAA,UAAA,CACf;AAAA,QAAA,CACF;AAAA,MACH;AAAA,IACF;AAAA,EAAA;AAEJ;"}
|
|
@@ -68,9 +68,16 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
68
68
|
} else {
|
|
69
69
|
const routesDirectoryPath = getRoutesDirectoryPath();
|
|
70
70
|
const chokidar = await import("chokidar");
|
|
71
|
-
chokidar.watch(routesDirectoryPath).on("add", async () => {
|
|
71
|
+
chokidar.watch(routesDirectoryPath, { ignoreInitial: true }).on("add", async () => {
|
|
72
72
|
await run(generate);
|
|
73
73
|
});
|
|
74
|
+
let generated = false;
|
|
75
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
76
|
+
if (!generated) {
|
|
77
|
+
generated = true;
|
|
78
|
+
return run(generate);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
74
81
|
}
|
|
75
82
|
},
|
|
76
83
|
async webpack(compiler) {
|
|
@@ -80,9 +87,16 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
80
87
|
} else {
|
|
81
88
|
const routesDirectoryPath = getRoutesDirectoryPath();
|
|
82
89
|
const chokidar = await import("chokidar");
|
|
83
|
-
chokidar.watch(routesDirectoryPath).on("add", async () => {
|
|
90
|
+
chokidar.watch(routesDirectoryPath, { ignoreInitial: true }).on("add", async () => {
|
|
84
91
|
await run(generate);
|
|
85
92
|
});
|
|
93
|
+
let generated = false;
|
|
94
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
95
|
+
if (!generated) {
|
|
96
|
+
generated = true;
|
|
97
|
+
return run(generate);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
86
100
|
}
|
|
87
101
|
if (compiler.options.mode === "production") {
|
|
88
102
|
compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-generator-plugin.js","sources":["../../../src/core/router-generator-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize, resolve } from 'node:path'\nimport { generator } from '@tanstack/router-generator'\n\nimport { getConfig } from './config'\nimport { CONFIG_FILE_NAME } from './constants'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\n\nlet lock = false\nconst checkLock = () => lock\nconst setLock = (bool: boolean) => {\n lock = bool\n}\n\nconst PLUGIN_NAME = 'unplugin:router-generator'\n\nexport const unpluginRouterGeneratorFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}) => {\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const getRoutesDirectoryPath = () => {\n return isAbsolute(userConfig.routesDirectory)\n ? userConfig.routesDirectory\n : join(ROOT, userConfig.routesDirectory)\n }\n\n const generate = async () => {\n if (checkLock()) {\n return\n }\n\n setLock(true)\n\n try {\n await generator(userConfig)\n } catch (err) {\n console.error(err)\n console.info()\n } finally {\n setLock(false)\n }\n }\n\n const handleFile = async (\n file: string,\n event: 'create' | 'update' | 'delete',\n ) => {\n const filePath = normalize(file)\n\n if (filePath === join(ROOT, CONFIG_FILE_NAME)) {\n userConfig = getConfig(options, ROOT)\n return\n }\n\n if (\n event === 'update' &&\n filePath === resolve(userConfig.generatedRouteTree)\n ) {\n // skip generating routes if the generated route tree is updated\n return\n }\n\n const routesDirectoryPath = getRoutesDirectoryPath()\n if (filePath.startsWith(routesDirectoryPath)) {\n await generate()\n }\n }\n\n const run: (cb: () => Promise<void> | void) => Promise<void> = async (cb) => {\n if (userConfig.enableRouteGeneration ?? true) {\n await cb()\n }\n }\n\n return {\n name: 'router-generator-plugin',\n async watchChange(id, { event }) {\n await run(async () => {\n await handleFile(id, event)\n })\n },\n vite: {\n async configResolved(config) {\n ROOT = config.root\n userConfig = getConfig(options, ROOT)\n\n await run(generate)\n },\n },\n async rspack(compiler) {\n userConfig = getConfig(options, ROOT)\n\n
|
|
1
|
+
{"version":3,"file":"router-generator-plugin.js","sources":["../../../src/core/router-generator-plugin.ts"],"sourcesContent":["import { isAbsolute, join, normalize, resolve } from 'node:path'\nimport { generator } from '@tanstack/router-generator'\n\nimport { getConfig } from './config'\nimport { CONFIG_FILE_NAME } from './constants'\nimport type { UnpluginFactory } from 'unplugin'\nimport type { Config } from './config'\n\nlet lock = false\nconst checkLock = () => lock\nconst setLock = (bool: boolean) => {\n lock = bool\n}\n\nconst PLUGIN_NAME = 'unplugin:router-generator'\n\nexport const unpluginRouterGeneratorFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}) => {\n let ROOT: string = process.cwd()\n let userConfig = options as Config\n\n const getRoutesDirectoryPath = () => {\n return isAbsolute(userConfig.routesDirectory)\n ? userConfig.routesDirectory\n : join(ROOT, userConfig.routesDirectory)\n }\n\n const generate = async () => {\n if (checkLock()) {\n return\n }\n\n setLock(true)\n\n try {\n await generator(userConfig)\n } catch (err) {\n console.error(err)\n console.info()\n } finally {\n setLock(false)\n }\n }\n\n const handleFile = async (\n file: string,\n event: 'create' | 'update' | 'delete',\n ) => {\n const filePath = normalize(file)\n\n if (filePath === join(ROOT, CONFIG_FILE_NAME)) {\n userConfig = getConfig(options, ROOT)\n return\n }\n\n if (\n event === 'update' &&\n filePath === resolve(userConfig.generatedRouteTree)\n ) {\n // skip generating routes if the generated route tree is updated\n return\n }\n\n const routesDirectoryPath = getRoutesDirectoryPath()\n if (filePath.startsWith(routesDirectoryPath)) {\n await generate()\n }\n }\n\n const run: (cb: () => Promise<void> | void) => Promise<void> = async (cb) => {\n if (userConfig.enableRouteGeneration ?? true) {\n await cb()\n }\n }\n\n return {\n name: 'router-generator-plugin',\n async watchChange(id, { event }) {\n await run(async () => {\n await handleFile(id, event)\n })\n },\n vite: {\n async configResolved(config) {\n ROOT = config.root\n userConfig = getConfig(options, ROOT)\n\n await run(generate)\n },\n },\n async rspack(compiler) {\n userConfig = getConfig(options, ROOT)\n\n if (compiler.options.mode === 'production') {\n await run(generate)\n } else {\n // rspack watcher doesn't register newly created files\n const routesDirectoryPath = getRoutesDirectoryPath()\n const chokidar = await import('chokidar')\n chokidar\n .watch(routesDirectoryPath, { ignoreInitial: true })\n .on('add', async () => {\n await run(generate)\n })\n\n let generated = false\n compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n if (!generated) {\n generated = true\n return run(generate)\n }\n })\n }\n },\n async webpack(compiler) {\n userConfig = getConfig(options, ROOT)\n\n if (compiler.options.mode === 'production') {\n await run(generate)\n } else {\n // webpack watcher doesn't register newly created files\n const routesDirectoryPath = getRoutesDirectoryPath()\n const chokidar = await import('chokidar')\n chokidar\n .watch(routesDirectoryPath, { ignoreInitial: true })\n .on('add', async () => {\n await run(generate)\n })\n\n let generated = false\n compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {\n if (!generated) {\n generated = true\n return run(generate)\n }\n })\n }\n\n if (compiler.options.mode === 'production') {\n compiler.hooks.done.tap(PLUGIN_NAME, (stats) => {\n console.info('✅ ' + PLUGIN_NAME + ': route-tree generation done')\n setTimeout(() => {\n process.exit(0)\n })\n })\n }\n },\n }\n}\n"],"names":[],"mappings":";;;;AAQA,IAAI,OAAO;AACX,MAAM,YAAY,MAAM;AACxB,MAAM,UAAU,CAAC,SAAkB;AAC1B,SAAA;AACT;AAEA,MAAM,cAAc;AAEb,MAAM,iCAET,CAAC,UAAU,OAAO;AAChB,MAAA,OAAe,QAAQ;AAC3B,MAAI,aAAa;AAEjB,QAAM,yBAAyB,MAAM;AAC5B,WAAA,WAAW,WAAW,eAAe,IACxC,WAAW,kBACX,KAAK,MAAM,WAAW,eAAe;AAAA,EAAA;AAG3C,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf;AAAA,IACF;AAEA,YAAQ,IAAI;AAER,QAAA;AACF,YAAM,UAAU,UAAU;AAAA,aACnB,KAAK;AACZ,cAAQ,MAAM,GAAG;AACjB,cAAQ,KAAK;AAAA,IAAA,UACb;AACA,cAAQ,KAAK;AAAA,IACf;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IACF;AAAA,IACA,MAAM,QAAQ,UAAU;AACT,mBAAA,UAAU,SAAS,IAAI;AAEhC,UAAA,SAAS,QAAQ,SAAS,cAAc;AAC1C,cAAM,IAAI,QAAQ;AAAA,MAAA,OACb;AAEL,cAAM,sBAAsB;AACtB,cAAA,WAAW,MAAM,OAAO,UAAU;AAErC,iBAAA,MAAM,qBAAqB,EAAE,eAAe,MAAM,EAClD,GAAG,OAAO,YAAY;AACrB,gBAAM,IAAI,QAAQ;AAAA,QAAA,CACnB;AAEH,YAAI,YAAY;AAChB,iBAAS,MAAM,SAAS,WAAW,aAAa,YAAY;AAC1D,cAAI,CAAC,WAAW;AACF,wBAAA;AACZ,mBAAO,IAAI,QAAQ;AAAA,UACrB;AAAA,QAAA,CACD;AAAA,MACH;AAEI,UAAA,SAAS,QAAQ,SAAS,cAAc;AAC1C,iBAAS,MAAM,KAAK,IAAI,aAAa,CAAC,UAAU;AACtC,kBAAA,KAAK,OAAO,cAAc,8BAA8B;AAChE,qBAAW,MAAM;AACf,oBAAQ,KAAK,CAAC;AAAA,UAAA,CACf;AAAA,QAAA,CACF;AAAA,MACH;AAAA,IACF;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isAbsolute, join } from 'node:path'
|
|
1
|
+
import { isAbsolute, join, normalize } from 'node:path'
|
|
2
2
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
3
3
|
|
|
4
4
|
import { getConfig } from './config'
|
|
@@ -15,12 +15,17 @@ function capitalizeFirst(str: string): string {
|
|
|
15
15
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
function fileIsInRoutesDirectory(
|
|
18
|
+
function fileIsInRoutesDirectory(
|
|
19
|
+
filePath: string,
|
|
20
|
+
routesDirectory: string,
|
|
21
|
+
): boolean {
|
|
19
22
|
const routesDirectoryPath = isAbsolute(routesDirectory)
|
|
20
23
|
? routesDirectory
|
|
21
24
|
: join(process.cwd(), routesDirectory)
|
|
22
25
|
|
|
23
|
-
|
|
26
|
+
const path = normalize(filePath)
|
|
27
|
+
|
|
28
|
+
return path.startsWith(routesDirectoryPath)
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
type BannedBeforeExternalPlugin = {
|
|
@@ -92,28 +92,48 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
92
92
|
async rspack(compiler) {
|
|
93
93
|
userConfig = getConfig(options, ROOT)
|
|
94
94
|
|
|
95
|
-
// rspack watcher doesn't register newly created files
|
|
96
95
|
if (compiler.options.mode === 'production') {
|
|
97
96
|
await run(generate)
|
|
98
97
|
} else {
|
|
98
|
+
// rspack watcher doesn't register newly created files
|
|
99
99
|
const routesDirectoryPath = getRoutesDirectoryPath()
|
|
100
100
|
const chokidar = await import('chokidar')
|
|
101
|
-
chokidar
|
|
102
|
-
|
|
101
|
+
chokidar
|
|
102
|
+
.watch(routesDirectoryPath, { ignoreInitial: true })
|
|
103
|
+
.on('add', async () => {
|
|
104
|
+
await run(generate)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
let generated = false
|
|
108
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
109
|
+
if (!generated) {
|
|
110
|
+
generated = true
|
|
111
|
+
return run(generate)
|
|
112
|
+
}
|
|
103
113
|
})
|
|
104
114
|
}
|
|
105
115
|
},
|
|
106
116
|
async webpack(compiler) {
|
|
107
117
|
userConfig = getConfig(options, ROOT)
|
|
108
118
|
|
|
109
|
-
// webpack watcher doesn't register newly created files
|
|
110
119
|
if (compiler.options.mode === 'production') {
|
|
111
120
|
await run(generate)
|
|
112
121
|
} else {
|
|
122
|
+
// webpack watcher doesn't register newly created files
|
|
113
123
|
const routesDirectoryPath = getRoutesDirectoryPath()
|
|
114
124
|
const chokidar = await import('chokidar')
|
|
115
|
-
chokidar
|
|
116
|
-
|
|
125
|
+
chokidar
|
|
126
|
+
.watch(routesDirectoryPath, { ignoreInitial: true })
|
|
127
|
+
.on('add', async () => {
|
|
128
|
+
await run(generate)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
let generated = false
|
|
132
|
+
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
133
|
+
if (!generated) {
|
|
134
|
+
generated = true
|
|
135
|
+
return run(generate)
|
|
136
|
+
}
|
|
117
137
|
})
|
|
118
138
|
}
|
|
119
139
|
|