@tanstack/router-plugin 1.39.5 → 1.39.11
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/code-splitter.cjs +3 -0
- package/dist/cjs/code-splitter.cjs.map +1 -1
- package/dist/cjs/compilers.cjs +1 -1
- package/dist/cjs/compilers.cjs.map +1 -1
- package/dist/cjs/composed.cjs +16 -0
- package/dist/cjs/composed.cjs.map +1 -0
- package/dist/cjs/composed.d.cts +4 -0
- package/dist/cjs/router-generator.cjs +8 -0
- package/dist/cjs/router-generator.cjs.map +1 -1
- package/dist/cjs/router-generator.d.cts +1 -1
- package/dist/cjs/rspack.cjs +20 -0
- package/dist/cjs/rspack.cjs.map +1 -0
- package/dist/cjs/rspack.d.cts +107 -0
- package/dist/cjs/vite.cjs +2 -6
- package/dist/cjs/vite.cjs.map +1 -1
- package/dist/cjs/vite.d.cts +47 -4
- package/dist/esm/code-splitter.js +3 -0
- package/dist/esm/code-splitter.js.map +1 -1
- package/dist/esm/compilers.js +1 -1
- package/dist/esm/compilers.js.map +1 -1
- package/dist/esm/composed.d.ts +4 -0
- package/dist/esm/composed.js +16 -0
- package/dist/esm/composed.js.map +1 -0
- package/dist/esm/router-generator.d.ts +1 -1
- package/dist/esm/router-generator.js +8 -0
- package/dist/esm/router-generator.js.map +1 -1
- package/dist/esm/rspack.d.ts +107 -0
- package/dist/esm/rspack.js +20 -0
- package/dist/esm/rspack.js.map +1 -0
- package/dist/esm/vite.d.ts +47 -4
- package/dist/esm/vite.js +2 -6
- package/dist/esm/vite.js.map +1 -1
- package/package.json +16 -2
- package/src/code-splitter.ts +9 -0
- package/src/compilers.ts +4 -1
- package/src/composed.ts +30 -0
- package/src/router-generator.ts +12 -1
- package/src/rspack.ts +74 -0
- package/src/vite.ts +30 -7
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compilers.js","sources":["../../src/compilers.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport * as template from '@babel/template'\nimport { splitPrefix } from './constants'\nimport { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers'\nimport type * as babel from '@babel/core'\nimport type { CompileAstFn } from './ast'\n\ntype SplitModulesById = Record<\n string,\n { id: string; node: t.FunctionExpression }\n>\n\ninterface State {\n filename: string\n opts: {\n minify: boolean\n root: string\n }\n imported: Record<string, boolean>\n refs: Set<any>\n serverIndex: number\n splitIndex: number\n splitModulesById: SplitModulesById\n}\n\nexport async function compileFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`\n\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n let found = false\n\n const hasImportedOrDefinedIdentifier = (\n name: string,\n ) => {\n return programPath.scope.hasBinding(name)\n }\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === 'component') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n // Check to see if lazyRouteComponent is already imported before attempting\n // to import it again\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyRouteComponent',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyRouteComponent } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitComponentImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitComponentImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyRouteComponent($$splitComponentImporter, 'component')`,\n )() as any\n\n programPath.pushContainer('body', [\n template.smart(\n `function DummyComponent() { return null }`,\n )() as t.Statement,\n ])\n\n found = true\n } else if (prop.key.name === 'loader') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyFn',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyFn } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitLoaderImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitLoaderImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyFn($$splitLoaderImporter, 'loader')`,\n )() as any\n\n found = true\n }\n }\n }\n\n programPath.scope.crawl()\n })\n }\n\n if (found as boolean) {\n programPath.pushContainer('body', [\n template.smart(\n `function TSR_Dummy_Component() {}`,\n )() as t.Statement,\n ])\n }\n }\n }\n }\n },\n },\n state,\n )\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n\n// Reusable function to get literal value or resolve variable to literal\nfunction resolveIdentifier(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (\n binding\n // && binding.kind === 'const'\n ) {\n const declarator = binding.path.node\n if (t.isObjectExpression(declarator.init)) {\n return declarator.init\n } else if (t.isFunctionDeclaration(declarator.init)) {\n return declarator.init\n }\n }\n return undefined\n }\n\n return node\n}\n\nfunction removeIdentifierLiteral(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (binding) {\n binding.path.remove()\n }\n }\n}\n\nconst splitNodeTypes = ['component', 'loader'] as const\ntype SplitNodeType = (typeof splitNodeTypes)[number]\n\nexport async function splitFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitNodesByType: Record<\n SplitNodeType,\n t.Node | undefined\n > = {\n component: undefined,\n loader: undefined,\n }\n\n // Find the node\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n splitNodeTypes.forEach((type) => {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === type) {\n splitNodesByType[type] = prop.value\n }\n }\n })\n }\n })\n\n // Remove all of the options\n options.properties = []\n }\n }\n }\n }\n },\n },\n state,\n )\n\n splitNodeTypes.forEach((splitType) => {\n let splitNode = splitNodesByType[splitType]\n\n if (!splitNode) {\n return\n }\n\n while (t.isIdentifier(splitNode)) {\n const binding = programPath.scope.getBinding(\n splitNode.name,\n )\n splitNode = binding?.path.node\n }\n\n // Add the node to the program\n if (splitNode) {\n if (t.isFunctionDeclaration(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n t.functionExpression(\n splitNode.id || null, // Anonymize the function expression\n splitNode.params,\n splitNode.body,\n splitNode.generator,\n splitNode.async,\n ),\n ),\n ]),\n )\n } else if (\n t.isFunctionExpression(splitNode) ||\n t.isArrowFunctionExpression(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode as any,\n ),\n ]),\n )\n } else if (t.isImportSpecifier(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode.local,\n ),\n ]),\n )\n } else {\n console.info(splitNode)\n throw new Error(\n `Unexpected splitNode type ☝️: ${splitNode.type}`,\n )\n }\n }\n\n // If the splitNode exists at the top of the program\n // then we need to remove that copy\n programPath.node.body = programPath.node.body.filter(\n (node) => {\n return node !== splitNode\n },\n )\n\n // Export the node\n programPath.pushContainer('body', [\n t.exportNamedDeclaration(null, [\n t.exportSpecifier(\n t.identifier(splitType),\n t.identifier(splitType),\n ),\n ]),\n ])\n })\n\n // convert exports to imports from the original file\n programPath.traverse({\n ExportNamedDeclaration(path) {\n // e.g. export const x = 1 or export { x }\n // becomes\n // import { x } from '${opts.id}'\n\n if (path.node.declaration) {\n if (t.isVariableDeclaration(path.node.declaration)) {\n path.replaceWith(\n t.importDeclaration(\n path.node.declaration.declarations.map((decl) =>\n t.importSpecifier(\n t.identifier((decl.id as any).name),\n t.identifier((decl.id as any).name),\n ),\n ),\n t.stringLiteral(\n opts.filename.split(\n `?${splitPrefix}`,\n )[0] as string,\n ),\n ),\n )\n }\n }\n },\n })\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n"],"names":[],"mappings":";;;;AAyBA,eAAsB,YAAY,MAI/B;AACM,SAAA,MAAM,KAAK,WAAW;AAAA,IAC3B,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,IACf,gBAAgB,OAAO;AAAA,MACrB,SAAS;AAAA,QACP;AAAA,UACE;AAAA,YACE,SAAS;AAAA,cACP,SAAS;AAAA,gBACP,MA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AI,aAAa;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,EACA,OAAO,OAAO;AAAA,IAAA;AAAA,EAClB,CACD;AACH;"}
|
|
1
|
+
{"version":3,"file":"compilers.js","sources":["../../src/compilers.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport * as template from '@babel/template'\nimport { splitPrefix } from './constants'\nimport { eliminateUnreferencedIdentifiers } from './eliminateUnreferencedIdentifiers'\nimport type * as babel from '@babel/core'\nimport type { CompileAstFn } from './ast'\n\ntype SplitModulesById = Record<\n string,\n { id: string; node: t.FunctionExpression }\n>\n\ninterface State {\n filename: string\n opts: {\n minify: boolean\n root: string\n }\n imported: Record<string, boolean>\n refs: Set<any>\n serverIndex: number\n splitIndex: number\n splitModulesById: SplitModulesById\n}\n\nexport async function compileFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitUrl = `${splitPrefix}:${opts.filename}?${splitPrefix}`\n\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n let found = false\n\n const hasImportedOrDefinedIdentifier = (\n name: string,\n ) => {\n return programPath.scope.hasBinding(name)\n }\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === 'component') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n // Check to see if lazyRouteComponent is already imported before attempting\n // to import it again\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyRouteComponent',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyRouteComponent } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitComponentImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitComponentImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyRouteComponent($$splitComponentImporter, 'component')`,\n )() as any\n\n programPath.pushContainer('body', [\n template.smart(\n `function DummyComponent() { return null }`,\n )() as t.Statement,\n ])\n\n found = true\n } else if (prop.key.name === 'loader') {\n const value = prop.value\n\n if (t.isIdentifier(value)) {\n removeIdentifierLiteral(path, value)\n }\n\n // Prepend the import statement to the program along with the importer function\n\n if (\n !hasImportedOrDefinedIdentifier(\n 'lazyFn',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `import { lazyFn } from '@tanstack/react-router'`,\n )() as t.Statement,\n ])\n }\n\n if (\n !hasImportedOrDefinedIdentifier(\n '$$splitLoaderImporter',\n )\n ) {\n programPath.unshiftContainer('body', [\n template.smart(\n `const $$splitLoaderImporter = () => import('${splitUrl}')`,\n )() as t.Statement,\n ])\n }\n\n prop.value = template.expression(\n `lazyFn($$splitLoaderImporter, 'loader')`,\n )() as any\n\n found = true\n }\n }\n }\n\n programPath.scope.crawl()\n })\n }\n\n if (found as boolean) {\n programPath.pushContainer('body', [\n template.smart(\n `function TSR_Dummy_Component() {}`,\n )() as t.Statement,\n ])\n }\n }\n }\n }\n },\n },\n state,\n )\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n\n// Reusable function to get literal value or resolve variable to literal\nfunction resolveIdentifier(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (\n binding\n // && binding.kind === 'const'\n ) {\n const declarator = binding.path.node\n if (t.isObjectExpression(declarator.init)) {\n return declarator.init\n } else if (t.isFunctionDeclaration(declarator.init)) {\n return declarator.init\n }\n }\n return undefined\n }\n\n return node\n}\n\nfunction removeIdentifierLiteral(path: any, node: any) {\n if (t.isIdentifier(node)) {\n const binding = path.scope.getBinding(node.name)\n if (binding) {\n binding.path.remove()\n }\n }\n}\n\nconst splitNodeTypes = ['component', 'loader'] as const\ntype SplitNodeType = (typeof splitNodeTypes)[number]\n\nexport async function splitFile(opts: {\n code: string\n compileAst: CompileAstFn\n filename: string\n}) {\n return await opts.compileAst({\n code: opts.code,\n filename: opts.filename,\n getBabelConfig: () => ({\n plugins: [\n [\n {\n visitor: {\n Program: {\n enter(programPath: babel.NodePath<t.Program>, state: State) {\n const splitNodesByType: Record<\n SplitNodeType,\n t.Node | undefined\n > = {\n component: undefined,\n loader: undefined,\n }\n\n // Find the node\n programPath.traverse(\n {\n CallExpression: (path) => {\n if (path.node.callee.type === 'Identifier') {\n if (\n path.node.callee.name === 'createRoute' ||\n path.node.callee.name === 'createFileRoute'\n ) {\n if (\n path.parentPath.node.type === 'CallExpression'\n ) {\n const options = resolveIdentifier(\n path,\n path.parentPath.node.arguments[0],\n )\n\n if (t.isObjectExpression(options)) {\n options.properties.forEach((prop) => {\n if (t.isObjectProperty(prop)) {\n splitNodeTypes.forEach((type) => {\n if (t.isIdentifier(prop.key)) {\n if (prop.key.name === type) {\n splitNodesByType[type] = prop.value\n }\n }\n })\n }\n })\n\n // Remove all of the options\n options.properties = []\n }\n }\n }\n }\n },\n },\n state,\n )\n\n splitNodeTypes.forEach((splitType) => {\n let splitNode = splitNodesByType[splitType]\n\n if (!splitNode) {\n return\n }\n\n while (t.isIdentifier(splitNode)) {\n const binding = programPath.scope.getBinding(\n splitNode.name,\n )\n splitNode = binding?.path.node\n }\n\n // Add the node to the program\n if (splitNode) {\n if (t.isFunctionDeclaration(splitNode)) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n t.functionExpression(\n splitNode.id || null, // Anonymize the function expression\n splitNode.params,\n splitNode.body,\n splitNode.generator,\n splitNode.async,\n ),\n ),\n ]),\n )\n } else if (\n t.isFunctionExpression(splitNode) ||\n t.isArrowFunctionExpression(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode as any,\n ),\n ]),\n )\n } else if (\n t.isImportSpecifier(splitNode) ||\n t.isImportDefaultSpecifier(splitNode)\n ) {\n programPath.pushContainer(\n 'body',\n t.variableDeclaration('const', [\n t.variableDeclarator(\n t.identifier(splitType),\n splitNode.local,\n ),\n ]),\n )\n } else {\n console.info(splitNode)\n throw new Error(\n `Unexpected splitNode type ☝️: ${splitNode.type}`,\n )\n }\n }\n\n // If the splitNode exists at the top of the program\n // then we need to remove that copy\n programPath.node.body = programPath.node.body.filter(\n (node) => {\n return node !== splitNode\n },\n )\n\n // Export the node\n programPath.pushContainer('body', [\n t.exportNamedDeclaration(null, [\n t.exportSpecifier(\n t.identifier(splitType),\n t.identifier(splitType),\n ),\n ]),\n ])\n })\n\n // convert exports to imports from the original file\n programPath.traverse({\n ExportNamedDeclaration(path) {\n // e.g. export const x = 1 or export { x }\n // becomes\n // import { x } from '${opts.id}'\n\n if (path.node.declaration) {\n if (t.isVariableDeclaration(path.node.declaration)) {\n path.replaceWith(\n t.importDeclaration(\n path.node.declaration.declarations.map((decl) =>\n t.importSpecifier(\n t.identifier((decl.id as any).name),\n t.identifier((decl.id as any).name),\n ),\n ),\n t.stringLiteral(\n opts.filename.split(\n `?${splitPrefix}`,\n )[0] as string,\n ),\n ),\n )\n }\n }\n },\n })\n\n eliminateUnreferencedIdentifiers(programPath)\n },\n },\n },\n },\n {\n root: process.cwd(),\n minify: process.env.NODE_ENV === 'production',\n },\n ],\n ].filter(Boolean),\n }),\n })\n}\n"],"names":[],"mappings":";;;;AA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ACf,CAAC;AAAA,8BACL;AAAA,4BACF;AAAA,0BAAA;AAAA,wBAEJ;AAAA,sBACF;AAAA,oBACF;AAAA,kBAAA,CACD;AAED,mDAAiC,WAAW;AAAA,gBAC9C;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,UACA;AAAA,YACE,MAAM,QAAQ,IAAI;AAAA,YAClB,QAAQ,QAAQ,IAAI,aAAa;AAAA,UACnC;AAAA,QACF;AAAA,MAAA,EACA,OAAO,OAAO;AAAA,IAAA;AAAA,EAClB,CACD;AACH;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { unpluginRouterGeneratorFactory } from "./router-generator.js";
|
|
2
|
+
import { unpluginRouterCodeSplitterFactory } from "./code-splitter.js";
|
|
3
|
+
const unpluginRouterComposedFactory = (options = {}, meta) => {
|
|
4
|
+
const routerGenerator = unpluginRouterGeneratorFactory(options);
|
|
5
|
+
const routerGeneratorOptions = Array.isArray(routerGenerator) ? routerGenerator : [routerGenerator];
|
|
6
|
+
const routerCodeSplitter = unpluginRouterCodeSplitterFactory(options, meta);
|
|
7
|
+
let routerCodeSplitterOptions = Array.isArray(routerCodeSplitter) ? routerCodeSplitter : [routerCodeSplitter];
|
|
8
|
+
if (meta.framework === "rspack") {
|
|
9
|
+
routerCodeSplitterOptions = [];
|
|
10
|
+
}
|
|
11
|
+
return [...routerGeneratorOptions, ...routerCodeSplitterOptions];
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
unpluginRouterComposedFactory
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=composed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composed.js","sources":["../../src/composed.ts"],"sourcesContent":["import { unpluginRouterGeneratorFactory } from './router-generator'\nimport { unpluginRouterCodeSplitterFactory } from './code-splitter'\n\nimport type { Config } from './config'\nimport type { UnpluginFactory } from 'unplugin'\n\nexport const unpluginRouterComposedFactory: UnpluginFactory<\n Partial<Config> | undefined\n> = (options = {}, meta) => {\n const routerGenerator = unpluginRouterGeneratorFactory(options, meta)\n\n const routerGeneratorOptions = Array.isArray(routerGenerator)\n ? routerGenerator\n : [routerGenerator]\n\n const routerCodeSplitter = unpluginRouterCodeSplitterFactory(options, meta)\n let routerCodeSplitterOptions = Array.isArray(routerCodeSplitter)\n ? routerCodeSplitter\n : [routerCodeSplitter]\n\n // Rspack doesn't support the `resolveId` and `transform` hooks provided by unplugin\n // so we need to disable the code splitter for it\n // If you're using Rspack, and know how to implement the code splitting, please let us know\n // We'd love to support it, but we're not sure how to do it yet\n if (meta.framework === 'rspack') {\n routerCodeSplitterOptions = []\n }\n\n return [...routerGeneratorOptions, ...routerCodeSplitterOptions]\n}\n"],"names":[],"mappings":";;AAMO,MAAM,gCAET,CAAC,UAAU,IAAI,SAAS;AACpB,QAAA,kBAAkB,+BAA+B,OAAa;AAEpE,QAAM,yBAAyB,MAAM,QAAQ,eAAe,IACxD,kBACA,CAAC,eAAe;AAEd,QAAA,qBAAqB,kCAAkC,SAAS,IAAI;AAC1E,MAAI,4BAA4B,MAAM,QAAQ,kBAAkB,IAC5D,qBACA,CAAC,kBAAkB;AAMnB,MAAA,KAAK,cAAc,UAAU;AAC/B,gCAA4B,CAAA;AAAA,EAC9B;AAEA,SAAO,CAAC,GAAG,wBAAwB,GAAG,yBAAyB;AACjE;"}
|
|
@@ -7,6 +7,7 @@ const checkLock = () => lock;
|
|
|
7
7
|
const setLock = (bool) => {
|
|
8
8
|
lock = bool;
|
|
9
9
|
};
|
|
10
|
+
const PLUGIN_NAME = "unplugin:router-generator";
|
|
10
11
|
const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
11
12
|
let ROOT = process.cwd();
|
|
12
13
|
let userConfig = options;
|
|
@@ -56,6 +57,13 @@ const unpluginRouterGeneratorFactory = (options = {}) => {
|
|
|
56
57
|
userConfig = await getConfig(options, ROOT);
|
|
57
58
|
await run(generate);
|
|
58
59
|
}
|
|
60
|
+
},
|
|
61
|
+
async rspack(compiler) {
|
|
62
|
+
userConfig = await getConfig(options, ROOT);
|
|
63
|
+
await run(generate);
|
|
64
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, async () => {
|
|
65
|
+
await run(generate);
|
|
66
|
+
});
|
|
59
67
|
}
|
|
60
68
|
};
|
|
61
69
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-generator.js","sources":["../../src/router-generator.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 {
|
|
1
|
+
{"version":3,"file":"router-generator.js","sources":["../../src/router-generator.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 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 = await 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 = isAbsolute(userConfig.routesDirectory)\n ? userConfig.routesDirectory\n : join(ROOT, userConfig.routesDirectory)\n\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 = await getConfig(options, ROOT)\n\n await run(generate)\n },\n },\n async rspack(compiler) {\n userConfig = await getConfig(options, ROOT)\n\n await run(generate)\n\n compiler.hooks.watchRun.tap(PLUGIN_NAME, async () => {\n await run(generate)\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,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;AAAA,EAAA;AAGI,QAAA,aAAa,OACjB,MACA,UACG;AACG,UAAA,WAAW,UAAU,IAAI;AAE/B,QAAI,aAAa,KAAK,MAAM,gBAAgB,GAAG;AAChC,mBAAA,MAAM,UAAU,SAAS,IAAI;AAC1C;AAAA,IACF;AAEA,QACE,UAAU,YACV,aAAa,QAAQ,WAAW,kBAAkB,GAClD;AAEA;AAAA,IACF;AAEM,UAAA,sBAAsB,WAAW,WAAW,eAAe,IAC7D,WAAW,kBACX,KAAK,MAAM,WAAW,eAAe;AAErC,QAAA,SAAS,WAAW,mBAAmB,GAAG;AAC5C,YAAM,SAAS;AAAA,IACjB;AAAA,EAAA;AAGI,QAAA,MAAyD,OAAO,OAAO;AACvE,QAAA,WAAW,yBAAyB,MAAM;AAC5C,YAAM,GAAG;AAAA,IACX;AAAA,EAAA;AAGK,SAAA;AAAA,IACL,MAAM;AAAA,IACN,MAAM,YAAY,IAAI,EAAE,SAAS;AAC/B,YAAM,IAAI,YAAY;AACd,cAAA,WAAW,IAAI,KAAK;AAAA,MAAA,CAC3B;AAAA,IACH;AAAA,IACA,MAAM;AAAA,MACJ,MAAM,eAAe,QAAQ;AAC3B,eAAO,OAAO;AACD,qBAAA,MAAM,UAAU,SAAS,IAAI;AAE1C,cAAM,IAAI,QAAQ;AAAA,MACpB;AAAA,IACF;AAAA,IACA,MAAM,OAAO,UAAU;AACR,mBAAA,MAAM,UAAU,SAAS,IAAI;AAE1C,YAAM,IAAI,QAAQ;AAElB,eAAS,MAAM,SAAS,IAAI,aAAa,YAAY;AACnD,cAAM,IAAI,QAAQ;AAAA,MAAA,CACnB;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { configSchema, Config } from './config.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* export default defineConfig({
|
|
7
|
+
* // ...
|
|
8
|
+
* tools: {
|
|
9
|
+
* rspack: {
|
|
10
|
+
* plugins: [TanStackRouterGeneratorRspack()],
|
|
11
|
+
* },
|
|
12
|
+
* },
|
|
13
|
+
* })
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare const TanStackRouterGeneratorRspack: (options?: Partial<{
|
|
17
|
+
routeFileIgnorePrefix: string;
|
|
18
|
+
routesDirectory: string;
|
|
19
|
+
generatedRouteTree: string;
|
|
20
|
+
quoteStyle: "single" | "double";
|
|
21
|
+
semicolons: boolean;
|
|
22
|
+
disableTypes: boolean;
|
|
23
|
+
addExtensions: boolean;
|
|
24
|
+
disableLogging: boolean;
|
|
25
|
+
routeTreeFileHeader: string[];
|
|
26
|
+
routeTreeFileFooter: string[];
|
|
27
|
+
enableRouteGeneration?: boolean | undefined;
|
|
28
|
+
experimental?: {
|
|
29
|
+
enableCodeSplitting?: boolean | undefined;
|
|
30
|
+
} | undefined;
|
|
31
|
+
routeFilePrefix?: string | undefined;
|
|
32
|
+
routeFileIgnorePattern?: string | undefined;
|
|
33
|
+
}> | undefined) => _rspack_core_dist_config_zod.RspackPluginInstance;
|
|
34
|
+
/**
|
|
35
|
+
* @experimental Do not use this plugin yet
|
|
36
|
+
*
|
|
37
|
+
* Unplugin's Rspack integration doesn't support the `resolveId` and `transform` hooks.
|
|
38
|
+
* The code-splitter won't work with Rspack and will probably break your dev and build.
|
|
39
|
+
*
|
|
40
|
+
* If you're familiar with Rspack and know how to overcome our `resolveId` and `transform`
|
|
41
|
+
* limitations, please let us know.
|
|
42
|
+
* We'd love to support it, but we're not sure how to do it yet 😅.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* export default defineConfig({
|
|
47
|
+
* // ...
|
|
48
|
+
* tools: {
|
|
49
|
+
* rspack: {
|
|
50
|
+
* plugins: [unstable_TanStackRouterCodeSplitterRspack()],
|
|
51
|
+
* },
|
|
52
|
+
* },
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare const unstable_TanStackRouterCodeSplitterRspack: (options?: Partial<{
|
|
57
|
+
routeFileIgnorePrefix: string;
|
|
58
|
+
routesDirectory: string;
|
|
59
|
+
generatedRouteTree: string;
|
|
60
|
+
quoteStyle: "single" | "double";
|
|
61
|
+
semicolons: boolean;
|
|
62
|
+
disableTypes: boolean;
|
|
63
|
+
addExtensions: boolean;
|
|
64
|
+
disableLogging: boolean;
|
|
65
|
+
routeTreeFileHeader: string[];
|
|
66
|
+
routeTreeFileFooter: string[];
|
|
67
|
+
enableRouteGeneration?: boolean | undefined;
|
|
68
|
+
experimental?: {
|
|
69
|
+
enableCodeSplitting?: boolean | undefined;
|
|
70
|
+
} | undefined;
|
|
71
|
+
routeFilePrefix?: string | undefined;
|
|
72
|
+
routeFileIgnorePattern?: string | undefined;
|
|
73
|
+
}> | undefined) => _rspack_core_dist_config_zod.RspackPluginInstance;
|
|
74
|
+
/**
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* export default defineConfig({
|
|
78
|
+
* // ...
|
|
79
|
+
* tools: {
|
|
80
|
+
* rspack: {
|
|
81
|
+
* plugins: [TanStackRouterRspack()],
|
|
82
|
+
* },
|
|
83
|
+
* },
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
declare const TanStackRouterRspack: (options?: Partial<{
|
|
88
|
+
routeFileIgnorePrefix: string;
|
|
89
|
+
routesDirectory: string;
|
|
90
|
+
generatedRouteTree: string;
|
|
91
|
+
quoteStyle: "single" | "double";
|
|
92
|
+
semicolons: boolean;
|
|
93
|
+
disableTypes: boolean;
|
|
94
|
+
addExtensions: boolean;
|
|
95
|
+
disableLogging: boolean;
|
|
96
|
+
routeTreeFileHeader: string[];
|
|
97
|
+
routeTreeFileFooter: string[];
|
|
98
|
+
enableRouteGeneration?: boolean | undefined;
|
|
99
|
+
experimental?: {
|
|
100
|
+
enableCodeSplitting?: boolean | undefined;
|
|
101
|
+
} | undefined;
|
|
102
|
+
routeFilePrefix?: string | undefined;
|
|
103
|
+
routeFileIgnorePattern?: string | undefined;
|
|
104
|
+
}> | undefined) => _rspack_core_dist_config_zod.RspackPluginInstance;
|
|
105
|
+
export default TanStackRouterRspack;
|
|
106
|
+
export { configSchema, TanStackRouterRspack, TanStackRouterGeneratorRspack, unstable_TanStackRouterCodeSplitterRspack, };
|
|
107
|
+
export type { Config };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createRspackPlugin } from "unplugin";
|
|
2
|
+
import { unpluginRouterCodeSplitterFactory } from "./code-splitter.js";
|
|
3
|
+
import { configSchema } from "./config.js";
|
|
4
|
+
import { unpluginRouterGeneratorFactory } from "./router-generator.js";
|
|
5
|
+
import { unpluginRouterComposedFactory } from "./composed.js";
|
|
6
|
+
const TanStackRouterGeneratorRspack = /* @__PURE__ */ createRspackPlugin(
|
|
7
|
+
unpluginRouterGeneratorFactory
|
|
8
|
+
);
|
|
9
|
+
const unstable_TanStackRouterCodeSplitterRspack = /* @__PURE__ */ createRspackPlugin(unpluginRouterCodeSplitterFactory);
|
|
10
|
+
const TanStackRouterRspack = /* @__PURE__ */ createRspackPlugin(
|
|
11
|
+
unpluginRouterComposedFactory
|
|
12
|
+
);
|
|
13
|
+
export {
|
|
14
|
+
TanStackRouterGeneratorRspack,
|
|
15
|
+
TanStackRouterRspack,
|
|
16
|
+
configSchema,
|
|
17
|
+
TanStackRouterRspack as default,
|
|
18
|
+
unstable_TanStackRouterCodeSplitterRspack
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=rspack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rspack.js","sources":["../../src/rspack.ts"],"sourcesContent":["import { createRspackPlugin } from 'unplugin'\nimport { unpluginRouterCodeSplitterFactory } from './code-splitter'\nimport { configSchema } from './config'\nimport { unpluginRouterGeneratorFactory } from './router-generator'\nimport { unpluginRouterComposedFactory } from './composed'\nimport type { Config } from './config'\n\n/**\n * @example\n * ```ts\n * export default defineConfig({\n * // ...\n * tools: {\n * rspack: {\n * plugins: [TanStackRouterGeneratorRspack()],\n * },\n * },\n * })\n * ```\n */\nconst TanStackRouterGeneratorRspack = /* #__PURE__ */ createRspackPlugin(\n unpluginRouterGeneratorFactory,\n)\n\n/**\n * @experimental Do not use this plugin yet\n *\n * Unplugin's Rspack integration doesn't support the `resolveId` and `transform` hooks.\n * The code-splitter won't work with Rspack and will probably break your dev and build.\n *\n * If you're familiar with Rspack and know how to overcome our `resolveId` and `transform`\n * limitations, please let us know.\n * We'd love to support it, but we're not sure how to do it yet 😅.\n *\n * @example\n * ```ts\n * export default defineConfig({\n * // ...\n * tools: {\n * rspack: {\n * plugins: [unstable_TanStackRouterCodeSplitterRspack()],\n * },\n * },\n * })\n * ```\n */\nconst unstable_TanStackRouterCodeSplitterRspack =\n /* #__PURE__ */ createRspackPlugin(unpluginRouterCodeSplitterFactory)\n\n/**\n * @example\n * ```ts\n * export default defineConfig({\n * // ...\n * tools: {\n * rspack: {\n * plugins: [TanStackRouterRspack()],\n * },\n * },\n * })\n * ```\n */\nconst TanStackRouterRspack = /* #__PURE__ */ createRspackPlugin(\n unpluginRouterComposedFactory,\n)\n\nexport default TanStackRouterRspack\nexport {\n configSchema,\n TanStackRouterRspack,\n TanStackRouterGeneratorRspack,\n unstable_TanStackRouterCodeSplitterRspack,\n}\nexport type { Config }\n"],"names":[],"mappings":";;;;;AAoBA,MAAM,gCAAgD;AAAA,EACpD;AACF;AAwBM,MAAA,+EAC+B,iCAAiC;AAetE,MAAM,uBAAuC;AAAA,EAC3C;AACF;"}
|
package/dist/esm/vite.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { configSchema, Config } from './config.js';
|
|
2
|
-
import { VitePlugin } from 'unplugin';
|
|
3
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* export default defineConfig({
|
|
7
|
+
* plugins: [TanStackRouterGeneratorVite()],
|
|
8
|
+
* // ...
|
|
9
|
+
* })
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
4
12
|
declare const TanStackRouterGeneratorVite: (options?: Partial<{
|
|
5
13
|
routeFileIgnorePrefix: string;
|
|
6
14
|
routesDirectory: string;
|
|
@@ -18,7 +26,16 @@ declare const TanStackRouterGeneratorVite: (options?: Partial<{
|
|
|
18
26
|
} | undefined;
|
|
19
27
|
routeFilePrefix?: string | undefined;
|
|
20
28
|
routeFileIgnorePattern?: string | undefined;
|
|
21
|
-
}> | undefined) =>
|
|
29
|
+
}> | undefined) => import('vite').Plugin<any> | import('vite').Plugin<any>[];
|
|
30
|
+
/**
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* export default defineConfig({
|
|
34
|
+
* plugins: [TanStackRouterCodeSplitterVite()],
|
|
35
|
+
* // ...
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
22
39
|
declare const TanStackRouterCodeSplitterVite: (options?: Partial<{
|
|
23
40
|
routeFileIgnorePrefix: string;
|
|
24
41
|
routesDirectory: string;
|
|
@@ -36,8 +53,34 @@ declare const TanStackRouterCodeSplitterVite: (options?: Partial<{
|
|
|
36
53
|
} | undefined;
|
|
37
54
|
routeFilePrefix?: string | undefined;
|
|
38
55
|
routeFileIgnorePattern?: string | undefined;
|
|
39
|
-
}> | undefined) =>
|
|
40
|
-
|
|
56
|
+
}> | undefined) => import('vite').Plugin<any> | import('vite').Plugin<any>[];
|
|
57
|
+
/**
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* export default defineConfig({
|
|
61
|
+
* plugins: [TanStackRouterVite()],
|
|
62
|
+
* // ...
|
|
63
|
+
* })
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare const TanStackRouterVite: (options?: Partial<{
|
|
67
|
+
routeFileIgnorePrefix: string;
|
|
68
|
+
routesDirectory: string;
|
|
69
|
+
generatedRouteTree: string;
|
|
70
|
+
quoteStyle: "single" | "double";
|
|
71
|
+
semicolons: boolean;
|
|
72
|
+
disableTypes: boolean;
|
|
73
|
+
addExtensions: boolean;
|
|
74
|
+
disableLogging: boolean;
|
|
75
|
+
routeTreeFileHeader: string[];
|
|
76
|
+
routeTreeFileFooter: string[];
|
|
77
|
+
enableRouteGeneration?: boolean | undefined;
|
|
78
|
+
experimental?: {
|
|
79
|
+
enableCodeSplitting?: boolean | undefined;
|
|
80
|
+
} | undefined;
|
|
81
|
+
routeFilePrefix?: string | undefined;
|
|
82
|
+
routeFileIgnorePattern?: string | undefined;
|
|
83
|
+
}> | undefined) => import('vite').Plugin<any> | import('vite').Plugin<any>[];
|
|
41
84
|
export default TanStackRouterVite;
|
|
42
85
|
export { configSchema, TanStackRouterGeneratorVite, TanStackRouterCodeSplitterVite, TanStackRouterVite, };
|
|
43
86
|
export type { Config };
|
package/dist/esm/vite.js
CHANGED
|
@@ -2,18 +2,14 @@ import { createVitePlugin } from "unplugin";
|
|
|
2
2
|
import { unpluginRouterCodeSplitterFactory } from "./code-splitter.js";
|
|
3
3
|
import { configSchema } from "./config.js";
|
|
4
4
|
import { unpluginRouterGeneratorFactory } from "./router-generator.js";
|
|
5
|
+
import { unpluginRouterComposedFactory } from "./composed.js";
|
|
5
6
|
const TanStackRouterGeneratorVite = createVitePlugin(
|
|
6
7
|
unpluginRouterGeneratorFactory
|
|
7
8
|
);
|
|
8
9
|
const TanStackRouterCodeSplitterVite = createVitePlugin(
|
|
9
10
|
unpluginRouterCodeSplitterFactory
|
|
10
11
|
);
|
|
11
|
-
|
|
12
|
-
return [
|
|
13
|
-
TanStackRouterGeneratorVite(inlineConfig),
|
|
14
|
-
TanStackRouterCodeSplitterVite(inlineConfig)
|
|
15
|
-
];
|
|
16
|
-
}
|
|
12
|
+
const TanStackRouterVite = createVitePlugin(unpluginRouterComposedFactory);
|
|
17
13
|
export {
|
|
18
14
|
TanStackRouterCodeSplitterVite,
|
|
19
15
|
TanStackRouterGeneratorVite,
|
package/dist/esm/vite.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite.js","sources":["../../src/vite.ts"],"sourcesContent":["import { createVitePlugin } from 'unplugin'\nimport { unpluginRouterCodeSplitterFactory } from './code-splitter'\nimport { configSchema } from './config'\nimport { unpluginRouterGeneratorFactory } from './router-generator'\
|
|
1
|
+
{"version":3,"file":"vite.js","sources":["../../src/vite.ts"],"sourcesContent":["import { createVitePlugin } from 'unplugin'\nimport { unpluginRouterCodeSplitterFactory } from './code-splitter'\nimport { configSchema } from './config'\nimport { unpluginRouterGeneratorFactory } from './router-generator'\nimport { unpluginRouterComposedFactory } from './composed'\n\nimport type { Config } from './config'\n\n/**\n * @example\n * ```ts\n * export default defineConfig({\n * plugins: [TanStackRouterGeneratorVite()],\n * // ...\n * })\n * ```\n */\nconst TanStackRouterGeneratorVite = createVitePlugin(\n unpluginRouterGeneratorFactory,\n)\n\n/**\n * @example\n * ```ts\n * export default defineConfig({\n * plugins: [TanStackRouterCodeSplitterVite()],\n * // ...\n * })\n * ```\n */\nconst TanStackRouterCodeSplitterVite = createVitePlugin(\n unpluginRouterCodeSplitterFactory,\n)\n\n/**\n * @example\n * ```ts\n * export default defineConfig({\n * plugins: [TanStackRouterVite()],\n * // ...\n * })\n * ```\n */\nconst TanStackRouterVite = createVitePlugin(unpluginRouterComposedFactory)\n\nexport default TanStackRouterVite\nexport {\n configSchema,\n TanStackRouterGeneratorVite,\n TanStackRouterCodeSplitterVite,\n TanStackRouterVite,\n}\nexport type { Config }\n"],"names":[],"mappings":";;;;;AAiBA,MAAM,8BAA8B;AAAA,EAClC;AACF;AAWA,MAAM,iCAAiC;AAAA,EACrC;AACF;AAWM,MAAA,qBAAqB,iBAAiB,6BAA6B;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/router-plugin",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.11",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,6 +36,16 @@
|
|
|
36
36
|
"default": "./dist/cjs/vite.cjs"
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
|
+
"./rspack": {
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./dist/esm/rspack.d.ts",
|
|
42
|
+
"default": "./dist/esm/rspack.js"
|
|
43
|
+
},
|
|
44
|
+
"require": {
|
|
45
|
+
"types": "./dist/cjs/rspack.d.cts",
|
|
46
|
+
"default": "./dist/cjs/rspack.cjs"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
39
49
|
"./package.json": "./package.json"
|
|
40
50
|
},
|
|
41
51
|
"sideEffects": false,
|
|
@@ -56,11 +66,15 @@
|
|
|
56
66
|
"src/**"
|
|
57
67
|
],
|
|
58
68
|
"peerDependencies": {
|
|
59
|
-
"vite": ">=5"
|
|
69
|
+
"vite": ">=5",
|
|
70
|
+
"@rsbuild/core": ">=0.7.9"
|
|
60
71
|
},
|
|
61
72
|
"peerDependenciesMeta": {
|
|
62
73
|
"vite": {
|
|
63
74
|
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@rsbuild/core": {
|
|
77
|
+
"optional": true
|
|
64
78
|
}
|
|
65
79
|
},
|
|
66
80
|
"dependencies": {
|
package/src/code-splitter.ts
CHANGED
|
@@ -49,6 +49,8 @@ plugins: [
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
const PLUGIN_NAME = 'unplugin:router-code-splitter'
|
|
53
|
+
|
|
52
54
|
export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
53
55
|
Partial<Config> | undefined
|
|
54
56
|
> = (options = {}, { framework }) => {
|
|
@@ -120,6 +122,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
120
122
|
return {
|
|
121
123
|
name: 'router-code-splitter-plugin',
|
|
122
124
|
enforce: 'pre',
|
|
125
|
+
|
|
123
126
|
resolveId(source) {
|
|
124
127
|
if (!userConfig.experimental?.enableCodeSplitting) {
|
|
125
128
|
return null
|
|
@@ -130,6 +133,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
130
133
|
}
|
|
131
134
|
return null
|
|
132
135
|
},
|
|
136
|
+
|
|
133
137
|
async transform(code, id) {
|
|
134
138
|
if (!userConfig.experimental?.enableCodeSplitting) {
|
|
135
139
|
return null
|
|
@@ -160,11 +164,16 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
160
164
|
|
|
161
165
|
return null
|
|
162
166
|
},
|
|
167
|
+
|
|
163
168
|
vite: {
|
|
164
169
|
async configResolved(config) {
|
|
165
170
|
ROOT = config.root
|
|
166
171
|
userConfig = await getConfig(options, ROOT)
|
|
167
172
|
},
|
|
168
173
|
},
|
|
174
|
+
|
|
175
|
+
async rspack(compiler) {
|
|
176
|
+
userConfig = await getConfig(options, ROOT)
|
|
177
|
+
},
|
|
169
178
|
}
|
|
170
179
|
}
|
package/src/compilers.ts
CHANGED
|
@@ -332,7 +332,10 @@ export async function splitFile(opts: {
|
|
|
332
332
|
),
|
|
333
333
|
]),
|
|
334
334
|
)
|
|
335
|
-
} else if (
|
|
335
|
+
} else if (
|
|
336
|
+
t.isImportSpecifier(splitNode) ||
|
|
337
|
+
t.isImportDefaultSpecifier(splitNode)
|
|
338
|
+
) {
|
|
336
339
|
programPath.pushContainer(
|
|
337
340
|
'body',
|
|
338
341
|
t.variableDeclaration('const', [
|
package/src/composed.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { unpluginRouterGeneratorFactory } from './router-generator'
|
|
2
|
+
import { unpluginRouterCodeSplitterFactory } from './code-splitter'
|
|
3
|
+
|
|
4
|
+
import type { Config } from './config'
|
|
5
|
+
import type { UnpluginFactory } from 'unplugin'
|
|
6
|
+
|
|
7
|
+
export const unpluginRouterComposedFactory: UnpluginFactory<
|
|
8
|
+
Partial<Config> | undefined
|
|
9
|
+
> = (options = {}, meta) => {
|
|
10
|
+
const routerGenerator = unpluginRouterGeneratorFactory(options, meta)
|
|
11
|
+
|
|
12
|
+
const routerGeneratorOptions = Array.isArray(routerGenerator)
|
|
13
|
+
? routerGenerator
|
|
14
|
+
: [routerGenerator]
|
|
15
|
+
|
|
16
|
+
const routerCodeSplitter = unpluginRouterCodeSplitterFactory(options, meta)
|
|
17
|
+
let routerCodeSplitterOptions = Array.isArray(routerCodeSplitter)
|
|
18
|
+
? routerCodeSplitter
|
|
19
|
+
: [routerCodeSplitter]
|
|
20
|
+
|
|
21
|
+
// Rspack doesn't support the `resolveId` and `transform` hooks provided by unplugin
|
|
22
|
+
// so we need to disable the code splitter for it
|
|
23
|
+
// If you're using Rspack, and know how to implement the code splitting, please let us know
|
|
24
|
+
// We'd love to support it, but we're not sure how to do it yet
|
|
25
|
+
if (meta.framework === 'rspack') {
|
|
26
|
+
routerCodeSplitterOptions = []
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [...routerGeneratorOptions, ...routerCodeSplitterOptions]
|
|
30
|
+
}
|
package/src/router-generator.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { generator } from '@tanstack/router-generator'
|
|
|
3
3
|
|
|
4
4
|
import { getConfig } from './config'
|
|
5
5
|
import { CONFIG_FILE_NAME } from './constants'
|
|
6
|
-
import type { Config } from './config'
|
|
7
6
|
import type { UnpluginFactory } from 'unplugin'
|
|
7
|
+
import type { Config } from './config'
|
|
8
8
|
|
|
9
9
|
let lock = false
|
|
10
10
|
const checkLock = () => lock
|
|
@@ -12,6 +12,8 @@ const setLock = (bool: boolean) => {
|
|
|
12
12
|
lock = bool
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
const PLUGIN_NAME = 'unplugin:router-generator'
|
|
16
|
+
|
|
15
17
|
export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
16
18
|
Partial<Config> | undefined
|
|
17
19
|
> = (options = {}) => {
|
|
@@ -84,5 +86,14 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
84
86
|
await run(generate)
|
|
85
87
|
},
|
|
86
88
|
},
|
|
89
|
+
async rspack(compiler) {
|
|
90
|
+
userConfig = await getConfig(options, ROOT)
|
|
91
|
+
|
|
92
|
+
await run(generate)
|
|
93
|
+
|
|
94
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, async () => {
|
|
95
|
+
await run(generate)
|
|
96
|
+
})
|
|
97
|
+
},
|
|
87
98
|
}
|
|
88
99
|
}
|