|
1 | 1 | import { resolve as resolvePath } from 'node:path' |
2 | 2 |
|
| 3 | +import { extname } from 'node:path' |
| 4 | + |
3 | 5 | import { normalizePath } from 'vite' |
4 | 6 |
|
5 | 7 | import { |
@@ -449,6 +451,33 @@ function getModuleFile(module: RspackModule): string { |
449 | 451 | return normalizeFilePath(getModuleResource(module) ?? module.identifier()) |
450 | 452 | } |
451 | 453 |
|
| 454 | +const IMPORT_PROTECTION_PARSEABLE_EXTENSIONS = new Set([ |
| 455 | + '.ts', |
| 456 | + '.tsx', |
| 457 | + '.mts', |
| 458 | + '.cts', |
| 459 | + '.js', |
| 460 | + '.jsx', |
| 461 | + '.mjs', |
| 462 | + '.cjs', |
| 463 | +]) |
| 464 | + |
| 465 | +function isImportProtectionSourceFile(file: string | undefined): boolean { |
| 466 | + if (!file) { |
| 467 | + return false |
| 468 | + } |
| 469 | + |
| 470 | + const extension = extname(normalizeFilePath(file)) |
| 471 | + return ( |
| 472 | + extension.length > 0 && |
| 473 | + IMPORT_PROTECTION_PARSEABLE_EXTENSIONS.has(extension) |
| 474 | + ) |
| 475 | +} |
| 476 | + |
| 477 | +function isImportProtectionSourceModule(module: RspackModule): boolean { |
| 478 | + return isImportProtectionSourceFile(getModuleResource(module)) |
| 479 | +} |
| 480 | + |
452 | 481 | function addTransformResult( |
453 | 482 | cache: Map<string, TransformResult>, |
454 | 483 | key: string, |
@@ -790,6 +819,10 @@ async function getMarkerKindForFile(opts: { |
790 | 819 | markerKindCache: Map<string, Promise<'server' | 'client' | undefined>> |
791 | 820 | file: string |
792 | 821 | }): Promise<'server' | 'client' | undefined> { |
| 822 | + if (!isImportProtectionSourceFile(opts.file)) { |
| 823 | + return undefined |
| 824 | + } |
| 825 | + |
793 | 826 | let cached = opts.markerKindCache.get(opts.file) |
794 | 827 | if (!cached) { |
795 | 828 | cached = (async () => { |
@@ -1359,7 +1392,7 @@ export function registerImportProtection( |
1359 | 1392 | : () => Promise.resolve(undefined), |
1360 | 1393 | ) |
1361 | 1394 | const allModules = Array.from(context.compilation.modules) |
1362 | | - const relevantModules = allModules |
| 1395 | + const relevantModules = allModules.filter(isImportProtectionSourceModule) |
1363 | 1396 |
|
1364 | 1397 | const provider = await buildTransformResultProvider({ |
1365 | 1398 | modules: relevantModules, |
|
0 commit comments