/
index.js
75 lines (64 loc) · 2.3 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import mxdStatic from '../mxd'
import mxdStaticJSON from '../mxd.json'
async function setView(view) {
const { msg } = await import(`../views/${view}.js`)
text('.view', msg)
}
;['foo', 'bar'].forEach((id) => {
document.querySelector(`.${id}`).addEventListener('click', () => setView(id))
})
// literal dynamic
document.querySelector('.baz').addEventListener('click', async () => {
const { msg } = await import('../views/baz.js')
text('.view', msg)
})
// full dynamic
const arr = ['qux.js']
const view = `/${arr[0]}`
document.querySelector('.qux').addEventListener('click', async () => {
const { msg } = await import(/*@vite-ignore*/ view)
text('.view', msg)
})
// mixed static and dynamic
document.querySelector('.mxd').addEventListener('click', async () => {
const view = 'mxd'
const { default: mxdDynamic } = await import(`../${view}.js`)
text('.view', mxdStatic === mxdDynamic)
})
document.querySelector('.mxd2').addEventListener('click', async () => {
const test = { jss: '../mxd.js' }
const ttest = test
const view = 'mxd'
const { default: mxdDynamic } = await import(/*@vite-ignore*/ test.jss)
text('.view', mxdStatic === mxdDynamic)
})
document.querySelector('.mxdjson').addEventListener('click', async () => {
const view = 'mxd'
const { default: mxdDynamicJSON } = await import(`../${view}.json`)
text('.view', mxdStaticJSON === mxdDynamicJSON)
})
// data URLs (`blob:`)
const code1 = 'export const msg = "blob"'
const blob = new Blob([code1], { type: 'text/javascript;charset=UTF-8' })
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const blobURL = URL.createObjectURL(blob)
document.querySelector('.issue-2658-1').addEventListener('click', async () => {
const { msg } = await import(/*@vite-ignore*/ blobURL)
text('.view', msg)
})
// data URLs (`data:`)
const code2 = 'export const msg = "data";'
const dataURL = `data:text/javascript;charset=utf-8,${encodeURIComponent(
code2
)}`
document.querySelector('.issue-2658-2').addEventListener('click', async () => {
const { msg } = await import(/*@vite-ignore*/ dataURL)
text('.view', msg)
})
document.querySelector('.css').addEventListener('click', async () => {
await import('../css/index.css')
text('.view', 'dynamic import css')
})
function text(el, text) {
document.querySelector(el).textContent = text
}