-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
index.html
142 lines (132 loc) · 3.85 KB
/
index.html
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<h1>Glob import</h1>
<h2>Normal</h2>
<pre class="result"></pre>
<h2>Eager</h2>
<pre class="result-eager"></pre>
<h2>node_modules</h2>
<pre class="result-node_modules"></pre>
<h2>Raw</h2>
<pre class="globraw"></pre>
<h2>Property access</h2>
<pre class="property-access"></pre>
<h2>Relative raw</h2>
<pre class="relative-glob-raw"></pre>
<h2>Side effect</h2>
<pre class="side-effect-result"></pre>
<h2>Tree shake Eager CSS</h2>
<p class="tree-shake-eager-css">Should be orange</p>
<p class="no-tree-shake-eager-css">Should be orange</p>
<pre class="no-tree-shake-eager-css-result"></pre>
<h2>Escape relative glob</h2>
<pre class="escape-relative"></pre>
<h2>Escape alias glob</h2>
<pre class="escape-alias"></pre>
<script type="module" src="./dir/index.js"></script>
<script type="module">
function useImports(modules, selector) {
for (const path in modules) {
modules[path]().then((mod) => {
console.log(path, mod)
})
}
const keys = Object.keys(modules)
Promise.all(keys.map((key) => modules[key]())).then((mods) => {
const res = {}
mods.forEach((m, i) => {
res[keys[i]] = m
})
document.querySelector(selector).textContent = JSON.stringify(
res,
null,
2
)
})
}
const modules = import.meta.glob(
'/dir/**'
// for test: annotation contain ")"
/*
* for test: annotation contain ")"
* */
)
useImports(modules, '.result')
const eagerModules = import.meta.glob('/dir/**', { eager: true })
document.querySelector('.result-eager').textContent = JSON.stringify(
eagerModules,
null,
2
)
const nodeModules = import.meta.glob('/dir/node_modules/**')
useImports(nodeModules, '.result-node_modules')
</script>
<script type="module">
const rawModules = import.meta.glob('/dir/*.json', {
as: 'raw',
eager: true
})
const globraw = {}
Object.keys(rawModules).forEach((key) => {
globraw[key] = JSON.parse(rawModules[key])
})
document.querySelector('.globraw').textContent = JSON.stringify(
globraw,
null,
2
)
</script>
<script type="module">
const bazJson = import.meta.glob('/dir/*.json', {
as: 'raw',
eager: true
})['/dir/baz.json']
document.querySelector('.property-access').textContent = JSON.stringify(
JSON.parse(bazJson),
null,
2
)
</script>
<script type="module">
const relativeRawModules = import.meta.glob('../glob-import/dir/*.json', {
as: 'raw',
eager: true
})
const relativeGlobRaw = {}
Object.keys(relativeRawModules).forEach((key) => {
relativeGlobRaw[key] = JSON.parse(relativeRawModules[key])
})
document.querySelector('.relative-glob-raw').textContent = JSON.stringify(
relativeGlobRaw,
null,
2
)
</script>
<script type="module">
const neverInvoke = () =>
(document.querySelector('.side-effect-result').textContent =
"Syntax generated from 'import.meta.glob' must do ASI.")
const notInvocation = neverInvoke
import.meta.glob('/side-effect/**', { eager: true })
</script>
<script type="module">
import.meta.glob('/tree-shake.css', { eager: true })
const results = import.meta.glob('/no-tree-shake.css', { eager: true })
document.querySelector('.no-tree-shake-eager-css-result').textContent =
results['/no-tree-shake.css'].default
</script>
<script type="module">
const globs = import.meta.glob('/escape/**/glob.js', {
eager: true
})
console.log(globs)
globalThis.globs = globs
const relative = Object.entries(globs)
.filter(([_, mod]) => Object.keys(mod?.relative ?? {}).length === 1)
.map(([glob]) => glob)
document.querySelector('.escape-relative').textContent = relative
.sort()
.join('\n')
const alias = Object.entries(globs)
.filter(([_, mod]) => Object.keys(mod?.alias ?? {}).length === 1)
.map(([glob]) => glob)
document.querySelector('.escape-alias').textContent = alias.sort().join('\n')
</script>