Skip to content
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

fix: fixed dynamic import lose vue style after build cjs (fix #4277) #4954

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
51 changes: 47 additions & 4 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,46 @@ function preload(baseModule: () => Promise<{}>, deps?: string[]) {
})
).then(() => baseModule())
}

function ParseCjsDynamicImports(code: string) {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
/**
* cjs
* https://rollupjs.org/guide/en/#outputinlinedynamicimports
* import('a.vue') => Promise.resolve().then(function(){ return require('a.vue') })
*/
const Imports: ImportSpecifier[] = []
let start = 0
const CodeArray = code.split(preloadMethod)
for (let i = 1; i < CodeArray.length; i++) {
const CurrentCode = CodeArray[i]
start += CodeArray[i - 1].length + preloadMethod.length
const CodeEndIndex = CurrentCode.indexOf(preloadMarker)
if (CodeEndIndex > -1) {
const DynamicImportMatch = CurrentCode.slice(0, CodeEndIndex).match(
/require\(["|'](.+)["|']\)/
)
if (DynamicImportMatch) {
// Dynamic imports are indicated by imports[2].d > -1
// In this case the "d" index is the start of the dynamic import
const startIndex =
start +
DynamicImportMatch.index! +
DynamicImportMatch[0].indexOf(DynamicImportMatch[1]) -
1
const endIndex = startIndex + DynamicImportMatch[1].length + 2
Imports.push({
n: DynamicImportMatch[1],
s: startIndex,
e: endIndex,
ss: startIndex,
se: endIndex,
d: startIndex,
a: -1
})
}
}
}
return Imports
}
/**
* Build only. During serve this is performed as part of ./importAnalysis.
*/
Expand Down Expand Up @@ -209,7 +248,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
// make sure we only perform the preload logic in modern builds.
if (code.indexOf(isModernFlag) > -1) {
const re = new RegExp(isModernFlag, 'g')
const isModern = String(format === 'es')
const isModern = String(['es', 'cjs'].includes(format))
if (config.build.sourcemap) {
const s = new MagicString(code)
let match: RegExpExecArray | null
Expand All @@ -232,7 +271,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
},

generateBundle({ format }, bundle) {
if (format !== 'es' || ssr) {
if (!['es', 'cjs'].includes(format) || ssr) {
return
}

Expand All @@ -244,7 +283,11 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
const code = chunk.code
let imports: ImportSpecifier[]
try {
imports = parseImports(code)[0].filter((i) => i.d > -1)
if (format === 'cjs') {
imports = ParseCjsDynamicImports(code)
} else {
imports = parseImports(code)[0].filter((i) => i.d > -1)
}
} catch (e: any) {
this.error(e, e.idx)
}
Expand Down