-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
86 lines (69 loc) · 1.83 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
//@ts-check
/// <reference types="node" />
import RollupPluginCommonjs from '@rollup/plugin-commonjs';
import RollupPluginNodeResolve from '@rollup/plugin-node-resolve';
import RollupPluginJson from '@rollup/plugin-json';
import RollupPluginTs from '@wessberg/rollup-plugin-ts';
import { builtinModules } from 'module';
import * as Path from 'path';
import * as Rollup from 'rollup';
import * as Package from './package.json';
const SPEC_RX = /^((@[^/]+\/[^/@]+|[^./@][^/@]*)(?:@([^/]+))?)(.*)?$/;
function parseBareModuleSpec(bareModuleSpec) {
const matches = bareModuleSpec.match(SPEC_RX);
if (matches) {
const [, nameSpec, name, spec, path = ''] = matches;
return {
nameSpec,
name,
spec,
path,
};
}
return null;
}
function createIsExternal(packageJson) {
const dependencies = new Set([...Object.keys(packageJson.dependencies || {}), ...builtinModules]);
return function isExternal(id) {
const spec = parseBareModuleSpec(id);
if (!spec) return false;
const result = dependencies.has(spec.name);
return result;
};
}
/** @type {Rollup.OutputOptions[]} */
const output = [];
if (Package.main) {
output.push({
exports: 'named',
file: Path.resolve(process.cwd(), Package.main),
format: 'commonjs',
sourcemap: true,
});
}
if (Package.module) {
output.push({
exports: 'named',
file: Path.resolve(process.cwd(), Package.module),
format: 'esm',
sourcemap: true,
});
}
/** @type {Rollup.RollupOptions} */
const config = {
input: './src/index.ts',
output,
external: createIsExternal(Package),
plugins: [
RollupPluginJson(),
RollupPluginCommonjs(),
RollupPluginTs({
exclude: ['node_modules'],
transpiler: 'babel',
}),
RollupPluginNodeResolve({
mainFields: ['module', 'main'],
}),
],
};
export default config;