forked from vitejs/vite
/
cjs-dynamic.js
53 lines (45 loc) · 1.36 KB
/
cjs-dynamic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// test dynamic import to cjs deps
// mostly ensuring consistency between dev server behavior and build behavior
// of @rollup/plugin-commonjs
;(async () => {
const { useState } = await import('react')
const React = (await import('react')).default
const ReactDOM = await import('react-dom')
const clip = await import('clipboard')
if (typeof clip.default === 'function') {
text('.cjs-dynamic-clipboard', 'ok')
}
const { Socket } = await import('phoenix')
if (typeof Socket === 'function') {
text('.cjs-dynamic-phoenix', 'ok')
}
const cjsFromESM = await import('dep-cjs-compiled-from-esm')
console.log('cjsFromESM', cjsFromESM)
if (typeof cjsFromESM.default === 'function') {
text('.cjs-dynamic-dep-cjs-compiled-from-esm', 'ok')
}
const cjsFromCJS = await import('dep-cjs-compiled-from-cjs')
console.log('cjsFromCJS', cjsFromCJS)
if (typeof cjsFromCJS.default === 'function') {
text('.cjs-dynamic-dep-cjs-compiled-from-cjs', 'ok')
}
function App() {
const [count, setCount] = useState(0)
return React.createElement(
'button',
{
onClick() {
setCount(count + 1)
}
},
`count is ${count}`
)
}
ReactDOM.render(
React.createElement(App),
document.querySelector('.cjs-dynamic')
)
function text(el, text) {
document.querySelector(el).textContent = text
}
})()