Skip to content

Commit 7049ae0

Browse files
authored
fix: process all files when dynamic import (#60)
1 parent 698b878 commit 7049ae0

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

Diff for: src/output/moduleCompiler.ts

+56-18
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,55 @@ function processFile(
6060
return processHtmlFile(store, file.code, file.filename, processed, seen)
6161
}
6262

63-
let [js, importedFiles] = processModule(
63+
let {
64+
code: js,
65+
importedFiles,
66+
hasDynamicImport
67+
} = processModule(
6468
store,
6569
isSSR ? file.compiled.ssr : file.compiled.js,
6670
file.filename
6771
)
72+
processChildFiles(
73+
store,
74+
importedFiles,
75+
hasDynamicImport,
76+
processed,
77+
seen,
78+
isSSR
79+
)
6880
// append css
6981
if (!isSSR && file.compiled.css) {
7082
js += `\nwindow.__css__ += ${JSON.stringify(file.compiled.css)}`
7183
}
72-
// crawl child imports
73-
if (importedFiles.size) {
84+
85+
// push self
86+
processed.push(js)
87+
}
88+
89+
function processChildFiles(
90+
store: Store,
91+
importedFiles: Set<string>,
92+
hasDynamicImport: boolean,
93+
processed: string[],
94+
seen: Set<File>,
95+
isSSR: boolean
96+
) {
97+
if (hasDynamicImport) {
98+
// process all files
99+
for (const file of Object.values(store.state.files)) {
100+
if (seen.has(file)) continue
101+
processFile(store, file, processed, seen, isSSR)
102+
}
103+
} else if (importedFiles.size > 0) {
104+
// crawl child imports
74105
for (const imported of importedFiles) {
75106
processFile(store, store.state.files[imported], processed, seen, isSSR)
76107
}
77108
}
78-
// push self
79-
processed.push(js)
80109
}
81110

82-
function processModule(
83-
store: Store,
84-
src: string,
85-
filename: string
86-
): [string, Set<string>] {
111+
function processModule(store: Store, src: string, filename: string) {
87112
const s = new MagicString(src)
88113

89114
const ast = babelParse(src, {
@@ -255,11 +280,13 @@ function processModule(
255280
}
256281

257282
// 4. convert dynamic imports
258-
;(walk as any)(ast, {
283+
let hasDynamicImport = false
284+
walk(ast, {
259285
enter(node: Node, parent: Node) {
260286
if (node.type === 'Import' && parent.type === 'CallExpression') {
261287
const arg = parent.arguments[0]
262288
if (arg.type === 'StringLiteral' && arg.value.startsWith('./')) {
289+
hasDynamicImport = true
263290
s.overwrite(node.start!, node.start! + 6, dynamicImportKey)
264291
s.overwrite(
265292
arg.start!,
@@ -271,7 +298,11 @@ function processModule(
271298
}
272299
})
273300

274-
return [s.toString(), importedFiles]
301+
return {
302+
code: s.toString(),
303+
importedFiles,
304+
hasDynamicImport
305+
}
275306
}
276307

277308
const scriptRE = /<script\b(?:\s[^>]*>|>)([^]*?)<\/script>/gi
@@ -289,12 +320,19 @@ function processHtmlFile(
289320
let jsCode = ''
290321
const html = src
291322
.replace(scriptModuleRE, (_, content) => {
292-
const [code, importedFiles] = processModule(store, content, filename)
293-
if (importedFiles.size) {
294-
for (const imported of importedFiles) {
295-
processFile(store, store.state.files[imported], deps, seen, false)
296-
}
297-
}
323+
const { code, importedFiles, hasDynamicImport } = processModule(
324+
store,
325+
content,
326+
filename
327+
)
328+
processChildFiles(
329+
store,
330+
importedFiles,
331+
hasDynamicImport,
332+
deps,
333+
seen,
334+
false
335+
)
298336
jsCode += '\n' + code
299337
return ''
300338
})

0 commit comments

Comments
 (0)