-
Notifications
You must be signed in to change notification settings - Fork 4
/
tsup.config.ts
93 lines (84 loc) · 3.06 KB
/
tsup.config.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
import { execa } from 'execa'
import fs from 'fs-extra'
import { createHash } from 'node:crypto'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineConfig, type Options } from 'tsup'
import { loadEnv } from 'vite'
import logger from '~/utils/logger'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function buildExternals(option: Options): Options[] {
function pathToHash(path: string) {
return createHash('md5').update(path).digest('hex').slice(0, 8)
}
function copyJsonFile(root: string, source: string, target: string, fileName: string) {
const sourcePath = path.resolve(root, source, fileName)
const targetPath = path.resolve(root, target, fileName)
fs.ensureDirSync(path.dirname(targetPath))
fs.copyFileSync(sourcePath, targetPath)
}
return [
{
entry: ['lib/**/*.ts'],
outDir: 'dist/lib',
format: ['cjs'],
target: 'node16',
minify: !option.watch,
esbuildPlugins: [
{
name: 'resolve-relative-packagejson',
setup(build) {
build.onLoad({ filter: /\.js$/ }, async (args) => {
const SEARCH_FILES = ['prebuild-install/bin', 'napi-build-utils/index']
if (SEARCH_FILES.some((file) => args.path.includes(file))) {
const SEARCH_RESOLVE_JSON =
/(^[\w\W]*path\.resolve\s*\(\s*['"`]\s*)(package\.json)(\s*['"`]\s*\)[\w\W]*$)/
const SEARCH_EXISTS_JSJON =
/(^[\w\W]*fs\.existsSync\s*\(\s*['"`]\s*)(package\.json)(\s*['"`]\s*\)[\w\W]*$)/
const matchTarget = [SEARCH_RESOLVE_JSON, SEARCH_EXISTS_JSJON]
const sharp = path.resolve(__dirname, 'node_modules/@minko-fe/sharp/package.json')
const jsonFolder = path.resolve(__dirname, 'dist/lib/json')
let code = fs.readFileSync(args.path, 'utf-8')
for (const match of matchTarget) {
const res = code.match(match)
if (!res) {
continue
}
const fileName = path.basename(sharp)
const folderName = pathToHash(sharp)
copyJsonFile(__dirname, path.dirname(sharp), path.join(jsonFolder, folderName), fileName)
code = `${res[1]}json/${folderName}/${fileName}${res[3]}`
}
return {
contents: code,
}
}
})
},
},
],
},
]
}
export default defineConfig((option) => [
{
entry: ['src/extension.ts'],
format: 'cjs',
external: ['vscode', '@minko-fe/sharp'],
noExternal: ['@minko-fe/sharp/package.json'],
clean: !option.watch,
dts: false,
target: 'node16',
minify: !option.watch,
env: {
NODE_ENV: option.watch ? 'development' : 'production',
...loadEnv('', __dirname, 'IM_'),
},
onSuccess() {
execa('npm', ['run', 'build:i18n'])
logger.success('i18n build success')
return Promise.resolve()
},
},
...buildExternals(option),
])