-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
89 lines (74 loc) · 2.5 KB
/
build.js
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
const { build } = require('esbuild')
const { Generator } = require('npm-dts')
const package = require('./package.json')
const packageExpose = require('./expose.json')
const fs = require('node:fs/promises')
const path = require('path')
const { resolve } = require('node:path')
const version = '1.2.3'
const entryFile = './src/index.ts'
const start = async () => {
const shared = {
sourcemap: false,
entryPoints: [entryFile],
bundle: true,
external: Object.keys(package.peerDependencies),
}
const banner = {
js: `/**
* vite-vue-route-generator
* @version ${version}
* @author weiquanju <anbine@qq.com>
*/`
}
// clear dist
await fs.rm('../vite-vue-route-generator-dist', { recursive: true, force: true })
await Promise.allSettled([
build({
...shared,
outfile: '../vite-vue-route-generator-dist/index.cjs',
format: 'cjs',
banner,
}),
build({
...shared,
outfile: '../vite-vue-route-generator-dist/index.js',
format: 'iife',
banner,
}),
build({
...shared,
outfile: '../vite-vue-route-generator-dist/index.mjs',
format: 'esm',
banner,
})
])
}
const after = async () => {
// copy package.json 到 src 下
await fs.copyFile('./package.json', './src/package.json')
await new Generator({
entry: resolve(__dirname, './src/index.ts'),
root: resolve(__dirname, './src'),
output: resolve(__dirname, '../vite-vue-route-generator-dist/index.d.ts')
}, true, true).generate().finally(() => {
setTimeout(() => {
fs.rm('./src/package.json', { force: true })
}, 10);
})
package.version = version
await fs.writeFile('../vite-vue-route-generator-dist/package.json', JSON.stringify({ ...package, ...packageExpose, devDependencies: { vite: "^2.9.0" }, scripts: undefined, packageManager: undefined }, null, ' '))
await fs.writeFile('./package.json', JSON.stringify(package, null, ' '))
const setVersion = (s) => s.replace(/npm-\d+\.\d+\.\d+/mg, 'npm-' + version)
await Promise.allSettled(['README.md', 'README-ZH.md'].map(async (f) => {
const readPath = path.resolve(__dirname, f)
const writePath = path.resolve(__dirname, '../vite-vue-route-generator-dist', f)
const data = await fs.readFile(readPath, { encoding: 'utf-8' })
await fs.writeFile(readPath, setVersion(data), { encoding: 'utf-8', flag: 'w' })
return await fs.writeFile(writePath, setVersion(data), { encoding: 'utf-8', flag: 'w' })
}))
}
; (async () => {
await start()
await after()
})();