Skip to content

Commit 737a9b0

Browse files
author
Brijesh Bittu
committed
Update buildTypes to support base-ui requirements
while not requiring any changes in core repo.
1 parent c048038 commit 737a9b0

3 files changed

Lines changed: 45 additions & 25 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
"fast-glob": "^3.3.3",
175175
"fs-extra": "^11.3.0",
176176
"globby": "^14.1.0",
177+
"jsonc-parser": "^3.3.1",
177178
"karma": "^6.4.4",
178179
"karma-browserstack-launcher": "~1.6.0",
179180
"karma-chrome-launcher": "^3.2.0",

pnpm-lock.yaml

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

scripts/buildTypes.mts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import path from 'path';
44
import yargs from 'yargs';
55
import { $ } from 'execa';
66
import * as babel from '@babel/core';
7+
import { parse } from 'jsonc-parser';
78

89
const $$ = $({ stdio: 'inherit' });
910

1011
async function emitDeclarations(tsconfig: string, outDir: string) {
12+
// eslint-disable-next-line no-console
1113
console.log(`Building types for ${path.resolve(tsconfig)}`);
1214
await $$`tsc -p ${tsconfig} --outDir ${outDir} --declaration --emitDeclarationOnly`;
1315
}
1416

1517
async function addImportExtensions(folder: string) {
18+
// eslint-disable-next-line no-console
1619
console.log(`Adding import extensions`);
1720
const dtsFiles = await glob('**/*.d.ts', { absolute: true, cwd: folder });
1821
if (dtsFiles.length === 0) {
@@ -42,6 +45,7 @@ async function copyDeclarations(sourceDirectory: string, destinationDirectory: s
4245
const fullSourceDirectory = path.resolve(sourceDirectory);
4346
const fullDestinationDirectory = path.resolve(destinationDirectory);
4447

48+
// eslint-disable-next-line no-console
4549
console.log(`Copying declarations from ${fullSourceDirectory} to ${fullDestinationDirectory}`);
4650

4751
await fs.cp(fullSourceDirectory, fullDestinationDirectory, {
@@ -62,25 +66,30 @@ async function copyDeclarations(sourceDirectory: string, destinationDirectory: s
6266

6367
interface HandlerArgv {
6468
skipTsc: boolean;
69+
copy: string[];
6570
}
6671

6772
async function main(argv: HandlerArgv) {
6873
const packageRoot = process.cwd();
74+
const tsconfigPath = path.join(packageRoot, 'tsconfig.build.json');
75+
const tsconfigExists = await fs.access(tsconfigPath).then(
76+
() => true,
77+
() => false,
78+
);
79+
80+
const tsConfig = tsconfigExists
81+
? (parse(await fs.readFile(tsconfigPath, 'utf-8')) as { compilerOptions: { outDir: string } })
82+
: null;
6983

7084
const srcPath = path.join(packageRoot, 'src');
7185
const buildFolder = path.join(packageRoot, 'build');
72-
const esmFolder = path.join(buildFolder, 'esm');
73-
const modernFolder = path.join(buildFolder, 'modern');
86+
const esmOrOutDir = tsConfig?.compilerOptions.outDir
87+
? path.join(packageRoot, tsConfig.compilerOptions.outDir)
88+
: path.join(buildFolder, 'esm');
7489

75-
await copyDeclarations(srcPath, esmFolder);
90+
await copyDeclarations(srcPath, esmOrOutDir);
7691

7792
if (!argv.skipTsc) {
78-
const tsconfigPath = path.join(packageRoot, 'tsconfig.build.json');
79-
const tsconfigExists = await fs.access(tsconfigPath).then(
80-
() => true,
81-
() => false,
82-
);
83-
8493
if (!tsconfigExists) {
8594
throw new Error(
8695
'Unable to find a tsconfig to build this project. ' +
@@ -89,29 +98,36 @@ async function main(argv: HandlerArgv) {
8998
);
9099
}
91100

92-
await emitDeclarations(tsconfigPath, esmFolder);
101+
await emitDeclarations(tsconfigPath, esmOrOutDir);
93102
}
94103

95-
await addImportExtensions(esmFolder);
104+
await addImportExtensions(esmOrOutDir);
96105

97-
await copyDeclarations(esmFolder, buildFolder);
98-
await copyDeclarations(esmFolder, modernFolder);
106+
await Promise.all(
107+
argv.copy.map((copy) => copyDeclarations(esmOrOutDir, path.join(packageRoot, copy))),
108+
);
99109
}
100110

101111
yargs(process.argv.slice(2))
102-
.command<HandlerArgv>({
103-
command: '$0',
104-
description:
105-
'Builds a project with a fix for https://github.com/microsoft/TypeScript/issues/39117',
106-
builder: (command) => {
107-
return command.option('skipTsc', {
108-
type: 'boolean',
109-
default: false,
110-
describe: 'Set to `true` if you want the legacy behavior of just copying .d.ts files.',
111-
});
112+
.command<HandlerArgv>(
113+
'$0',
114+
'Builds type definition files and copies them to the specified directories with a fix for https://github.com/microsoft/TypeScript/issues/39117',
115+
(command) => {
116+
return command
117+
.option('skipTsc', {
118+
type: 'boolean',
119+
default: false,
120+
describe: 'Set to `true` if you want the legacy behavior of just copying .d.ts files.',
121+
})
122+
.option('copy', {
123+
alias: 'c',
124+
type: 'array',
125+
description: 'Directories where the type definition files should be copied',
126+
default: ['build', 'build/modern'],
127+
});
112128
},
113-
handler: main,
114-
})
129+
main,
130+
)
115131
.help()
116132
.strict(true)
117133
.version(false)

0 commit comments

Comments
 (0)