From d7fdda8d23a20892d36ada847cfdc24ae76ca4b1 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 31 Jul 2023 15:11:29 -0600 Subject: [PATCH] fix: set moduleResolution to Node16 (#750) * fix: set moduleResolution to Node16 * fix: use file urls * chore: add types --- src/module-loader.ts | 20 +++++++------------- tsconfig.json | 1 + 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/module-loader.ts b/src/module-loader.ts index 27640363e..fcf7843fa 100644 --- a/src/module-loader.ts +++ b/src/module-loader.ts @@ -15,12 +15,6 @@ const getPackageType = require('get-package-type') // eslint-disable-next-line camelcase const s_EXTENSIONS: string[] = ['.ts', '.js', '.mjs', '.cjs'] -/** - * Provides a mechanism to use dynamic import / import() with tsconfig -> module: commonJS as otherwise import() gets - * transpiled to require(). - */ -const _importDynamic = new Function('modulePath', 'return import(modulePath)') // eslint-disable-line no-new-func - /** * Provides a static class with several utility methods to work with Oclif config / plugin to load ESM or CJS Node * modules and source files. @@ -46,12 +40,12 @@ export default class ModuleLoader { * @returns {Promise<*>} The entire ESM module from dynamic import or CJS module by require. */ static async load(config: IConfig|IPlugin, modulePath: string): Promise { - let filePath - let isESM + let filePath: string | undefined + let isESM: boolean | undefined try { ({isESM, filePath} = ModuleLoader.resolvePath(config, modulePath)) - // It is important to await on _importDynamic to catch the error code. - return isESM ? await _importDynamic(url.pathToFileURL(filePath)) : require(filePath) + // It is important to await on import to catch the error code. + return isESM ? await import(url.pathToFileURL(filePath).href) : require(filePath) } catch (error: any) { if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { throw new ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}`) @@ -79,11 +73,11 @@ export default class ModuleLoader { * file path and whether the module is ESM. */ static async loadWithData(config: IConfig|IPlugin, modulePath: string): Promise<{isESM: boolean; module: any; filePath: string}> { - let filePath - let isESM + let filePath: string | undefined + let isESM: boolean | undefined try { ({isESM, filePath} = ModuleLoader.resolvePath(config, modulePath)) - const module = isESM ? await _importDynamic(url.pathToFileURL(filePath)) : require(filePath) + const module = isESM ? await import(url.pathToFileURL(filePath).href) : require(filePath) return {isESM, module, filePath} } catch (error: any) { if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { diff --git a/tsconfig.json b/tsconfig.json index 7c6088f3c..cd1e62b7d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,7 @@ "target": "es2020", "allowSyntheticDefaultImports": true, "noErrorTruncation": true, + "moduleResolution": "Node16" }, "include": [ "./src/**/*"