Skip to content

Commit 16db71e

Browse files
handle onViolation in prod, make it potentially async
1 parent 7c58156 commit 16db71e

4 files changed

Lines changed: 37 additions & 17 deletions

File tree

‎docs/start/framework/react/guide/import-protection.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ You can hook into violations for custom reporting or to override the verdict:
349349

350350
```ts
351351
importProtection: {
352-
onViolation: (info) => {
352+
onViolation: async (info) => {
353353
// info.env -- environment name (e.g. 'client', 'ssr', ...)
354354
// info.envType -- 'client' or 'server'
355355
// info.type -- 'specifier', 'file', or 'marker'
@@ -360,7 +360,7 @@ importProtection: {
360360
// info.snippet -- { lines, location } with the source code snippet (if available)
361361
// info.message -- the formatted diagnostic message
362362

363-
// Return false to allow this specific import (override the denial)
363+
// Return false (or Promise<false>) to allow this specific import (override the denial)
364364
if (info.specifier === 'some-special-case') {
365365
return false
366366
}
@@ -400,7 +400,9 @@ interface ImportProtectionOptions {
400400
specifiers?: Array<string | RegExp>
401401
files?: Array<string | RegExp>
402402
}
403-
onViolation?: (info: ViolationInfo) => boolean | void
403+
onViolation?: (
404+
info: ViolationInfo,
405+
) => boolean | void | Promise<boolean | void>
404406
}
405407
```
406408

‎packages/start-plugin-core/src/import-protection-plugin/INTERNALS.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ provide explicit ESM named exports. Only active in dev + mock mode.
5050

5151
A common pattern:
5252

53-
```
53+
```text
5454
db/index.ts → export { getUsers } from './db.server'
5555
export { userColumns } from './shared'
5656
@@ -86,7 +86,7 @@ analysing a module's exports. These are tracked via `serverFnLookupModules` and
8686

8787
### `shouldDefer` logic
8888

89-
```
89+
```ts
9090
shouldDefer = (isDevMock && !isPreTransformResolve) || isBuild
9191
```
9292

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ interface PluginConfig {
123123
markerSpecifiers: { serverOnly: Set<string>; clientOnly: Set<string> }
124124
envTypeMap: Map<string, 'client' | 'server'>
125125

126-
onViolation?: (info: ViolationInfo) => boolean | void
126+
onViolation?: (
127+
info: ViolationInfo,
128+
) => boolean | void | Promise<boolean | void>
127129
}
128130

129131
/**
@@ -787,7 +789,7 @@ export function importProtectionPlugin(
787789
}
788790

789791
if (config.onViolation) {
790-
const result = config.onViolation(pv.info)
792+
const result = await config.onViolation(pv.info)
791793
if (result === false) continue
792794
}
793795
warnFn(formatViolation(pv.info, config.root))
@@ -830,12 +832,17 @@ export function importProtectionPlugin(
830832
/** Counter for generating unique per-violation mock module IDs in build mode. */
831833
let buildViolationCounter = 0
832834

833-
function handleViolation(
835+
type HandleViolationResult =
836+
| { id: string; syntheticNamedExports: boolean }
837+
| string
838+
| undefined
839+
840+
async function handleViolation(
834841
ctx: ViolationReporter,
835842
env: EnvState,
836843
info: ViolationInfo,
837844
violationOpts?: { silent?: boolean },
838-
): { id: string; syntheticNamedExports: boolean } | string | undefined {
845+
): Promise<HandleViolationResult> {
839846
const key = dedupeKey(
840847
info.type,
841848
info.importer,
@@ -845,7 +852,7 @@ export function importProtectionPlugin(
845852

846853
if (!violationOpts?.silent) {
847854
if (config.onViolation) {
848-
const result = config.onViolation(info)
855+
const result = await config.onViolation(info)
849856
if (result === false) {
850857
return undefined
851858
}
@@ -924,9 +931,9 @@ export function importProtectionPlugin(
924931
info: ViolationInfo,
925932
shouldDefer: boolean,
926933
isPreTransformResolve: boolean,
927-
): Promise<ReturnType<typeof handleViolation>> {
934+
): Promise<HandleViolationResult> {
928935
if (shouldDefer) {
929-
const result = handleViolation(ctx, env, info, { silent: true })
936+
const result = await handleViolation(ctx, env, info, { silent: true })
930937

931938
if (config.command === 'build') {
932939
// Build mode: store for generateBundle tree-shaking check.
@@ -941,7 +948,7 @@ export function importProtectionPlugin(
941948

942949
return result
943950
}
944-
return handleViolation(ctx, env, info, {
951+
return await handleViolation(ctx, env, info, {
945952
silent: isPreTransformResolve,
946953
})
947954
}
@@ -1444,7 +1451,7 @@ export function importProtectionPlugin(
14441451
},
14451452
},
14461453

1447-
generateBundle(_options, bundle) {
1454+
async generateBundle(_options, bundle) {
14481455
const envName = this.environment.name
14491456
const env = envStates.get(envName)
14501457
if (!env || env.deferredBuildViolations.length === 0) return
@@ -1463,9 +1470,14 @@ export function importProtectionPlugin(
14631470
// in the bundle, the import was NOT tree-shaken — real leak.
14641471
const realViolations: Array<ViolationInfo> = []
14651472
for (const { info, mockModuleId } of env.deferredBuildViolations) {
1466-
if (survivingModules.has(mockModuleId)) {
1467-
realViolations.push(info)
1473+
if (!survivingModules.has(mockModuleId)) continue
1474+
1475+
if (config.onViolation) {
1476+
const result = await config.onViolation(info)
1477+
if (result === false) continue
14681478
}
1479+
1480+
realViolations.push(info)
14691481
}
14701482

14711483
if (realViolations.length === 0) return

‎packages/start-plugin-core/src/schema.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ const importProtectionOptionsSchema = z
4242
onViolation: z
4343
.function()
4444
.args(z.any())
45-
.returns(z.union([z.boolean(), z.void()]))
45+
.returns(
46+
z.union([
47+
z.boolean(),
48+
z.void(),
49+
z.promise(z.union([z.boolean(), z.void()])),
50+
]),
51+
)
4652
.optional(),
4753
include: z.array(patternSchema).optional(),
4854
exclude: z.array(patternSchema).optional(),

0 commit comments

Comments
 (0)