Skip to content

Commit 4b6ebc3

Browse files
committed
fix(optimizer): fix cjs export interop for webpacked output
fix #1830
1 parent 29bec9c commit 4b6ebc3

File tree

7 files changed

+57
-6
lines changed

7 files changed

+57
-6
lines changed

packages/playground/optimize-deps/__tests__/optimize-deps.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@ test('default + named imports from cjs dep (react)', async () => {
66
expect(await page.textContent('.cjs button')).toBe('count is 1')
77
})
88

9+
test('named imports from webpacked cjs (phoenix)', async () => {
10+
expect(await page.textContent('.cjs-phoenix')).toBe('ok')
11+
})
12+
13+
test('default import from webpacked cjs (clipboard)', async () => {
14+
expect(await page.textContent('.cjs-clipboard')).toBe('ok')
15+
})
16+
917
test('dynamic imports from cjs dep (react)', async () => {
1018
expect(await page.textContent('.cjs-dynamic button')).toBe('count is 0')
1119
await page.click('.cjs-dynamic button')
1220
expect(await page.textContent('.cjs-dynamic button')).toBe('count is 1')
1321
})
1422

23+
test('dynamic named imports from webpacked cjs (phoenix)', async () => {
24+
expect(await page.textContent('.cjs-dynamic-phoenix')).toBe('ok')
25+
})
26+
27+
test('dynamic default import from webpacked cjs (clipboard)', async () => {
28+
expect(await page.textContent('.cjs-dynamic-clipboard')).toBe('ok')
29+
})
30+
1531
test('dedupe', async () => {
1632
expect(await page.textContent('.dedupe button')).toBe('count is 0')
1733
await page.click('.dedupe button')

packages/playground/optimize-deps/cjs-dynamic.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
const React = (await import('react')).default
77
const ReactDOM = await import('react-dom')
88

9+
const clip = await import('clipboard')
10+
if (typeof clip.default === 'function') {
11+
document.querySelector('.cjs-dynamic-clipboard').textContent = 'ok'
12+
}
13+
14+
const { Socket } = await import('phoenix')
15+
if (typeof Socket === 'function') {
16+
document.querySelector('.cjs-dynamic-phoenix').textContent = 'ok'
17+
}
18+
919
function App() {
1020
const [count, setCount] = useState(0)
1121

packages/playground/optimize-deps/cjs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
// are not statically detectable by @rollup/plugin-commonjs.
44
import React, { useState } from 'react'
55
import ReactDOM from 'react-dom'
6+
import { Socket } from 'phoenix'
7+
import clip from 'clipboard'
8+
9+
if (typeof clip === 'function') {
10+
document.querySelector('.cjs-clipboard').textContent = 'ok'
11+
}
12+
13+
if (typeof Socket === 'function') {
14+
document.querySelector('.cjs-phoenix').textContent = 'ok'
15+
}
616

717
function App() {
818
const [count, setCount] = useState(0)

packages/playground/optimize-deps/index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@ <h1>Optimize Deps</h1>
22

33
<h2>CommonJS w/ named imports (react)</h2>
44
<div class="cjs"></div>
5+
<h2>CommonJS w/ named imports (phoenix)</h2>
6+
<div class="cjs-phoenix">fail</div>
7+
<h2>CommonJS w/ default export (clipboard)</h2>
8+
<div class="cjs-clipboard">fail</div>
9+
510
<script type="module" src="./cjs.js"></script>
611

7-
<h2>CommonJS dynamic import (react)</h2>
12+
<h2>CommonJS dynamic import default + named (react)</h2>
813
<div class="cjs-dynamic"></div>
14+
<h2>CommonJS dynamic import named (phoenix)</h2>
15+
<div class="cjs-dynamic-phoenix"></div>
16+
<h2>CommonJS dynamic import default (clipboard)</h2>
17+
<div class="cjs-dynamic-clipboard"></div>
18+
919
<script type="module" src="./cjs-dynamic.js"></script>
1020

1121
<h2>Dedupe (dep in linked & optimized package)</h2>

packages/playground/optimize-deps/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
},
1111
"dependencies": {
1212
"axios": "^0.21.1",
13+
"clipboard": "^2.0.6",
14+
"dep-cjs-named-only": "link:./dep-cjs-named-only",
1315
"dep-linked": "link:./dep-linked",
1416
"dep-linked-include": "link:./dep-linked-include",
15-
"dep-cjs-named-only": "link:./dep-cjs-named-only",
17+
"phoenix": "^1.5.7",
1618
"react": "^17.0.1",
1719
"react-dom": "^17.0.1",
1820
"resolve-linked": "0.0.0",

packages/vite/src/node/optimizer/esbuildDepPlugin.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ export function esbuildDepPlugin(
130130
const [imports, exports] = exportsData[id]
131131
if (!imports.length && !exports.length) {
132132
// cjs
133-
contents +=
134-
`import d from "${relativePath}";export default d;` +
135-
`\nexport * from "${relativePath}"`
133+
contents += `export default require("${relativePath}");`
136134
} else {
137135
if (exports.includes('default')) {
138136
contents += `import d from "${relativePath}";export default d;`

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ cli-truncate@^2.1.0:
20542054
slice-ansi "^3.0.0"
20552055
string-width "^4.2.0"
20562056

2057-
clipboard@^2.0.0:
2057+
clipboard@^2.0.0, clipboard@^2.0.6:
20582058
version "2.0.6"
20592059
resolved "https://registry.yarnpkg.com/clipboard/-/clipboard-2.0.6.tgz#52921296eec0fdf77ead1749421b21c968647376"
20602060
integrity sha512-g5zbiixBRk/wyKakSwCKd7vQXDjFnAMGHoEyBogG/bw9kTD9GvdAvaoRR1ALcEzt3pVKxZR0pViekPMIS0QyGg==
@@ -5962,6 +5962,11 @@ periscopic@^2.0.3:
59625962
estree-walker "^2.0.2"
59635963
is-reference "^1.1.4"
59645964

5965+
phoenix@^1.5.7:
5966+
version "1.5.7"
5967+
resolved "https://registry.yarnpkg.com/phoenix/-/phoenix-1.5.7.tgz#86775bc51271e49930fd7d879ec3ec2addd6bf08"
5968+
integrity sha512-RgVdTRsK5NpnUPkjPyLg9P8qQQvuDaUsazH06t+ARu9EnPryQ7asE76VDjVZ43fqjY/p8er6y6OQb17YViG47g==
5969+
59655970
picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1, picomatch@^2.2.2:
59665971
version "2.2.2"
59675972
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"

0 commit comments

Comments
 (0)