Skip to content

Commit 46bd74b

Browse files
fix
1 parent 55406b0 commit 46bd74b

4 files changed

Lines changed: 77 additions & 17 deletions

File tree

‎packages/start-plugin-core/src/vite/import-protection-plugin/plugin.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,13 +1132,13 @@ export function importProtectionPlugin(
11321132
}
11331133

11341134
const importers = env.graph.reverseEdges.get(current)
1135-
if (!importers || importers.size === 0) {
1136-
if (current !== file) {
1137-
return 'reachable'
1138-
}
1135+
if (!importers) {
1136+
continue
1137+
}
11391138

1139+
if (importers.size === 0) {
11401140
const routesDirectory = normalizePath(`${config.srcDirectory}/routes`)
1141-
if (isInsideDirectory(current, routesDirectory)) {
1141+
if (current === file || isInsideDirectory(current, routesDirectory)) {
11421142
return 'reachable'
11431143
}
11441144
continue

‎packages/start-plugin-core/src/vite/plugins.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function createDevClientEntryPlugin(opts: {
3131
}
3232

3333
return (
34-
getReactRefreshPreambleCode(this.environment.config.base) +
34+
getReactRefreshPreambleCode(reactRefresh.id) +
3535
`\nawait import(${clientEntry})`
3636
)
3737
}
@@ -42,19 +42,31 @@ export function createDevClientEntryPlugin(opts: {
4242
}
4343

4444
function shouldInjectReactRefreshPreamble(
45-
environment: any,
45+
environment: ReactRefreshEnvironment | undefined,
4646
framework: CompileStartFrameworkOptions,
4747
) {
48+
const config = environment?.config
49+
4850
return (
4951
framework === 'react' &&
50-
environment?.config?.command === 'serve' &&
51-
environment.config.consumer === 'client' &&
52-
environment.config.server?.hmr !== false
52+
config?.command === 'serve' &&
53+
config.consumer === 'client' &&
54+
config.server?.hmr !== false
5355
)
5456
}
5557

56-
function getReactRefreshPreambleCode(base = '/') {
57-
return `import { injectIntoGlobalHook } from ${JSON.stringify(`${base}@react-refresh`)};
58+
interface ReactRefreshEnvironment {
59+
config?: {
60+
command?: string
61+
consumer?: string
62+
server?: {
63+
hmr?: boolean
64+
}
65+
}
66+
}
67+
68+
function getReactRefreshPreambleCode(reactRefreshId: string) {
69+
return `import { injectIntoGlobalHook } from ${JSON.stringify(reactRefreshId)};
5870
injectIntoGlobalHook(window);
5971
window.$RefreshReg$ = () => {};
6072
window.$RefreshSig$ = () => (type) => type;

‎packages/start-plugin-core/tests/importProtection/plugin.resolve.test.ts‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test, vi } from 'vitest'
22
import { importProtectionPlugin } from '../../src/vite/import-protection-plugin/plugin'
33

4-
describe('importProtectionPlugin resolveId', () => {
4+
describe('importProtectionPlugin', () => {
55
test('returns the nested resolver result when a resolved import is allowed', async () => {
66
const root = '/repo'
77
const plugin = createPlugin(root)
@@ -38,9 +38,53 @@ describe('importProtectionPlugin resolveId', () => {
3838
skipSelf: true,
3939
})
4040
})
41+
42+
test('keeps bundled-dev client violations pending until route importers are known', async () => {
43+
const root = '/repo'
44+
const { plugin, transformCachePlugin } = createPlugins(root, {
45+
command: 'serve',
46+
bundledDev: true,
47+
})
48+
plugin.buildStart()
49+
50+
const routeFile = `${root}/src/routes/leak.tsx`
51+
const deniedFile = `${root}/src/lib/secret.server.ts`
52+
const warn = vi.fn()
53+
const resolve = vi.fn(async (source: string) =>
54+
source === '../lib/secret.server' ? { id: deniedFile } : null,
55+
)
56+
57+
await transformCachePlugin.transform.handler.call(
58+
{
59+
environment: { name: 'client' },
60+
resolve,
61+
warn,
62+
error(message: string) {
63+
throw new Error(message)
64+
},
65+
getCombinedSourcemap: () => null,
66+
},
67+
`import { secret } from '../lib/secret.server'
68+
export const value = secret
69+
`,
70+
routeFile,
71+
)
72+
73+
expect(warn).not.toHaveBeenCalled()
74+
})
4175
})
4276

4377
function createPlugin(root: string): any {
78+
return createPlugins(root).plugin
79+
}
80+
81+
function createPlugins(
82+
root: string,
83+
opts: {
84+
command?: 'build' | 'serve'
85+
bundledDev?: boolean
86+
} = {},
87+
): { plugin: any; transformCachePlugin: any } {
4488
const plugins = importProtectionPlugin({
4589
framework: 'react',
4690
providerEnvName: 'ssr',
@@ -62,12 +106,16 @@ function createPlugin(root: string): any {
62106
const plugin = plugins.find(
63107
(item) => item.name === 'tanstack-start-core:import-protection',
64108
)!
109+
const transformCachePlugin = plugins.find(
110+
(item) =>
111+
item.name === 'tanstack-start-core:import-protection-transform-cache',
112+
)!
65113

66114
plugin.configResolved({
67115
root,
68-
command: 'build',
69-
experimental: { bundledDev: false },
116+
command: opts.command ?? 'build',
117+
experimental: { bundledDev: opts.bundledDev ?? false },
70118
})
71119

72-
return plugin
120+
return { plugin, transformCachePlugin }
73121
}

‎packages/start-plugin-core/tests/virtual-client-entry-plugin.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('createDevClientEntryPlugin', () => {
5050
)
5151

5252
expect(code).toContain(
53-
'import { injectIntoGlobalHook } from "/base/@react-refresh"',
53+
'import { injectIntoGlobalHook } from "/@react-refresh"',
5454
)
5555
expect(code).toContain('injectIntoGlobalHook(window)')
5656
expect(code).toContain(

0 commit comments

Comments
 (0)