-
Notifications
You must be signed in to change notification settings - Fork 19
/
unpack.ts
63 lines (53 loc) · 1.88 KB
/
unpack.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
import { nonNullable } from '@wakaru/shared/array'
import { jscodeshiftWithParser as j, printSourceWithErrorLoc } from '@wakaru/shared/jscodeshift'
import { getModulesFromBrowserify } from './extractors/browserify'
import { getModulesFromWebpack } from './extractors/webpack'
import { Module } from './Module'
import { postScanRuntime, scanExports, scanImports, scanRuntime } from './module-scan'
import type { ModuleMapping } from '@wakaru/ast-utils/types'
import type { Collection } from 'jscodeshift'
type ModuleWithRoot = Module & { root: Collection }
/**
* Unpacks the given source code from supported bundlers.
*/
export function unpack(sourceCode: string): {
modules: Module[]
moduleIdMapping: ModuleMapping
} {
const root = j(sourceCode)
const result = getModulesFromWebpack(j, root)
|| getModulesFromBrowserify(j, root)
// Fallback to a single module
|| {
modules: new Set([new Module(0, root.find(j.Program), true)]),
moduleIdMapping: {
0: 'entry.js',
},
}
const { modules, moduleIdMapping } = result
// module as key, root as value
const modulesArray = [...modules]
const modulesWithRoot = modulesArray.map<ModuleWithRoot | null>((module) => {
try {
const root = j(module.code)
return { ...module, root }
}
catch (err: any) {
console.error(`\nFailed to parse module ${module.id} with jscodeshift`, err)
printSourceWithErrorLoc(err, module.code)
return null
}
}).filter(nonNullable)
modulesWithRoot.forEach((module) => {
scanImports(j, module)
scanExports(j, module)
})
modulesWithRoot.forEach((module) => {
scanRuntime(j, module)
})
postScanRuntime(j, modulesWithRoot)
return {
modules: modulesArray,
moduleIdMapping,
}
}