-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel.config.umd.mjs
72 lines (60 loc) · 1.76 KB
/
babel.config.umd.mjs
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
import { fileURLToPath } from 'url'
import { basename, dirname, join, normalize, extname } from 'path'
import fs from 'fs'
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename)
const dir = join(__dirname, './es')
/** @type {Map<string, string[]>} */
const moduleIds = new Map
function getModuleId(/** @type {string} */name) {
const file = basename(name, '.js')
const dir = basename(dirname(name))
if (dir == 'bop') {
if (file == 'index') return ['sugar', 'bop']
return ['sugar', 'bop', file]
}
if (dir == 'math') {
if (file == 'index') return ['sugar', 'math']
return ['sugar', 'math', file]
}
if (file == 'index') return ['sugar']
return ['sugar', file]
}
function getDoduleIds(/** @type {string} */dir) {
const files = fs.readdirSync(dir)
for (const file of files) {
const path = normalize(join(dir, file))
if (extname(path) == '') {
getDoduleIds(path)
continue
}
const id = getModuleId(path)
moduleIds.set(path, id)
}
}
getDoduleIds(dir)
// console.log(moduleIds)
// console.log()
const globals = Object.fromEntries([...moduleIds.entries()].flatMap(([k, id]) => {
return [
[id.join('/'), id.join('.')],
[`.${k.substring(dir.length).replaceAll('\\', '/')}`, id.join('.')]
]
}))
// console.log(globals)
export default {
moduleIds: true,
getModuleId(/** @type {string} */name) {
const r = getModuleId(name).join('/');
return r
},
plugins: [
['@babel/plugin-transform-modules-umd', {
globals: globals,
exactGlobals: true,
}],
['babel-plugin-add-import-extension', {
extension: 'js'
}],
],
}