-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
rollup.config.js
95 lines (87 loc) · 3.11 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
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import path from 'path';
import dtsPlugin from 'rollup-plugin-dts';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import typescript from 'rollup-plugin-ts';
import * as tsModule from 'typescript';
//import { visualizer } from 'rollup-plugin-visualizer';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import tsconfig from './tsconfig.json' assert { type: 'json' };
const ts = tsModule.default;
export default async (options) => {
const {
dir,
packageJson,
prePlugins = [],
postPlugins = [],
external = [],
dts = true,
compilerOptions = {},
babelPresets = [],
babelPlugins = [],
disableCoreCompilation = false,
cancelBrowserListForTypescript = false
} = options;
const __dirname = dirname(fileURLToPath(import.meta.url));
const dirnameConfig = path.resolve(__dirname, dir);
const postcssConfig = (await import('./postcss.config.cjs')).default;
tsconfig.compilerOptions = { ...tsconfig.compilerOptions, ...compilerOptions };
const defaultPresets = ['@babel/preset-env', ['@babel/preset-typescript', tsconfig.compilerOptions]];
const inputs = [
disableCoreCompilation
? null
: {
input: options.input || path.resolve(dirnameConfig, 'src/index.ts'),
output: [
{
file: packageJson.main,
format: 'cjs',
sourcemap: true,
inlineDynamicImports: true
},
{
file: packageJson.module,
format: 'esm',
sourcemap: true,
inlineDynamicImports: true
}
],
treeshake: true,
external,
plugins: [
...prePlugins,
nodeResolve({ extensions: ['.js', '.ts', '.tsx'] }),
json(),
typescript({
browserslist: cancelBrowserListForTypescript ? false : undefined,
tsconfig: { ...tsconfig.compilerOptions, emitDeclarationOnly: true }
}),
...postPlugins,
babel({
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }, ...babelPlugins]],
extensions: ['.js', '.ts', '.tsx'],
presets: babelPresets.length > 0 ? [...babelPresets, ...defaultPresets] : defaultPresets,
babelHelpers: 'bundled',
ignore: [/node_modules/]
}),
postcss(postcssConfig),
peerDepsExternal(),
commonjs()
// visualizer() // TODO next version, use a strategy for visualizer
]
},
dts && !disableCoreCompilation
? {
input: path.resolve(dirnameConfig, packageJson.module.replace('.js', '.d.ts')),
output: [{ file: path.resolve(dirnameConfig, 'dist/index.d.ts'), format: 'esm' }],
plugins: [dtsPlugin()]
}
: null
];
return inputs.filter((x) => x);
};