Skip to content

Commit

Permalink
fix import
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Jun 11, 2024
1 parent 1af0182 commit f05296d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-lazy-import",
"version": "1.0.4",
"version": "1.0.5",
"description": "Used for vite Lazy import js and style",
"scripts": {
"update": "npm install --legacy-peer-deps",
Expand All @@ -14,9 +14,9 @@
"module": "./dist/index.mjs",
"typings": "types/index.d.ts",
"dependencies": {
"rollup": "^4.18.0",
"@rollup/pluginutils": "^5.1.0",
"es-module-lexer": "^1.5.3"
"rollup": "4.18.0",
"@rollup/pluginutils": "5.1.0",
"es-module-lexer": "1.5.3"
},
"devDependencies": {
"@babel/core": "^7.4.4",
Expand Down
20 changes: 16 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ export function lazyImport (options: LazyImportConfig): Plugin<any> {
function transformImport (id:string, exports: readonly ExportSpecifier[], resOpts: LazyImportResolver) {
const importCodes: string[] = []
exports.forEach(item => {
const importName = item.ln || item.n
let importName = item.n
let variableCode = item.n
if (item.ln !== item.n) {
variableCode = `${importName} as ${item.n}`
let asName = ''
if (item.ln && item.ln !== item.n) {
importName = item.ln
asName = item.n
variableCode = `${importName} as ${asName}`
}

let jsPath = ''
Expand Down Expand Up @@ -102,6 +105,14 @@ export function lazyImport (options: LazyImportConfig): Plugin<any> {
return importCodes.join(';\n')
}

function toVariableExport (impCode: string) {
const rest = impCode.replace(/\n/, ' ').match(/\{[a-zA-Z,_'"\s]+?\}/)
if (rest) {
return `export ${rest[0]}`
}
return ''
}

return {
name: 'vite:lazy-import',
enforce: 'post',
Expand All @@ -120,7 +131,8 @@ export function lazyImport (options: LazyImportConfig): Plugin<any> {
if (!syntaxItem) {
return
}
const exports = parse(restCode.slice(syntaxItem.ss, syntaxItem.se).replace('import', 'export'))[1]
const exportCode = toVariableExport(restCode.slice(syntaxItem.ss, syntaxItem.se))
const exports = parse(exportCode)[1]
if (!exports.length) {
return
}
Expand Down

0 comments on commit f05296d

Please sign in to comment.