Skip to content

Commit a80e7b7

Browse files
brijeshb42Brijesh Bittu
and
Brijesh Bittu
authored
[core] Update scripts to support base-ui (#45784)
Co-authored-by: Brijesh Bittu <[email protected]>
1 parent 11f7526 commit a80e7b7

File tree

6 files changed

+88
-32
lines changed

6 files changed

+88
-32
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module.exports = /** @type {Config} */ ({
6464
'plugin:eslint-plugin-import/recommended',
6565
'plugin:eslint-plugin-import/typescript',
6666
'eslint-config-airbnb',
67-
'./eslint/config-airbnb-typescript.js',
67+
require.resolve('./eslint/config-airbnb-typescript.js'),
6868
'eslint-config-prettier',
6969
],
7070
parser: '@typescript-eslint/parser',

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/build.mjs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ const validBundles = [
1717
'stable',
1818
];
1919

20+
const bundleTypes = {
21+
modern: 'module',
22+
stable: 'module',
23+
node: 'commonjs',
24+
};
25+
2026
async function run(argv) {
21-
const { bundle, largeFiles, outDir: outDirBase, verbose } = argv;
27+
const { bundle, largeFiles, outDir: outDirBase, verbose, cjsDir } = argv;
2228

2329
if (!validBundles.includes(bundle)) {
2430
throw new TypeError(
@@ -53,7 +59,7 @@ async function run(argv) {
5359
const outFileExtension = '.js';
5460

5561
const relativeOutDir = {
56-
node: './',
62+
node: cjsDir,
5763
modern: './modern',
5864
stable: './esm',
5965
}[bundle];
@@ -107,12 +113,15 @@ async function run(argv) {
107113
// `--extensions-.cjs --out-file-extension .cjs`
108114
await cjsCopy({ from: srcDir, to: outDir });
109115

110-
const isEsm = bundle === 'modern' || bundle === 'stable';
111-
if (isEsm && !argv.skipEsmPkg) {
116+
// Write a package.json file in the output directory if we are building the modern or stable bundle
117+
// or if the output directory is not the root of the package.
118+
const shouldWriteBundlePackageJson =
119+
bundle === 'modern' || bundle === 'stable' || relativeOutDir !== './';
120+
if (shouldWriteBundlePackageJson && !argv.skipEsmPkg) {
112121
const rootBundlePackageJson = path.join(outDir, 'package.json');
113122
await fs.writeFile(
114123
rootBundlePackageJson,
115-
JSON.stringify({ type: 'module', sideEffects: packageJson.sideEffects }),
124+
JSON.stringify({ type: bundleTypes[bundle], sideEffects: packageJson.sideEffects }),
116125
);
117126
}
118127

@@ -143,6 +152,11 @@ yargs(process.argv.slice(2))
143152
describe:
144153
"Set to `true` if you don't want to generate a package.json file in the /esm folder.",
145154
})
155+
.option('cjsDir', {
156+
default: './',
157+
type: 'string',
158+
description: 'The directory to copy the cjs files to.',
159+
})
146160
.option('out-dir', { default: './build', type: 'string' })
147161
.option('verbose', { type: 'boolean' });
148162
},

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)

scripts/utils.mjs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
import path from 'path';
22
import url from 'url';
3+
import fs from 'fs';
4+
5+
function findUpFile(fileName, cwd = process.cwd(), maxIterations = 5) {
6+
const pathName = path.join(cwd, fileName);
7+
if (fs.existsSync(pathName)) {
8+
return pathName;
9+
}
10+
if (maxIterations === 0) {
11+
return null;
12+
}
13+
return findUpFile(fileName, path.dirname(cwd), maxIterations - 1);
14+
}
315

416
/**
5-
* Returns the full path of the root directory of this repository.
17+
* Returns the full path of the root directory of the monorepo.
618
*/
719
// eslint-disable-next-line import/prefer-default-export
820
export function getWorkspaceRoot() {
21+
// Use this when available. Avoids the need to check for the workspace file.
22+
if (process.env.NX_WORKSPACE_ROOT) {
23+
return process.env.NX_WORKSPACE_ROOT;
24+
}
25+
26+
const workspaceFilePath = findUpFile('pnpm-workspace.yaml', process.cwd());
27+
if (workspaceFilePath) {
28+
return path.dirname(workspaceFilePath);
29+
}
30+
931
const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));
1032
const workspaceRoot = path.resolve(currentDirectory, '..');
1133
return workspaceRoot;

0 commit comments

Comments
 (0)