@tanstack/router-plugin 1.95.5 → 1.96.0

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.
@@ -1,6 +1,7 @@
1
1
  import { isAbsolute, join, normalize } from 'node:path'
2
- import { fileURLToPath, pathToFileURL } from 'node:url'
3
2
 
3
+ import { fileURLToPath, pathToFileURL } from 'node:url'
4
+ import { logDiff } from '../logger'
4
5
  import { getConfig } from './config'
5
6
  import {
6
7
  compileCodeSplitReferenceRoute,
@@ -11,6 +12,10 @@ import { splitPrefix } from './constants'
11
12
  import type { Config } from './config'
12
13
  import type { UnpluginContextMeta, UnpluginFactory } from 'unplugin'
13
14
 
15
+ const debug =
16
+ process.env.TSR_VITE_DEBUG &&
17
+ ['true', 'router-plugin'].includes(process.env.TSR_VITE_DEBUG)
18
+
14
19
  function capitalizeFirst(str: string): string {
15
20
  return str.charAt(0).toUpperCase() + str.slice(1)
16
21
  }
@@ -57,26 +62,15 @@ plugins: [
57
62
  }
58
63
 
59
64
  const PLUGIN_NAME = 'unplugin:router-code-splitter'
60
- const JoinedSplitPrefix = splitPrefix + ':'
61
65
 
62
66
  export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
63
67
  Partial<Config> | undefined
64
68
  > = (options = {}, { framework }) => {
65
- const debug = Boolean(process.env.TSR_VITE_DEBUG)
66
-
67
69
  let ROOT: string = process.cwd()
68
70
  let userConfig = options as Config
69
71
 
70
72
  const handleSplittingFile = (code: string, id: string) => {
71
- if (debug) console.info('Splitting route: ', id)
72
-
73
- if (debug) console.info('')
74
- if (debug) console.info('Split Route Input: ', id)
75
- if (debug) console.info('')
76
- if (debug) console.info(code)
77
- if (debug) console.info('')
78
- if (debug) console.info('')
79
- if (debug) console.info('')
73
+ if (debug) console.info('Splitting Route: ', id)
80
74
 
81
75
  const compiledVirtualRoute = compileCodeSplitVirtualRoute({
82
76
  code,
@@ -84,19 +78,16 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
84
78
  filename: id,
85
79
  })
86
80
 
87
- if (debug) console.info('')
88
- if (debug) console.info('Split Route Output: ', id)
89
- if (debug) console.info('')
90
- if (debug) console.info(compiledVirtualRoute.code)
91
- if (debug) console.info('')
92
- if (debug) console.info('')
93
- if (debug) console.info('')
81
+ if (debug) {
82
+ logDiff(code, compiledVirtualRoute.code)
83
+ console.log('Output:\n', compiledVirtualRoute.code)
84
+ }
94
85
 
95
86
  return compiledVirtualRoute
96
87
  }
97
88
 
98
89
  const handleCompilingFile = (code: string, id: string) => {
99
- if (debug) console.info('Handling createRoute: ', id)
90
+ if (debug) console.info('Compiling Route: ', id)
100
91
 
101
92
  const compiledReferenceRoute = compileCodeSplitReferenceRoute({
102
93
  code,
@@ -104,13 +95,10 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
104
95
  filename: id,
105
96
  })
106
97
 
107
- if (debug) console.info('')
108
- if (debug) console.info('Handling createRoute output: ', id)
109
- if (debug) console.info('')
110
- if (debug) console.info(compiledReferenceRoute.code)
111
- if (debug) console.info('')
112
- if (debug) console.info('')
113
- if (debug) console.info('')
98
+ if (debug) {
99
+ logDiff(code, compiledReferenceRoute.code)
100
+ console.log('Output:\n', compiledReferenceRoute.code + '\n\n')
101
+ }
114
102
 
115
103
  return compiledReferenceRoute
116
104
  }
@@ -119,17 +107,6 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
119
107
  name: 'router-code-splitter-plugin',
120
108
  enforce: 'pre',
121
109
 
122
- resolveId(source) {
123
- if (!userConfig.autoCodeSplitting) {
124
- return null
125
- }
126
-
127
- if (source.startsWith(splitPrefix + ':')) {
128
- return source.replace(splitPrefix + ':', '')
129
- }
130
- return null
131
- },
132
-
133
110
  transform(code, id) {
134
111
  if (!userConfig.autoCodeSplitting) {
135
112
  return null
@@ -161,17 +138,11 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
161
138
  return null
162
139
  },
163
140
 
164
- transformInclude(transformId) {
141
+ transformInclude(id) {
165
142
  if (!userConfig.autoCodeSplitting) {
166
143
  return undefined
167
144
  }
168
145
 
169
- let id = transformId
170
-
171
- if (id.startsWith(JoinedSplitPrefix)) {
172
- id = id.replace(JoinedSplitPrefix, '')
173
- }
174
-
175
146
  if (
176
147
  fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||
177
148
  id.includes(splitPrefix)
@@ -191,41 +162,11 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
191
162
 
192
163
  rspack(compiler) {
193
164
  ROOT = process.cwd()
194
-
195
- compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {
196
- self.normalModuleFactory.hooks.beforeResolve.tap(
197
- PLUGIN_NAME,
198
- (resolveData: { request: string }) => {
199
- if (resolveData.request.includes(JoinedSplitPrefix)) {
200
- resolveData.request = resolveData.request.replace(
201
- JoinedSplitPrefix,
202
- '',
203
- )
204
- }
205
- },
206
- )
207
- })
208
-
209
165
  userConfig = getConfig(options, ROOT)
210
166
  },
211
167
 
212
168
  webpack(compiler) {
213
169
  ROOT = process.cwd()
214
-
215
- compiler.hooks.beforeCompile.tap(PLUGIN_NAME, (self) => {
216
- self.normalModuleFactory.hooks.beforeResolve.tap(
217
- PLUGIN_NAME,
218
- (resolveData: { request: string }) => {
219
- if (resolveData.request.includes(JoinedSplitPrefix)) {
220
- resolveData.request = resolveData.request.replace(
221
- JoinedSplitPrefix,
222
- '',
223
- )
224
- }
225
- },
226
- )
227
- })
228
-
229
170
  userConfig = getConfig(options, ROOT)
230
171
 
231
172
  if (
package/src/logger.ts ADDED
@@ -0,0 +1,59 @@
1
+ import chalk from 'chalk'
2
+ import { diffWords } from 'diff'
3
+
4
+ export function logDiff(oldStr: string, newStr: string) {
5
+ const differences = diffWords(oldStr, newStr)
6
+
7
+ let output = ''
8
+ let unchangedLines = ''
9
+
10
+ function processUnchangedLines(lines: string): string {
11
+ const lineArray = lines.split('\n')
12
+ if (lineArray.length > 4) {
13
+ return [
14
+ chalk.dim(lineArray[0]),
15
+ chalk.dim(lineArray[1]),
16
+ '',
17
+ chalk.dim.bold(`... (${lineArray.length - 4} lines) ...`),
18
+ '',
19
+ chalk.dim(lineArray[lineArray.length - 2]),
20
+ chalk.dim(lineArray[lineArray.length - 1]),
21
+ ].join('\n')
22
+ }
23
+ return chalk.dim(lines)
24
+ }
25
+
26
+ differences.forEach((part, index) => {
27
+ const nextPart = differences[index + 1]
28
+
29
+ if (part.added) {
30
+ if (unchangedLines) {
31
+ output += processUnchangedLines(unchangedLines)
32
+ unchangedLines = ''
33
+ }
34
+ output += chalk.green.bold(part.value)
35
+ if (nextPart?.removed) output += ' '
36
+ } else if (part.removed) {
37
+ if (unchangedLines) {
38
+ output += processUnchangedLines(unchangedLines)
39
+ unchangedLines = ''
40
+ }
41
+ output += chalk.red.bold(part.value)
42
+ if (nextPart?.added) output += ' '
43
+ } else {
44
+ unchangedLines += part.value
45
+ }
46
+ })
47
+
48
+ // Process any remaining unchanged lines at the end
49
+ if (unchangedLines) {
50
+ output += processUnchangedLines(unchangedLines)
51
+ }
52
+
53
+ if (output) {
54
+ console.log('\nDiff:')
55
+ console.log(output + '\n')
56
+ } else {
57
+ console.log('No changes')
58
+ }
59
+ }