Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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