Skip to content

Commit b110d41

Browse files
Amour1688Single-Dancer
authored andcommitted
refactor(runner): extract shared bundler layer into @tarojs/runner-utils
1 parent 033c823 commit b110d41

62 files changed

Lines changed: 6722 additions & 6071 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/taro-runner-utils/package.json‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@
66
"license": "MIT",
77
"main": "dist/index.js",
88
"types": "dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"types": "./dist/index.d.ts",
12+
"default": "./dist/index.js"
13+
},
14+
"./bundler/*.d.ts": "./dist/bundler/*.d.ts",
15+
"./bundler/*.js": {
16+
"types": "./dist/bundler/*.d.ts",
17+
"default": "./dist/bundler/*.js"
18+
},
19+
"./bundler/*": {
20+
"types": "./dist/bundler/*.d.ts",
21+
"default": "./dist/bundler/*.js"
22+
},
23+
"./package.json": "./package.json"
24+
},
25+
"typesVersions": {
26+
"*": {
27+
"bundler/*.d.ts": [
28+
"./dist/bundler/*.d.ts"
29+
],
30+
"bundler/*.js": [
31+
"./dist/bundler/*.d.ts"
32+
],
33+
"bundler/*": [
34+
"./dist/bundler/*.d.ts"
35+
]
36+
}
37+
},
938
"files": [
1039
"dist"
1140
],
@@ -38,6 +67,7 @@
3867
"@types/sax": "^1.2.4",
3968
"autoprefixer": "^10.4.19",
4069
"detect-port": "^1.5.1",
70+
"html-minifier": "^4.0.0",
4171
"jsdom": "^24.0.0",
4272
"loader-utils": "^3.2.1",
4373
"miniprogram-simulate": "^1.6.1",
@@ -50,12 +80,14 @@
5080
"postcss-pxtransform": "workspace:*",
5181
"postcss-url": "^10.1.3",
5282
"sax": "1.2.4",
83+
"tapable": "^2.2.1",
5384
"vm2": "^3.9.19",
5485
"webpack-format-messages": "^3.0.1"
5586
},
5687
"peerDependencies": {
5788
"autoprefixer": "^10.4.19",
5889
"detect-port": "^1.5.1",
90+
"html-minifier": "^4.0.0",
5991
"jsdom": "^24.0.0",
6092
"loader-utils": "^3.2.1",
6193
"miniprogram-simulate": "^1.6.1",
@@ -68,6 +100,7 @@
68100
"postcss-pxtransform": "workspace:*",
69101
"postcss-url": "^10.1.3",
70102
"sax": "1.2.4",
103+
"tapable": "^2.2.1",
71104
"vm2": "^3.9.19",
72105
"webpack-format-messages": "^3.0.1"
73106
},
@@ -78,6 +111,9 @@
78111
"detect-port": {
79112
"optional": true
80113
},
114+
"html-minifier": {
115+
"optional": true
116+
},
81117
"jsdom": {
82118
"optional": true
83119
},
@@ -114,6 +150,9 @@
114150
"sax": {
115151
"optional": true
116152
},
153+
"tapable": {
154+
"optional": true
155+
},
117156
"vm2": {
118157
"optional": true
119158
},
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)