Repository: TanStack/router
Pull request #2155 (2024-08-20 12:07:35)

refactor(router plugin): restructure the plugin's internals
Prior to the migration to unplugin, the router's plugin "messy" structure had been growing, but since there wasn't really a standard to adhere to, it was fine.
Now that we've moved over to unplugin, it makes sense to follow their conventions on structuring plugins so contributors in the future don't have to spend time trying to figure out what file in the `src` folder did what.

nx-cloud[bot]: # ☁️ Nx Cloud Report

CI is running for commit 01aadf4aa2e7ea2c163a92124caa9d0853b1aa04.

[📂 Click to track the progress, see the status, the terminal output, and the build insights.](https://cloud.nx.app/cipes/66c4871db883cb14d7bb9c7a?utm_source=pull-request&utm_medium=comment)


---

Sent with 💌 from [NxCloud.](https://nx.app?utm_source=pull-request&utm_medium=comment)
pkg-pr-new[bot]: 
_commit: <a href="https://github.com/TanStack/router/runs/28998103298"><code>01aadf4</code></a>_


<details><summary><b>@tanstack/history</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/history@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/react-cross-context</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-cross-context@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/react-router</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-router@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/react-router-with-query</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/react-router-with-query@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-arktype-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-arktype-adapter@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-cli</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-cli@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-devtools</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-devtools@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-generator</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-generator@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-plugin@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-valibot-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-valibot-adapter@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-vite-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-vite-plugin@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/router-zod-adapter</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/router-zod-adapter@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/start</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/start@2155
```

</p></details>
      
    

<details><summary><b>@tanstack/start-vite-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/@tanstack/start-vite-plugin@2155
```

</p></details>
      
    

[Open in Stackblitz](https://pkg.pr.new/template/d3ea7e74-335a-40fe-99e7-b02fc57074f5)
<details><summary><b>More templates</b></summary><p>

- [tanstack-router-react-example-authenticated-routes](https://pkg.pr.new/template/ba65ce6a-5607-4e00-8bc2-6fa1f98dfa3d)
- [tanstack-router-react-example-basic](https://pkg.pr.new/template/4b0d6fad-0384-4eab-bb07-03996b256de0)
- [tanstack-router-react-example-basic-default-search-params](https://pkg.pr.new/template/c73e7caf-ea31-420c-a5c6-6ebf270b6c90)
- [tanstack-router-react-example-basic-file-based](https://pkg.pr.new/template/e7a9cb14-f65d-4402-a2d9-dd95ddd41164)
- [tanstack-router-react-example-basic-file-based-codesplitting](https://pkg.pr.new/template/e652aa94-8fd4-4a07-8bda-94457a99d4f8)
- [tanstack-router-react-example-react-query](https://pkg.pr.new/template/8b658a0b-772e-4134-b504-f8635a727091)
- [tanstack-router-react-example-basic-react-query-file-based](https://pkg.pr.new/template/eee9d759-7c10-492d-b24a-7f9813ac5b01)
- [tanstack-router-react-example-basic-ssr-file-based](https://pkg.pr.new/template/38104ec1-e80d-41ff-9c75-ea4fad64cbbb)
- [tanstack-router-react-example-basic-ssr-streaming-file-based](https://pkg.pr.new/template/b4bf07e9-2a5f-4695-afc3-ccc00e11568c)
- [tanstack-router-react-example-deferred-data](https://pkg.pr.new/template/ca53ca49-c53e-4331-979e-517e837001bf)
- [tanstack-router-react-example-kitchen-sink](https://pkg.pr.new/template/4385f0a9-a1a0-4f9f-b902-05aaaa3abdc7)
- [tanstack-router-react-example-kitchen-sink-file-based](https://pkg.pr.new/template/b1077787-26e4-4f78-ae41-37e272fb55a0)
- [tanstack-router-react-example-kitchen-sink-react-query](https://pkg.pr.new/template/1ad87d80-49f7-4079-a439-7e5d2a3fc0fb)
- [tanstack-router-react-example-kitchen-sink-react-query-file-based](https://pkg.pr.new/template/5a19a5ca-a067-4647-a030-657ca0e44222)
- [tanstack-router-react-example-large-file-based](https://pkg.pr.new/template/5ebee14f-ab5c-4a28-b2be-bc63baede6c3)
- [tanstack-router-react-example-location-masking](https://pkg.pr.new/template/5d8cf321-8151-40a5-a53b-342f1bb74fde)
- [tanstack-router-react-example-navigation-blocking](https://pkg.pr.new/template/923ccf6f-a318-4c75-9f17-edbf781dd2ff)
- [tanstack-router-react-example-quickstart](https://pkg.pr.new/template/c8f78096-a288-4cc7-b3e4-a84e66e6963e)
- [tanstack-router-react-example-quickstart-file-based](https://pkg.pr.new/template/ccafdf43-8a5f-4b05-9ecb-d08cacdc2255)
- [tanstack-router-react-example-quickstart-rspack-file-based](https://pkg.pr.new/template/5f1d8e1d-3abf-4803-bd63-772c20bd9875)
- [tanstack-router-react-example-quickstart-webpack-file-based](https://pkg.pr.new/template/c1f315bb-55a9-4bbb-8d8b-d9566a5e920b)
- [tanstack-router-react-example-scroll-restoration](https://pkg.pr.new/template/18ea4411-c022-461e-aac4-2de249c30781)
- [tanstack-router-search-validator-adapters](https://pkg.pr.new/template/761e1932-0af5-43e7-8793-68f5fa7a0397)
- [tanstack-router-example-react-start-basic](https://pkg.pr.new/template/88dff5ab-346b-44b6-bf71-82121d315248)
- [tanstack-router-example-react-start-basic-counter](https://pkg.pr.new/template/05812cff-8dea-46ed-8a1d-3dca07e75e7a)
- [tanstack-router-example-react-start-basic-react-query](https://pkg.pr.new/template/f4177304-427d-4a2d-8768-7085968edd6a)
- [tanstack-router-example-react-start-basic-rsc](https://pkg.pr.new/template/753bf882-6618-4d33-a9ae-ce1fbabaa2f4)
- [tanstack-router-example-react-start-trello-trellaux](https://pkg.pr.new/template/bf6be495-6e15-48c3-8cf7-483690f8ceb1)
- [tanstack-router-example-react-start-basic-trellaux](https://pkg.pr.new/template/4489f62e-e854-48ba-8a4f-bdcef8975e5c)
- [tanstack-router-react-example-with-framer-motion](https://pkg.pr.new/template/2b22c336-d3f4-4c70-bf1e-e009aa28849c)
- [tanstack-router-react-example-with-trpc](https://pkg.pr.new/template/74c64a15-33e8-4ebc-8222-fbaf85358da4)
- [tanstack-router-react-example-with-trpc-react-query](https://pkg.pr.new/template/b2e5cf8b-6097-4594-a630-fd23f0676034)

</p></details>
      
    


Patch:
diff --git a/packages/router-plugin/src/ast.ts b/packages/router-plugin/src/core/code-splitter/ast.ts
similarity index 100%
rename from packages/router-plugin/src/ast.ts
rename to packages/router-plugin/src/core/code-splitter/ast.ts
diff --git a/packages/router-plugin/src/compilers.ts b/packages/router-plugin/src/core/code-splitter/compilers.ts
similarity index 99%
rename from packages/router-plugin/src/compilers.ts
rename to packages/router-plugin/src/core/code-splitter/compilers.ts
index 77a354ca7..b7feabd10 100644
--- a/packages/router-plugin/src/compilers.ts
+++ b/packages/router-plugin/src/core/code-splitter/compilers.ts
@@ -4,7 +4,7 @@ import _generate from '@babel/generator'
 import * as template from '@babel/template'
 import { deadCodeElimination } from 'babel-dead-code-elimination'
 
-import { splitPrefix } from './constants'
+import { splitPrefix } from '../constants'
 import { parseAst } from './ast'
 import type { ParseAstOptions } from './ast'
 
diff --git a/packages/router-plugin/src/config.ts b/packages/router-plugin/src/core/config.ts
similarity index 100%
rename from packages/router-plugin/src/config.ts
rename to packages/router-plugin/src/core/config.ts
diff --git a/packages/router-plugin/src/constants.ts b/packages/router-plugin/src/core/constants.ts
similarity index 100%
rename from packages/router-plugin/src/constants.ts
rename to packages/router-plugin/src/core/constants.ts
diff --git a/packages/router-plugin/src/code-splitter.ts b/packages/router-plugin/src/core/router-code-splitter-plugin.ts
similarity index 99%
rename from packages/router-plugin/src/code-splitter.ts
rename to packages/router-plugin/src/core/router-code-splitter-plugin.ts
index 03fc49685..638f6b754 100644
--- a/packages/router-plugin/src/code-splitter.ts
+++ b/packages/router-plugin/src/core/router-code-splitter-plugin.ts
@@ -1,11 +1,11 @@
 import { isAbsolute, join } from 'node:path'
 import { fileURLToPath, pathToFileURL } from 'node:url'
 
+import { getConfig } from './config'
 import {
   compileCodeSplitReferenceRoute,
   compileCodeSplitVirtualRoute,
-} from './compilers'
-import { getConfig } from './config'
+} from './code-splitter/compilers'
 import { splitPrefix } from './constants'
 
 import type { Config } from './config'
diff --git a/packages/router-plugin/src/composed.ts b/packages/router-plugin/src/core/router-composed-plugin.ts
similarity index 88%
rename from packages/router-plugin/src/composed.ts
rename to packages/router-plugin/src/core/router-composed-plugin.ts
index a6e7ea6c8..a8256f8b4 100644
--- a/packages/router-plugin/src/composed.ts
+++ b/packages/router-plugin/src/core/router-composed-plugin.ts
@@ -1,5 +1,5 @@
-import { unpluginRouterGeneratorFactory } from './router-generator'
-import { unpluginRouterCodeSplitterFactory } from './code-splitter'
+import { unpluginRouterGeneratorFactory } from './router-generator-plugin'
+import { unpluginRouterCodeSplitterFactory } from './router-code-splitter-plugin'
 
 import type { Config } from './config'
 import type { UnpluginFactory } from 'unplugin'
diff --git a/packages/router-plugin/src/router-generator.ts b/packages/router-plugin/src/core/router-generator-plugin.ts
similarity index 100%
rename from packages/router-plugin/src/router-generator.ts
rename to packages/router-plugin/src/core/router-generator-plugin.ts
diff --git a/packages/router-plugin/src/index.ts b/packages/router-plugin/src/index.ts
index 42bd84014..43e52c85d 100644
--- a/packages/router-plugin/src/index.ts
+++ b/packages/router-plugin/src/index.ts
@@ -1,4 +1,4 @@
-export { unpluginRouterCodeSplitterFactory } from './code-splitter'
-export { unpluginRouterGeneratorFactory } from './router-generator'
-export { configSchema } from './config'
-export type { Config } from './config'
+export { configSchema } from './core/config'
+export { unpluginRouterCodeSplitterFactory } from './core/router-code-splitter-plugin'
+export { unpluginRouterGeneratorFactory } from './core/router-generator-plugin'
+export type { Config } from './core/config'
diff --git a/packages/router-plugin/src/rspack.ts b/packages/router-plugin/src/rspack.ts
index ddf38a4fe..ebcafb8b3 100644
--- a/packages/router-plugin/src/rspack.ts
+++ b/packages/router-plugin/src/rspack.ts
@@ -1,9 +1,10 @@
 import { createRspackPlugin } from 'unplugin'
-import { unpluginRouterCodeSplitterFactory } from './code-splitter'
-import { configSchema } from './config'
-import { unpluginRouterGeneratorFactory } from './router-generator'
-import { unpluginRouterComposedFactory } from './composed'
-import type { Config } from './config'
+
+import { configSchema } from './core/config'
+import { unpluginRouterCodeSplitterFactory } from './core/router-code-splitter-plugin'
+import { unpluginRouterGeneratorFactory } from './core/router-generator-plugin'
+import { unpluginRouterComposedFactory } from './core/router-composed-plugin'
+import type { Config } from './core/config'
 
 /**
  * @example
diff --git a/packages/router-plugin/src/vite.ts b/packages/router-plugin/src/vite.ts
index 3ea51911a..a66e94241 100644
--- a/packages/router-plugin/src/vite.ts
+++ b/packages/router-plugin/src/vite.ts
@@ -1,10 +1,11 @@
 import { createVitePlugin } from 'unplugin'
-import { unpluginRouterCodeSplitterFactory } from './code-splitter'
-import { configSchema } from './config'
-import { unpluginRouterGeneratorFactory } from './router-generator'
-import { unpluginRouterComposedFactory } from './composed'
 
-import type { Config } from './config'
+import { configSchema } from './core/config'
+import { unpluginRouterCodeSplitterFactory } from './core/router-code-splitter-plugin'
+import { unpluginRouterGeneratorFactory } from './core/router-generator-plugin'
+import { unpluginRouterComposedFactory } from './core/router-composed-plugin'
+
+import type { Config } from './core/config'
 
 /**
  * @example
diff --git a/packages/router-plugin/src/webpack.ts b/packages/router-plugin/src/webpack.ts
index 99f05602d..45e8c173e 100644
--- a/packages/router-plugin/src/webpack.ts
+++ b/packages/router-plugin/src/webpack.ts
@@ -1,9 +1,10 @@
 import { createWebpackPlugin } from 'unplugin'
-import { unpluginRouterCodeSplitterFactory } from './code-splitter'
-import { configSchema } from './config'
-import { unpluginRouterGeneratorFactory } from './router-generator'
-import { unpluginRouterComposedFactory } from './composed'
-import type { Config } from './config'
+
+import { configSchema } from './core/config'
+import { unpluginRouterCodeSplitterFactory } from './core/router-code-splitter-plugin'
+import { unpluginRouterGeneratorFactory } from './core/router-generator-plugin'
+import { unpluginRouterComposedFactory } from './core/router-composed-plugin'
+import type { Config } from './core/config'
 
 /**
  * @example


Test patch:
diff --git a/packages/router-plugin/tests/code-splitter.test.ts b/packages/router-plugin/tests/code-splitter.test.ts
index 7ccaafad0..31633185a 100644
--- a/packages/router-plugin/tests/code-splitter.test.ts
+++ b/packages/router-plugin/tests/code-splitter.test.ts
@@ -5,8 +5,8 @@ import { describe, expect, it } from 'vitest'
 import {
   compileCodeSplitReferenceRoute,
   compileCodeSplitVirtualRoute,
-} from '../src/compilers'
-import { splitPrefix } from '../src/constants'
+} from '../src/core/code-splitter/compilers'
+import { splitPrefix } from '../src/core/constants'
 
 async function getFilenames() {
   return await readdir(path.resolve(__dirname, './code-splitter/test-files'))