Skip to content

Commit

Permalink
fix: export "ESM" types when discord.js is imported in ESM land (#10009)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
vladfrangu and kodiakhq[bot] committed Nov 30, 2023
1 parent 30f6a5f commit e412a22
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/discord.js/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
}
},
{
"files": ["typings/*.ts"],
"files": ["typings/*.ts", "scripts/*.mjs"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"rules": {
Expand Down
4 changes: 4 additions & 0 deletions packages/discord.js/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ docs/**/*
# Miscellaneous
.turbo
.tmp

# Generated files
typings/index.d.mts
typings/rawDataTypes.d.mts
14 changes: 13 additions & 1 deletion packages/discord.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:test": "docgen -i './src/*.js' './src/**/*.js' -c ./docs/index.json -r ../../",
"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/*'",
"release": "cliff-jumper"
},
"main": "./src/index.js",
"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": {
"lib": "src",
"test": "test"
Expand Down
38 changes: 38 additions & 0 deletions packages/discord.js/scripts/esmDts.mjs
Original file line number Diff line number Diff line change
@@ -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)]);
2 changes: 1 addition & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ import {
RawWelcomeScreenData,
RawWidgetData,
RawWidgetMemberData,
} from './rawDataTypes';
} from './rawDataTypes.js';

declare module 'node:events' {
class EventEmitter {
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/typings/rawDataTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import {
Snowflake,
APIGuildScheduledEvent,
} from 'discord-api-types/v10';
import { GuildChannel, Guild, PermissionOverwrites } from '.';
import { GuildChannel, Guild, PermissionOverwrites } from './index.js';

export type RawActivityData = GatewayActivity;

Expand Down

0 comments on commit e412a22

Please sign in to comment.