-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix ESM build to contain
import
statements with full file exte…
…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
Showing
14 changed files
with
82 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters