Skip to content

Commit

Permalink
Duplicate .d.ts files into cjs (#1147)
Browse files Browse the repository at this point in the history
* Duplicate .d.ts files into cjs

* 0.81.1
  • Loading branch information
TarikGul authored Oct 18, 2024
1 parent 4bda725 commit 0293540
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.81.1

- Duplicate .d.ts files into cjs


## 0.80.1

- Typescript 5.5.4
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.80.1",
"version": "0.81.0",
"versions": {
"git": "0.80.1",
"npm": "0.80.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.80.1",
"version": "0.81.0",
"main": "./index.js",
"exports": {
"./globals.d.ts": "./src/globals.d.ts"
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.80.1",
"version": "0.81.0",
"main": "./index.js",
"exports": {
"./globals.d.ts": "./src/globals.d.ts"
Expand Down
6 changes: 3 additions & 3 deletions packages/dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"sideEffects": false,
"type": "module",
"version": "0.80.1",
"version": "0.81.0",
"bin": {
"polkadot-ci-ghact-build": "./scripts/polkadot-ci-ghact-build.mjs",
"polkadot-ci-ghact-docs": "./scripts/polkadot-ci-ghact-docs.mjs",
Expand Down Expand Up @@ -50,8 +50,8 @@
},
"dependencies": {
"@eslint/js": "^8.56.0",
"@polkadot/dev-test": "^0.80.1",
"@polkadot/dev-ts": "^0.80.1",
"@polkadot/dev-test": "^0.81.0",
"@polkadot/dev-ts": "^0.81.0",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
Expand Down
33 changes: 27 additions & 6 deletions packages/dev/scripts/polkadot-dev-build-ts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,12 @@ function relativePath (value) {
function createMapEntry (rootDir, jsPath = '', noTypes) {
jsPath = relativePath(jsPath);

const typesPath = jsPath.replace('.js', '.d.ts');
const cjsPath = jsPath.replace('./', './cjs/');
const cjsTypesPath = typesPath.replace('./', './cjs/');
const hasCjs = fs.existsSync(path.join(rootDir, cjsPath));
const typesPath = jsPath.replace('.js', '.d.ts');
const hasTypes = !noTypes && jsPath.endsWith('.js') && fs.existsSync(path.join(rootDir, typesPath));
const hasTypesCjs = fs.existsSync(path.join(rootDir, cjsTypesPath));
const hasTypes = !hasTypesCjs && !noTypes && jsPath.endsWith('.js') && fs.existsSync(path.join(rootDir, typesPath));
const field = hasCjs
? {
// As per TS, the types key needs to be first
Expand All @@ -453,12 +455,21 @@ function createMapEntry (rootDir, jsPath = '', noTypes) {
// bundler-specific path, eg. webpack & rollup
...(
jsPath.endsWith('.js')
? { module: jsPath }
? hasTypesCjs
// eslint-disable-next-line sort-keys
? { module: { types: typesPath, default: jsPath } }
: { module: jsPath }
: {}
),
require: cjsPath,
require: hasTypesCjs
// eslint-disable-next-line sort-keys
? { types: cjsTypesPath, default: cjsPath }
: cjsPath,
// eslint-disable-next-line sort-keys
default: hasTypesCjs
// eslint-disable-next-line sort-keys
default: jsPath
? { types: typesPath, default: jsPath }
: jsPath
}
: hasTypes
? {
Expand Down Expand Up @@ -496,7 +507,10 @@ function copyBuildFiles (compileType, dir) {
copyDirSync('src', 'build', ['.patch', '.js', '.cjs', '.mjs', '.json', '.d.ts', '.d.cts', '.d.mts', '.css', '.gif', '.hbs', '.md', '.jpg', '.png', '.rs', '.svg']);

// copy all *.d.ts files
copyDirSync([path.join('../../build', dir, 'src'), path.join('../../build/packages', dir, 'src')], 'build', ['.d.ts']);
const dtsPaths = ['build-tsc', path.join('../../build', dir, 'src'), path.join('../../build/packages', dir, 'src')];

copyDirSync(dtsPaths, 'build', ['.d.ts']);
copyDirSync(dtsPaths, 'build/cjs', ['.d.ts']);

// copy all from build-{babel|swc|tsc|...}-esm to build
copyDirSync(`build-${compileType}-esm`, 'build');
Expand Down Expand Up @@ -787,6 +801,8 @@ function buildExports () {

pkg.exports = listRoot
.filter(([path, config]) =>
// skip d.ts files
!path.endsWith('.d.ts') &&
// we handle the CJS path at the root below
path !== './cjs/package.json' &&
// we don't export ./deno/* paths (e.g. wasm)
Expand Down Expand Up @@ -1255,6 +1271,11 @@ async function buildJs (compileType, repoPath, dir, locals) {

orderPackageJson(repoPath, dir, pkgJson);

// move the tsc-generated *.d.ts build files to build-tsc
if (fs.existsSync('build')) {
copyDirSync('build', 'build-tsc', ['.d.ts']);
}

if (!fs.existsSync(path.join(process.cwd(), '.skip-build'))) {
const srcHeader = `// Copyright 2017-${new Date().getFullYear()} ${name} authors & contributors\n// SPDX-License-Identifier: Apache-2.0\n`;
const genHeader = `${srcHeader}\n// Do not edit, auto-generated by @polkadot/dev\n`;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ __metadata:
languageName: node
linkType: hard

"@polkadot/dev-test@npm:^0.80.1, @polkadot/dev-test@workspace:packages/dev-test":
"@polkadot/dev-test@npm:^0.81.0, @polkadot/dev-test@workspace:packages/dev-test":
version: 0.0.0-use.local
resolution: "@polkadot/dev-test@workspace:packages/dev-test"
dependencies:
Expand All @@ -443,7 +443,7 @@ __metadata:
languageName: unknown
linkType: soft

"@polkadot/dev-ts@npm:^0.80.1, @polkadot/dev-ts@workspace:packages/dev-ts":
"@polkadot/dev-ts@npm:^0.81.0, @polkadot/dev-ts@workspace:packages/dev-ts":
version: 0.0.0-use.local
resolution: "@polkadot/dev-ts@workspace:packages/dev-ts"
dependencies:
Expand All @@ -458,8 +458,8 @@ __metadata:
resolution: "@polkadot/dev@workspace:packages/dev"
dependencies:
"@eslint/js": "npm:^8.56.0"
"@polkadot/dev-test": "npm:^0.80.1"
"@polkadot/dev-ts": "npm:^0.80.1"
"@polkadot/dev-test": "npm:^0.81.0"
"@polkadot/dev-ts": "npm:^0.81.0"
"@rollup/plugin-alias": "npm:^5.1.0"
"@rollup/plugin-commonjs": "npm:^25.0.7"
"@rollup/plugin-dynamic-import-vars": "npm:^2.1.2"
Expand Down

0 comments on commit 0293540

Please sign in to comment.