Skip to content

Commit b72149b

Browse files
committed
chore: replace babel and build script with rollup and swc
Removes `build.js` script Most `babel` dependencies and config are still needed for `storybook` `@babel/cli` has been removed
1 parent 724cf0c commit b72149b

File tree

23 files changed

+10142
-7339
lines changed

23 files changed

+10142
-7339
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ lib
1616
esm
1717
src/style-config/assets.scss
1818
storybook-static/
19+
.swc/
20+
bundle-stats/
1921

2022
playwright/test-results
2123
playwright/test-report
22-
playwright/.cache
24+
playwright/.cache

.swcrc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"jsc": {
3+
"target": "es2020",
4+
"parser": {
5+
"syntax": "typescript",
6+
"decorators": true,
7+
"dynamicImport": true
8+
},
9+
"transform": {
10+
"decoratorMetadata": true,
11+
"legacyDecorator": true
12+
},
13+
"keepClassNames": true,
14+
"externalHelpers": true,
15+
"loose": true
16+
},
17+
"module": {
18+
"type": "es6"
19+
},
20+
"sourceMaps": true,
21+
"exclude": [
22+
"jest.config.ts",
23+
".*\\.spec.tsx?$",
24+
".*\\.test.tsx?$",
25+
"./src/jest-setup.ts$",
26+
"./**/jest-setup.ts$"
27+
]
28+
}

package-lock.json

Lines changed: 9312 additions & 7168 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
"test-update": "jest --config=./jest.config.ts --updateSnapshot",
1414
"format": "prettier --write './{src,playwright}/**/*.{js,jsx,ts,tsx}'",
1515
"lint": "eslint ./src ./playwright",
16-
"build": "node ./scripts/build.js",
17-
"copy-files": "node ./scripts/copy-files.js",
1816
"precompile": "npm run type-check && npm run clean-lib && npm run build && npm run build:types && npm run copy-files",
1917
"prepublishOnly": "npm run precompile",
20-
"watch": "npm run clean-lib && npm run copy-files -- --watch & npm run babel -- --watch",
2118
"build-storybook": "dotenvx run -- storybook build -c .storybook",
2219
"build-storybook:prod": "dotenvx run -f .env.production -- storybook build -c .storybook",
2320
"start:static": "npx http-server -p 9001 ./storybook-static",
2421
"clean-lib": "rimraf ./lib && rimraf ./esm",
2522
"commit": "git-cz",
2623
"generate-metadata": "node ./scripts/generate_metadata/index.mjs",
27-
"type-check:carbon": "tsc --noEmit",
24+
"type-check:carbon": "tsc --noEmit -p ./tsconfig-build.json",
2825
"type-check:playwright": "tsc -p ./tsconfig-playwright.json",
2926
"type-check": "npm run type-check:carbon && npm run type-check:playwright",
3027
"type-check:watch": "concurrently --names 'src,playwright' -c 'cyan,yellow' 'npm run type-check:carbon -- --watch' 'npm run type-check:playwright -- --watch'",
@@ -36,7 +33,10 @@
3633
"clear-playwright-cache": "rimraf ./playwright/.cache",
3734
"clear-playwright-coverage-json": "rimraf ./playwright/coverage/*.json",
3835
"test:ct:coverage": "npm run clear-playwright-cache && npm run clear-playwright-coverage-json && rimraf ./playwright/coverage && playwright test -c playwright-ct.config.ts",
39-
"pw:coverage:report": "nyc report --reporter=html --report-dir=./playwright/coverage --temp-dir=./playwright/coverage"
36+
"pw:coverage:report": "nyc report --reporter=html --report-dir=./playwright/coverage --temp-dir=./playwright/coverage",
37+
"build": "npm run clean-lib && rollup -c \"./rollup.config.mjs\" && npm run build:generate-package-json-files && npm run build:move-svg && npm run build:types",
38+
"build:generate-package-json-files": "node ./scripts/generate_package_json_files/index.js",
39+
"build:move-svg": "node ./scripts/copy_svg/index.js"
4040
},
4141
"repository": {
4242
"type": "git",
@@ -56,7 +56,6 @@
5656
},
5757
"devDependencies": {
5858
"@axe-core/playwright": "~4.10.1",
59-
"@babel/cli": "^7.23.4",
6059
"@babel/core": "^7.23.3",
6160
"@babel/eslint-parser": "^7.23.3",
6261
"@babel/plugin-proposal-class-properties": "^7.18.6",
@@ -71,6 +70,11 @@
7170
"@dotenvx/dotenvx": "^1.25.1",
7271
"@playwright/experimental-ct-react": "^1.52.0",
7372
"@playwright/test": "^1.52.0",
73+
"@rollup/plugin-commonjs": "^28.0.2",
74+
"@rollup/plugin-json": "^6.1.0",
75+
"@rollup/plugin-node-resolve": "^16.0.0",
76+
"@rollup/plugin-terser": "^0.3.0",
77+
"@rollup/plugin-url": "^8.0.2",
7478
"@sage/design-tokens": "~4.29.0",
7579
"@semantic-release/changelog": "^6.0.3",
7680
"@semantic-release/exec": "^6.0.3",
@@ -89,6 +93,8 @@
8993
"@storybook/react-webpack5": "~8.6.7",
9094
"@storybook/theming": "~8.6.7",
9195
"@storybook/types": "~8.6.7",
96+
"@swc/helpers": "^0.5.15",
97+
"@swc/plugin-styled-components": "^7.1.0",
9298
"@testing-library/dom": "^10.4.0",
9399
"@testing-library/jest-dom": "^6.6.3",
94100
"@testing-library/react": "^16.1.0",
@@ -159,6 +165,12 @@
159165
"react-markdown": "^8.0.7",
160166
"remark-gfm": "^4.0.0",
161167
"rimraf": "^3.0.2",
168+
"rollup": "^3.29.5",
169+
"rollup-plugin-copy": "^3.5.0",
170+
"rollup-plugin-ignore": "^1.0.10",
171+
"rollup-plugin-postcss": "^4.0.2",
172+
"rollup-plugin-swc3": "^0.12.1",
173+
"rollup-plugin-visualizer": "^5.14.0",
162174
"semantic-release": "^24.2.3",
163175
"semver": "^7.5.4",
164176
"sprintf-js": "^1.1.3",

rollup.config.mjs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { fileURLToPath } from "url";
2+
import resolve from "@rollup/plugin-node-resolve";
3+
import commonjs from "@rollup/plugin-commonjs";
4+
import json from "@rollup/plugin-json";
5+
import terser from "@rollup/plugin-terser";
6+
import path from "path";
7+
import glob from "glob";
8+
import postcss from "rollup-plugin-postcss";
9+
import swc from "rollup-plugin-swc3";
10+
import copy from "rollup-plugin-copy";
11+
import { visualizer } from "rollup-plugin-visualizer";
12+
13+
/* Convert import.meta.url to a file path and get the directory name */
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = path.dirname(__filename);
16+
17+
export default {
18+
input: Object.fromEntries(
19+
glob
20+
.sync(path.join(__dirname, "src/**/*.{ts,tsx}"), {
21+
ignore: [
22+
"**/*.types.ts",
23+
/** This is just a type file so ignored to avoid an empty chunk */
24+
"**/icon-type.ts",
25+
/** This is just a type file so ignored to avoid an empty chunk */
26+
"**/locale.ts",
27+
"**/*.test.ts",
28+
"**/*.spec.ts",
29+
"**/*.stories.ts",
30+
"**/*.test.tsx",
31+
"**/*.spec.tsx",
32+
"**/*.stories.tsx",
33+
"**/*.pw.tsx",
34+
"**/*.test-pw.tsx",
35+
"**/*.d.ts",
36+
/* Ignores SVGs, they are moved in post-build */
37+
"**/*.svg",
38+
"**/test-utils.ts",
39+
],
40+
})
41+
.map((file) => [
42+
/**
43+
* Removes `src/` as well as the file extension from each file
44+
* e.g. src/components/foo.js becomes components/foo
45+
* */
46+
path.relative(
47+
path.join(__dirname, "src"),
48+
file.slice(0, file.length - path.extname(file).length),
49+
),
50+
/**
51+
* This expands the relative paths to absolute paths
52+
* e.g. src/components/foo becomes /project/src/components/foo.js
53+
* */
54+
fileURLToPath(new URL(file, import.meta.url)),
55+
]),
56+
),
57+
external: [
58+
/node_modules/,
59+
/^[^./]|^\.[^./]|^\.\.[^/]/,
60+
/* Treat all SVGs as external */
61+
/\.svg$/,
62+
"styled-components",
63+
"@swc/helpers",
64+
"react",
65+
"react-dom",
66+
],
67+
plugins: [
68+
resolve({
69+
extensions: [".js", ".jsx", ".ts", ".tsx"],
70+
preferBuiltins: true,
71+
browser: true,
72+
moduleDirectories: ["node_modules"],
73+
resolveOnly: [
74+
/* Exclude @swc/helpers from custom resolution */
75+
/^(?!@swc\/helpers)/,
76+
/* Exclude styled-components from custom resolution */
77+
/^(?!styled-components)/,
78+
],
79+
}),
80+
commonjs({
81+
include: "node_modules/**",
82+
requireReturnsDefault: "auto",
83+
transformMixedEsModules: true,
84+
ignoreDynamicRequires: true,
85+
}),
86+
json(),
87+
postcss({
88+
extract: true,
89+
minimize: true,
90+
}),
91+
swc({
92+
jsc: {
93+
parser: {
94+
syntax: "typescript",
95+
tsx: true,
96+
decorators: true,
97+
},
98+
transform: {
99+
react: {
100+
runtime: "automatic",
101+
},
102+
legacyDecorator: true,
103+
decoratorVersion: "2022-03"
104+
},
105+
externalHelpers: false,
106+
experimental: {
107+
plugins: [
108+
["@swc/plugin-styled-components", {
109+
displayName: true,
110+
ssr: true,
111+
fileName: true,
112+
minify: true,
113+
transpileTemplateLiterals: true
114+
}]
115+
]
116+
},
117+
},
118+
}),
119+
terser({
120+
maxWorkers: 4,
121+
compress: {
122+
/* Keep console.error and console.warn */
123+
pure_funcs: [
124+
"console.log",
125+
"console.info",
126+
"console.debug"
127+
],
128+
passes: 2,
129+
unused: true,
130+
dead_code: true
131+
},
132+
mangle: true,
133+
format: {
134+
comments: false,
135+
},
136+
/* Matches es6 target in tsconfig */
137+
ecma: 2015,
138+
toplevel: true
139+
}),
140+
copy({
141+
targets: [
142+
{ src: "src/style/assets/**/*", dest: "lib/style/assets" },
143+
{ src: "src/style/assets/**/*", dest: "esm/style/assets" },
144+
{ src: "src/style/fonts.css", dest: "lib/style/" },
145+
{ src: "src/style/fonts.css", dest: "esm/style/" },
146+
{ src: "src/global.d.ts", dest: "lib/" },
147+
{ src: "src/global.d.ts", dest: "esm/" },
148+
]
149+
}),
150+
visualizer({
151+
filename: "bundle-stats/index.html",
152+
gzipSize: true,
153+
brotliSize: true,
154+
template: "sunburst",
155+
title: "Carbon Bundle Stats",
156+
}),
157+
],
158+
output: [
159+
{
160+
dir: path.join(__dirname, "lib"),
161+
format: "cjs",
162+
preserveModules: true,
163+
preserveModulesRoot: "src",
164+
entryFileNames: "[name].js",
165+
/* Preserves the folder structure of the source files */
166+
chunkFileNames: ({ name }) => `${name}.js`,
167+
exports: 'named',
168+
interop: 'auto',
169+
},
170+
{
171+
dir: path.join(__dirname, "esm"),
172+
format: "esm",
173+
preserveModules: true,
174+
preserveModulesRoot: "src",
175+
entryFileNames: "[name].js",
176+
/* Preserves the folder structure of the source files */
177+
chunkFileNames: ({ name }) => `${name}.js`,
178+
},
179+
],
180+
};

scripts/build.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)