Skip to content

Commit

Permalink
fix: triple ESM fix (#508)
Browse files Browse the repository at this point in the history
* fix: fixed esm bundle reading cjs code and failing

* test: update tests to import from new path of index.ts

* docs: fix typedoc entrypoint
  • Loading branch information
favna authored Dec 5, 2023
1 parent 38d72a0 commit f0760bc
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/index.ts → src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { find } from '@discordjs/node-pre-gyp';
import { join, resolve } from 'path';
// Note that this is in a subfolder within src to mimick that when compiled it will be put in dist/{cjs,esm} which is
// equally a subfolder within dist. This is to ensure that the import path to the package.json is correct.

import nodePreGyp from '@discordjs/node-pre-gyp';
import { join, resolve } from 'node:path';

declare namespace Internals {
export function getPromiseDetails(thing: any): number[];
export function getProxyDetails(thing: any): number[];
}

const bindingPath = find(resolve(join(__dirname, '..', './package.json')));
const bindingPath = nodePreGyp.find(resolve(join(__dirname, '..', '..', './package.json')));

// eslint-disable-next-line @typescript-eslint/no-var-requires
export const { getPromiseDetails, getProxyDetails } = require(bindingPath) as typeof Internals;
Expand Down
2 changes: 1 addition & 1 deletion tests/arrays.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Arrays', () => {
test('array(empty)', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/functions.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Functions', () => {
test('function(empty)', (): void => {
Expand Down
2 changes: 1 addition & 1 deletion tests/internals.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPromiseDetails, getProxyDetails } from '../src';
import { getPromiseDetails, getProxyDetails } from '../src/lib/index.js';

describe('Internals', () => {
describe('getPromiseDetails', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/maps.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Maps', () => {
test('map(empty)', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/objects.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Objects', () => {
test('object(generic)', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/primitives.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Primitives', () => {
test('number', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/promises.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Promises', () => {
test('promise(resolve)', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/proxies.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Proxies', () => {
test('proxy(object)', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/sets.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../src';
import { Type } from '../src/lib/index.js';

describe('Sets', () => {
test('set(empty)', () => {
Expand Down
23 changes: 21 additions & 2 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig, type Options } from 'tsup';

const baseOptions: Options = {
clean: true,
entry: ['src/index.ts'],
entry: ['src/lib/index.ts'],
dts: true,
minify: false,
skipNodeModulesBundle: true,
Expand All @@ -23,6 +23,25 @@ export default [
defineConfig({
...baseOptions,
outDir: 'dist/esm',
format: 'esm'
format: 'esm',
shims: true,
esbuildOptions: (options, context) => {
switch (context.format) {
case 'esm': {
options.banner = {
js: [
//
"import { createRequire } from 'node:module';",
'const require = createRequire(import.meta.url);'
].join('\n')
};
break;
}
// If it's not esm then we do nothing
default: {
break;
}
}
}
})
];
2 changes: 1 addition & 1 deletion typedoc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://typedoc.org/schema.json",
"entryPoints": ["src/index.ts"],
"entryPoints": ["src/lib/index.ts"],
"json": "docs/api.json",
"tsconfig": "src/tsconfig.json"
}

0 comments on commit f0760bc

Please sign in to comment.