Open Maxim-Mazurok opened 1 year ago
The following patch works for me:
Looks like we don't need any logic at all, just import works fine.
Also, with original logic vitest in nx fails to import `node-fetch`.
See https://github.com/cspotcode/tsimportlib/issues/8
diff --git a/node_modules/tsimportlib/index.d.ts b/node_modules/tsimportlib/index.d.ts
index 369a3a6..dbf0199 100644
--- a/node_modules/tsimportlib/index.d.ts
+++ b/node_modules/tsimportlib/index.d.ts
@@ -1,18 +1,12 @@
-export interface MinimalNodeModule {
- filename: string;
-}
-
/**
* Asynchronously import a native ESM module from within a `.ts` file compiled to CommonJS.
*
* Usage:
*
* ```
- * await dynamicImport(module, 'native-esm-module');
+ * await dynamicImport('native-esm-module');
* ```
- *
- * @param module node `module` object for the importing file
*/
-export function dynamicImport(importSpecifier: string, module: MinimalNodeModule): Promise<any>;
+export function dynamicImport(importSpecifier: string): Promise<any>;
export {dynamicImport as importEsm};
diff --git a/node_modules/tsimportlib/index.js b/node_modules/tsimportlib/index.js
index e5096d6..47bbb5d 100644
--- a/node_modules/tsimportlib/index.js
+++ b/node_modules/tsimportlib/index.js
@@ -1,25 +1,6 @@
-const Module = require('module');
-const {isAbsolute} = require('path');
-const {pathToFileURL} = require('url');
-
exports.dynamicImport = importEsm;
exports.importEsm = importEsm;
-async function importEsm(specifier, module) {
- if(isAbsolute(specifier)) {
- return import(pathToFileURL(specifier).href);
- }
- let resolvedPath;
- try {
- const req = Module.createRequire(module.filename);
- try {
- resolvedPath = req.resolve(Path.posix.join(specifier, 'package.json'));
- } catch {
- resolvedPath = req.resolve(specifier);
- }
- resolvedPath = pathToFileURL(resolvedPath).href;
- } catch {
- throw new Error(`Unable to locate module "${specifier}" relative to "${module?.filename}" using the CommonJS resolver. Consider passing an absolute path to the target module.`);
- }
- return import(resolvedPath);
+async function importEsm(specifier) {
+ return import(specifier);
}
If the code runs fine without being called from the test code, I think you should check whether your test environment supports ES modules.
And the problem in your patch is that it can't import es module by relative path. Given a relative path, it will look for files at its location, so that's why we need resolvedPath
.
Please check your test environment :)
I think the code was working fine outside of the testing environment, yes.
I'm using the latest vitest
- 0.31.4
, which is supposed to support ESM out of the box.
All of my files/packages are CJS, so I guess I have no need for relative paths.
I guess my main goal here was to share what works for me, so hopefully it will help someone. I don't really need this to be addressed as the patch seems to work and I'm pretty happy with it. So feel free to close as not planned if you'd like, cheers!
My code:
My test: