Skip to content

Commit

Permalink
fix: set moduleResolution to Node16 (#750)
Browse files Browse the repository at this point in the history
* fix: set moduleResolution to Node16

* fix: use file urls

* chore: add types
  • Loading branch information
mdonnalley authored Jul 31, 2023
1 parent 24a12e3 commit d7fdda8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/module-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<any> {
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}`)
Expand Down Expand Up @@ -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') {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"target": "es2020",
"allowSyntheticDefaultImports": true,
"noErrorTruncation": true,
"moduleResolution": "Node16"
},
"include": [
"./src/**/*"
Expand Down

0 comments on commit d7fdda8

Please sign in to comment.