-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathmoduleCompiler.ts
351 lines (326 loc) · 9.95 KB
/
moduleCompiler.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import type { File, Store } from '../store'
import {
MagicString,
babelParse,
extractIdentifiers,
isInDestructureAssignment,
isStaticProperty,
walk,
walkIdentifiers,
} from 'vue/compiler-sfc'
import type { ExportSpecifier, Identifier, Node } from '@babel/types'
export function compileModulesForPreview(store: Store, isSSR = false) {
const seen = new Set<File>()
const processed: string[] = []
processFile(store, store.files[store.mainFile], processed, seen, isSSR)
if (!isSSR) {
// also add css files that are not imported
for (const filename in store.files) {
if (filename.endsWith('.css')) {
const file = store.files[filename]
if (!seen.has(file)) {
processed.push(
`\nwindow.__css__.push(${JSON.stringify(file.compiled.css)})`,
)
}
}
}
}
return processed
}
const modulesKey = `__modules__`
const exportKey = `__export__`
const dynamicImportKey = `__dynamic_import__`
const moduleKey = `__module__`
// similar logic with Vite's SSR transform, except this is targeting the browser
function processFile(
store: Store,
file: File,
processed: string[],
seen: Set<File>,
isSSR: boolean,
) {
if (seen.has(file)) {
return []
}
seen.add(file)
if (!isSSR && file.filename.endsWith('.html')) {
return processHtmlFile(store, file.code, file.filename, processed, seen)
}
let {
code: js,
importedFiles,
hasDynamicImport,
} = processModule(
store,
isSSR ? file.compiled.ssr : file.compiled.js,
file.filename,
)
processChildFiles(
store,
importedFiles,
hasDynamicImport,
processed,
seen,
isSSR,
)
// append css
if (file.compiled.css && !isSSR) {
js += `\nwindow.__css__.push(${JSON.stringify(file.compiled.css)})`
}
// push self
processed.push(js)
}
function processChildFiles(
store: Store,
importedFiles: Set<string>,
hasDynamicImport: boolean,
processed: string[],
seen: Set<File>,
isSSR: boolean,
) {
if (hasDynamicImport) {
// process all files
for (const file of Object.values(store.files)) {
if (seen.has(file)) continue
processFile(store, file, processed, seen, isSSR)
}
} else if (importedFiles.size > 0) {
// crawl child imports
for (const imported of importedFiles) {
processFile(store, store.files[imported], processed, seen, isSSR)
}
}
}
function processModule(store: Store, src: string, filename: string) {
const s = new MagicString(src)
const ast = babelParse(src, {
sourceFilename: filename,
sourceType: 'module',
}).program.body
const idToImportMap = new Map<string, string>()
const declaredConst = new Set<string>()
const importedFiles = new Set<string>()
const importToIdMap = new Map<string, string>()
function resolveImport(raw: string): string | undefined {
const files = store.files
let resolved = raw
const file =
files[resolved] ||
files[(resolved = raw + '.ts')] ||
files[(resolved = raw + '.js')]
return file ? resolved : undefined
}
function defineImport(node: Node, source: string) {
const filename = resolveImport(source.replace(/^\.\/+/, 'src/'))
if (!filename) {
throw new Error(`File "${source}" does not exist.`)
}
if (importedFiles.has(filename)) {
return importToIdMap.get(filename)!
}
importedFiles.add(filename)
const id = `__import_${importedFiles.size}__`
importToIdMap.set(filename, id)
s.appendLeft(
node.start!,
`const ${id} = ${modulesKey}[${JSON.stringify(filename)}]\n`,
)
return id
}
function defineExport(name: string, local = name) {
s.append(`\n${exportKey}(${moduleKey}, "${name}", () => ${local})`)
}
// 0. instantiate module
s.prepend(
`const ${moduleKey} = ${modulesKey}[${JSON.stringify(
filename,
)}] = { [Symbol.toStringTag]: "Module" }\n\n`,
)
// 1. check all import statements and record id -> importName map
for (const node of ast) {
// import foo from 'foo' --> foo -> __import_foo__.default
// import { baz } from 'foo' --> baz -> __import_foo__.baz
// import * as ok from 'foo' --> ok -> __import_foo__
if (node.type === 'ImportDeclaration') {
const source = node.source.value
if (source.startsWith('./')) {
const importId = defineImport(node, node.source.value)
for (const spec of node.specifiers) {
if (spec.type === 'ImportSpecifier') {
idToImportMap.set(
spec.local.name,
`${importId}.${(spec.imported as Identifier).name}`,
)
} else if (spec.type === 'ImportDefaultSpecifier') {
idToImportMap.set(spec.local.name, `${importId}.default`)
} else {
// namespace specifier
idToImportMap.set(spec.local.name, importId)
}
}
s.remove(node.start!, node.end!)
}
}
}
// 2. check all export statements and define exports
for (const node of ast) {
// named exports
if (node.type === 'ExportNamedDeclaration') {
if (node.declaration) {
if (
node.declaration.type === 'FunctionDeclaration' ||
node.declaration.type === 'ClassDeclaration'
) {
// export function foo() {}
defineExport(node.declaration.id!.name)
} else if (node.declaration.type === 'VariableDeclaration') {
// export const foo = 1, bar = 2
for (const decl of node.declaration.declarations) {
for (const id of extractIdentifiers(decl.id)) {
defineExport(id.name)
}
}
}
s.remove(node.start!, node.declaration.start!)
} else if (node.source && node.source.value.startsWith('./')) {
// export { foo, bar } from './foo'
const importId = defineImport(node, node.source.value)
for (const spec of node.specifiers) {
defineExport(
(spec.exported as Identifier).name,
`${importId}.${(spec as ExportSpecifier).local.name}`,
)
}
s.remove(node.start!, node.end!)
} else {
// export { foo, bar }
for (const spec of node.specifiers) {
const local = (spec as ExportSpecifier).local.name
const binding = idToImportMap.get(local)
defineExport((spec.exported as Identifier).name, binding || local)
}
s.remove(node.start!, node.end!)
}
}
// default export
if (node.type === 'ExportDefaultDeclaration') {
if ('id' in node.declaration && node.declaration.id) {
// named hoistable/class exports
// export default function foo() {}
// export default class A {}
const { name } = node.declaration.id
s.remove(node.start!, node.start! + 15)
s.append(`\n${exportKey}(${moduleKey}, "default", () => ${name})`)
} else {
// anonymous default exports
s.overwrite(node.start!, node.start! + 14, `${moduleKey}.default =`)
}
}
// export * from './foo'
if (node.type === 'ExportAllDeclaration') {
const importId = defineImport(node, node.source.value)
s.remove(node.start!, node.end!)
s.append(`\nfor (const key in ${importId}) {
if (key !== 'default') {
${exportKey}(${moduleKey}, key, () => ${importId}[key])
}
}`)
}
}
// 3. convert references to import bindings
for (const node of ast) {
if (node.type === 'ImportDeclaration') continue
walkIdentifiers(node, (id, parent, parentStack) => {
const binding = idToImportMap.get(id.name)
if (!binding) {
return
}
if (parent && isStaticProperty(parent) && parent.shorthand) {
// let binding used in a property shorthand
// { foo } -> { foo: __import_x__.foo }
// skip for destructure patterns
if (
!(parent as any).inPattern ||
isInDestructureAssignment(parent, parentStack)
) {
s.appendLeft(id.end!, `: ${binding}`)
}
} else if (
parent &&
parent.type === 'ClassDeclaration' &&
id === parent.superClass
) {
if (!declaredConst.has(id.name)) {
declaredConst.add(id.name)
// locate the top-most node containing the class declaration
const topNode = parentStack[1]
s.prependRight(topNode.start!, `const ${id.name} = ${binding};\n`)
}
} else {
s.overwrite(id.start!, id.end!, binding)
}
})
}
// 4. convert dynamic imports
let hasDynamicImport = false
walk(ast, {
enter(node: Node, parent: Node) {
if (node.type === 'Import' && parent.type === 'CallExpression') {
const arg = parent.arguments[0]
if (arg.type === 'StringLiteral' && arg.value.startsWith('./')) {
hasDynamicImport = true
s.overwrite(node.start!, node.start! + 6, dynamicImportKey)
s.overwrite(
arg.start!,
arg.end!,
JSON.stringify(arg.value.replace(/^\.\/+/, 'src/')),
)
}
}
},
})
return {
code: s.toString(),
importedFiles,
hasDynamicImport,
}
}
const scriptRE = /<script\b(?:\s[^>]*>|>)([^]*?)<\/script>/gi
const scriptModuleRE =
/<script\b[^>]*type\s*=\s*(?:"module"|'module')[^>]*>([^]*?)<\/script>/gi
function processHtmlFile(
store: Store,
src: string,
filename: string,
processed: string[],
seen: Set<File>,
) {
const deps: string[] = []
let jsCode = ''
const html = src
.replace(scriptModuleRE, (_, content) => {
const { code, importedFiles, hasDynamicImport } = processModule(
store,
content,
filename,
)
processChildFiles(
store,
importedFiles,
hasDynamicImport,
deps,
seen,
false,
)
jsCode += '\n' + code
return ''
})
.replace(scriptRE, (_, content) => {
jsCode += '\n' + content
return ''
})
processed.push(`document.body.innerHTML = ${JSON.stringify(html)}`)
processed.push(...deps)
processed.push(jsCode)
}