@graphql-tools/code-file-loader
Version:
A set of utils for faster development of GraphQL tools
29 lines (28 loc) • 1.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toImportSpecifier = toImportSpecifier;
const path_1 = require("path");
const url_1 = require("url");
/**
* Convert a module specifier for use with dynamic `import()`.
*
* Absolute filesystem paths must be `file://` URLs when `import()` is a real
* ESM import — on Windows a bare `C:\...` path is parsed as a `c:` protocol
* scheme and rejected with `ERR_UNSUPPORTED_ESM_URL_SCHEME`.
*
* Bob's CJS build rewrites `import()` to `require()`, which needs a filesystem
* path (not a `file://` URL). Detect CJS via `__filename` and leave absolute
* paths unchanged there. Bare package / relative specifiers are always unchanged.
*
* @internal
*/
function toImportSpecifier(specifier) {
if (!(0, path_1.isAbsolute)(specifier)) {
return specifier;
}
// Present in CJS (including Jest); absent in native ESM / tsx ESM.
if (typeof __filename === 'string') {
return specifier;
}
return (0, url_1.pathToFileURL)(specifier).href;
}