From d923d0ab966b129a0eeb07e8618d24ba6c36397c Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 8 Oct 2022 02:54:45 +0200 Subject: [PATCH] build: switch to `esbuild-plugin-version-injector` for injecting version strings (#8723) --- packages/builders/package.json | 1 + packages/builders/src/index.ts | 9 +++++++++ packages/builders/tsup.config.js | 5 ++++- packages/collection/package.json | 1 + packages/collection/src/index.ts | 9 +++++++++ packages/collection/tsup.config.js | 5 ++++- packages/rest/package.json | 1 + packages/rest/src/index.ts | 9 +++++++++ packages/rest/src/lib/utils/constants.ts | 4 +--- packages/rest/tsup.config.js | 5 ++++- packages/voice/package.json | 1 + packages/voice/src/index.ts | 9 +++++++++ .../voice/src/util/generateDependencyReport.ts | 9 +++++---- packages/voice/tsup.config.js | 5 ++++- packages/ws/package.json | 1 + packages/ws/src/index.ts | 9 +++++++++ packages/ws/src/utils/constants.ts | 3 +-- packages/ws/tsup.config.js | 2 ++ tsup.config.js | 2 ++ yarn.lock | 14 ++++++++++++++ 20 files changed, 91 insertions(+), 13 deletions(-) diff --git a/packages/builders/package.json b/packages/builders/package.json index 03fb1477c..4f38cda6f 100644 --- a/packages/builders/package.json +++ b/packages/builders/package.json @@ -68,6 +68,7 @@ "@vitest/coverage-c8": "^0.24.0", "cross-env": "^7.0.3", "downlevel-dts": "^0.10.1", + "esbuild-plugin-version-injector": "^1.0.0", "eslint": "^8.24.0", "eslint-config-neon": "^0.1.35", "prettier": "^2.7.1", diff --git a/packages/builders/src/index.ts b/packages/builders/src/index.ts index 35a04f53c..bc6054a5b 100644 --- a/packages/builders/src/index.ts +++ b/packages/builders/src/index.ts @@ -40,3 +40,12 @@ export * from './util/componentUtil.js'; export * from './util/normalizeArray.js'; export * from './util/validation.js'; export * from '@discordjs/util'; + +/** + * The [\@discordjs/builders](https://github.com/discordjs/discord.js/blob/main/packages/builders/#readme) version + * that you are currently using. + * + * Note to developers: This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild + */ +// eslint-disable-next-line @typescript-eslint/no-inferrable-types +export const version: string = '[VI]{{inject}}[/VI]'; diff --git a/packages/builders/tsup.config.js b/packages/builders/tsup.config.js index 2e679fd0a..afd45736d 100644 --- a/packages/builders/tsup.config.js +++ b/packages/builders/tsup.config.js @@ -1,3 +1,6 @@ +import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector'; import { createTsupConfig } from '../../tsup.config.js'; -export default createTsupConfig(); +export default createTsupConfig({ + esbuildPlugins: [esbuildPluginVersionInjector()], +}); diff --git a/packages/collection/package.json b/packages/collection/package.json index 06924385d..bfe5cbfa7 100644 --- a/packages/collection/package.json +++ b/packages/collection/package.json @@ -56,6 +56,7 @@ "@vitest/coverage-c8": "^0.24.0", "cross-env": "^7.0.3", "downlevel-dts": "^0.10.1", + "esbuild-plugin-version-injector": "^1.0.0", "eslint": "^8.24.0", "eslint-config-neon": "^0.1.35", "prettier": "^2.7.1", diff --git a/packages/collection/src/index.ts b/packages/collection/src/index.ts index da5b1e467..d732240b0 100644 --- a/packages/collection/src/index.ts +++ b/packages/collection/src/index.ts @@ -1 +1,10 @@ export * from './collection.js'; + +/** + * The [\@discordjs/collection](https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme) version + * that you are currently using. + * + * Note to developers: This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild + */ +// eslint-disable-next-line @typescript-eslint/no-inferrable-types +export const version: string = '[VI]{{inject}}[/VI]'; diff --git a/packages/collection/tsup.config.js b/packages/collection/tsup.config.js index 2e679fd0a..afd45736d 100644 --- a/packages/collection/tsup.config.js +++ b/packages/collection/tsup.config.js @@ -1,3 +1,6 @@ +import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector'; import { createTsupConfig } from '../../tsup.config.js'; -export default createTsupConfig(); +export default createTsupConfig({ + esbuildPlugins: [esbuildPluginVersionInjector()], +}); diff --git a/packages/rest/package.json b/packages/rest/package.json index 65c6537f5..3b7e7c513 100644 --- a/packages/rest/package.json +++ b/packages/rest/package.json @@ -68,6 +68,7 @@ "@vitest/coverage-c8": "^0.24.0", "cross-env": "^7.0.3", "downlevel-dts": "^0.10.1", + "esbuild-plugin-version-injector": "^1.0.0", "eslint": "^8.24.0", "eslint-config-neon": "^0.1.35", "prettier": "^2.7.1", diff --git a/packages/rest/src/index.ts b/packages/rest/src/index.ts index ab6fa949b..fe2a0575e 100644 --- a/packages/rest/src/index.ts +++ b/packages/rest/src/index.ts @@ -6,3 +6,12 @@ export * from './lib/RequestManager.js'; export * from './lib/REST.js'; export * from './lib/utils/constants.js'; export { makeURLSearchParams, parseResponse } from './lib/utils/utils.js'; + +/** + * The [\@discordjs/rest](https://github.com/discordjs/discord.js/blob/main/packages/rest/#readme) version + * that you are currently using. + * + * Note to developers: This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild + */ +// eslint-disable-next-line @typescript-eslint/no-inferrable-types +export const version: string = '[VI]{{inject}}[/VI]'; diff --git a/packages/rest/src/lib/utils/constants.ts b/packages/rest/src/lib/utils/constants.ts index 0a5854cf2..2057ae259 100644 --- a/packages/rest/src/lib/utils/constants.ts +++ b/packages/rest/src/lib/utils/constants.ts @@ -2,10 +2,8 @@ import process from 'node:process'; import { APIVersion } from 'discord-api-types/v10'; import { Agent } from 'undici'; import type { RESTOptions } from '../REST.js'; -// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -const Package = require('../../../package.json'); -export const DefaultUserAgent = `DiscordBot (${Package.homepage}, ${Package.version})`; +export const DefaultUserAgent = `DiscordBot (https://discord.js.org, [VI]{{inject}}[/VI])`; export const DefaultRestOptions: Required = { get agent() { diff --git a/packages/rest/tsup.config.js b/packages/rest/tsup.config.js index 2e679fd0a..afd45736d 100644 --- a/packages/rest/tsup.config.js +++ b/packages/rest/tsup.config.js @@ -1,3 +1,6 @@ +import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector'; import { createTsupConfig } from '../../tsup.config.js'; -export default createTsupConfig(); +export default createTsupConfig({ + esbuildPlugins: [esbuildPluginVersionInjector()], +}); diff --git a/packages/voice/package.json b/packages/voice/package.json index 189aa3184..f5eee15ba 100644 --- a/packages/voice/package.json +++ b/packages/voice/package.json @@ -68,6 +68,7 @@ "@types/node": "^16.11.64", "cross-env": "^7.0.3", "downlevel-dts": "^0.10.1", + "esbuild-plugin-version-injector": "^1.0.0", "eslint": "^8.24.0", "eslint-config-neon": "^0.1.35", "jest": "^29.1.2", diff --git a/packages/voice/src/index.ts b/packages/voice/src/index.ts index 2ed09b69e..eb2ffb7f7 100644 --- a/packages/voice/src/index.ts +++ b/packages/voice/src/index.ts @@ -19,3 +19,12 @@ export { } from './VoiceConnection'; export { type JoinConfig, getVoiceConnection, getVoiceConnections, getGroups } from './DataStore'; + +/** + * The [\@discordjs/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/#readme) version + * that you are currently using. + * + * Note to developers: This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild + */ +// eslint-disable-next-line @typescript-eslint/no-inferrable-types +export const version: string = '[VI]{{inject}}[/VI]'; diff --git a/packages/voice/src/util/generateDependencyReport.ts b/packages/voice/src/util/generateDependencyReport.ts index d8808bca1..2d8455d20 100644 --- a/packages/voice/src/util/generateDependencyReport.ts +++ b/packages/voice/src/util/generateDependencyReport.ts @@ -33,10 +33,11 @@ function findPackageJSON( */ function version(name: string): string { try { - const pkg = - name === '@discordjs/voice' - ? require('../../package.json') - : findPackageJSON(dirname(require.resolve(name)), name, 3); + if (name === '@discordjs/voice') { + return '[VI]{{inject}}[/VI]'; + } + + const pkg = findPackageJSON(dirname(require.resolve(name)), name, 3); return pkg?.version ?? 'not found'; } catch { return 'not found'; diff --git a/packages/voice/tsup.config.js b/packages/voice/tsup.config.js index 2e679fd0a..afd45736d 100644 --- a/packages/voice/tsup.config.js +++ b/packages/voice/tsup.config.js @@ -1,3 +1,6 @@ +import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector'; import { createTsupConfig } from '../../tsup.config.js'; -export default createTsupConfig(); +export default createTsupConfig({ + esbuildPlugins: [esbuildPluginVersionInjector()], +}); diff --git a/packages/ws/package.json b/packages/ws/package.json index 2334dbe4a..f17a2d12f 100644 --- a/packages/ws/package.json +++ b/packages/ws/package.json @@ -69,6 +69,7 @@ "@vitest/coverage-c8": "^0.24.0", "cross-env": "^7.0.3", "downlevel-dts": "^0.10.1", + "esbuild-plugin-version-injector": "^1.0.0", "eslint": "^8.24.0", "eslint-config-neon": "^0.1.35", "mock-socket": "^9.1.5", diff --git a/packages/ws/src/index.ts b/packages/ws/src/index.ts index 17a0b45c9..d50275398 100644 --- a/packages/ws/src/index.ts +++ b/packages/ws/src/index.ts @@ -11,3 +11,12 @@ export * from './utils/IdentifyThrottler.js'; export * from './ws/WebSocketManager.js'; export * from './ws/WebSocketShard.js'; + +/** + * The [\@discordjs/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/#readme) version + * that you are currently using. + * + * Note to developers: This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild + */ +// eslint-disable-next-line @typescript-eslint/no-inferrable-types +export const version: string = '[VI]{{inject}}[/VI]'; diff --git a/packages/ws/src/utils/constants.ts b/packages/ws/src/utils/constants.ts index 3856605ed..72d20ec65 100644 --- a/packages/ws/src/utils/constants.ts +++ b/packages/ws/src/utils/constants.ts @@ -2,7 +2,6 @@ import process from 'node:process'; import { Collection } from '@discordjs/collection'; import { lazy } from '@discordjs/util'; import { APIVersion, GatewayOpcodes } from 'discord-api-types/v10'; -import { version } from '../../package.json'; import type { OptionalWebSocketManagerOptions, SessionInfo } from '../ws/WebSocketManager.js'; /** @@ -19,7 +18,7 @@ export enum CompressionMethod { ZlibStream = 'zlib-stream', } -export const DefaultDeviceProperty = `@discordjs/ws ${version}`; +export const DefaultDeviceProperty = `@discordjs/ws [VI]{{inject}}[/VI]`; const getDefaultSessionStore = lazy(() => new Collection()); diff --git a/packages/ws/tsup.config.js b/packages/ws/tsup.config.js index 5ef6ebd92..3245f1430 100644 --- a/packages/ws/tsup.config.js +++ b/packages/ws/tsup.config.js @@ -1,3 +1,4 @@ +import { esbuildPluginVersionInjector } from 'esbuild-plugin-version-injector'; import { createTsupConfig } from '../../tsup.config.js'; export default createTsupConfig({ @@ -6,4 +7,5 @@ export default createTsupConfig({ worker: 'src/strategies/sharding/worker.ts', }, external: ['zlib-sync'], + esbuildPlugins: [esbuildPluginVersionInjector()], }); diff --git a/tsup.config.js b/tsup.config.js index dc60e3984..39741bb06 100644 --- a/tsup.config.js +++ b/tsup.config.js @@ -15,6 +15,7 @@ export function createTsupConfig({ keepNames = true, dts = true, sourcemap = true, + esbuildPlugins = [] } = {}) { return defineConfig({ entry, @@ -31,5 +32,6 @@ export function createTsupConfig({ keepNames, dts, sourcemap, + esbuildPlugins }); } diff --git a/yarn.lock b/yarn.lock index f028e23f2..1aa8f8301 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2029,6 +2029,7 @@ __metadata: cross-env: ^7.0.3 discord-api-types: ^0.37.12 downlevel-dts: ^0.10.1 + esbuild-plugin-version-injector: ^1.0.0 eslint: ^8.24.0 eslint-config-neon: ^0.1.35 fast-deep-equal: ^3.1.3 @@ -2051,6 +2052,7 @@ __metadata: "@vitest/coverage-c8": ^0.24.0 cross-env: ^7.0.3 downlevel-dts: ^0.10.1 + esbuild-plugin-version-injector: ^1.0.0 eslint: ^8.24.0 eslint-config-neon: ^0.1.35 prettier: ^2.7.1 @@ -2207,6 +2209,7 @@ __metadata: cross-env: ^7.0.3 discord-api-types: ^0.37.12 downlevel-dts: ^0.10.1 + esbuild-plugin-version-injector: ^1.0.0 eslint: ^8.24.0 eslint-config-neon: ^0.1.35 file-type: ^18.0.0 @@ -2308,6 +2311,7 @@ __metadata: cross-env: ^7.0.3 discord-api-types: ^0.37.12 downlevel-dts: ^0.10.1 + esbuild-plugin-version-injector: ^1.0.0 eslint: ^8.24.0 eslint-config-neon: ^0.1.35 jest: ^29.1.2 @@ -2395,6 +2399,7 @@ __metadata: cross-env: ^7.0.3 discord-api-types: ^0.37.12 downlevel-dts: ^0.10.1 + esbuild-plugin-version-injector: ^1.0.0 eslint: ^8.24.0 eslint-config-neon: ^0.1.35 mock-socket: ^9.1.5 @@ -8654,6 +8659,15 @@ __metadata: languageName: node linkType: hard +"esbuild-plugin-version-injector@npm:^1.0.0": + version: 1.0.0 + resolution: "esbuild-plugin-version-injector@npm:1.0.0" + dependencies: + "@sapphire/result": ^2.5.0 + checksum: 2a6eb3f2b3d3d24c6ca8af28925ad8e6f6d0a75f41cdb089bd76c7a74fbd6948d8f64563aa58bd65751054a28be0cfc081a0f56102a1952a5ba4ac541c2ff336 + languageName: node + linkType: hard + "esbuild-sunos-64@npm:0.14.47": version: 0.14.47 resolution: "esbuild-sunos-64@npm:0.14.47"