Skip to content

Commit

Permalink
fix: fix ESM build to contain import statements with full file exte…
Browse files Browse the repository at this point in the history
…nsions (#49)

* fix ESM build import statements full paths with file extensions

* set a fixed version of \`pnpm\` to install for the reusable CI setup which is compatible with the Node version 16.14.0 that's used in the meantime

* rename the main `tsconfig-esm.json` into a more IDE-friendly `tsconfig.json`
  • Loading branch information
shtaif authored Jun 22, 2024
1 parent 815d357 commit 2aad91d
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/actions/ci-common-setup/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ runs:
shell: bash

- name: Install pnpm
run: npm install -g pnpm
run: npm install -g pnpm@^8
shell: bash

- name: Restore possibly cached dependencies
Expand Down
2 changes: 2 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
spec: ./spec/**/*.spec.ts
file:
- ./spec/testsGlobalSetup.ts
watch-files:
- ./src/**/*.ts
- ./spec/**/*.ts
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"typescript",
"esm"
],
"type": "commonjs",
"engines": {
"node": ">=10.21.0"
},
Expand All @@ -46,9 +47,9 @@
"dist"
],
"scripts": {
"test": "ts-mocha -p ./tsconfig-cjs.json",
"build": "rimraf ./dist && tsc -p tsconfig-esm.json && tsc -p tsconfig-cjs.json && ts-node ./scripts/set-module-type-in-dist-builds.ts",
"check-typings": "tsc --noEmit -p ./tsconfig-esm.json",
"test": "ts-mocha -p ./spec/tsconfig-tests.json",
"build": "rimraf ./dist && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ts-node -p tsconfig.json ./scripts/set-module-type-in-dist-builds.ts",
"check-typings": "tsc --noEmit -p ./tsconfig.json",
"prepublishOnly": "npm run build"
},
"devDependencies": {
Expand All @@ -61,7 +62,7 @@
"eslint-config-prettier": "^8.9.0",
"eslint-config-standard": "^17.1.0",
"expect": "^29.6.2",
"mocha": "^10.2.0",
"mocha": "^10.4.0",
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"sinon": "^15.2.0",
Expand Down
36 changes: 15 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions spec/iterified.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'mocha';
import expect from 'expect';
import * as sinon from 'sinon';
import { iterified, type ExecutorFn } from '../src';
import nextTick from './utils/nextTick';
import getPromiseState from './utils/getPromiseState';
import collectAsyncIterable from './utils/collectAsyncIterable';
import sortPromisesByResolutionOrder from './utils/sortPromisesByResolutionOrder';
import { iterified, type ExecutorFn } from '../src/index.js';
import nextTick from './utils/nextTick.js';
import getPromiseState from './utils/getPromiseState.js';
import collectAsyncIterable from './utils/collectAsyncIterable.js';
import sortPromisesByResolutionOrder from './utils/sortPromisesByResolutionOrder.js';

describe('`iterified` function', () => {
it('generating a single value and consuming via multiple iterators', async () => {
Expand Down
6 changes: 3 additions & 3 deletions spec/iterifiedUnwrapped.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'mocha';
import expect from 'expect';
import { iterifiedUnwrapped } from '../src';
import getPromiseState from './utils/getPromiseState';
import collectAsyncIterable from './utils/collectAsyncIterable';
import { iterifiedUnwrapped } from '../src/index.js';
import getPromiseState from './utils/getPromiseState.js';
import collectAsyncIterable from './utils/collectAsyncIterable.js';

describe('`iterifiedUnwrapped` function', () => {
it('generating a single value and consuming via multiple iterators', async () => {
Expand Down
25 changes: 25 additions & 0 deletions spec/testsGlobalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mod from 'module';

(() => {
/*
It appears that `ts-mocha` errors out on import statements with paths having explicit `.js` extension.
However, the `.js` extensions currently present on all imports across the project CANNOT be omitted for the sake of running the tests
since without them, while TypeScript is able to accept extension-less imports as well (having set `"moduleResolution": "NodeNext"` in `tsconfig.json`),
when fed such imports while compiling to an ESM format, it would output them as-are. ESM code with extension-less imports is
INVALID ESM code, so the build output is actually non-executable even though it would complete without any issue from
the TypeScript compilation.
To work around this (only during test run-time) - we're patching Node's file import mechanism to intercept
every `.js`-ending import path and feed it back to Node as itself-minus-the-`.js` extension. This way,
as long as we've set `"module": "commonjs"` in `tsconfig.json` that accompanies our test runs, we can still write
the entire code-base with "fully-qualified" ESM-compatible imports statements AS WELL AS have Mocha be able to
execute over the same code without issues.
Inspired by https://github.com/ReactiveX/rxjs/blob/6d011f0dc67b736adc0979d3bc14d93b49064efa/spec/support/mocha-path-mappings.js
*/

const origResolveFilename = (mod as any)._resolveFilename;

(mod as any)._resolveFilename = function (path: string, ...rest: unknown[]) {
const pathPossiblyPatched = path.endsWith('.js') ? path.slice(0, -3) : path;
return origResolveFilename.call(this, pathPossiblyPatched, ...rest);
};
})();
10 changes: 10 additions & 0 deletions spec/tsconfig-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"include": ["./**/*.ts"],
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
type ExecutorFn,
type TeardownFn,
type Iterified,
} from './iterified';
import { iterifiedUnwrapped, type IterifiedUnwrapped } from './iterifiedUnwrapped';
} from './iterified.js';
import { iterifiedUnwrapped, type IterifiedUnwrapped } from './iterifiedUnwrapped.js';

export {
iterified,
Expand Down
4 changes: 2 additions & 2 deletions src/iterified.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMulticastChannel } from './utils/createMulticastChannel';
import { type MaybePromise } from './utils/types/MaybePromise';
import { createMulticastChannel } from './utils/createMulticastChannel.js';
import { type MaybePromise } from './utils/types/MaybePromise.js';

export {
iterified,
Expand Down
4 changes: 2 additions & 2 deletions src/iterifiedUnwrapped.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type IterifiedIterable } from './iterified';
import { createMulticastChannel } from './utils/createMulticastChannel';
import { type IterifiedIterable } from './iterified.js';
import { createMulticastChannel } from './utils/createMulticastChannel.js';

export { iterifiedUnwrapped, type IterifiedUnwrapped };

Expand Down
4 changes: 2 additions & 2 deletions src/utils/createMulticastChannel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Deferred } from './createDeferred';
import { type MaybePromise } from './types/MaybePromise';
import { Deferred } from './createDeferred.js';
import { type MaybePromise } from './types/MaybePromise.js';

export { createMulticastChannel, type MulticastChannel, type MulticastChannelIterator };

Expand Down
9 changes: 5 additions & 4 deletions tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"files": ["./src/index.ts"],
"extends": "./tsconfig-esm.json",
"include": ["./src/**/*.ts"],
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist/cjs",
"module": "CommonJS"
"module": "CommonJS",
"moduleResolution": "Node10"
}
}
}
5 changes: 3 additions & 2 deletions tsconfig-esm.json → tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"files": ["./src/index.ts"],
"include": ["./src/**/*.ts"],
"compilerOptions": {
"outDir": "./dist/esm",
"module": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "es2019",
"strict": true,
"noImplicitAny": true,
Expand Down

0 comments on commit 2aad91d

Please sign in to comment.