babel-plugin-esm-resolver
Advanced tools
Comparing version
182
lib/index.js
@@ -7,14 +7,9 @@ "use strict"; | ||
exports.default = void 0; | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _path = _interopRequireDefault(require("path")); | ||
var _module = _interopRequireDefault(require("module")); | ||
var _process = _interopRequireDefault(require("process")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
let moduleIsBuiltinCache = null; | ||
let moduleIsBuiltinCache = null; | ||
/** | ||
@@ -26,3 +21,2 @@ * Check if a module name is a built-in module. | ||
*/ | ||
function moduleIsBuiltin(name) { | ||
@@ -32,15 +26,12 @@ if (/^node:/.test(name)) { | ||
} | ||
while (!moduleIsBuiltinCache) { | ||
// Node v9.3.0+ | ||
const list = _module.default.builtinModules; | ||
if (list) { | ||
moduleIsBuiltinCache = new Set(list); | ||
break; | ||
} // Older versions: | ||
} | ||
// Older versions: | ||
const natives = _process.default.binding('natives'); | ||
if (natives) { | ||
@@ -51,8 +42,7 @@ const list = Object.keys(natives).filter(s => !/^internal\//.test(s)); | ||
} | ||
throw new Error('Cannot lookup builtin modules'); | ||
} | ||
return moduleIsBuiltinCache.has(name); | ||
} | ||
/** | ||
@@ -64,4 +54,2 @@ * Stat path if exists. | ||
*/ | ||
function pathStat(path) { | ||
@@ -75,2 +63,3 @@ try { | ||
} | ||
/** | ||
@@ -82,4 +71,2 @@ * Read JSON from path. | ||
*/ | ||
function readJson(path) { | ||
@@ -89,2 +76,3 @@ // eslint-disable-next-line no-sync | ||
} | ||
/** | ||
@@ -96,7 +84,6 @@ * Read module package.json file. | ||
*/ | ||
function readPackageJson(moduleDir) { | ||
return readJson(_path.default.join(moduleDir, 'package.json')); | ||
} | ||
/** | ||
@@ -108,7 +95,6 @@ * Trim ./ from head of string. | ||
*/ | ||
function trimDotSlash(path) { | ||
return path.replace(/^(\.\/)+/, ''); | ||
} | ||
/** | ||
@@ -120,7 +106,6 @@ * Check if import path is a built-in module. | ||
*/ | ||
function importIsBuiltin(path) { | ||
return moduleIsBuiltin(path); | ||
} | ||
/** | ||
@@ -132,7 +117,6 @@ * Check if import path is a URL. | ||
*/ | ||
function importIsUrl(path) { | ||
return /^[^/]+:\/\//.test(path); | ||
} | ||
/** | ||
@@ -144,7 +128,6 @@ * Check if import path is a file. | ||
*/ | ||
function importIsFile(path) { | ||
return path === '.' || path === '..' || /^\.?\.?\//.test(path); | ||
} | ||
/** | ||
@@ -156,7 +139,4 @@ * Parse bare imort path, namespace aware. | ||
*/ | ||
function importBareParse(path) { | ||
const ns = path.match(/^(@[^/]+\/[^/]+)([\s\S]*)$/); | ||
if (ns) { | ||
@@ -168,5 +148,3 @@ return { | ||
} | ||
const pk = path.match(/^([^/]+)([\s\S]*)$/); | ||
if (pk) { | ||
@@ -178,5 +156,5 @@ return { | ||
} | ||
return null; | ||
} | ||
/** | ||
@@ -188,8 +166,5 @@ * Expand extensions options into a list. | ||
*/ | ||
function expandExtensions(e) { | ||
let src = null; | ||
let dst = null; | ||
if (Array.isArray(e)) { | ||
@@ -202,3 +177,2 @@ const l = e.length; | ||
} | ||
const srcs = Array.isArray(src) ? src : [src]; | ||
@@ -210,2 +184,3 @@ return { | ||
} | ||
/** | ||
@@ -217,4 +192,2 @@ * Get module paths for a file. | ||
*/ | ||
function modulePathsForFile(file) { | ||
@@ -224,12 +197,13 @@ // Node v12.2.0+: | ||
return _module.default.createRequire(file).resolve.paths(''); | ||
} // Node v10.12.0+: | ||
} | ||
// Node v10.12.0+: | ||
if (_module.default.createRequireFromPath) { | ||
return _module.default.createRequireFromPath(file).resolve.paths(''); | ||
} // Older versions: | ||
} | ||
// Older versions: | ||
return [].concat(_module.default._nodeModulePaths(_path.default.dirname(file)), _module.default.globalPaths); | ||
} | ||
/** | ||
@@ -242,10 +216,6 @@ * Resolve the named module director for a file. | ||
*/ | ||
function resolveModuleDir(name, file) { | ||
const paths = modulePathsForFile(file); | ||
for (const p of paths) { | ||
const full = _path.default.join(p, name); | ||
if (pathStat(full)) { | ||
@@ -255,5 +225,5 @@ return full; | ||
} | ||
throw new Error(`Failed to resolve directory for module: ${name} in: ${file}`); | ||
} | ||
/** | ||
@@ -264,11 +234,8 @@ * Resolve extension for a path base. | ||
* @param {Array|string} extensions Extensions option. | ||
* @param {boolean} [expand=false] Should extensions be expanded. | ||
* @param {boolean} expand Should extensions be expanded. | ||
* @returns {string|null} Resolved extension. | ||
*/ | ||
function resolveExtension(base, extensions, expand = false) { | ||
const stat = pathStat(base); | ||
const paths = ['']; | ||
if (stat) { | ||
@@ -281,3 +248,2 @@ if (stat.isDirectory()) { | ||
} | ||
for (const path of paths) { | ||
@@ -292,6 +258,4 @@ for (const extension of extensions) { | ||
}; | ||
for (const src of srcs) { | ||
const stat = pathStat(`${base}${path}${src}`); | ||
if (stat && !stat.isDirectory()) { | ||
@@ -303,5 +267,5 @@ return dst === null ? `${path}${src}` : `${path}${dst}`; | ||
} | ||
return null; | ||
} | ||
/** | ||
@@ -313,4 +277,2 @@ * Visitor callback for declaration with path. | ||
*/ | ||
function visitDeclarationPath(nodePath, state) { | ||
@@ -320,22 +282,20 @@ const { | ||
} = nodePath.node; | ||
const src = source.value; // Parse options. | ||
const src = source.value; | ||
// Parse options. | ||
const optsSource = (state.opts || {}).source; | ||
if (!optsSource) { | ||
return; | ||
} | ||
const extensions = optsSource.extensions || []; | ||
const ignoreUnresolved = optsSource.ignoreUnresolved || false; // Resolve the file base. | ||
const ignoreUnresolved = optsSource.ignoreUnresolved || false; | ||
// Resolve the file base. | ||
const { | ||
filename | ||
} = state.file.opts; | ||
const resolveBase = _path.default.join(_path.default.dirname(filename), src); | ||
const resolveBase = _path.default.join(_path.default.dirname(filename), src); // Resolve from the base. | ||
// Resolve from the base. | ||
const resolved = resolveExtension(resolveBase, extensions, true); | ||
if (resolved === null) { | ||
@@ -349,2 +309,3 @@ if (!ignoreUnresolved) { | ||
} | ||
/** | ||
@@ -357,4 +318,2 @@ * Visitor callback for declaration with bare import path. | ||
*/ | ||
function visitDeclarationBarePath(nodePath, state, bareImport) { | ||
@@ -364,14 +323,14 @@ const { | ||
} = nodePath.node; | ||
const src = source.value; // Parse options. | ||
const src = source.value; | ||
// Parse options. | ||
const optsSubmodule = (state.opts || {}).submodule; | ||
if (!optsSubmodule) { | ||
return; | ||
} | ||
const extensions = optsSubmodule.extensions || []; | ||
const ignoreUnresolved = optsSubmodule.ignoreUnresolved || false; | ||
const ignoreExports = optsSubmodule.ignoreExports || false; // Resolve the module base, or fail. | ||
const ignoreExports = optsSubmodule.ignoreExports || false; | ||
// Resolve the module base, or fail. | ||
const { | ||
@@ -381,12 +340,12 @@ filename | ||
const moduleName = bareImport.name; | ||
const moduleDir = resolveModuleDir(moduleName, filename); // Optionally ignore modules that have exports. | ||
const moduleDir = resolveModuleDir(moduleName, filename); | ||
// Optionally ignore modules that have exports. | ||
if (ignoreExports && 'exports' in readPackageJson(moduleDir)) { | ||
return; | ||
} // Resolve the file then resolve extension. | ||
} | ||
// Resolve the file then resolve extension. | ||
const resolveBase = `${moduleDir}${bareImport.path}`; | ||
const resolved = resolveExtension(resolveBase, extensions); | ||
if (resolved === null) { | ||
@@ -403,2 +362,3 @@ if (!ignoreUnresolved) { | ||
} | ||
/** | ||
@@ -411,20 +371,15 @@ * Visitor callback for declaration with bare import main. | ||
*/ | ||
function visitDeclarationBareMain(nodePath, state, bareImport) { | ||
// Parse options. | ||
const optsModule = (state.opts || {}).module; | ||
if (!optsModule) { | ||
return; | ||
} | ||
const entry = optsModule.entry || []; | ||
const ignoreExports = optsModule.ignoreExports || false; | ||
if (!entry.length) { | ||
return; | ||
} // Resolve the module base, or fail. | ||
} | ||
// Resolve the module base, or fail. | ||
const { | ||
@@ -434,16 +389,17 @@ filename | ||
const moduleName = bareImport.name; | ||
const moduleDir = resolveModuleDir(moduleName, filename); // Optionally ignore modules that have exports. | ||
const moduleDir = resolveModuleDir(moduleName, filename); | ||
// Optionally ignore modules that have exports. | ||
let pkg = null; | ||
if (ignoreExports && 'exports' in (pkg = readPackageJson(moduleDir))) { | ||
return; | ||
} // Try different entry resolvers. | ||
} | ||
// Try different entry resolvers. | ||
for (const info of entry) { | ||
const entryType = info.type; | ||
let filePath = null; | ||
let extensions = []; // Handle the different types. | ||
let extensions = []; | ||
// Handle the different types. | ||
switch (entryType) { | ||
@@ -457,3 +413,2 @@ case 'package.json': | ||
} | ||
case 'file': | ||
@@ -465,3 +420,2 @@ { | ||
} | ||
default: | ||
@@ -471,19 +425,17 @@ { | ||
} | ||
} // Skip if empty path. | ||
} | ||
// Skip if empty path. | ||
if (!filePath) { | ||
continue; | ||
} // Resolve entry if possible. | ||
} | ||
// Resolve entry if possible. | ||
const resolveBase = _path.default.join(moduleDir, filePath); | ||
const resolved = resolveExtension(resolveBase, extensions); | ||
if (resolved === null) { | ||
continue; | ||
} // Update path and finish. | ||
} | ||
// Update path and finish. | ||
const { | ||
@@ -496,2 +448,3 @@ source | ||
} | ||
/** | ||
@@ -503,4 +456,2 @@ * Visitor callback for declarations. | ||
*/ | ||
function visitDeclaration(nodePath, state) { | ||
@@ -510,28 +461,27 @@ const { | ||
} = nodePath.node; | ||
if (!source) { | ||
return; | ||
} // Get source, check if needs resolving, and how. | ||
} | ||
// Get source, check if needs resolving, and how. | ||
const src = source.value; | ||
const src = source.value; // Ignore any URL imports. | ||
// Ignore any URL imports. | ||
if (importIsUrl(src)) { | ||
return; | ||
} // Ignore any builin modules. | ||
} | ||
// Ignore any builin modules. | ||
if (importIsBuiltin(src)) { | ||
return; | ||
} // Check if file path. | ||
} | ||
// Check if file path. | ||
if (importIsFile(src)) { | ||
visitDeclarationPath(nodePath, state); | ||
return; | ||
} // Check if bare import (a module or submodule). | ||
} | ||
// Check if bare import (a module or submodule). | ||
const bareImport = importBareParse(src); | ||
if (bareImport) { | ||
@@ -543,6 +493,6 @@ if (bareImport.path) { | ||
} | ||
return; | ||
} | ||
} | ||
/** | ||
@@ -554,4 +504,2 @@ * Babel plugin entry point. | ||
// eslint-disable-next-line arrow-body-style, import/no-default-export | ||
var _default = () => { | ||
@@ -569,3 +517,2 @@ return { | ||
}, | ||
/** | ||
@@ -580,3 +527,2 @@ * Visitor callback for export all declarations. | ||
}, | ||
/** | ||
@@ -591,8 +537,6 @@ * Visitor callback for export names declarations. | ||
} | ||
} | ||
}; | ||
}; | ||
exports.default = _default; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "babel-plugin-esm-resolver", | ||
"description": "A Babel plugin for resolving ESM import and export paths", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"keywords": [ | ||
@@ -20,3 +20,3 @@ "babel", | ||
"scripts": { | ||
"clean": "rimraf npm-debug.log* yarn-debug.log* yarn-error.log* lib", | ||
"clean": "rimraf *.log* lib", | ||
"lint": "eslint . --ext js,mjs", | ||
@@ -26,3 +26,3 @@ "format": "prettier -w .", | ||
"build": "babel src --out-dir lib --source-maps true", | ||
"test": "jasmine", | ||
"test": "node ./lib/index.spec.js", | ||
"all": "run-s clean build test lint formatted", | ||
@@ -35,20 +35,17 @@ "watch": "nodemon --exec 'run-s all'", | ||
"author": "Alexander O'Mara", | ||
"copyright": "Copyright (c) 2019-2022 Alexander O'Mara", | ||
"copyright": "Copyright (c) 2019-2023 Alexander O'Mara", | ||
"license": "MPL-2.0", | ||
"devDependencies": { | ||
"@babel/cli": "^7.18.6", | ||
"@babel/core": "^7.18.6", | ||
"@babel/preset-env": "^7.18.6", | ||
"eslint": "^8.19.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-jsdoc": "^39.3.3", | ||
"jasmine": "3.6.1", | ||
"jasmine-spec-reporter": "^7.0.0", | ||
"nodemon": "^2.0.19", | ||
"@babel/cli": "^7.22.10", | ||
"@babel/core": "^7.22.10", | ||
"@babel/preset-env": "^7.22.10", | ||
"eslint": "^8.47.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-import": "^2.28.0", | ||
"eslint-plugin-jsdoc": "^46.4.6", | ||
"nodemon": "^3.0.1", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.7.1", | ||
"rimraf": "^3.0.2", | ||
"source-map-support": "^0.5.21" | ||
"prettier": "^3.0.1", | ||
"rimraf": "^5.0.1" | ||
} | ||
} |
@@ -31,2 +31,4 @@ # Babel Plugin: ESM Resolver | ||
NOTE: The `module` and `submodule` options are generally no longer needed now that conditional exports are available. | ||
# Bugs | ||
@@ -38,3 +40,3 @@ | ||
Copyright (c) 2019-2022 Alexander O'Mara | ||
Copyright (c) 2019-2023 Alexander O'Mara | ||
@@ -41,0 +43,0 @@ Licensed under the Mozilla Public License, v. 2.0. |
Sorry, the diff of this file is not supported yet
54336
0.22%11
-21.43%453
5.1%44
4.76%