Open History to see the last commit
1import { createRspackPlugin } from 'unplugin'
2β
3import { configSchema } from './core/config'
4import { createRouterCodeSplitterPlugin } from './core/router-code-splitter-plugin'
5import { createRouterGeneratorPlugin } from './core/router-generator-plugin'
6import { unpluginRouterComposedFactory } from './core/router-composed-plugin'
7import { createRouterPluginContext } from './core/router-plugin-context'
8import type { CodeSplittingOptions, Config } from './core/config'
9import type { RouterPluginContext } from './core/router-plugin-context'
10β
11type RspackRouterPluginOptions = Partial<Config> | (() => Partial<Config>)
12β
13const defaultRouterPluginContext = createRouterPluginContext()
14β
15/**
16 * Rspack uses webpack-compatible `module.hot` / `import.meta.webpackHot` HMR.
17 * Force `plugin.hmr.style = 'webpack'` so the router HMR adapter emits
18 * `module.hot`-style accept/dispose code instead of Vite's callback-receive
19 * variant, regardless of what the user passes (or doesn't pass).
20 */
21function withWebpackHmrStyle(
22 options: RspackRouterPluginOptions | undefined,
23): RspackRouterPluginOptions {
24 const mergeHmrStyle = (
25 config: Partial<Config> | undefined,
26 ): Partial<Config> => ({
27 ...config,
28 plugin: {
29 ...config?.plugin,
30 hmr: {
31 ...config?.plugin?.hmr,
32 style: 'webpack',
33 },
34 },
35 })
36β
37 if (typeof options === 'function') {
38 return () => mergeHmrStyle(options())
39 }
40β
41 return mergeHmrStyle(options)
42}
43β
44/**
45 * @example
46 * ```ts
47 * export default defineConfig({
48 * // ...
49 * tools: {
50 * rspack: {
51 * plugins: [TanStackRouterGeneratorRspack()],
52 * },
53 * },
54 * })
55 * ```
56 */
57const TanStackRouterGeneratorRspack = (
58 options?: RspackRouterPluginOptions,
59 routerPluginContext?: RouterPluginContext,
60) => {
61 const pluginContext = routerPluginContext ?? defaultRouterPluginContext
62 return createRspackPlugin((pluginOptions) =>
63 createRouterGeneratorPlugin(
64 pluginOptions as Partial<Config | (() => Config)> | undefined,
65 pluginContext,
66 ),
67 )(options)
68}
69β
70/**
71 * @example
72 * ```ts
73 * export default defineConfig({
74 * // ...
75 * tools: {
76 * rspack: {
77 * plugins: [TanStackRouterCodeSplitterRspack()],
78 * },
79 * },
80 * })
81 * ```
82 */
83const TanStackRouterCodeSplitterRspack = (
84 options?: RspackRouterPluginOptions,
85 routerPluginContext?: RouterPluginContext,
86) => {
87 const pluginContext = routerPluginContext ?? defaultRouterPluginContext
88 return createRspackPlugin((pluginOptions) =>
89 createRouterCodeSplitterPlugin(
90 withWebpackHmrStyle(
91 pluginOptions as RspackRouterPluginOptions | undefined,
92 ) as Partial<Config | (() => Config)>,
93 pluginContext,
94 ),
95 )(options)
96}
97β
98/**
99 * @example
100 * ```ts
101 * export default defineConfig({
102 * // ...
103 * tools: {
104 * rspack: {
105 * plugins: [tanstackRouter()],
106 * },
107 * },
108 * })
109 * ```
110 */
111const TanStackRouterRspack = /* #__PURE__ */ createRspackPlugin(
112 (options, meta) =>
113 unpluginRouterComposedFactory(
114 withWebpackHmrStyle(
115 options as RspackRouterPluginOptions | undefined,
116 ) as Partial<Config | (() => Config)>,
117 meta,
118 ),
119)
120const tanstackRouter = TanStackRouterRspack
121export default TanStackRouterRspack
122export {
123 configSchema,
124 TanStackRouterRspack,
125 TanStackRouterGeneratorRspack,
126 TanStackRouterCodeSplitterRspack,
127 tanstackRouter,
128}
129export type { Config, CodeSplittingOptions, RouterPluginContext }