Skip to content

Commit

Permalink
fix!: properly split CJS and ESM
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This ensures that Pieces will properly load the files
from the appropiate folder and we properly bundle CJS and ESM.
To use this version of @sapphire/pieces you have to use @sapphire/framework
v5.x or higher, or update your own usage of @sapphire/pieces to also
properly split ESM and CJS.
  • Loading branch information
favna committed Dec 4, 2023
1 parent 155479b commit 41e8cec
Show file tree
Hide file tree
Showing 10 changed files with 758 additions and 193 deletions.
1 change: 1 addition & 0 deletions .rollup-type-bundlerrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
onlyBundle: true
36 changes: 24 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@
"name": "@sapphire/pieces",
"version": "3.10.0",
"description": "Sapphire's piece loader.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"default": "./dist/cjs/index.cjs"
}
},
"author": "@sapphire",
"license": "MIT",
"scripts": {
"lint": "eslint src scripts --ext mjs,ts --fix",
"format": "prettier --write {src,scripts}/**/*.ts",
"format": "prettier --write \"{src,scripts}/**/*.ts\"",
"docs": "typedoc-json-parser",
"update": "yarn upgrade-interactive",
"clean": "node scripts/clean-dist.mjs",
"build": "tsc -b src && node scripts/make-import.mjs && gen-esm-wrapper dist/index.js dist/index.mjs",
"watch": "tsc -b src -w",
"clean": "rimraf dist",
"build": "tsup && concurrently \"yarn:postbuild:*\"",
"postbuild:internal": "node scripts/make-import.mjs",
"postbuild:types:cjs": "rollup-type-bundler -d dist/cjs",
"postbuild:types:esm": "rollup-type-bundler -d dist/esm -t .mts",
"typecheck": "tsc -b src",
"bump": "cliff-jumper",
"check-update": "cliff-jumper --dry-run",
"prepack": "rollup-type-bundler"
"prepack": "yarn build"
},
"dependencies": {
"@discordjs/collection": "^1.5.3",
Expand All @@ -33,20 +42,23 @@
"@commitlint/config-conventional": "^18.4.3",
"@favware/cliff-jumper": "^2.2.3",
"@favware/npm-deprecate": "^1.0.7",
"@favware/rollup-type-bundler": "^3.1.0",
"@favware/rollup-type-bundler": "^3.2.0",
"@sapphire/eslint-config": "^5.0.2",
"@sapphire/prettier-config": "^2.0.0",
"@sapphire/ts-config": "^5.0.0",
"@types/node": "^20.10.2",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"concurrently": "^8.2.2",
"cz-conventional-changelog": "^3.3.0",
"esbuild-plugin-file-path-extensions": "^2.0.0",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.0.1",
"gen-esm-wrapper": "^1.1.3",
"lint-staged": "^15.1.0",
"prettier": "^3.1.0",
"rimraf": "^5.0.5",
"tsup": "^8.0.1",
"typedoc": "^0.25.4",
"typedoc-json-parser": "^9.0.1",
"typescript": "^5.3.2"
Expand Down
2 changes: 0 additions & 2 deletions scripts/clean-dist.mjs

This file was deleted.

19 changes: 17 additions & 2 deletions scripts/make-import.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { writeFile } from 'fs/promises';

const path = new URL('../dist/lib/internal/internal.js', import.meta.url);
await writeFile(path, ['"use strict";', 'exports.mjsImport = (path) => import(path);', ''].join('\n'), 'utf-8');
const pathCJS = new URL('../dist/cjs/lib/internal/internal.cjs', import.meta.url);
const pathESM = new URL('../dist/esm/lib/internal/internal.mjs', import.meta.url);

const baseInternalImport = [
'const __defProp = Object.defineProperty;',
'const __name = (target, value) => __defProp(target, "name", { value, configurable: true });',
'',
'function mjsImport(path) {',
' return import(path);',
'}',
'',
'__name(mjsImport, "mjsImport");',
''
];

await writeFile(pathCJS, ['"use strict";', '', ...baseInternalImport, 'exports.mjsImport = mjsImport;', ''].join('\n'), 'utf-8');
await writeFile(pathESM, [...baseInternalImport, 'export { mjsImport };', ''].join('\n'), { encoding: 'utf-8' });
4 changes: 2 additions & 2 deletions src/lib/structures/Store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection } from '@discordjs/collection';
import { Constructor, classExtends, isClass, type AbstractConstructor } from '@sapphire/utilities';
import { classExtends, isClass, type AbstractConstructor, type Constructor } from '@sapphire/utilities';
import { join } from 'path';
import { LoaderError, LoaderErrorType } from '../errors/LoaderError';
import { resolvePath, type Path } from '../internal/Path';
Expand All @@ -8,7 +8,7 @@ import { container, type Container } from '../shared/Container';
import type { HydratedModuleData, ILoaderResultEntry, ILoaderStrategy, ModuleData } from '../strategies/ILoaderStrategy';
import { LoaderStrategy } from '../strategies/LoaderStrategy';
import type { Piece } from './Piece';
import { StoreRegistry, StoreRegistryKey, type StoreRegistryEntries } from './StoreRegistry';
import { StoreRegistry, type StoreRegistryEntries, type StoreRegistryKey } from './StoreRegistry';

/**
* The options for the store, this features both hooks (changes the behaviour) and handlers (similar to event listeners).
Expand Down
3 changes: 1 addition & 2 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"extends": "../tsconfig.base.json",
"compilerOptions": {
"rootDir": "./",
"outDir": "../dist",
"composite": true
"outDir": "../dist"
},
"include": ["."]
}
6 changes: 5 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": ["@sapphire/ts-config", "@sapphire/ts-config/extra-strict"]
"extends": ["@sapphire/ts-config", "@sapphire/ts-config/extra-strict", "@sapphire/ts-config/bundler", "@sapphire/ts-config/verbatim"],
"compilerOptions": {
"incremental": false,
"noEmit": true
}
}
2 changes: 1 addition & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.base.json",
"include": ["src", "scripts"]
"include": ["src", "scripts", "tsup.config.ts"]
}
30 changes: 30 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { esbuildPluginFilePathExtensions } from 'esbuild-plugin-file-path-extensions';
import { defineConfig, type Options } from 'tsup';

const baseOptions: Options = {
clean: true,
entry: ['src/**/*.ts', '!src/**/*.d.ts'],
dts: true,
minify: false,
skipNodeModulesBundle: true,
sourcemap: true,
target: 'es2021',
tsconfig: 'src/tsconfig.json',
keepNames: true,
esbuildPlugins: [esbuildPluginFilePathExtensions()],
treeshake: true
};

export default [
defineConfig({
...baseOptions,
outDir: 'dist/cjs',
format: 'cjs',
outExtension: () => ({ js: '.cjs' })
}),
defineConfig({
...baseOptions,
outDir: 'dist/esm',
format: 'esm'
})
];
Loading

0 comments on commit 41e8cec

Please sign in to comment.