You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How to require (or import) Native ESM modules while using a require hook like require.extensions.
Note: I do know that require.extensions is deprecated and is not recommended to modify, but as ts-node does, I am using this as a part of a temporary workaround.
Goal
I want to dynamic import a configuration file, jiwon.config.ts, which should import other JS/TS extensions successfully.
Restrictions
I cannot use other dependencies like webpack or esbuild to bundle and resolve the imports.
The only option is to use SWC.
Work Flow
importConfig()
looks for jiwon.config.ts, exists
REGISTER REQUIRE HOOK
readFile() the config
SWC transform the code
writeFile() the config as .js
dynamic import jiwon.config.js
REGISTER REQUIRE HOOK
Why? To not bundle and do fewer operations on resolving the config file. Especially utilizing the high performance of SWC.
TL;DR
How to require (or import) Native ESM modules while using a require hook like
require.extensions
.Goal
I want to dynamic import a configuration file,
jiwon.config.ts
, which should import other JS/TS extensions successfully.Restrictions
I cannot use other dependencies like
webpack
oresbuild
to bundle and resolve the imports.The only option is to use
SWC
.Work Flow
importConfig()
jiwon.config.ts
, existsreadFile()
the configtransform
the codewriteFile()
the config as.js
jiwon.config.js
REGISTER REQUIRE HOOK
Why? To not bundle and do fewer operations on resolving the config file. Especially utilizing the high performance of SWC.
Current Status
By doing so, I've achieved to cover 90% of the expected & edge cases of resolving the config file.
The only thing that I am struggling with is:
.js
(ESM) file..js
).They both have in common that the format is ESM but the extension is
.js
.As I tried to overwrite the
require.extensions['.js']
, threw an error during the process of importing other.js
files.Requesting for Help
If you...
.js
file with the current statusplease share your expertise and insights, will be very grateful for your help.
Node.js version
Example code
See above.
Operating system
Darwin MacBook-Pro.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:32:11 PDT 2023; root:xnu-10002.41.9~7/RELEASE_ARM64_T6030 arm64
Scope
Custom compiling through modifying
require.extensions
.Module and version
Not applicable.
The text was updated successfully, but these errors were encountered: