-
Notifications
You must be signed in to change notification settings - Fork 282
/
build.mjs
131 lines (109 loc) · 3.31 KB
/
build.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
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
/* eslint-disable no-console */
import { exec } from 'node:child_process'
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import * as path from 'node:path'
import { promisify } from 'node:util'
import * as babel from '@babel/core'
import * as fsWalk from '@nodelib/fs.walk'
const pkg = JSON.parse(fs.readFileSync('package.json').toString())
/**
* @param {'esm'|'cjs'} module
*/
function options(module) {
const plugins = [
[
'@babel/plugin-transform-modules-commonjs',
{
importInterop: 'node',
},
],
['@upleveled/remove-node-prefix'],
[
'replace-import-extension',
{
extMapping: {
'.ts': extMap[module],
'.js': extMap[module],
},
},
],
[
'babel-plugin-transform-replace-expressions',
{
replace: {
'process.env.MINIO_JS_PACKAGE_VERSION': JSON.stringify(pkg.version),
},
},
],
]
return {
sourceMaps: 'inline',
assumptions: {
constantSuper: true,
noIncompleteNsImportDetection: true,
constantReexports: true,
},
plugins: module === 'esm' ? plugins.splice(1) : plugins,
presets: [['@babel/env', { targets: { node: '14' }, modules: false }], ['@babel/preset-typescript']],
}
}
const extMap = { cjs: '.js', esm: '.mjs' }
async function buildFiles({ files, module, outDir }) {
console.log(`building for ${module}`)
await promisify(exec)(`npx tsc --outDir ${outDir}`, { stdio: 'inherit' })
const ext = extMap[module]
const opt = options(module)
for (const file of files) {
if (!file.dirent.isFile()) {
continue
}
const outFilePath = path.join(outDir, path.relative('src/', file.path))
const outDirPath = path.dirname(outFilePath)
await fsp.mkdir(outDirPath, { recursive: true })
const distCodePath = outFilePath.replace(/\.[tj]s$/g, ext)
if (file.path.endsWith('.d.ts')) {
await fsp.copyFile(file.path, outFilePath)
continue
}
try {
const result = await babel.transformAsync(await fsp.readFile(file.path, 'utf-8'), {
filename: file.path,
...opt,
})
await fsp.writeFile(distCodePath, result.code)
} catch (e) {
console.error(`failed to transpile ${file.path}`)
throw e
}
}
for (const file of fsWalk.walkSync(outDir)) {
if (file.dirent.isDirectory()) {
continue
}
if (!file.path.endsWith('.d.ts')) {
continue
}
const fileContent = fs.readFileSync(file.path).toString()
const mts = babel.transformSync(fileContent, {
filename: file.path,
sourceMaps: true,
plugins: [['@babel/plugin-syntax-typescript'], ['replace-import-extension', { extMapping: { '.ts': ext } }]],
})
await fsp.unlink(file.path)
let outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.ts'
if (module === 'esm') {
outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.mts'
}
await fsp.writeFile(outFilePath, mts.code)
}
}
async function main() {
await fsp.rm('dist', { recursive: true, force: true })
const entries = fsWalk.walkSync('src/')
await Promise.all([
buildFiles({ files: entries, module: 'cjs', outDir: './dist/main/' }),
buildFiles({ files: entries, module: 'esm', outDir: './dist/esm/' }),
])
}
await main()