-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.mjs
102 lines (94 loc) · 2.88 KB
/
rollup.config.mjs
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
/**
* NOTE: There is currently an open issue for adding 'use client' directive
* https://github.com/rollup/rollup/issues/4699
*/
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
import alias from '@rollup/plugin-alias';
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import analyze from 'rollup-plugin-analyzer';
import preserveDirectives from 'rollup-plugin-preserve-directives';
const require = createRequire(import.meta.url);
const pkg = require('./package.json');
/**
* Used for generating external dependencies
* Credit: Mateusz Burzyński (https://github.com/Andarist)
* Source: https://github.com/rollup/rollup-plugin-babel/issues/148#issuecomment-399696316
*/
const makeExternalPredicate = (externalArr) => {
if (externalArr.length === 0) {
return () => false;
}
const pattern = new RegExp(`^(${externalArr.join('|')})($|/)`);
return (id) => pattern.test(id);
};
const babelRuntimeVersion = pkg.dependencies['@babel/runtime'].replace(/^[^0-9]*/, '');
const outputOptions = {
exports: 'named',
preserveModules: true,
// Ensures that CJS default exports are imported properly (based on __esModule)
// If needed, can switch to 'compat' which checks for .default prop on the default export instead
// see https://rollupjs.org/configuration-options/#output-interop
interop: 'auto',
banner: `/*
* Rollup Library Starter
* {@link https://github.com/mryechkin/rollup-library-starter}
* @copyright Mykhaylo Ryechkin (@mryechkin)
* @license MIT
*/`,
};
const config = {
input: 'src/index.js',
output: [
{
dir: 'dist/esm',
format: 'esm',
...outputOptions,
},
{
dir: 'dist/cjs',
format: 'cjs',
...outputOptions,
},
],
external: makeExternalPredicate([
// Handles both dependencies and peer dependencies so we don't have to manually maintain a list
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
]),
plugins: [
alias({
entries: {
src: fileURLToPath(new URL('src', import.meta.url)),
},
}),
nodeResolve(),
commonjs({ include: ['node_modules/**'] }),
babel({
babelHelpers: 'runtime',
exclude: /node_modules/,
plugins: [['@babel/plugin-transform-runtime', { version: babelRuntimeVersion }]],
presets: [
['@babel/preset-env', { targets: 'defaults' }],
['@babel/preset-react', { runtime: 'automatic' }],
],
}),
preserveDirectives(),
terser(),
analyze({
hideDeps: true,
limit: 0,
summaryOnly: true,
}),
],
// Ignore warnings when using "use client" directive
onwarn(warning, warn) {
if (warning.code !== 'MODULE_LEVEL_DIRECTIVE') {
warn(warning);
}
},
};
export default config;