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: cjs interop export names local clash, fix #8950 #8953

Merged
merged 2 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/vite/src/node/__tests__/plugins/import.spec.ts
Expand Up @@ -70,9 +70,9 @@ describe('transformCjsImport', () => {
)
).toBe(
'import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; ' +
'const useState = __vite__cjsImport0_react["useState"]; ' +
'const Component = __vite__cjsImport0_react["Component"]; ' +
'export { useState, Component }'
'const __vite__cjsExport_useState = __vite__cjsImport0_react["useState"]; ' +
'const __vite__cjsExport_Component = __vite__cjsImport0_react["Component"]; ' +
'export { __vite__cjsExport_useState as useState, __vite__cjsExport_Component as Component }'
)

expect(
Expand All @@ -84,9 +84,9 @@ describe('transformCjsImport', () => {
)
).toBe(
'import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; ' +
'const useStateAlias = __vite__cjsImport0_react["useState"]; ' +
'const ComponentAlias = __vite__cjsImport0_react["Component"]; ' +
'export { useStateAlias, ComponentAlias }'
'const __vite__cjsExport_useStateAlias = __vite__cjsImport0_react["useState"]; ' +
'const __vite__cjsExport_ComponentAlias = __vite__cjsImport0_react["Component"]; ' +
'export { __vite__cjsExport_useStateAlias as useStateAlias, __vite__cjsExport_ComponentAlias as ComponentAlias }'
)
})

Expand All @@ -108,8 +108,8 @@ describe('transformCjsImport', () => {
)
).toBe(
'import __vite__cjsImport0_react from "./node_modules/.vite/deps/react.js"; ' +
'const React = __vite__cjsImport0_react.__esModule ? __vite__cjsImport0_react.default : __vite__cjsImport0_react; ' +
'export { React }'
'const __vite__cjsExport_React = __vite__cjsImport0_react.__esModule ? __vite__cjsImport0_react.default : __vite__cjsImport0_react; ' +
'export { __vite__cjsExport_React as React }'
)

expect(
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -824,6 +824,7 @@ export function transformCjsImport(
spec.exported.type === 'Identifier'
) {
// for ExportSpecifier, local name is same as imported name
// prefix the variable name to avoid clashing with other local variables
const importedName = spec.local.name
// we want to specify exported name as variable and re-export it
const exportedName = spec.exported.name
Expand All @@ -833,8 +834,11 @@ export function transformCjsImport(
)
importNames.push({ importedName, localName: defaultExports })
} else {
importNames.push({ importedName, localName: exportedName })
exportNames.push(exportedName)
const localName = makeLegalIdentifier(
`__vite__cjsExport_${exportedName}`
)
importNames.push({ importedName, localName })
exportNames.push(`${localName} as ${exportedName}`)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions playground/alias/index.html
Expand Up @@ -45,6 +45,8 @@ <h1>Alias</h1>

// aliased to an absolute URL in CJS, should be optimized
import { isFunction } from '@vue/shared'
// also check name clash for aliased deps
export { isFunction } from '@vue/shared'
console.log(isFunction(() => {}))
</script>

Expand Down
5 changes: 5 additions & 0 deletions playground/optimize-deps/cjs.js
Expand Up @@ -6,6 +6,11 @@ import ReactDOM from 'react-dom/client'
import { Socket } from 'phoenix'
import clip from 'clipboard'

// Test exporting a name that was already imported
export { useState } from 'react'
export { useState as anotherNameForUseState } from 'react'
export { default as React } from 'react'

if (typeof clip === 'function') {
text('.cjs-clipboard', 'ok')
}
Expand Down