Skip to content

Commit

Permalink
chore: release v0.14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Nov 7, 2023
1 parent f26268b commit 8272728
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
28 changes: 9 additions & 19 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -297,39 +298,28 @@ export async function tryScanGlobalName(code: string) {
const identifier = node.declarations[0].id
if (t.isIdentifier(identifier)) return identifier.name
}
const bucket = new Set<string>()
let globalName = ''
let regName = ''
// umd
// @ts-ignore
traverse(ast, {
ExpressionStatement: (path) => {
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
Expand Down

0 comments on commit 8272728

Please sign in to comment.