fix: export "ESM" types when discord.js is imported in ESM land (#10009)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Vlad Frangu
2023-12-01 01:19:22 +02:00
committed by GitHub
parent 30f6a5fc56
commit e412a22ceb
6 changed files with 58 additions and 4 deletions

View File

@@ -202,7 +202,7 @@
} }
}, },
{ {
"files": ["typings/*.ts"], "files": ["typings/*.ts", "scripts/*.mjs"],
"parser": "@typescript-eslint/parser", "parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"], "plugins": ["@typescript-eslint"],
"rules": { "rules": {

View File

@@ -25,3 +25,7 @@ docs/**/*
# Miscellaneous # Miscellaneous
.turbo .turbo
.tmp .tmp
# Generated files
typings/index.d.mts
typings/rawDataTypes.d.mts

View File

@@ -12,12 +12,24 @@
"docs": "docgen -i './src/*.js' './src/**/*.js' -c ./docs/index.json -r ../../ -o ./docs/docs.json && pnpm run docs:new", "docs": "docgen -i './src/*.js' './src/**/*.js' -c ./docs/index.json -r ../../ -o ./docs/docs.json && pnpm run docs:new",
"docs:test": "docgen -i './src/*.js' './src/**/*.js' -c ./docs/index.json -r ../../", "docs:test": "docgen -i './src/*.js' './src/**/*.js' -c ./docs/index.json -r ../../",
"docs:new": "api-extractor run --local --minify", "docs:new": "api-extractor run --local --minify",
"prepack": "pnpm run lint && pnpm run test", "prepack": "pnpm run lint && pnpm run test && node ./scripts/esmDts.mjs",
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/discord.js/*'", "changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/discord.js/*'",
"release": "cliff-jumper" "release": "cliff-jumper"
}, },
"main": "./src/index.js", "main": "./src/index.js",
"types": "./typings/index.d.ts", "types": "./typings/index.d.ts",
"exports": {
".": {
"import": {
"types": "./typings/index.d.mts",
"default": "./src/index.js"
},
"require": {
"types": "./typings/index.d.ts",
"default": "./src/index.js"
}
}
},
"directories": { "directories": {
"lib": "src", "lib": "src",
"test": "test" "test": "test"

View File

@@ -0,0 +1,38 @@
import { readFile, writeFile } from 'node:fs/promises';
const rawTypesDTS = new URL('../typings/rawDataTypes.d.ts', import.meta.url);
const rawIndexDTS = new URL('../typings/index.d.ts', import.meta.url);
const rawTypesMDTS = new URL('../typings/rawDataTypes.d.mts', import.meta.url);
const rawIndexMTS = new URL('../typings/index.d.mts', import.meta.url);
const [rawTypesString, rawIndexString] = await Promise.all([
readFile(rawTypesDTS, 'utf8'),
readFile(rawIndexDTS, 'utf8'),
]);
/**
*
* @param {string} source
* @param {[from: string, to: string][]} imports
*/
function updateImports(source, imports) {
return imports.reduce((code, [from, to]) => {
return code.replaceAll(from, to);
}, source);
}
/** @type {[string, string][]} */
const rawTypesImports = [
['./index.js', './index.mjs'], //
];
/** @type {[string, string][]} */
const rawIndexImports = [
['./rawDataTypes.js', './rawDataTypes.mjs'], //
];
const rawTypesMDTSString = updateImports(rawTypesString, rawTypesImports);
const rawIndexMTSString = updateImports(rawIndexString, rawIndexImports);
await Promise.all([writeFile(rawTypesMDTS, rawTypesMDTSString), writeFile(rawIndexMTS, rawIndexMTSString)]);

View File

@@ -231,7 +231,7 @@ import {
RawWelcomeScreenData, RawWelcomeScreenData,
RawWidgetData, RawWidgetData,
RawWidgetMemberData, RawWidgetMemberData,
} from './rawDataTypes'; } from './rawDataTypes.js';
declare module 'node:events' { declare module 'node:events' {
class EventEmitter { class EventEmitter {

View File

@@ -77,7 +77,7 @@ import {
Snowflake, Snowflake,
APIGuildScheduledEvent, APIGuildScheduledEvent,
} from 'discord-api-types/v10'; } from 'discord-api-types/v10';
import { GuildChannel, Guild, PermissionOverwrites } from '.'; import { GuildChannel, Guild, PermissionOverwrites } from './index.js';
export type RawActivityData = GatewayActivity; export type RawActivityData = GatewayActivity;