-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
186 lines (172 loc) · 4.84 KB
/
rollup.config.mjs
File metadata and controls
186 lines (172 loc) · 4.84 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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 nodeExternal from 'rollup-plugin-node-externals';
import summary from 'rollup-plugin-summary';
const inputExists = (config) => {
try {
return fs.existsSync(config.input);
} catch {
return false;
}
};
export const outputGlobals = {
react: 'React',
'react-dom': 'ReactDOM',
};
// TODO search for rollup-plugin-postcss alternatives if the legacy-js-api deprecation warnings will not be fixed.
const createConfig = ({ dir, format, baseUrl, sourcemap, globals }) => ({
input: path.join(baseUrl, 'src/index.tsx'),
output: {
dir,
sourcemap,
format,
compact: true,
globals,
},
plugins: [
peerDepsExternal({ includeDependencies: true }),
nodeExternal(),
nodeResolve({ browser: true }),
postcss({
extensions: ['.css', '.scss'],
minimize: true,
use: {
sass: {
silenceDeprecations: ['legacy-js-api'],
},
},
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}))
}
}
`;
},
}),
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(),
],
});
const baseUrl = cwd();
function emitCssDts() {
return {
name: 'emit-css-dts',
generateBundle() {
const dtsSource = `declare const css: string;\nexport default css;\nexport const stylesheet: string;\n`;
this.emitFile({
type: 'asset',
fileName: 'css.d.mts',
source: dtsSource,
});
},
};
}
function emitStylesheetLoader() {
return {
name: 'emit-stylesheet-loader',
generateBundle(options, bundle) {
const css = bundle['css.mjs'].code;
const cssWithoutExport = css.replace(/export\s*{.*};/, '');
const constructedStylesheetCode = `const constructedStylesheet = new CSSStyleSheet();\nconstructedStylesheet.replaceSync(stylesheet);\nexport default constructedStylesheet;`;
this.emitFile({
type: 'asset',
fileName: 'stylesheet.mjs',
source: cssWithoutExport + constructedStylesheetCode,
});
this.emitFile({
type: 'asset',
fileName: 'stylesheet.d.mts',
source: `declare const constructedStylesheet: CSSStyleSheet;\nexport default constructedStylesheet;\n`,
});
},
};
}
const configs = [
createConfig({ format: 'cjs', dir: './dist/cjs', baseUrl, sourcemap: true, globals: outputGlobals }),
createConfig({ format: 'esm', dir: './dist/mjs', baseUrl, sourcemap: true, globals: outputGlobals }),
{
input: 'src/index.scss',
output: {
dir: './dist',
sourcemap: false,
format: 'esm',
compact: true,
},
plugins: [
postcss({
extensions: ['.css', '.scss'],
plugins: [discardDuplicates()],
extract: true,
use: {
sass: {
silenceDeprecations: ['legacy-js-api'],
},
},
}),
],
},
{
input: 'src/index.scss',
output: {
dir: './dist/mjs',
sourcemap: false,
entryFileNames: 'css.mjs',
format: 'esm',
compact: true,
},
plugins: [
postcss({
extensions: ['.css', '.scss'],
extract: false,
inject: false,
use: {
sass: {
silenceDeprecations: ['legacy-js-api'],
},
},
}),
emitCssDts(),
emitStylesheetLoader(),
],
},
]
.map(({ input, ...config }) => ({
...config,
input: path.resolve(cwd(), input),
}))
.filter(inputExists);
export default configs;