-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
76 lines (64 loc) · 1.93 KB
/
rollup.config.mjs
File metadata and controls
76 lines (64 loc) · 1.93 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
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import alias from '@rollup/plugin-alias';
import { builtinModules } from 'module';
import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packageJson = JSON.parse(
fs.readFileSync(`${__dirname}/package.json`, 'utf8'),
);
const tsconfig = JSON.parse(
fs.readFileSync(`${__dirname}/tsconfig.json`, 'utf8'),
);
const peerDependencies = packageJson.peerDependencies || {};
const tsconfigPaths = tsconfig.compilerOptions.paths || {};
export default {
input: 'src/index.tsx',
output: [
{
file: 'dist/index.cjs.jsx',
format: 'cjs',
sourcemap: true,
},
{
file: 'dist/index.esm.jsx',
format: 'es',
sourcemap: true,
},
],
plugins: [
alias({
entries: Object.entries(tsconfigPaths).map(([key, values]) => {
if (values.length !== 1) {
throw new Error('Only one path is supported');
}
const value = values[0];
const postFix = '/*';
if (!key.endsWith(postFix) || !value.endsWith(postFix)) {
throw new Error(
`Only paths ending with /* are supported (${key}, ${value})!`,
);
}
const alias = key.slice(0, -postFix.length);
const targetPath = value.slice(0, -postFix.length);
const resolvedPath = path.resolve(process.cwd(), targetPath);
if (!fs.existsSync(resolvedPath)) {
throw new Error(
`Path does not exist: ${resolvedPath} (${key}, ${value})`,
);
}
return {
find: alias,
replacement: resolvedPath,
};
}),
}),
resolve(),
commonjs(),
typescript(),
],
external: [...builtinModules, ...Object.keys(peerDependencies)],
};