Skip to content

Commit e415b12

Browse files
committed
fix(vite-runner): 修复解析页面模板超时
1 parent 4d05bb9 commit e415b12

File tree

10 files changed

+133
-86
lines changed

10 files changed

+133
-86
lines changed

packages/taro-vite-runner/src/common/vite-plugin-assets.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function (viteCompilerContext: ViteH5CompilerContext | ViteMiniCo
4242
return cachedValue
4343
}
4444

45-
const source = await fs.readFile(id)
45+
const source = fs.readFileSync(id)
4646

4747
const {
4848
imageUrlLoaderOption = {},
@@ -78,7 +78,7 @@ export default function (viteCompilerContext: ViteH5CompilerContext | ViteMiniCo
7878
const referenceId = this.emitFile({
7979
type: 'asset',
8080
fileName,
81-
source
81+
source: Uint8Array.from(source)
8282
})
8383
url = `__VITE_ASSET__${referenceId}__`
8484
}

packages/taro-vite-runner/src/h5/entry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
6969
this.emitFile({
7070
type: 'asset',
7171
fileName,
72-
source: await fs.readFile(filePath)
72+
source: Uint8Array.from(fs.readFileSync(filePath))
7373
})
7474
this.addWatchFile(filePath)
7575
}

packages/taro-vite-runner/src/harmony/entry.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
7676

7777
// native components
7878
for (const comp of viteCompilerContext.nativeComponents.values()) {
79-
if (comp.isPackage) continue
80-
this.emitFile({
81-
type: 'chunk',
82-
id: comp.templatePath + QUERY_IS_NATIVE_SCRIPT,
83-
fileName: path.relative(viteCompilerContext.sourceDir, comp.templatePath) + QUERY_IS_NATIVE_SCRIPT,
84-
implicitlyLoadedAfterOneOf: [rawId]
85-
})
79+
viteCompilerContext.generateNativeComponent(this, comp, [rawId])
8680
}
8781

8882
// emit tabbar
@@ -97,7 +91,7 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
9791
this.emitFile({
9892
type: 'asset',
9993
fileName: removePathPrefix(iconPath),
100-
source: await fs.readFile(filePath)
94+
source: Uint8Array.from(fs.readFileSync(filePath))
10195
})
10296
if (!isFinished) {
10397
this.addWatchFile(filePath)
@@ -109,7 +103,7 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
109103
this.emitFile({
110104
type: 'asset',
111105
fileName: removePathPrefix(selectedIconPath),
112-
source: await fs.readFile(filePath)
106+
source: Uint8Array.from(fs.readFileSync(filePath))
113107
})
114108
if (!isFinished) {
115109
this.addWatchFile(filePath)
@@ -124,7 +118,7 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
124118
this.emitFile({
125119
type: 'asset',
126120
fileName: appConfig.themeLocation,
127-
source: fs.readFileSync(themePath)
121+
source: Uint8Array.from(fs.readFileSync(themePath))
128122
})
129123
if (!isFinished) {
130124
this.addWatchFile(themePath)

packages/taro-vite-runner/src/harmony/page.ts

+45-22
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
6363
}
6464
return null
6565
},
66-
async load (id) {
66+
load (id) {
6767
if (!viteCompilerContext) return
6868
const { taroConfig, cwd: appPath, app, loaderMeta } = viteCompilerContext
6969
const appConfig = app.config
@@ -100,9 +100,28 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
100100
code: parse.parse(tabbarId, tabbarPages as TaroHarmonyPageMeta[], name, this.resolve),
101101
exports: ['default'],
102102
})
103-
await Promise.all(tabbarPages.map(async page => {
104-
await viteCompilerContext.collectedDeps(this, escapePath(page.scriptPath), filter)
105-
}))
103+
tabbarPages.forEach(async page => {
104+
const deps = await viteCompilerContext.collectedDeps(this, escapePath(page.scriptPath), filter)
105+
const ncObj: Record<string, [string, string]> = {}
106+
deps.forEach(dep => {
107+
Object.entries(nCompCache.get(dep) || {}).forEach(([key, value]) => {
108+
const absPath = value[0]
109+
const ext = path.extname(absPath)
110+
const basename = path.basename(absPath, ext)
111+
ncObj[key] = [path.join(path.dirname(path.relative(path.dirname(rawId), absPath)), basename), value[1]]
112+
})
113+
})
114+
if (!page.isNative) {
115+
page.config.usingComponents = {
116+
...page.config.usingComponents,
117+
...ncObj,
118+
}
119+
}
120+
const nativeComps = viteCompilerContext.collectNativeComponents(page)
121+
nativeComps.forEach(comp => {
122+
viteCompilerContext.generateNativeComponent(this, comp, [rawId])
123+
})
124+
})
106125
}
107126
} else {
108127
const list: string[] = []
@@ -118,7 +137,7 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
118137
list.push(page.name)
119138
}
120139

121-
await Promise.all(list.map(async pageName => {
140+
list.forEach(pageName => {
122141
pageName = removeHeadSlash(pageName)
123142
if (!pageName) {
124143
pageName = 'index'
@@ -135,29 +154,33 @@ export default function (viteCompilerContext: ViteHarmonyCompilerContext): Plugi
135154
code: parse.parse(path.resolve(appRoot, pageName), page_, name, this.resolve),
136155
exports: ['default'],
137156
})
138-
const deps: Set<string> = await viteCompilerContext.collectedDeps(this, escapePath(rawId), filter)
139-
const ncObj: Record<string, [string, string]> = {}
140-
deps.forEach(dep => {
141-
Object.entries(nCompCache.get(dep) || {}).forEach(([key, value]) => {
142-
const absPath = value[0]
143-
const ext = path.extname(absPath)
144-
const basename = path.basename(absPath, ext)
145-
ncObj[key] = [path.join(path.dirname(path.relative(path.dirname(rawId), absPath)), basename), value[1]]
157+
viteCompilerContext.collectedDeps(this, escapePath(rawId), filter).then(deps => {
158+
const ncObj: Record<string, [string, string]> = {}
159+
deps.forEach(dep => {
160+
Object.entries(nCompCache.get(dep) || {}).forEach(([key, value]) => {
161+
const absPath = value[0]
162+
const ext = path.extname(absPath)
163+
const basename = path.basename(absPath, ext)
164+
ncObj[key] = [path.join(path.dirname(path.relative(path.dirname(rawId), absPath)), basename), value[1]]
165+
})
146166
})
147-
})
148-
if (!page.isNative) {
149-
page.config.usingComponents = {
150-
...page.config.usingComponents,
151-
...ncObj,
167+
if (!page.isNative) {
168+
page.config.usingComponents = {
169+
...page.config.usingComponents,
170+
...ncObj,
171+
}
152172
}
153-
}
154-
viteCompilerContext.collectNativeComponents(page)
155-
}))
173+
const nativeComps = viteCompilerContext.collectNativeComponents(page)
174+
nativeComps.forEach(comp => {
175+
viteCompilerContext.generateNativeComponent(this, comp, [rawId])
176+
})
177+
})
178+
})
156179
}
157180
return parse.parseEntry(rawId, page as TaroHarmonyPageMeta)
158181
}
159182
},
160-
async transform(code, id) {
183+
transform(code, id) {
161184
if (/\.m?[jt]sx?$/.test(id) && filter(id)) {
162185
const scopeNativeComp = new Map<string, [string, string]>()
163186
let enableImportComponent = true

packages/taro-vite-runner/src/mini/entry.ts

+7-24
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { isString } from '@tarojs/shared'
55

66
import { appendVirtualModulePrefix, escapePath, prettyPrintJson, stripVirtualModulePrefix } from '../utils'
77
import { baseCompName, customWrapperName } from '../utils/constants'
8-
import { miniTemplateLoader, QUERY_IS_NATIVE_COMP, QUERY_IS_NATIVE_PAGE } from './native-support'
9-
import { PAGE_SUFFIX } from './page'
8+
import { miniTemplateLoader, QUERY_IS_NATIVE_PAGE } from './native-support'
109

1110
import type { ViteMiniCompilerContext } from '@tarojs/taro/types/compile/viteCompilerContext'
1211
import type { PluginOption } from 'vite'
@@ -57,7 +56,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
5756
}
5857

5958
// pages
60-
await Promise.all(viteCompilerContext.pages.map(async page => {
59+
viteCompilerContext.pages.forEach(async page => {
6160
// 小程序原生页面
6261
if (page.isNative) {
6362
if (page.templatePath) {
@@ -76,27 +75,11 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
7675
fileName: viteCompilerContext.getScriptPath(page.name),
7776
implicitlyLoadedAfterOneOf: [rawId]
7877
})
79-
await this.load({
80-
id: appendVirtualModulePrefix(page.scriptPath + PAGE_SUFFIX),
81-
resolveDependencies: true
82-
})
83-
}))
78+
})
8479

8580
// native components
8681
for (const comp of viteCompilerContext.nativeComponents.values()) {
87-
this.emitFile({
88-
type: 'chunk',
89-
id: comp.scriptPath + QUERY_IS_NATIVE_COMP,
90-
fileName: viteCompilerContext.getScriptPath(comp.name),
91-
implicitlyLoadedAfterOneOf: [rawId]
92-
})
93-
const source = miniTemplateLoader(this, comp.templatePath, viteCompilerContext.sourceDir)
94-
this.emitFile({
95-
type: 'asset',
96-
fileName: viteCompilerContext.getTemplatePath(comp.name),
97-
source
98-
})
99-
comp.cssPath && this.addWatchFile(comp.cssPath)
82+
viteCompilerContext.generateNativeComponent(this, comp, [rawId])
10083
}
10184

10285
// comp' script
@@ -128,7 +111,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
128111
this.emitFile({
129112
type: 'asset',
130113
fileName: removePathPrefix(iconPath),
131-
source: await fs.readFile(filePath)
114+
source: Uint8Array.from(fs.readFileSync(filePath))
132115
})
133116
this.addWatchFile(filePath)
134117
}
@@ -138,7 +121,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
138121
this.emitFile({
139122
type: 'asset',
140123
fileName: removePathPrefix(selectedIconPath),
141-
source: await fs.readFile(filePath)
124+
source: Uint8Array.from(fs.readFileSync(filePath))
142125
})
143126
this.addWatchFile(filePath)
144127
}
@@ -151,7 +134,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
151134
this.emitFile({
152135
type: 'asset',
153136
fileName: appConfig.themeLocation,
154-
source: fs.readFileSync(themePath)
137+
source: Uint8Array.from(fs.readFileSync(themePath))
155138
})
156139
this.addWatchFile(themePath)
157140
}

packages/taro-vite-runner/src/mini/native-support.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext | undefine
7171
} else if (IS_NATIVE_STYLE_REG.test(id)) {
7272
let source = id.replace(new RegExp(`\\${QUERY_IS_NATIVE_STYLE}`), '')
7373
source = viteCompilerContext.getTargetFilePath(source, viteCompilerContext.fileType.style)
74-
const code = await fs.readFile(source, 'utf-8')
74+
const code = fs.readFileSync(source, 'utf-8')
7575
return {
7676
code
7777
}
@@ -157,7 +157,7 @@ export function miniTemplateLoader (ctx: PluginContext, templatePath: string, so
157157
ctx.emitFile({
158158
type: 'asset',
159159
fileName: requests[i].replace(sourceDir, '').replace(/^\//, ''),
160-
source: await fs.readFile(requests[i])
160+
source: Uint8Array.from(fs.readFileSync(requests[i]))
161161
})
162162
ctx.addWatchFile(requests[i])
163163
}

packages/taro-vite-runner/src/mini/page.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
4747
}
4848
return null
4949
},
50-
async load (id) {
50+
load (id) {
5151
if (viteCompilerContext && id.endsWith(PAGE_SUFFIX)) {
5252
const rawId = stripVirtualModulePrefix(id).replace(PAGE_SUFFIX, '')
5353
const page = viteCompilerContext.getPageById(rawId)
@@ -65,23 +65,27 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
6565
instantiatePage = viteCompilerContext.loaderMeta.modifyInstantiate(instantiatePage, 'page')
6666
}
6767

68-
const deps: Set<string> = await viteCompilerContext.collectedDeps(this, escapePath(rawId), filter)
69-
const ncObj: Record<string, string> = {}
70-
deps.forEach(dep => {
71-
Object.entries(nCompCache.get(dep) || {}).forEach(([key, value]) => {
72-
const absPath = value
73-
const ext = path.extname(absPath)
74-
const basename = path.basename(absPath, ext)
75-
ncObj[key] = path.join(path.dirname(path.relative(path.dirname(rawId), absPath)), basename)
68+
viteCompilerContext.collectedDeps(this, escapePath(rawId), filter).then(deps => {
69+
const ncObj: Record<string, string> = {}
70+
deps.forEach(dep => {
71+
Object.entries(nCompCache.get(dep) || {}).forEach(([key, value]) => {
72+
const absPath = value
73+
const ext = path.extname(absPath)
74+
const basename = path.basename(absPath, ext)
75+
ncObj[key] = path.join(path.dirname(path.relative(path.dirname(rawId), absPath)), basename)
76+
})
7677
})
77-
})
78-
if (!page.isNative) {
79-
page.config.usingComponents = {
80-
...page.config.usingComponents,
81-
...ncObj,
78+
if (!page.isNative) {
79+
page.config.usingComponents = {
80+
...page.config.usingComponents,
81+
...ncObj,
82+
}
8283
}
83-
}
84-
viteCompilerContext.collectNativeComponents(page)
84+
const nativeComps = viteCompilerContext.collectNativeComponents(page)
85+
nativeComps.forEach(comp => {
86+
viteCompilerContext.generateNativeComponent(this, comp, [rawId])
87+
})
88+
})
8589

8690
return [
8791
'import { createPageConfig } from "@tarojs/runtime"',
@@ -93,7 +97,7 @@ export default function (viteCompilerContext: ViteMiniCompilerContext): PluginOp
9397
].join('\n')
9498
}
9599
},
96-
async transform(code, id) {
100+
transform(code, id) {
97101
if (/\.m?[jt]sx?$/.test(id) && filter(id)) {
98102
const scopeNativeComp = new Map<string, string>()
99103
let enableImportComponent = true

packages/taro-vite-runner/src/utils/compiler/harmony.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { isArray, isFunction } from '@tarojs/shared'
1414
import JSON5 from 'json5'
1515

1616
import defaultConfig from '../../defaultConfig/defaultConfig.harmony'
17+
import { QUERY_IS_NATIVE_SCRIPT } from '../../harmony/ets'
1718
import { TARO_TABBAR_PAGE_PATH } from '../../harmony/page'
1819
import { componentConfig } from '../../utils/component'
1920
import { parseRelativePath } from '..'
@@ -28,6 +29,7 @@ import type {
2829
ViteNativeCompMeta,
2930
VitePageMeta,
3031
} from '@tarojs/taro/types/compile/viteCompilerContext'
32+
import type { PluginContext } from 'rollup'
3133

3234
export function readJsonSync(file: string) {
3335
const ext = path.extname(file)
@@ -128,11 +130,12 @@ export class TaroCompilerContext extends CompilerContext<ViteHarmonyBuildConfig>
128130
return importPath
129131
}
130132

131-
collectNativeComponents(meta: ViteAppMeta | VitePageMeta | ViteNativeCompMeta) {
133+
collectNativeComponents(meta: ViteAppMeta | VitePageMeta | ViteNativeCompMeta): ViteNativeCompMeta[] {
132134
const { name, scriptPath, config } = meta
133135
const { usingComponents } = config
134136

135-
if (!usingComponents) return
137+
const list: ViteNativeCompMeta[] = []
138+
if (!usingComponents) return list
136139

137140
Object.entries(usingComponents).forEach(([compName, value]) => {
138141
const compPath = value instanceof Array ? value[0] : value
@@ -181,8 +184,21 @@ export class TaroCompilerContext extends CompilerContext<ViteHarmonyBuildConfig>
181184
componentConfig.thirdPartyComponents.set(compName, new Set())
182185
}
183186

184-
this.collectNativeComponents(nativeCompMeta)
187+
list.push(...this.collectNativeComponents(nativeCompMeta), nativeCompMeta)
185188
})
189+
return list
190+
}
191+
192+
generateNativeComponent (rollupCtx: PluginContext, meta: ViteNativeCompMeta, implicitlyLoadedAfterOneOf: string[] = []) {
193+
if (meta.isGenerated || meta.isPackage) return
194+
195+
rollupCtx.emitFile({
196+
type: 'chunk',
197+
id: meta.templatePath + QUERY_IS_NATIVE_SCRIPT,
198+
fileName: path.relative(this.sourceDir, meta.templatePath) + QUERY_IS_NATIVE_SCRIPT,
199+
implicitlyLoadedAfterOneOf
200+
})
201+
meta.isGenerated = true
186202
}
187203

188204
modifyHarmonyResources(id = '', data: any = {}) {

0 commit comments

Comments
 (0)