|
| 1 | +export function getAsyncRootAssetNames (assets: Record<string, any>, asyncRoot: string): string[] { |
| 2 | + return Object.keys(assets).filter(name => name.startsWith(`${asyncRoot}/`) && name.endsWith('.js')) |
| 3 | +} |
| 4 | + |
| 5 | +export function normalizePathForAsyncSubPackage (value: string): string { |
| 6 | + return value.replace(/\\/g, '/') |
| 7 | +} |
| 8 | + |
| 9 | +export function getSourceRootForResource (resource: string | undefined, sourceDir: string, asyncRootMap: Map<string, string>): string | null { |
| 10 | + if (!resource) return null |
| 11 | + const normalizedResource = normalizePathForAsyncSubPackage(resource) |
| 12 | + const normalizedSourceDir = normalizePathForAsyncSubPackage(sourceDir).replace(/\/$/, '') |
| 13 | + let relativeResource = normalizedResource |
| 14 | + if (normalizedResource.startsWith(`${normalizedSourceDir}/`)) { |
| 15 | + relativeResource = normalizedResource.slice(normalizedSourceDir.length + 1) |
| 16 | + } |
| 17 | + |
| 18 | + const sourceRoots = Array.from(asyncRootMap.keys()).sort((a, b) => b.length - a.length) |
| 19 | + for (const sourceRoot of sourceRoots) { |
| 20 | + const normalizedSourceRoot = normalizePathForAsyncSubPackage(sourceRoot) |
| 21 | + if (relativeResource === normalizedSourceRoot || relativeResource.startsWith(`${normalizedSourceRoot}/`) || relativeResource.startsWith(`${normalizedSourceRoot}.`)) { |
| 22 | + return sourceRoot |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return null |
| 27 | +} |
| 28 | + |
| 29 | +export function getAsyncRootForModule ( |
| 30 | + module: any, |
| 31 | + sourceDir: string, |
| 32 | + asyncRootMap: Map<string, string>, |
| 33 | + moduleGraph?: any, |
| 34 | + visited: Set<any> = new Set(), |
| 35 | + cache?: WeakMap<any, string | null | undefined> |
| 36 | +): string | null | undefined { |
| 37 | + if (!module || visited.has(module)) return undefined |
| 38 | + if (cache?.has(module)) return cache.get(module) |
| 39 | + visited.add(module) |
| 40 | + |
| 41 | + const resource = module.resource || module.nameForCondition?.() |
| 42 | + const normalizedSourceDir = normalizePathForAsyncSubPackage(sourceDir).replace(/\/$/, '') |
| 43 | + const normalizedResource = resource ? normalizePathForAsyncSubPackage(resource) : '' |
| 44 | + const isProjectSource = normalizedResource.startsWith(`${normalizedSourceDir}/`) |
| 45 | + const sourceRoot = getSourceRootForResource(resource, sourceDir, asyncRootMap) |
| 46 | + if (sourceRoot) { |
| 47 | + const asyncRoot = asyncRootMap.get(sourceRoot) |
| 48 | + cache?.set(module, asyncRoot) |
| 49 | + return asyncRoot |
| 50 | + } |
| 51 | + |
| 52 | + const asyncRoots = new Set<string>() |
| 53 | + let hasBlockingProjectIssuer = false |
| 54 | + const issuerModules = new Set<any>() |
| 55 | + const incomingConnections = moduleGraph?.getIncomingConnections?.(module) |
| 56 | + if (incomingConnections) { |
| 57 | + for (const connection of incomingConnections) { |
| 58 | + if (connection?.originModule) issuerModules.add(connection.originModule) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const issuer = moduleGraph?.getIssuer?.(module) |
| 63 | + if (issuer) issuerModules.add(issuer) |
| 64 | + |
| 65 | + for (const issuerModule of issuerModules) { |
| 66 | + const asyncRoot = getAsyncRootForModule(issuerModule, sourceDir, asyncRootMap, moduleGraph, new Set(visited), cache) |
| 67 | + if (asyncRoot === null) { |
| 68 | + hasBlockingProjectIssuer = true |
| 69 | + } else if (asyncRoot) { |
| 70 | + asyncRoots.add(asyncRoot) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + let result: string | null | undefined |
| 75 | + if (hasBlockingProjectIssuer || asyncRoots.size > 1) { |
| 76 | + result = null |
| 77 | + } else if (asyncRoots.size === 1) { |
| 78 | + result = Array.from(asyncRoots)[0] |
| 79 | + } else { |
| 80 | + result = isProjectSource ? null : undefined |
| 81 | + } |
| 82 | + |
| 83 | + cache?.set(module, result) |
| 84 | + return result |
| 85 | +} |
| 86 | + |
| 87 | +export function getSingleAsyncRootForModules ( |
| 88 | + modules: any[], |
| 89 | + sourceDir: string, |
| 90 | + asyncRootMap: Map<string, string>, |
| 91 | + moduleGraph?: any, |
| 92 | + cache?: WeakMap<any, string | null | undefined> |
| 93 | +): string | null { |
| 94 | + const asyncRoots = new Set<string>() |
| 95 | + |
| 96 | + for (const module of modules) { |
| 97 | + const asyncRoot = getAsyncRootForModule(module, sourceDir, asyncRootMap, moduleGraph, undefined, cache) |
| 98 | + if (asyncRoot === null) return null |
| 99 | + if (asyncRoot) { |
| 100 | + asyncRoots.add(asyncRoot) |
| 101 | + if (asyncRoots.size > 1) return null |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (asyncRoots.size !== 1) return null |
| 106 | + return Array.from(asyncRoots)[0] |
| 107 | +} |
| 108 | + |
| 109 | +export function createAsyncCommonChunkName (asyncRoot: string, chunkName: string | undefined): string { |
| 110 | + const safeChunkName = (chunkName || 'common').replace(/[^a-zA-Z0-9_/-]/g, '_').replace(/\//g, '_') |
| 111 | + return `${asyncRoot}/async-${safeChunkName}` |
| 112 | +} |
0 commit comments