forked from discord/react-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
195 lines (178 loc) · 4.73 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import fs from 'fs-extra'
import path from 'path'
import { crawl } from 'recrawl-sync'
import dts from 'rollup-plugin-dts'
import babel from '@rollup/plugin-babel'
import esbuild from 'rollup-plugin-esbuild'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
const root = process.platform === 'win32' ? path.resolve('/') : '/'
const external = id => !id.startsWith('.') && !id.startsWith(root)
const extensions = ['.tsx', '.ts', '.js']
const packages = readPackages()
const packageNames = Object.keys(packages)
// Every module in the "input" directory gets its own bundle.
export const multiBundle = ({
input = 'src',
output = 'dist',
...config
} = {}) =>
fs.readdirSync(input).reduce(
(configs, file) =>
configs.concat(
bundle({
input: path.join(input, file),
output: path.join(output, file.replace(/\.tsx?$/, '.js')),
...config,
})
),
[]
)
const getBundleConfig = ({
input = 'src/index.ts',
output = 'dist/index.js',
sourcemap = true,
sourcemapExcludeSources = true,
sourceRoot = path.dirname(input),
} = {}) => ({
input,
output,
sourcemap,
sourcemapExcludeSources,
sourceRoot,
})
export const bundle = config => {
config = getBundleConfig(config)
return [
esmBundle(config),
cjsBundle(config),
dtsBundle(config, 'es'), //
]
}
export const esmBundle = config => ({
input: config.input,
output: {
file: config.output,
format: 'esm',
paths: rewritePaths(),
sourcemap: config.sourcemap,
sourcemapPathTransform: rewriteSourcePaths(config),
sourcemapExcludeSources: config.sourcemapExcludeSources,
},
external,
plugins: [esbuild({ target: 'es2018' })],
})
export const cjsBundle = config => ({
input: config.input,
output: {
file: config.output.replace(/\.js$/, '.cjs.js'),
format: 'cjs',
paths: rewritePaths({ cjs: true }),
sourcemap: config.sourcemap,
sourcemapPathTransform: rewriteSourcePaths(config),
sourcemapExcludeSources: config.sourcemapExcludeSources,
},
external,
plugins: [esbuild({ target: 'es2018' })],
})
// Used for the ".umd" bundle
const globals = {
react: 'React',
'react-dom': 'ReactDOM',
'prop-types': 'PropTypes',
'react-spring': 'ReactSpring',
}
export const umdBundle = (name, config) => {
config = getBundleConfig(config)
return {
input: config.input,
output: {
file: config.output.replace(/\.js$/, '.umd.js'),
format: 'umd',
name,
globals,
sourcemap: true,
},
external: Object.keys(globals),
plugins: [
resolve({ extensions: ['.js', '.ts', '.tsx'] }),
commonjs({ include: /node_modules/ }),
esbuild({ target: 'es2018' }),
babel({
presets: [['@babel/env', { targets: 'defaults' }]],
babelHelpers: 'bundled',
}),
],
}
}
export const dtsBundle = (config, format) => ({
input: config.input,
output: [
{
file: config.output.replace(
/\.js$/,
(format == 'cjs' ? '.cjs' : '') + '.d.ts'
),
format,
paths: rewritePaths({
cjs: format == 'cjs',
}),
},
],
plugins: [dts()],
external,
})
const pkgCache = Object.create(null)
const readPackageJson = dir =>
pkgCache[dir] ||
(pkgCache[dir] = fs.readJsonSync(path.join(dir, 'package.json')))
const pkg = fs.readJsonSync(path.resolve('package.json'))
const rewritePaths = (opts = {}) => {
const deps = pkg.dependencies
const locals = Object.entries(deps).filter(
entry => entry[1].startsWith('link:') && (entry[1] = entry[1].slice(5))
)
const localPkgs = locals.reduce((pkgs, [name, version]) => {
pkgs[name] = readPackageJson(path.resolve(version))
return pkgs
}, Object.create(null))
const resolveLocal = modulePath => {
for (const [name, version] of locals) {
if (modulePath == name || modulePath.startsWith(name + '/')) {
const dep = localPkgs[name]
return modulePath.replace(name, dep.name)
}
}
}
return modulePath => {
let depId = resolveLocal(modulePath)
if (!depId) {
return modulePath
}
if (opts.cjs) {
const name = packageNames.find(name => name === depId)
if (name) {
depId = path.join(name, packages[name].main)
}
}
return depId
}
}
const rewriteSourcePaths = config => {
const outToIn = path.relative(
path.dirname(config.output),
path.dirname(config.input)
)
return file =>
path.join(config.sourceRoot || '', path.relative(outToIn, file))
}
function readPackages() {
return crawl('.', {
only: ['dist/package.json'],
ignore: ['node_modules'],
}).reduce((packages, jsonPath) => {
const json = fs.readJsonSync(jsonPath)
packages[json.name] = json
return packages
}, {})
}