Skip to content

fix: correctly identify imports with resolution-mode (#582) - #595

Open
FoxxMD wants to merge 1 commit into
YousefED:masterfrom
FoxxMD:importResolutionFix
Open

fix: correctly identify imports with resolution-mode (#582)#595
FoxxMD wants to merge 1 commit into
YousefED:masterfrom
FoxxMD:importResolutionFix

Conversation

@FoxxMD

@FoxxMD FoxxMD commented Apr 3, 2024

Copy link
Copy Markdown

Used fix provided by @jer-sen and closes #582

I believe this only occurs for ESM projects with "moduleResolution": "NodeNext" in tsconfig.json and introduced in TS 4.7. This fix is backwards compatible.

Comment thread typescript-json-schema.ts
const vm = require("vm");

const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g;
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, \{ assert: \{ "resolution-mode": "(import|require)" \} \})?\)|".*?")\.| /g;

@derdeka derdeka Aug 26, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @FoxxMD,
thank you for pointing in the right direction. I had to use a different regex when using typescript@22.14.0 and typescript@5.8.3 with esm modules:

Suggested change
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, \{ assert: \{ "resolution-mode": "(import|require)" \} \})?\)|".*?")\.| /g;
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(.*?\)|".*?")\.| /g;

I'm patching this line with the help of patch-package into typescript-json-schema to make it working again.

@YousefED Thank you for this great package! I would like to politely ask if this regular expression could be updated to work with ESM packages.

edit:
as just discovered @puckey suggests this change:

Suggested change
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, \{ assert: \{ "resolution-mode": "(import|require)" \} \})?\)|".*?")\.| /g;
const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"(, [^)]+)?\)|".*?")\.| /g;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

imports with resolution-mode not replaced

2 participants