diff --git a/CHANGELOG.md b/CHANGELOG.md index ab37558..62d78ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.14.1 + +### Patches + +- Fix `tryScanGlobalName` can't work with some libs. +- Fix `transformWithBabel` can't anonymous function. + ## 0.14.0 ### Features diff --git a/package.json b/package.json index ad43fb3..bd62b18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-cdn2", - "version": "0.14.0", + "version": "0.14.1", "description": "A Vite plugin that allowed you replace module with CDN", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/src/transform.ts b/src/transform.ts index 956501b..2cbd72c 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -244,7 +244,8 @@ function externalGlobals(options: TransformWithBabelOptions): PluginTarget { Declaration: (path) => { if (!isTopLevelCalled(path)) return if (t.isClassDeclaration(path.node) || t.isFunctionDeclaration(path.node)) { - const def = path.node.id.name + const def = path.node.id?.name + if (!def) return if (declarations.has(def)) { const p = declarations.get(def) p.remove() @@ -297,8 +298,8 @@ export async function tryScanGlobalName(code: string) { const identifier = node.declarations[0].id if (t.isIdentifier(identifier)) return identifier.name } - const bucket = new Set() let globalName = '' + let regName = '' // umd // @ts-ignore traverse(ast, { @@ -306,30 +307,19 @@ export async function tryScanGlobalName(code: string) { if (t.isCallExpression(path.node.expression)) { if (t.isFunctionExpression(path.node.expression.callee)) { const params = path.node.expression.callee.params - if (len(params)) { - params.forEach((i) => { - if (i.type === 'Identifier') { - bucket.add(i.name) - } - }) - } + if (len(params) && params[0].type === 'Identifier' && !regName) regName = params[0].name } } }, AssignmentExpression: (path) => { + if (globalName) return path.skip() const op = path.get('left') - if ( - op.node.type === 'MemberExpression' && - (path.parent.type === 'CallExpression' || path.parent.type === 'ConditionalExpression' || path.parent.type === 'ExpressionStatement') - ) { - if (!globalName) { - if (t.isIdentifier(op.node.object) && !bucket.has(op.node.object.name)) return - if (!t.isIdentifier(op.node.property)) return - if (op.node.property.name === 'exports') return - globalName = op.node.property.name + if (op.node.type === 'MemberExpression') { + const { start, end } = op.node.object + if (new RegExp(regName || 'global', 'i').test(code.slice(start, end)) && t.isIdentifier(op.node.property)) { + if (!globalName) globalName = op.node.property.name } } - path.skip() } }) return globalName