Skip to content

Commit

Permalink
fix #1234 Properly compiled .d.mts files in packages so it work with …
Browse files Browse the repository at this point in the history
…TS node16 target.
  • Loading branch information
SBoudrias committed May 20, 2023
1 parent 441c3e3 commit 5079af2
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 30 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
yarn.lock
.prettierignore
coverage
4 changes: 2 additions & 2 deletions packages/checkbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Inquirer checkbox prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -80,7 +80,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/confirm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.9",
"description": "Inquirer confirm prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -78,7 +78,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Core Inquirer prompt API",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -89,7 +89,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.9",
"description": "Inquirer multiline editor prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -79,7 +79,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/expand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.9",
"description": "Inquirer checkbox prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -79,7 +79,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.1.0",
"description": "Inquirer input text prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -81,7 +81,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/password/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Inquirer password prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -78,7 +78,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/prompts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.2.1",
"description": "Inquirer prompts, combined in a single package",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -71,7 +71,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rawlist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.1.1",
"description": "Inquirer rawlist prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -78,7 +78,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.1.5",
"description": "Inquirer select/list prompt",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -80,7 +80,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"description": "Inquirer testing utilities",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -78,7 +78,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.3",
"description": "Inquirer core TS types",
"main": "./dist/cjs/index.js",
"typings": "./dist/cjs/types/index.d.mts",
"typings": "./dist/cjs/types/index.d.cts",
"files": [
"dist/**/*"
],
Expand Down Expand Up @@ -71,7 +71,7 @@
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.mts",
"types": "./dist/cjs/types/index.d.cts",
"default": "./dist/cjs/index.js"
}
}
Expand Down
36 changes: 31 additions & 5 deletions tools/rename-ext.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import fs from 'node:fs/promises';
import { globby } from 'globby';

// Because we're using .mts files, TS compiles to .mjs files disregarding the target. So here we
// manually rename the common.js files to .js
const paths: string[] = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']);

paths.forEach(async (pathname: string) => {
// manually rename the common.js files and their imports to .js
const mjsFiles: string[] = await globby(['dist/cjs/**/*.mjs', '!**/node_modules']);
mjsFiles.forEach(async (pathname: string) => {
// 1. Rename imports
const fileContent = await fs.readFile(pathname, 'utf-8');
await fs.writeFile(pathname, fileContent.replace(/\.mjs/g, '.js'));
await fs.writeFile(
pathname,
fileContent.replace(/require\(['"]([^'"]*)\.mjs['"]\)/g, "require('$1.js')")
);

// 2. Rename files
const newPath = path.format({
...path.parse(pathname),
base: '',
Expand All @@ -19,3 +23,25 @@ paths.forEach(async (pathname: string) => {
console.log(`Renaming ${pathname} to ${newPath}...`);
await fs.rename(pathname, newPath);
});

// Similarly, we rename the .d.mts files to .d.cts. This is because Typescript `node16` target will
// masquerade as ESM otherwise.
const dmtsFiles: string[] = await globby(['dist/cjs/**/*.d.mts', '!**/node_modules']);
dmtsFiles.forEach(async (pathname: string) => {
// 1. Rename imports
const fileContent = await fs.readFile(pathname, 'utf-8');
await fs.writeFile(
pathname,
fileContent.replace(/from '([^']*)\.mjs'/g, "from '$1.d.cts'")
);

// 2. Rename files
const newPath = path.format({
...path.parse(pathname),
base: '',
ext: '.cts',
});

console.log(`Renaming ${pathname} to ${newPath}...`);
await fs.rename(pathname, newPath);
});
2 changes: 1 addition & 1 deletion tools/setup-packages.mts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ paths.forEach(async (pkgPath) => {
default: './dist/esm/index.mjs',
},
require: {
types: './dist/cjs/types/index.d.mts',
types: './dist/cjs/types/index.d.cts',
default: './dist/cjs/index.js',
},
},
Expand Down

0 comments on commit 5079af2

Please sign in to comment.