Repository navigation
fix(router-plugin): throw on autoCodeSplitting with esbuild #8852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/router-plugin': patch | ||
| --- | ||
|
|
||
| The esbuild plugin now throws when `autoCodeSplitting` is enabled, and `TanStackRouterCodeSplitterEsbuild` throws, instead of silently bundling every route without splitting. Route generation with esbuild is unchanged. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ export default { | |
| }), | ||
| tanstackRouter({ | ||
| target: 'vue', | ||
| autoCodeSplitting: true, | ||
| }), | ||
| ], | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import { createEsbuildPlugin } from 'unplugin' | ||
|
|
||
| import { configSchema } from './core/config' | ||
| import { createRouterCodeSplitterPlugin } from './core/router-code-splitter-plugin' | ||
| import { configSchema, getConfig } from './core/config' | ||
| import { createRouterGeneratorPlugin } from './core/router-generator-plugin' | ||
| import { unpluginRouterComposedFactory } from './core/router-composed-plugin' | ||
| import { createRouterPluginContext } from './core/router-plugin-context' | ||
|
|
@@ -13,6 +12,20 @@ type RouterPluginOptions = Partial<Config | (() => Config)> | undefined | |
|
|
||
| const defaultRouterPluginContext = createRouterPluginContext() | ||
|
|
||
| const ESBUILD_SPLITTING_ERROR = | ||
| '@tanstack/router-plugin: route code splitting (`autoCodeSplitting`) is not supported with esbuild. ' + | ||
| 'Use the generator alone (`TanStackRouterGeneratorEsbuild`), or set `autoCodeSplitting: false`.' | ||
|
|
||
| function refuseSplitting(options: RouterPluginOptions) { | ||
| const config = | ||
| typeof options === 'function' | ||
| ? options() | ||
| : getConfig(options ?? {}, process.cwd()) | ||
| if (config.autoCodeSplitting) { | ||
| throw new Error(ESBUILD_SPLITTING_ERROR) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @example | ||
| * ```ts | ||
|
|
@@ -33,22 +46,14 @@ const TanStackRouterGeneratorEsbuild = ( | |
| } | ||
|
|
||
| /** | ||
| * @example | ||
| * ```ts | ||
| * export default { | ||
| * plugins: [TanStackRouterCodeSplitterEsbuild()], | ||
| * // ... | ||
| * } | ||
| * ``` | ||
| * Route code splitting is not available with esbuild: this entry refuses | ||
| * clearly. | ||
| */ | ||
| const TanStackRouterCodeSplitterEsbuild = ( | ||
| options?: RouterPluginOptions, | ||
| routerPluginContext?: RouterPluginContext, | ||
| ) => { | ||
| const pluginContext = routerPluginContext ?? defaultRouterPluginContext | ||
| return createEsbuildPlugin((pluginOptions: RouterPluginOptions) => | ||
| createRouterCodeSplitterPlugin(pluginOptions, pluginContext), | ||
| )(options) | ||
| _options?: RouterPluginOptions, | ||
| _routerPluginContext?: RouterPluginContext, | ||
| ): never => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| throw new Error(ESBUILD_SPLITTING_ERROR) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -60,7 +65,10 @@ const TanStackRouterCodeSplitterEsbuild = ( | |
| * } | ||
| * ``` | ||
| */ | ||
| const TanStackRouterEsbuild = createEsbuildPlugin(unpluginRouterComposedFactory) | ||
| const TanStackRouterEsbuild = (options: RouterPluginOptions = {}) => { | ||
| refuseSplitting(options) | ||
| return createEsbuildPlugin(unpluginRouterComposedFactory)(options) | ||
| } | ||
| const tanstackRouter = TanStackRouterEsbuild | ||
| export default TanStackRouterEsbuild | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { describe, expect, test } from 'vitest' | ||
| import { | ||
| TanStackRouterCodeSplitterEsbuild, | ||
| TanStackRouterGeneratorEsbuild, | ||
| tanstackRouter, | ||
| } from '../src/esbuild' | ||
|
|
||
| describe('esbuild plugin', () => { | ||
| test('refuses autoCodeSplitting', () => { | ||
| expect(() => | ||
| tanstackRouter({ target: 'react', autoCodeSplitting: true }), | ||
| ).toThrow(/autoCodeSplitting/) | ||
| }) | ||
|
|
||
| test('refuses the code splitter entry', () => { | ||
| expect(() => TanStackRouterCodeSplitterEsbuild()).toThrow( | ||
| /autoCodeSplitting/, | ||
| ) | ||
| }) | ||
|
|
||
| test('accepts route generation without code splitting', () => { | ||
| expect(() => tanstackRouter({ target: 'react' })).not.toThrow() | ||
| expect(() => | ||
| tanstackRouter({ target: 'react', autoCodeSplitting: false }), | ||
| ).not.toThrow() | ||
| expect(() => TanStackRouterGeneratorEsbuild()).not.toThrow() | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve function-valued options once before checking them.
If an options callback returns
autoCodeSplitting: falseon its first call andtrueon its second,refuseSplittingaccepts it. The composed plugin then enables the code splitter without the intended error. Pass the same resolved options to both the check and the composed plugin. (raw.githubusercontent.com)