build: tsup for better cjs / esm support

This commit is contained in:
iCrawl
2022-09-02 18:08:16 +02:00
parent b7eb96d456
commit df46ab8061
40 changed files with 759 additions and 1067 deletions

View File

@@ -1,92 +0,0 @@
import { relative, resolve } from 'node:path';
import glob from 'fast-glob';
import isCi from 'is-ci';
import typescript from 'rollup-plugin-typescript2';
import { defineBuildConfig, type BuildEntry } from 'unbuild';
interface ConfigOptions {
cjsBridge: boolean;
declaration: boolean;
emitCJS: boolean;
entries: (BuildEntry | string)[];
externals: string[];
minify: boolean;
preserveModules: boolean;
preserveModulesRoot: string;
sourcemap: boolean;
typeCheck: boolean;
}
export function createUnbuildConfig({
entries = [{ builder: 'rollup', input: 'src/index' }],
minify = false,
emitCJS = true,
cjsBridge = true,
externals = [],
sourcemap = true,
preserveModules = true,
preserveModulesRoot = 'src',
declaration = true,
typeCheck = isCi,
}: Partial<ConfigOptions> = {}) {
const files = glob
.sync('**', { cwd: 'src' })
.flatMap((file) => [`${file.slice(0, -2)}cjs`, `${file.slice(0, -2)}mjs`]);
return defineBuildConfig({
entries,
clean: true,
rollup: {
esbuild: {
minify,
minifyIdentifiers: false,
},
emitCJS,
cjsBridge,
json: {
namedExports: false,
preferConst: true,
},
},
externals: [...files, ...externals],
hooks: {
'rollup:options': (_, options) => {
// @ts-expect-error: This will always be an array
options.output![0] = {
// @ts-expect-error: This will always be an array
...options.output![0],
sourcemap,
preserveModules,
preserveModulesRoot,
};
if (emitCJS) {
// @ts-expect-error: This will always be an array
options.output![1] = {
// @ts-expect-error: This will always be an array
...options.output![1],
sourcemap,
preserveModules,
preserveModulesRoot,
};
}
if (declaration) {
options.plugins?.unshift(
typescript({
check: typeCheck,
tsconfig: relative(__dirname, resolve(process.cwd(), 'tsconfig.json')),
tsconfigOverride: {
compilerOptions: {
emitDeclarationOnly: true,
},
},
}),
);
}
},
},
});
}

View File

@@ -41,13 +41,12 @@
"@commitlint/config-angular": "^17.1.0",
"@favware/cliff-jumper": "^1.8.7",
"@favware/npm-deprecate": "^1.0.5",
"@types/is-ci": "^3.0.0",
"conventional-changelog-cli": "^2.2.2",
"fast-glob": "^3.2.11",
"husky": "^8.0.1",
"is-ci": "^3.0.1",
"lint-staged": "^13.0.3",
"turbo": "^1.4.3",
"tsup": "^6.2.3",
"turbo": "^1.4.4",
"typescript": "^4.8.2"
},
"resolutions": {

View File

@@ -1,11 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig({
entries: [
{ builder: 'rollup', input: 'src/index' },
{ builder: 'rollup', input: 'src/formatTag/index' },
],
preserveModules: false,
minify: true,
emitCJS: false,
});

View File

@@ -5,7 +5,7 @@
"private": true,
"scripts": {
"test": "vitest run",
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
"fmt": "yarn format"
@@ -48,9 +48,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -0,0 +1,7 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig({
entry: ['src/index.ts', 'src/formatTag/index.ts'],
format: ['esm'],
minify: true,
});

View File

@@ -4,12 +4,13 @@
"description": "Utilities for api-extractor",
"private": true,
"scripts": {
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src --ext mjs,js,ts --fix",
"fmt": "yarn format"
},
"main": "./dist/index.mjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"directories": {
"lib": "src"
@@ -38,9 +39,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"typescript": "^4.8.2",
"unbuild": "^0.8.9"
"tsup": "^6.2.3",
"typescript": "^4.8.2"
},
"engines": {
"node": ">=16.9.0"

View File

@@ -0,0 +1,5 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig({
minify: true,
});

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig();

View File

@@ -4,7 +4,7 @@
"description": "A set of builders that you can use when creating your bot",
"scripts": {
"test": "vitest run",
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
"fmt": "yarn format",
@@ -13,12 +13,12 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/builders/*'",
"release": "cliff-jumper"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -70,9 +70,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -0,0 +1,3 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig();

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig();

View File

@@ -4,7 +4,7 @@
"description": "Utility data structure used in discord.js",
"scripts": {
"test": "vitest run",
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
"fmt": "yarn format",
@@ -13,12 +13,12 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/collection/*'",
"release": "cliff-jumper"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -59,9 +59,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -0,0 +1,3 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig();

View File

@@ -1,9 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig({
entries: [
{ builder: 'rollup', input: 'src/index' },
{ builder: 'rollup', input: 'src/cli' },
],
minify: true,
});

View File

@@ -3,7 +3,7 @@
"version": "0.12.1",
"description": "The docs.json generator for discord.js and its related projects",
"scripts": {
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src --ext mjs,js,ts --fix",
"fmt": "yarn format",
@@ -11,7 +11,7 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/docgen/*'",
"release": "cliff-jumper"
},
"bin": "./dist/cli.cjs",
"bin": "./dist/cli.js",
"directories": {
"lib": "src"
},
@@ -45,7 +45,7 @@
"commander": "^9.4.0",
"jsdoc-to-markdown": "^7.1.1",
"tslib": "^2.4.0",
"typedoc": "^0.23.11"
"typedoc": "^0.23.13"
},
"devDependencies": {
"@favware/cliff-jumper": "^1.8.7",
@@ -54,9 +54,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"typescript": "^4.8.2",
"unbuild": "^0.8.9"
"tsup": "^6.2.3",
"typescript": "^4.8.2"
},
"engines": {
"node": ">=16.9.0"

View File

@@ -0,0 +1,7 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig({
entry: ['src/index.ts', 'src/cli.ts'],
minify: true,
dts: false,
});

View File

@@ -15,4 +15,4 @@ RUN yarn workspaces focus
COPY ./packages/proxy-container ./
RUN yarn build && yarn workspaces focus --production
CMD ["node", "--enable-source-maps", "./dist/index.mjs"]
CMD ["node", "--enable-source-maps", "./dist/index.js"]

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig({ emitCJS: false, cjsBridge: false });

View File

@@ -3,15 +3,15 @@
"version": "1.0.0",
"description": "Lightweight HTTP proxy for Discord's API, brought to you as a container 📦",
"scripts": {
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src --ext mjs,js,ts --fix",
"fmt": "yarn format",
"prepack": "yarn lint && yarn test && yarn build",
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/proxy-container/*'"
},
"main": "./dist/index.mjs",
"type": "module",
"module": "./dist/index.js",
"directories": {
"lib": "src"
},
@@ -53,9 +53,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"typescript": "^4.8.2",
"unbuild": "^0.8.9"
"tsup": "^6.2.3",
"typescript": "^4.8.2"
},
"engines": {
"node": ">=16.9.0"

View File

@@ -0,0 +1,6 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig({
format: ['esm'],
minify: true,
});

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig();

View File

@@ -4,7 +4,7 @@
"description": "Tools for running an HTTP proxy for Discord's API",
"scripts": {
"test": "vitest run",
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
"fmt": "yarn format",
@@ -13,12 +13,12 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/proxy/*'",
"release": "cliff-jumper"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"typings": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -69,10 +69,9 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"supertest": "^6.2.4",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -0,0 +1,3 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig();

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig();

View File

@@ -4,7 +4,7 @@
"description": "The REST API for discord.js",
"scripts": {
"test": "vitest run",
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
"fmt": "yarn format",
@@ -13,12 +13,12 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/rest/*'",
"release": "cliff-jumper"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"typings": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -70,9 +70,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -0,0 +1,3 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig();

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig({ minify: true });

View File

@@ -4,17 +4,17 @@
"description": "A set of scripts that we use for our workflows",
"private": true,
"scripts": {
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src --ext mjs,js,ts --fix",
"fmt": "yarn format"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -55,9 +55,8 @@
"eslint": "^8.23.0",
"eslint-config-neon": "^0.1.23",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -0,0 +1,5 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig({
minify: true,
});

View File

@@ -1,3 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig();

View File

@@ -3,7 +3,7 @@
"version": "0.12.0",
"description": "Implementation of the Discord Voice API for node.js",
"scripts": {
"build": "unbuild",
"build": "tsup",
"test": "jest --coverage",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
@@ -13,12 +13,12 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/voice/*'",
"release": "cliff-jumper"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"typings": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -65,7 +65,7 @@
"@discordjs/docgen": "workspace:^",
"@favware/cliff-jumper": "^1.8.7",
"@microsoft/api-extractor": "^7.29.5",
"@types/jest": "^28.1.8",
"@types/jest": "^29.0.0",
"@types/node": "^16.11.56",
"downlevel-dts": "^0.10.1",
"eslint": "^8.23.0",
@@ -74,10 +74,9 @@
"jest-websocket-mock": "^2.4.0",
"mock-socket": "^9.1.5",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"tweetnacl": "^1.0.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9"
"typescript": "^4.8.2"
},
"engines": {
"node": ">=16.9.0"

View File

@@ -0,0 +1,3 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig();

View File

@@ -40,7 +40,7 @@
"homepage": "https://discord.js.org",
"dependencies": {
"@discordjs/api-extractor-utils": "workspace:^",
"@emotion/react": "^11.10.0",
"@emotion/react": "^11.10.4",
"@emotion/server": "^11.10.0",
"@mantine/core": "^5.2.4",
"@mantine/hooks": "^5.2.4",
@@ -72,9 +72,9 @@
"@types/node": "^16.11.56",
"@types/react-dom": "^18.0.6",
"@types/react-syntax-highlighter": "^15.5.5",
"@unocss/cli": "^0.45.13",
"@unocss/preset-web-fonts": "^0.45.13",
"@unocss/reset": "^0.45.13",
"@unocss/cli": "^0.45.14",
"@unocss/preset-web-fonts": "^0.45.14",
"@unocss/reset": "^0.45.14",
"@vitejs/plugin-react": "^2.0.1",
"@vitest/coverage-c8": "^0.22.1",
"concurrently": "^7.3.0",
@@ -83,8 +83,8 @@
"happy-dom": "^6.0.4",
"prettier": "^2.7.1",
"typescript": "^4.8.2",
"unocss": "^0.45.13",
"vercel": "^28.2.0",
"unocss": "^0.45.14",
"vercel": "^28.2.2",
"vitest": "^0.22.1"
},
"engines": {

View File

@@ -177,7 +177,7 @@ test('spawn, connect, send a message, session info, and destroy', async () => {
await manager.connect();
expect(mockConstructor).toHaveBeenCalledWith(
expect.stringContaining('worker.cjs'),
expect.stringContaining('worker.js'),
expect.objectContaining({ workerData: expect.objectContaining({ shardIds: [0, 1] }) }),
);

View File

@@ -1,9 +0,0 @@
import { createUnbuildConfig } from '../../build.config';
export default createUnbuildConfig({
entries: [
{ builder: 'rollup', input: 'src/index' },
{ builder: 'rollup', input: 'src/strategies/sharding/worker' },
],
externals: ['zlib-sync'],
});

View File

@@ -4,7 +4,7 @@
"description": "Wrapper around Discord's gateway",
"scripts": {
"test": "vitest run",
"build": "unbuild",
"build": "tsup",
"lint": "prettier --check . && TIMING=1 eslint src __tests__ --ext mjs,js,ts",
"format": "prettier --write . && TIMING=1 eslint src __tests__ --ext mjs,js,ts --fix",
"docs": "downlevel-dts dist docs/dist --to=3.7 && docgen -i src/index.ts -c docs/index.json -o docs/docs.json --typescript && api-extractor run --local",
@@ -12,12 +12,12 @@
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/ws/*'",
"release": "cliff-jumper"
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"typings": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
@@ -72,9 +72,8 @@
"eslint-config-neon": "^0.1.23",
"mock-socket": "^9.1.5",
"prettier": "^2.7.1",
"rollup-plugin-typescript2": "^0.33.0",
"tsup": "^6.2.3",
"typescript": "^4.8.2",
"unbuild": "^0.8.9",
"undici": "^5.10.0",
"vitest": "^0.22.1",
"zlib-sync": "^0.1.7"

View File

@@ -92,7 +92,7 @@ export class WorkerShardingStrategy implements IShardingStrategy {
shardIds: slice,
};
const worker = new Worker(join(__dirname, 'worker.cjs'), { workerData });
const worker = new Worker(join(__dirname, 'worker.js'), { workerData });
await once(worker, 'online');
worker
.on('error', (err) => {

View File

@@ -0,0 +1,9 @@
import { createTsupConfig } from '../../tsup.config.js';
export default createTsupConfig({
entry: {
index: 'src/index.ts',
worker: 'src/strategies/sharding/worker.ts',
},
external: ['zlib-sync'],
});

35
tsup.config.js Normal file
View File

@@ -0,0 +1,35 @@
import { defineConfig } from 'tsup';
export function createTsupConfig({
entry = ['src/index.ts'],
external = [],
noExternal = [],
platform = 'node',
format = ['esm', 'cjs'],
target = 'es2022',
skipNodeModulesBundle = true,
clean = true,
shims = true,
minify = false,
splitting = false,
keepNames = true,
dts = true,
sourcemap = true,
} = {}) {
return defineConfig({
entry,
external,
noExternal,
platform,
format,
skipNodeModulesBundle,
target,
clean,
shims,
minify,
splitting,
keepNames,
dts,
sourcemap,
});
}

1470
yarn.lock

File diff suppressed because it is too large Load Diff