Skip to content

Commit 42233d3

Browse files
authored
test(optimizer): add test for cjs dep static import (vitejs#20011)
1 parent 3298e04 commit 42233d3

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ test('default import from webpacked cjs (clipboard)', async () => {
3030
await expectWithRetry(() => page.textContent('.cjs-clipboard')).toBe('ok')
3131
})
3232

33+
test('default import from cjs (cjs-dep-cjs-compiled-from-esm)', async () => {
34+
await expectWithRetry(() =>
35+
page.textContent('.cjs-dep-cjs-compiled-from-esm'),
36+
).toBe('ok')
37+
})
38+
39+
test('default import from cjs (cjs-dep-cjs-compiled-from-cjs)', async () => {
40+
await expectWithRetry(() =>
41+
page.textContent('.cjs-dep-cjs-compiled-from-cjs'),
42+
).toBe('ok')
43+
})
44+
3345
test('dynamic imports from cjs dep (react)', async () => {
3446
await expectWithRetry(() => page.textContent('.cjs-dynamic button')).toBe(
3547
'count is 0',

playground/optimize-deps/cjs-dynamic.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
}
1818

1919
const cjsFromESM = await import('@vitejs/test-dep-cjs-compiled-from-esm')
20-
console.log('cjsFromESM', cjsFromESM)
2120
if (typeof cjsFromESM.default === 'function') {
2221
text('.cjs-dynamic-dep-cjs-compiled-from-esm', 'ok')
2322
}
2423

2524
const cjsFromCJS = await import('@vitejs/test-dep-cjs-compiled-from-cjs')
26-
console.log('cjsFromCJS', cjsFromCJS)
2725
if (typeof cjsFromCJS.default === 'function') {
2826
text('.cjs-dynamic-dep-cjs-compiled-from-cjs', 'ok')
2927
}

playground/optimize-deps/cjs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import React, { useState } from 'react'
55
import ReactDOM from 'react-dom/client'
66
import { Socket } from 'phoenix'
77
import clip from 'clipboard'
8+
import cjsFromESM from '@vitejs/test-dep-cjs-compiled-from-esm'
9+
import cjsFromCJS from '@vitejs/test-dep-cjs-compiled-from-cjs'
810

911
// Test exporting a name that was already imported
1012
export { useState } from 'react'
@@ -19,6 +21,14 @@ if (typeof Socket === 'function') {
1921
text('.cjs-phoenix', 'ok')
2022
}
2123

24+
if (typeof cjsFromESM === 'function') {
25+
text('.cjs-dep-cjs-compiled-from-esm', 'ok')
26+
}
27+
28+
if (typeof cjsFromCJS === 'function') {
29+
text('.cjs-dep-cjs-compiled-from-cjs', 'ok')
30+
}
31+
2232
function App() {
2333
const [count, setCount] = useState(0)
2434

playground/optimize-deps/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ <h2>CommonJS w/ named imports (phoenix)</h2>
66
<div class="cjs-phoenix">fail</div>
77
<h2>CommonJS w/ default export (clipboard)</h2>
88
<div class="cjs-clipboard">fail</div>
9+
<h2>CommonJS import default (dep-cjs-compiled-from-esm)</h2>
10+
<div class="cjs-dep-cjs-compiled-from-esm"></div>
11+
<h2>CommonJS import default (dep-cjs-compiled-from-cjs)</h2>
12+
<div class="cjs-dep-cjs-compiled-from-cjs"></div>
913

1014
<script type="module" src="./cjs.js"></script>
1115

0 commit comments

Comments
 (0)