-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
147 lines (125 loc) · 3.09 KB
/
rollup.config.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { basename, dirname } from 'node:path'
import assert from 'node:assert'
import pkg from './package.json' with { type: 'json' }
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = ! process.env.ROLLUP_WATCH
let name = pkg.name
if (name.slice(0, 1) === '@') {
name = name.split('/')[1]
if (! name) {
throw new TypeError('pkg.name invalid')
}
}
name = parseName(name)
console.log({ name })
const deps = pkg.dependencies
const peerDeps = pkg.peerDependencies
const banner = `
/**
* ${pkg.name}
* ${pkg.description}
*
* @version ${pkg.version}
* @author ${pkg.author}
* @license ${pkg.license}
* @link ${pkg.homepage}
*/
`.trimStart()
const uglifyOpts = {
mangle: true,
compress: {
unused: false,
sequences: true,
dead_code: true,
conditionals: true,
booleans: true,
if_return: true,
join_vars: true,
drop_console: false,
drop_debugger: false,
typeofs: false,
},
output: {
preamble: banner,
},
}
const globals = {
'rxjs/operators': 'rxjs.operators',
'rxjs/websocket': 'rxjs.websocket',
}
let external = [
'rxjs', 'rxjs/operators', 'rxjs/websocket', 'rxjs/ajax',
]
const nodeModule = [
'fs', 'path', 'util', 'os',
]
if (deps && Object.keys(deps).length) {
for (const depName of Object.keys(deps)) {
external.push(depName)
}
}
if (peerDeps && Object.keys(peerDeps).length) {
for (const depName of Object.keys(peerDeps)) {
external.push(depName)
}
}
external = [...new Set(external)]
const config = []
if (pkg.bin) {
// const shebang = `#!/usr/bin/env node\n\n${banner}`
// const shebang = `#!/usr/bin/env ts-node-esm\n\n${banner}`
const shebang = `#!/usr/bin/env tsx\n\n${banner}`
for (const binPath of Object.values(pkg.bin)) {
if (! binPath) {
continue
}
const binSrcPath = binPath.includes('bin/') && ! binPath.includes('dist/bin/')
? binPath.replace('bin/', 'dist/bin/')
: binPath
config.push({
external: external.concat(nodeModule),
input: binSrcPath,
output: [
{
file: binPath,
banner: shebang,
format: 'esm',
globals,
},
],
})
}
}
// remove pkg.name extension if exists
function parseName(name) {
if (typeof name === 'string' && name) {
return basename(name)
}
else {
throw new TypeError('name invalid')
}
return name
}
function genFileNamesForCTS(row) {
const path = row.import
assert(path, 'path is required')
assert(typeof path === 'string', 'path must be a string')
let srcPath = row.types
let baseName = ''
let prefixDir = dirname(path).split('/').slice(2).join('/')
if (path.startsWith('./src/') && path.endsWith('.ts')) {
baseName = basename(path, '.ts')
srcPath = srcPath ?? path
}
else if (path.startsWith('./dist/') && path.endsWith('.js')) {
baseName = basename(path, '.js')
srcPath = srcPath ?? `./src/${prefixDir}/${baseName}.ts`
}
const ctsPath = `./dist/${prefixDir}/${baseName}.d.cts`
return {
srcPath,
ctsPath,
}
}
export default config