Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace tsc with swc for compiling TS source #2100

Merged
merged 13 commits into from
Jun 14, 2024
5 changes: 5 additions & 0 deletions .changeset/lemon-kings-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': minor
---

Replaced dependency on `tslib` with `@swc/helpers`. This is used mainly by the CommonJS build.
5 changes: 5 additions & 0 deletions .changeset/strange-spiders-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': patch
---

The build output is now (again) formatted using `prettier` for easier debugging.
r100-stack marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions .changeset/witty-ties-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': patch
---

Removed all code comments from the build output.
9 changes: 5 additions & 4 deletions packages/itwinui-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@
"ux"
],
"scripts": {
"build": "pnpm clean:build && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.react-table.json && pnpm build:styles && pnpm build:post",
"build": "pnpm clean:build && pnpm build:js && pnpm build:styles && pnpm build:types && pnpm build:post",
"build:js": "node ./scripts/build.mjs",
"build:types": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.react-table.json",
"build:styles": "vite build src/styles.js",
"build:watch": "concurrently \"tsc -p tsconfig.cjs.json --watch\" \"tsc -p tsconfig.esm.json --watch\"",
"build:post": "node ./scripts/postBuild.mjs",
"clean:build": "rimraf esm && rimraf cjs && rimraf react-table.d.ts",
"clean:coverage": "rimraf coverage",
Expand All @@ -85,11 +86,11 @@
"dependencies": {
"@floating-ui/react": "^0.26.10",
"@itwin/itwinui-illustrations-react": "^2.1.0",
"@swc/helpers": "^0.5.11",
"classnames": "^2.3.2",
"jotai": "^2.8.0",
"react-table": "^7.8.0",
"react-transition-group": "^4.4.5",
"tslib": "^2.6.0"
r100-stack marked this conversation as resolved.
Show resolved Hide resolved
"react-transition-group": "^4.4.5"
},
"devDependencies": {
"@swc/cli": "^0.3.12",
Expand Down
41 changes: 41 additions & 0 deletions packages/itwinui-react/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { execSync } from 'node:child_process';

// https://swc.rs/docs/usage/cli
const swcOptions = {
shared: [
'--strip-leading-paths',
'--no-swcrc',
'--ignore **/*.test.*,**/styles.js/*,**/styles*',
].join(' '),

// https://swc.rs/docs/configuration/compilation
compilerOptions: [
'',
'jsc.parser.syntax=typescript',
'jsc.parser.tsx=true',
'jsc.transform.react.useBuiltins=true',
'jsc.target=es2020',
'jsc.minify.format.comments=false',
'jsc.externalHelpers=true',
].join(' -C '),

get esm() {
return [this.shared, this.compilerOptions, '-C module.type=es6'].join(' ');
},

get cjs() {
return [this.shared, this.compilerOptions, '-C module.type=commonjs'].join(
' ',
);
},
};

execSync(`pnpm swc src -d esm ${swcOptions.esm}`);
console.log('✓ Built esm.');

execSync(`pnpm swc src -d cjs ${swcOptions.cjs}`);
console.log('✓ Built cjs.');
18 changes: 10 additions & 8 deletions packages/itwinui-react/scripts/postBuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import fs from 'node:fs';
import { exec } from 'node:child_process';
import { execSync } from 'node:child_process';

// This creates a dummy package.json file in the cjs folder.
// Without this, all cjs files would need the .cjs extension
Expand All @@ -15,10 +15,12 @@ try {
}

// Run prettier on all compiled output because it gets jumbled by tsc.
exec('pnpm exec prettier --write "{esm,cjs}/**/*.js"', (error) => {
if (error) {
console.error('Error when running prettier', error);
} else {
console.log('✓ Finished compiling @itwin/itwinui-react');
}
});
try {
execSync(
'npx prettier --write --ignore-path="../../.gitignore" "{esm,cjs}/**/*.js"',
);
} catch (error) {
console.error('Error when running prettier', error);
}

console.log('\x1b[32m✓ Finished building @itwin/itwinui-react');
2 changes: 1 addition & 1 deletion packages/itwinui-react/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"files": ["src/index.ts"],
"compilerOptions": {
"noUnusedLocals": false,
"importHelpers": true
"emitDeclarationOnly": true
}
}
38 changes: 24 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading