Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/taro-runner-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./bundler/*.d.ts": "./dist/bundler/*.d.ts",
"./bundler/*.js": {
"types": "./dist/bundler/*.d.ts",
"default": "./dist/bundler/*.js"
},
"./bundler/*": {
"types": "./dist/bundler/*.d.ts",
"default": "./dist/bundler/*.js"
},
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"bundler/*.d.ts": [
"./dist/bundler/*.d.ts"
],
"bundler/*.js": [
"./dist/bundler/*.d.ts"
],
"bundler/*": [
"./dist/bundler/*.d.ts"
]
}
},
"files": [
"dist"
],
Expand Down Expand Up @@ -38,6 +67,7 @@
"@types/sax": "^1.2.4",
"autoprefixer": "^10.4.19",
"detect-port": "^1.5.1",
"html-minifier": "^4.0.0",
"jsdom": "^24.0.0",
"loader-utils": "^3.2.1",
"miniprogram-simulate": "^1.6.1",
Expand All @@ -50,12 +80,14 @@
"postcss-pxtransform": "workspace:*",
"postcss-url": "^10.1.3",
"sax": "1.2.4",
"tapable": "^2.2.1",
"vm2": "^3.9.19",
"webpack-format-messages": "^3.0.1"
},
"peerDependencies": {
"autoprefixer": "^10.4.19",
"detect-port": "^1.5.1",
"html-minifier": "^4.0.0",
"jsdom": "^24.0.0",
"loader-utils": "^3.2.1",
"miniprogram-simulate": "^1.6.1",
Expand All @@ -68,6 +100,7 @@
"postcss-pxtransform": "workspace:*",
"postcss-url": "^10.1.3",
"sax": "1.2.4",
"tapable": "^2.2.1",
"vm2": "^3.9.19",
"webpack-format-messages": "^3.0.1"
},
Expand All @@ -78,6 +111,9 @@
"detect-port": {
"optional": true
},
"html-minifier": {
"optional": true
},
"jsdom": {
"optional": true
},
Expand Down Expand Up @@ -114,6 +150,9 @@
"sax": {
"optional": true
},
"tapable": {
"optional": true
},
"vm2": {
"optional": true
},
Expand Down
112 changes: 112 additions & 0 deletions packages/taro-runner-utils/src/bundler/asyncSubPackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
export function getAsyncRootAssetNames (assets: Record<string, any>, asyncRoot: string): string[] {
return Object.keys(assets).filter(name => name.startsWith(`${asyncRoot}/`) && name.endsWith('.js'))
}

export function normalizePathForAsyncSubPackage (value: string): string {
return value.replace(/\\/g, '/')
}

export function getSourceRootForResource (resource: string | undefined, sourceDir: string, asyncRootMap: Map<string, string>): string | null {
if (!resource) return null
const normalizedResource = normalizePathForAsyncSubPackage(resource)
const normalizedSourceDir = normalizePathForAsyncSubPackage(sourceDir).replace(/\/$/, '')
let relativeResource = normalizedResource
if (normalizedResource.startsWith(`${normalizedSourceDir}/`)) {
relativeResource = normalizedResource.slice(normalizedSourceDir.length + 1)
}

const sourceRoots = Array.from(asyncRootMap.keys()).sort((a, b) => b.length - a.length)
for (const sourceRoot of sourceRoots) {
const normalizedSourceRoot = normalizePathForAsyncSubPackage(sourceRoot)
if (relativeResource === normalizedSourceRoot || relativeResource.startsWith(`${normalizedSourceRoot}/`) || relativeResource.startsWith(`${normalizedSourceRoot}.`)) {
return sourceRoot
}
}

return null
}

export function getAsyncRootForModule (
module: any,
sourceDir: string,
asyncRootMap: Map<string, string>,
moduleGraph?: any,
visited: Set<any> = new Set(),
cache?: WeakMap<any, string | null | undefined>
): string | null | undefined {
if (!module || visited.has(module)) return undefined
if (cache?.has(module)) return cache.get(module)
visited.add(module)

const resource = module.resource || module.nameForCondition?.()
const normalizedSourceDir = normalizePathForAsyncSubPackage(sourceDir).replace(/\/$/, '')
const normalizedResource = resource ? normalizePathForAsyncSubPackage(resource) : ''
const isProjectSource = normalizedResource.startsWith(`${normalizedSourceDir}/`)
const sourceRoot = getSourceRootForResource(resource, sourceDir, asyncRootMap)
if (sourceRoot) {
const asyncRoot = asyncRootMap.get(sourceRoot)
cache?.set(module, asyncRoot)
return asyncRoot
}

const asyncRoots = new Set<string>()
let hasBlockingProjectIssuer = false
const issuerModules = new Set<any>()
const incomingConnections = moduleGraph?.getIncomingConnections?.(module)
if (incomingConnections) {
for (const connection of incomingConnections) {
if (connection?.originModule) issuerModules.add(connection.originModule)
}
}

const issuer = moduleGraph?.getIssuer?.(module)
if (issuer) issuerModules.add(issuer)

for (const issuerModule of issuerModules) {
const asyncRoot = getAsyncRootForModule(issuerModule, sourceDir, asyncRootMap, moduleGraph, new Set(visited), cache)
if (asyncRoot === null) {
hasBlockingProjectIssuer = true
} else if (asyncRoot) {
asyncRoots.add(asyncRoot)
}
}

let result: string | null | undefined
if (hasBlockingProjectIssuer || asyncRoots.size > 1) {
result = null
} else if (asyncRoots.size === 1) {
result = Array.from(asyncRoots)[0]
} else {
result = isProjectSource ? null : undefined
}

cache?.set(module, result)
return result
}

export function getSingleAsyncRootForModules (
modules: any[],
sourceDir: string,
asyncRootMap: Map<string, string>,
moduleGraph?: any,
cache?: WeakMap<any, string | null | undefined>
): string | null {
const asyncRoots = new Set<string>()

for (const module of modules) {
const asyncRoot = getAsyncRootForModule(module, sourceDir, asyncRootMap, moduleGraph, undefined, cache)
if (asyncRoot === null) return null
if (asyncRoot) {
asyncRoots.add(asyncRoot)
if (asyncRoots.size > 1) return null
}
}

if (asyncRoots.size !== 1) return null
return Array.from(asyncRoots)[0]
}

export function createAsyncCommonChunkName (asyncRoot: string, chunkName: string | undefined): string {
const safeChunkName = (chunkName || 'common').replace(/[^a-zA-Z0-9_/-]/g, '_').replace(/\//g, '_')
return `${asyncRoot}/async-${safeChunkName}`
}
Loading
Loading