create-esm-loader
Advanced tools
Comparing version
@@ -23,2 +23,3 @@ // # create-loader.js | ||
this.stack = null; | ||
// eslint-disable-next-line no-async-promise-executor | ||
const loading = new Promise(async resolve => { | ||
@@ -240,3 +241,3 @@ this.stack = await this.buildStack(loaders); | ||
}; | ||
} | ||
}; | ||
} | ||
@@ -243,0 +244,0 @@ |
@@ -5,3 +5,2 @@ import { createRequire } from 'module'; | ||
import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
const require = createRequire(import.meta.url); | ||
@@ -27,13 +26,7 @@ // # isRule(config) | ||
return { | ||
resolve(specifier, opts) { | ||
async resolve(specifier, opts) { | ||
if (!match(specifier)) return; | ||
let url; | ||
if (specifier.startsWith('.')) { | ||
url = new URL(specifier, opts.parentURL); | ||
} else { | ||
let file = require.resolve(specifier); | ||
url = pathToFileURL(file); | ||
} | ||
let url = await resolveSpecifier(specifier, opts); | ||
return { | ||
url: String(url), | ||
url, | ||
format: 'module', | ||
@@ -61,2 +54,23 @@ }; | ||
// # resolveSpecifier(specifier, opts) | ||
// Resolves the given specifier to a file. We use enhanced resolve here based | ||
// on the directory of the *parent module* so that we can properly support the | ||
// exports field. It doesn't work if we use `require.resolve()` because then it | ||
// doesn't support the exports as we're running in a different module! | ||
function resolveSpecifier(specifier, opts) { | ||
let parent = opts.parentURL; | ||
if (!parent) { | ||
parent = pathToFileURL(process.cwd()); | ||
} | ||
// IMPORTANT! We can't use `import.meta.resolve` here as that would result | ||
// in infinite loops! If we don't want to rely on require.resolve anymore, | ||
// we can also use the `enhanced-resolve` module from npm which has the | ||
// same functionality! | ||
const require = createRequire(parent); | ||
let file = require.resolve(specifier); | ||
return pathToFileURL(file).href; | ||
} | ||
// # makeMatcher(test) | ||
@@ -75,3 +89,3 @@ // Creates the function to be used to match a url based on the test. | ||
return regex.test(url); | ||
} | ||
}; | ||
} | ||
@@ -78,0 +92,0 @@ |
{ | ||
"name": "create-esm-loader", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "A utility library for creating Node loader hooks", | ||
@@ -17,3 +17,4 @@ "type": "module", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"lint": "eslint lib test" | ||
}, | ||
@@ -36,4 +37,6 @@ "repository": { | ||
"devDependencies": { | ||
"@whisthub/eslint-config": "^1.0.2", | ||
"chai": "^4.3.4", | ||
"chai-spies": "^1.0.0", | ||
"eslint": "^8.36.0", | ||
"mocha": "^9.1.3", | ||
@@ -40,0 +43,0 @@ "ts-loader": "^9.4.2", |
31179
2.78%555
2.21%8
33.33%