Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 872b39f

Browse files
authored
fix(package): Fix module declaration names in dist d.ts files (#4476)
1 parent 569c96b commit 872b39f

File tree

1 file changed

+15
-49
lines changed

1 file changed

+15
-49
lines changed

scripts/cp-pkgs.js

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const {spawnSync} = require('child_process');
3535
const {sync: globSync} = require('glob');
3636

3737
const ALL_IN_ONE_PACKAGE = 'material-components-web';
38-
const DECLARATION_FILE_PREFIX = 'mdc-';
3938
const D_TS_DIRECTORY = path.resolve(__dirname, '../build/packages');
39+
const PACKAGES_DIRECTORY = path.resolve(__dirname, '../packages');
4040
const PKG_RE = /(?:material\-components\-web)|(?:mdc\.[a-zA-Z\-]+)/;
4141

4242
const isValidCwd = (
@@ -76,7 +76,7 @@ function cleanPkgDistDirs() {
7676

7777
/**
7878
* @param {string} asset filepath relative to the root directory
79-
* @returns {string} destination package name
79+
* @return {string} destination package name
8080
*/
8181
function getAssetEntry(asset) {
8282
const [entryName] = path.parse(asset).name.match(PKG_RE);
@@ -90,7 +90,7 @@ function getAssetEntry(asset) {
9090

9191
/**
9292
* @param {string} asset filepath relative to the root directory
93-
* @returns {Promise<void>}
93+
* @return {Promise<void>}
9494
*/
9595
async function cpAsset(asset) {
9696
const assetPkg = path.join('packages', getAssetEntry(asset));
@@ -107,53 +107,27 @@ async function cpAsset(asset) {
107107
}
108108

109109
/**
110-
* Imports all files in index.d.ts and compiles a bundled .d.ts file for
111-
* UMD files.
110+
* Imports all files in index.d.ts and compiles a bundled .d.ts file for UMD bundles.
112111
*/
113112
function dtsBundler() {
114113
const packageDirectories = fs.readdirSync(D_TS_DIRECTORY);
115114
packageDirectories.forEach((packageDirectory) => {
116-
const main = path.resolve(D_TS_DIRECTORY, packageDirectory, './index.d.ts');
115+
const packagePath = path.join(PACKAGES_DIRECTORY, packageDirectory);
116+
const name = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json'), 'utf8')).name;
117+
const main = path.join(D_TS_DIRECTORY, packageDirectory, './index.d.ts');
118+
const isAllInOne = packageDirectory === ALL_IN_ONE_PACKAGE;
119+
const destBasename = isAllInOne ? packageDirectory : `mdc.${toCamelCase(packageDirectory.replace(/^mdc-/, ''))}`;
120+
const destFilename = path.join(packagePath, 'dist', `${destBasename}.d.ts`);
121+
122+
console.log(`Writing UMD declarations in ${destFilename.replace(process.cwd() + '/', '')}`);
117123
dts.bundle({
118-
name: packageDirectory,
124+
name,
119125
main,
126+
out: destFilename,
120127
});
121128
});
122129
}
123130

124-
/**
125-
* @param {string} asset filepath relative to the root directory
126-
* @param {string} assetPkg directory path to destination package
127-
* @returns {string} destination filepath for UMD declaration file.
128-
*/
129-
function getDeclarationFileName(asset, assetPkg) {
130-
const packageName = path.parse(asset).name.replace(/^mdc-|\.d$/g, '');
131-
const isAllInOne = packageName === ALL_IN_ONE_PACKAGE;
132-
const destFileName = isAllInOne ? packageName : `mdc.${toCamelCase(packageName)}`;
133-
return path.join(assetPkg, 'dist', `${destFileName}.d.ts`);
134-
}
135-
136-
/**
137-
* Copies the declaration file from the /build directory to its respective
138-
* destination directory.
139-
* @param {string} asset filepath relative to the root directory
140-
* @returns {Promise<void>}
141-
*/
142-
async function cpDeclarationAsset(asset) {
143-
const assetPkg = path.parse(asset.split('build/')[1]).dir;
144-
if (!fs.existsSync(assetPkg)) {
145-
return Promise.reject(new Error(`Non-existent asset package path ${assetPkg} for ${asset}`));
146-
}
147-
148-
const destDir = getDeclarationFileName(asset, assetPkg);
149-
return cpFile(asset, destDir).then(
150-
() => console.log(`cp ${asset} -> ${destDir}`),
151-
(err) => {
152-
throw err;
153-
}
154-
);
155-
}
156-
157131
/*
158132
* 1. Cleans all /dist directories in each package
159133
* 2. Copies generated css, js, and map files to the respective packages
@@ -167,13 +141,5 @@ Promise.all(globSync('build/*.{css,js,map}').map(cpAsset)).catch((err) => {
167141
process.exit(1);
168142
});
169143

170-
// this method builds the files that the next lines copy to each package
144+
// Build d.ts files for each UMD bundle and copy into each package's dist folder
171145
dtsBundler();
172-
173-
Promise.all(
174-
globSync(`build/packages/**/{${DECLARATION_FILE_PREFIX},${ALL_IN_ONE_PACKAGE}}*.d.ts`)
175-
.map(cpDeclarationAsset)
176-
).catch((err) => {
177-
console.error('Error encountered copying assets:', err);
178-
process.exit(1);
179-
});

0 commit comments

Comments
 (0)