-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
68 lines (60 loc) · 1.58 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
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { dependencies } from './package.json'
import { builtinModules } from "module"
let node_libs = builtinModules.flatMap(m=>[m, `node:${m}`])
let node_libs_map = Object.fromEntries(node_libs.map(m => [m, m.replaceAll(/[\@\/\:]/g, '_')]))
let pkg_deps = Object.keys(dependencies)
let pkg_dep_map = Object.fromEntries(pkg_deps.map(m => [m, m.replaceAll(/[\@\/\:]/g, '_')]))
export default defineConfig({
plugins: [
vue(),
vueJsx(),
shebang('./dist/index.js'),
del('./dist/index.umd.cjs')
],
build: {
minify: false,
lib: {
entry: './index.jsx',
fileName: 'index',
name: 'default',
},
rollupOptions: {
external: [ ...pkg_deps, ...node_libs ],
output: {
globals: { ...pkg_dep_map, ...node_libs_map }
}
}
}
})
import fs from 'fs'
function shebang (pathToBang) {
return {
name: 'remove-main-script',
enforce: 'post',
apply: 'build',
closeBundle: async () => {
if (!fs.existsSync(pathToBang)) return
let stringToPrepend = `#! /usr/bin/env node\n\n`
try {
const data = fs.readFileSync(pathToBang, 'utf8')
fs.writeFileSync(pathToBang, stringToPrepend + data)
} catch (err) {
console.error(err);
}
}
}
}
function del(pathToRm) {
return {
name: 'del',
enforce: 'post',
apply: 'build',
closeBundle: async () => {
if (!fs.existsSync(pathToRm)) return
fs.rmSync(pathToRm, { recursive: true } )
}
}
}