Repository navigation
Expand file tree
/
Copy pathrollup.config.js
More file actions
113 lines (103 loc) · 2.57 KB
/
Copy pathrollup.config.js
File metadata and controls
113 lines (103 loc) · 2.57 KB
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
import fs from 'fs'
import path from 'path'
import pkg from './package.json'
const external = [
'fs',
'path',
'crypto',
'stream',
'crc/lib/crc32.js',
'sha.js/sha1',
'sha.js/sha1.js',
...Object.keys(pkg.dependencies),
]
const nodeAliases = {
[path.resolve(__dirname, 'src/utils/shasumRange.js')]: path.resolve(
__dirname,
'src/utils/shasumRange.node.js'
),
}
const alias = aliases => ({
name: 'alias',
resolveId(source, importer) {
if (aliases[source]) return aliases[source]
if (!importer || !source.startsWith('.')) return null
const resolved = path.resolve(path.dirname(importer), source)
return aliases[resolved] || null
},
})
// Modern modules
const ecmaConfig = (input, output) => ({
input: `src/${input}`,
external: [...external],
output: [
{
format: 'es',
file: `${output}`,
},
],
})
// Legacy CommonJS2 modules
const nodeConfig = (input, output) => ({
input: `src/${input}`,
external: [...external],
plugins: [alias(nodeAliases)],
output: [
{
format: 'cjs',
file: `${output}`,
exports: 'named',
},
],
})
// Script tags that "export" a global var for those browser environments that
// still don't support `import` (Workers and ServiceWorkers)
const umdConfig = (input, output, name) => ({
input: `src/${input}`,
output: [
{
format: 'umd',
file: `${output}`,
name,
exports: 'named',
},
],
})
const template = umd =>
JSON.stringify(
{
type: 'module',
main: 'index.cjs',
module: 'index.js',
typings: 'index.d.ts',
unpkg: umd ? 'index.umd.js' : undefined,
},
null,
2
)
const pkgify = (input, output, name) => {
fs.mkdirSync(path.join(__dirname, output), { recursive: true })
fs.writeFileSync(
path.join(__dirname, output, 'package.json'),
template(!!name)
)
return [
ecmaConfig(`${input}/index.js`, `${output}/index.js`),
nodeConfig(`${input}/index.js`, `${output}/index.cjs`),
...(name
? [umdConfig(`${input}/index.js`, `${output}/index.umd.js`, name)]
: []),
]
}
export default [
ecmaConfig('index.js', 'index.js'),
nodeConfig('index.js', 'index.cjs'),
ecmaConfig('internal-apis.js', 'internal-apis.js'),
nodeConfig('internal-apis.js', 'internal-apis.cjs'),
ecmaConfig('managers/index.js', 'managers/index.js'),
nodeConfig('managers/index.js', 'managers/index.cjs'),
ecmaConfig('models/index.js', 'models/index.js'),
nodeConfig('models/index.js', 'models/index.cjs'),
...pkgify('http/node', 'http/node'),
...pkgify('http/web', 'http/web', 'GitHttp'),
]