-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathrollup.config.ts
98 lines (95 loc) · 3.42 KB
/
rollup.config.ts
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
import alias from '@rollup/plugin-alias';
import commonJsPlugin from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import fs from 'fs';
import * as path from 'path';
import type { RollupOptions } from 'rollup';
import externals from 'rollup-plugin-node-externals';
import ts from 'rollup-plugin-ts';
function createConfig({
bundleName,
format,
runtime,
}: {
bundleName: string;
format: 'cjs' | 'esm';
runtime: 'browser' | 'node' | 'react-native';
}): RollupOptions {
return {
input: 'src/index.ts',
output: {
file: 'lib/' + format + '/' + bundleName,
format,
},
plugins: [
alias({
entries: [
{
find: /^\./, // Relative paths.
replacement: '.',
async customResolver(source, importer, options) {
const resolved = await this.resolve(source, importer, {
skipSelf: true,
...options,
});
if (resolved == null) {
return;
}
const { id: resolvedId } = resolved;
const directory = path.dirname(resolvedId);
const moduleFilename = path.basename(resolvedId);
const forkPath = path.join(directory, '__forks__', runtime, moduleFilename);
const hasForkCacheKey = `has_fork:${forkPath}`;
let hasFork = this.cache.get(hasForkCacheKey);
if (hasFork === undefined) {
hasFork = fs.existsSync(forkPath);
this.cache.set(hasForkCacheKey, hasFork);
}
if (hasFork) {
return forkPath;
}
},
},
],
}),
externals(),
nodeResolve({
browser: runtime === 'browser',
extensions: ['.ts'],
preferBuiltins: runtime === 'node',
}),
replace({
preventAssignment: true,
values: {
'process.env.BROWSER': JSON.stringify(runtime === 'browser'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
},
}),
ts({
tsconfig: format === 'cjs' ? 'tsconfig.cjs.json' : 'tsconfig.json',
}),
commonJsPlugin({ exclude: 'node_modules', extensions: ['.js', '.ts'] }),
],
};
}
const config: RollupOptions[] = [
createConfig({ bundleName: 'index.js', format: 'cjs', runtime: 'node' }),
createConfig({
bundleName: 'index.browser.js',
format: 'cjs',
runtime: 'browser',
}),
createConfig({ bundleName: 'index.js', format: 'esm', runtime: 'node' }),
createConfig({
bundleName: 'index.browser.js',
format: 'esm',
runtime: 'browser',
}),
createConfig({
bundleName: 'index.native.js',
format: 'cjs',
runtime: 'react-native',
}),
];
export default config;