Skip to content

Commit 5022a60

Browse files
fix: only transform files in codesplitter if changes were made (#5460)
1 parent 9eb000a commit 5022a60

File tree

51 files changed

+1032
-708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1032
-708
lines changed

packages/router-plugin/src/core/code-splitter/compilers.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export function compileCodeSplitReferenceRoute(
112112
id: string
113113
addHmr?: boolean
114114
},
115-
): GeneratorResult {
115+
): GeneratorResult | null {
116116
const ast = parseAst(opts)
117117

118118
const refIdents = findReferencedIdentifiers(ast)
@@ -132,6 +132,7 @@ export function compileCodeSplitReferenceRoute(
132132

133133
let createRouteFn: string
134134

135+
let modified = false as boolean
135136
babel.traverse(ast, {
136137
Program: {
137138
enter(programPath) {
@@ -170,6 +171,7 @@ export function compileCodeSplitReferenceRoute(
170171
if (t.isObjectProperty(prop)) {
171172
if (t.isIdentifier(prop.key)) {
172173
if (opts.deleteNodes!.has(prop.key.name as any)) {
174+
modified = true
173175
return false
174176
}
175177
}
@@ -181,6 +183,7 @@ export function compileCodeSplitReferenceRoute(
181183
if (!splittableCreateRouteFns.includes(createRouteFn)) {
182184
// we can't split this route but we still add HMR handling if enabled
183185
if (opts.addHmr) {
186+
modified = true
184187
programPath.pushContainer('body', routeHmrStatement)
185188
}
186189
// exit traversal so this route is not split
@@ -268,6 +271,8 @@ export function compileCodeSplitReferenceRoute(
268271
return
269272
}
270273

274+
modified = true
275+
271276
// Prepend the import statement to the program along with the importer function
272277
// Check to see if lazyRouteComponent is already imported before attempting
273278
// to import it again
@@ -305,9 +310,8 @@ export function compileCodeSplitReferenceRoute(
305310
if (opts.addHmr) {
306311
programPath.pushContainer('body', routeHmrStatement)
307312
}
308-
}
309-
310-
if (splitNodeMeta.splitStrategy === 'lazyFn') {
313+
} else {
314+
// if (splitNodeMeta.splitStrategy === 'lazyFn') {
311315
const value = prop.value
312316

313317
let shouldSplit = true
@@ -339,6 +343,7 @@ export function compileCodeSplitReferenceRoute(
339343
if (!shouldSplit) {
340344
return
341345
}
346+
modified = true
342347

343348
// Prepend the import statement to the program along with the importer function
344349
if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) {
@@ -404,6 +409,7 @@ export function compileCodeSplitReferenceRoute(
404409
* specifiers
405410
*/
406411
if (removableImportPaths.size > 0) {
412+
modified = true
407413
programPath.traverse({
408414
ImportDeclaration(path) {
409415
if (path.node.specifiers.length > 0) return
@@ -417,6 +423,9 @@ export function compileCodeSplitReferenceRoute(
417423
},
418424
})
419425

426+
if (!modified) {
427+
return null
428+
}
420429
deadCodeElimination(ast, refIdents)
421430

422431
// if there are exported identifiers, then we need to add a warning

packages/router-plugin/src/core/router-code-splitter-plugin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
138138
(userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction,
139139
})
140140

141+
if (compiledReferenceRoute === null) {
142+
if (debug) {
143+
console.info(
144+
`No changes made to route "${id}", skipping code-splitting.`,
145+
)
146+
}
147+
return null
148+
}
141149
if (debug) {
142150
logDiff(code, compiledReferenceRoute.code)
143151
console.log('Output:\n', compiledReferenceRoute.code + '\n\n')

packages/router-plugin/tests/add-hmr.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('add-hmr works', () => {
3232
targetFramework: framework,
3333
})
3434

35-
await expect(compileResult.code).toMatchFileSnapshot(
35+
await expect(compileResult?.code || code).toMatchFileSnapshot(
3636
path.join(dirs.snapshots, filename.replace('.tsx', '@true.tsx')),
3737
)
3838
},
@@ -53,7 +53,7 @@ describe('add-hmr works', () => {
5353
targetFramework: framework,
5454
})
5555

56-
await expect(compileResult.code).toMatchFileSnapshot(
56+
await expect(compileResult?.code || code).toMatchFileSnapshot(
5757
path.join(dirs.snapshots, filename.replace('.tsx', '@false.tsx')),
5858
)
5959
},
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import thing from 'thing';
1+
import thing from 'thing'
2+
23
export function test() {
34
const {
45
foo: {
5-
bar: {
6-
destructured
7-
}
8-
}
9-
} = thing;
10-
return destructured;
11-
}
6+
bar: { destructured },
7+
},
8+
} = thing
9+
10+
return destructured
11+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import thing from 'thing';
1+
import thing from 'thing'
2+
23
export function test() {
34
const {
45
foo: {
5-
bar: {
6-
destructured
7-
}
8-
}
9-
} = thing;
10-
return destructured;
11-
}
6+
bar: { destructured },
7+
},
8+
} = thing
9+
10+
return destructured
11+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react';
2-
import { createFileRoute } from '@tanstack/react-router';
1+
import React from 'react'
2+
import { createFileRoute } from '@tanstack/react-router'
3+
34
export const Route = createFileRoute('/')({
4-
component: undefined
5-
});
5+
component: undefined,
6+
})
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react';
2-
import { createFileRoute } from '@tanstack/react-router';
1+
import React from 'react'
2+
import { createFileRoute } from '@tanstack/react-router'
3+
34
export const Route = createFileRoute('/')({
4-
component: undefined
5-
});
5+
component: undefined,
6+
})

packages/router-plugin/tests/code-splitter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('code-splitter works', () => {
6767
targetFramework: framework,
6868
})
6969

70-
await expect(compileResult.code).toMatchFileSnapshot(
70+
await expect(compileResult?.code || code).toMatchFileSnapshot(
7171
path.join(dirs.snapshots, groupName, filename),
7272
)
7373
},
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import thing from 'thing';
1+
import thing from 'thing'
2+
23
export function test() {
34
const {
45
foo: {
5-
bar: {
6-
destructured
7-
}
8-
}
9-
} = thing;
10-
return destructured;
11-
}
6+
bar: { destructured },
7+
},
8+
} = thing
9+
10+
return destructured
11+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react';
2-
import { createFileRoute } from '@tanstack/react-router';
1+
import React from 'react'
2+
import { createFileRoute } from '@tanstack/react-router'
3+
34
export const Route = createFileRoute('/')({
4-
component: undefined
5-
});
5+
component: undefined,
6+
})

0 commit comments

Comments
 (0)