Skip to content

fix: process all files when dynamic import #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2023
Merged
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
74 changes: 56 additions & 18 deletions src/output/moduleCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,55 @@ function processFile(
return processHtmlFile(store, file.code, file.filename, processed, seen)
}

let [js, importedFiles] = processModule(
let {
code: js,
importedFiles,
hasDynamicImport
} = processModule(
store,
isSSR ? file.compiled.ssr : file.compiled.js,
file.filename
)
processChildFiles(
store,
importedFiles,
hasDynamicImport,
processed,
seen,
isSSR
)
// append css
if (!isSSR && file.compiled.css) {
js += `\nwindow.__css__ += ${JSON.stringify(file.compiled.css)}`
}
// crawl child imports
if (importedFiles.size) {

// push self
processed.push(js)
}

function processChildFiles(
store: Store,
importedFiles: Set<string>,
hasDynamicImport: boolean,
processed: string[],
seen: Set<File>,
isSSR: boolean
) {
if (hasDynamicImport) {
// process all files
for (const file of Object.values(store.state.files)) {
if (seen.has(file)) continue
processFile(store, file, processed, seen, isSSR)
}
} else if (importedFiles.size > 0) {
// crawl child imports
for (const imported of importedFiles) {
processFile(store, store.state.files[imported], processed, seen, isSSR)
}
}
// push self
processed.push(js)
}

function processModule(
store: Store,
src: string,
filename: string
): [string, Set<string>] {
function processModule(store: Store, src: string, filename: string) {
const s = new MagicString(src)

const ast = babelParse(src, {
Expand Down Expand Up @@ -255,11 +280,13 @@ function processModule(
}

// 4. convert dynamic imports
;(walk as any)(ast, {
let hasDynamicImport = false
walk(ast, {
enter(node: Node, parent: Node) {
if (node.type === 'Import' && parent.type === 'CallExpression') {
const arg = parent.arguments[0]
if (arg.type === 'StringLiteral' && arg.value.startsWith('./')) {
hasDynamicImport = true
s.overwrite(node.start!, node.start! + 6, dynamicImportKey)
s.overwrite(
arg.start!,
Expand All @@ -271,7 +298,11 @@ function processModule(
}
})

return [s.toString(), importedFiles]
return {
code: s.toString(),
importedFiles,
hasDynamicImport
}
}

const scriptRE = /<script\b(?:\s[^>]*>|>)([^]*?)<\/script>/gi
Expand All @@ -289,12 +320,19 @@ function processHtmlFile(
let jsCode = ''
const html = src
.replace(scriptModuleRE, (_, content) => {
const [code, importedFiles] = processModule(store, content, filename)
if (importedFiles.size) {
for (const imported of importedFiles) {
processFile(store, store.state.files[imported], deps, seen, false)
}
}
const { code, importedFiles, hasDynamicImport } = processModule(
store,
content,
filename
)
processChildFiles(
store,
importedFiles,
hasDynamicImport,
deps,
seen,
false
)
jsCode += '\n' + code
return ''
})
Expand Down