-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrollup.config.mjs
130 lines (118 loc) · 3.29 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import typescript from 'rollup-plugin-typescript2';
import nodeResolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import svgr from '@svgr/rollup';
import fs from 'node:fs';
import path from 'node:path';
import { cwd } from 'node:process';
import postcss from 'rollup-plugin-postcss';
import discardDuplicates from 'postcss-discard-duplicates';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { readFileSync } from 'node:fs';
import summary from 'rollup-plugin-summary';
const tsconfig = JSON.parse(readFileSync('./tsconfig.json', 'utf8'));
const inputExists = (config) => {
try {
return fs.existsSync(config.input);
} catch {
return false;
}
};
const externalDependencies = ['react', 'react-dom'];
const internalDependencies = tsconfig.compilerOptions.paths && Object.keys(tsconfig.compilerOptions.paths);
const dependencies = externalDependencies.concat(internalDependencies);
const createConfig = ({ dir, format, baseUrl }) => ({
input: path.join(baseUrl, 'src/index.tsx'),
output: {
dir,
sourcemap: false,
format,
compact: true,
},
plugins: [
peerDepsExternal(),
postcss({
extensions: ['.css', '.scss'],
minimize: true,
inject: (css, { insertAt } = {}) => {
return `
if (typeof document !== 'undefined') {
const head = document.head || document.getElementsByTagName('head')[0]
const style = document.createElement('style')
style.type = 'text/css'
style.nonce = window.NONCE
if (${insertAt}=== 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild)
} else {
head.appendChild(style)
}
} else {
head.appendChild(style)
}
if (style.styleSheet) {
style.styleSheet.cssText = ${css}
} else {
style.appendChild(document.createTextNode(${css}))
}
}
`;
},
}),
nodeResolve(),
commonjs({ include: /node_modules/ }),
svgr({ icon: true, svgo: true, memo: true }),
typescript({
cacheDir: path.join(path.dirname(import.meta.url), '.rollup-cache'),
tsconfig: path.join(baseUrl, 'tsconfig.json'),
tsBuildInfoFile: path.join(baseUrl, '.tsbuildinfo'),
compilerOptions: {
outDir: dir,
},
}),
summary(),
],
external: dependencies,
});
const baseUrl = cwd();
const configs = [
createConfig({ format: 'cjs', dir: './dist/cjs', baseUrl }),
createConfig({ format: 'esm', dir: './dist/mjs', baseUrl }),
{
input: 'src/index.scss',
output: {
dir: './dist',
sourcemap: false,
format: 'esm',
compact: true,
},
plugins: [
postcss({
extensions: ['.css', '.scss'],
extract: true,
}),
],
},
{
input: 'src/index.scss',
output: {
dir: './dist',
sourcemap: false,
format: 'esm',
compact: true,
},
plugins: [
postcss({
extensions: ['.css', '.scss'],
plugins: [discardDuplicates()],
extract: true,
}),
],
},
]
.map(({ input, ...config }) => ({
...config,
input: path.resolve(cwd(), input),
}))
.filter(inputExists);
export default configs;