-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
216 lines (208 loc) · 5.97 KB
/
rollup.config.mjs
File metadata and controls
216 lines (208 loc) · 5.97 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import terser from "@rollup/plugin-terser";
import path from "path";
import glob from "glob";
import postcss from "rollup-plugin-postcss";
import swc from "rollup-plugin-swc3";
import copy from "rollup-plugin-copy";
import { visualizer } from "rollup-plugin-visualizer";
export default {
input: Object.fromEntries(
glob
.sync("src/**/*.{ts,tsx}", {
ignore: [
"**/*.types.ts",
/** This is just a type file so ignored to avoid an empty chunk */
"**/icon-type.ts",
/** This is just a type file so ignored to avoid an empty chunk */
"**/locale.ts",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.stories.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.stories.tsx",
"**/*.pw.tsx",
"**/*.test-pw.tsx",
"**/*.d.ts",
/* Ignores SVGs, they are moved in post-build */
"**/*.svg",
"**/test-utils.ts",
/* We only want to include the mocks and spec helpers intended for public use */
"**/__spec_helper__/__internal__/**",
/* ignoring the tokens hooks for now as we don't want to support dark mode in production yet */
"**/tokens-wrapper/__internal__/hooks/**",
/* don't build the TextEditor instance used in unit tests (TestEditor) */
"**/text-editor/__internal__/TestEditor.component.tsx",
],
})
.map((file) => {
// Normalize path separators for cross-platform compatibility
const normalizedFile = file.replace(/\\/g, "/");
return [
/**
* Removes `src/` as well as the file extension from each file
* e.g. src/components/foo.js becomes components/foo
* */
path.posix.relative(
"src",
normalizedFile.slice(
0,
normalizedFile.length - path.extname(normalizedFile).length,
),
),
/**
* This creates absolute paths using path.resolve for cross-platform compatibility
* */
path.resolve(file),
];
}),
),
external: (id) => {
// Externalise SVGs
if (id.endsWith(".svg")) {
return true;
}
// Don't externalise entry modules (resolved absolute paths)
if (path.isAbsolute(id)) {
return false;
}
// Don't externalise relative paths
if (id.startsWith("./") || id.startsWith("../")) {
return false;
}
// Externalise node_modules
if (id.includes("node_modules")) {
return true;
}
// Externalise specific packages
if (
["styled-components", "@swc/helpers", "react", "react-dom"].includes(id)
) {
return true;
}
// Externalise bare module imports (packages from node_modules)
// This regex matches imports that don't start with ./ or ../ or /
if (/^[^./]/.test(id)) {
return true;
}
return false;
},
plugins: [
resolve({
extensions: [".js", ".jsx", ".ts", ".tsx"],
preferBuiltins: true,
browser: true,
moduleDirectories: ["node_modules"],
exportConditions: ["node", "default"],
}),
commonjs({
include: "node_modules/**",
requireReturnsDefault: "auto",
transformMixedEsModules: true,
ignoreDynamicRequires: true,
}),
json(),
postcss({
extract: true,
minimize: true,
}),
swc({
jsc: {
parser: {
syntax: "typescript",
tsx: true,
decorators: true,
},
transform: {
react: {
runtime: "automatic",
},
legacyDecorator: true,
decoratorVersion: "2022-03",
},
externalHelpers: false,
experimental: {
plugins: [
[
"@swc/plugin-styled-components",
{
displayName: true,
ssr: true,
fileName: true,
minify: true,
transpileTemplateLiterals: true,
},
],
],
},
},
}),
terser({
maxWorkers: 4,
compress: {
/* Keep console.error and console.warn */
pure_funcs: ["console.log", "console.info", "console.debug"],
passes: 2,
unused: true,
dead_code: true,
},
mangle: true,
format: {
comments: false,
},
/* Matches es6 target in tsconfig */
ecma: 2015,
toplevel: true,
}),
copy({
targets: [
{ src: "src/style/assets/**/*", dest: "lib/style/assets" },
{ src: "src/style/assets/**/*", dest: "esm/style/assets" },
{
src: "src/components/icon/fonts/*.{woff,woff2}",
dest: "lib/components/icon/fonts",
},
{
src: "src/components/icon/fonts/*.{woff,woff2}",
dest: "esm/components/icon/fonts",
},
{ src: "src/style/fonts.css", dest: "lib/style/" },
{ src: "src/style/fonts.css", dest: "esm/style/" },
{ src: "src/global.d.ts", dest: "lib/" },
{ src: "src/global.d.ts", dest: "esm/" },
],
}),
visualizer({
filename: "bundle-stats/index.html",
gzipSize: true,
brotliSize: true,
template: "sunburst",
title: "Carbon Bundle Stats",
}),
],
output: [
{
dir: "lib",
format: "cjs",
preserveModules: true,
preserveModulesRoot: "src",
entryFileNames: "[name].js",
/* Preserves the folder structure of the source files */
chunkFileNames: ({ name }) => `${name}.js`,
exports: "named",
interop: "auto",
},
{
dir: "esm",
format: "esm",
preserveModules: true,
preserveModulesRoot: "src",
entryFileNames: "[name].js",
/* Preserves the folder structure of the source files */
chunkFileNames: ({ name }) => `${name}.js`,
},
],
};