@harves/nx-node-esm-plugin
Version:
Node executor including ESM module resolution for buildable libraries within Nx workspaces.
97 lines • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveFileToRunFromPackageJson = exports.fileExists = void 0;
const devkit_1 = require("@nx/devkit");
const fs_1 = require("fs");
const path_1 = require("path");
const chalk = require("chalk");
const resolve_pkg_maps_1 = require("resolve-pkg-maps");
/**
* Check whether file exists.
* @param pth
* @returns `true` if the pth exists and is an ordinary file
*/
function fileExists(pth) {
var _a, _b;
return (_b = (_a = (0, fs_1.statSync)(pth, { throwIfNoEntry: false })) === null || _a === void 0 ? void 0 : _a.isFile()) !== null && _b !== void 0 ? _b : false;
}
exports.fileExists = fileExists;
const legacyMainResolveExtensions = [
'',
'.js',
'.json',
'.node',
'/index.js',
'/index.json',
'/index.node',
];
const legacyMainResolveFallbacks = [
'./index.js',
'./index.json',
'./index.node',
];
/**
* Resolves the file to run for the specified `package.json` based
* library/application.
*
* Attempts resolution via the `exports` entry (using `resolve-pkg-maps`)
* and then via Node's legacy CommonJS resolution using the `main` field.
*
* @param outputPath the Nx project output path as a absolute path, usually in the `dist/` folder
* @returns the absolute path of the file to run
*
* @see libs/node-runner/src/lib/node-loader.ts
*/
function resolveFileToRunFromPackageJson(outputPath) {
// read package.json
const pkgJsonPath = (0, path_1.join)(outputPath, 'package.json');
if (!fileExists(pkgJsonPath)) {
devkit_1.logger.error(`Missing package.json: ${chalk.bold(pkgJsonPath)}`);
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let pkgJson;
try {
pkgJson = JSON.parse((0, fs_1.readFileSync)(pkgJsonPath, { encoding: 'utf-8' }));
}
catch (error) {
devkit_1.logger.error(`Error loading: ${chalk.bold(pkgJsonPath)}: ${chalk.red('' + error)}`);
return undefined;
}
// attempt resolution via exports
if (pkgJson.exports) {
const resolvedPaths = (0, resolve_pkg_maps_1.resolveExports)(pkgJson.exports, '', ['node', 'import']);
if (resolvedPaths === null || resolvedPaths === void 0 ? void 0 : resolvedPaths[0]) {
const resolvedPath = (0, path_1.join)(outputPath, resolvedPaths[0]);
if (fileExists(resolvedPath)) {
return resolvedPath;
}
else {
devkit_1.logger.error(`Missing export: ${chalk.bold(pkgJsonPath)}: ${chalk.red(resolvedPath)}`);
return undefined;
}
}
}
// Legacy CommonJS main resolution
if (pkgJson.main) {
for (const ext of legacyMainResolveExtensions) {
const resolvedPath = (0, path_1.join)(outputPath, `${pkgJson.main}${ext}`);
if (fileExists(resolvedPath)) {
return resolvedPath;
}
}
devkit_1.logger.error(`Unable to resolve package main entry: ${chalk.bold(pkgJsonPath)}: ${chalk.red(pkgJson.main)}`);
}
else {
for (const index of legacyMainResolveFallbacks) {
const resolvedPath = (0, path_1.join)(outputPath, index);
if (fileExists(resolvedPath)) {
return resolvedPath;
}
}
devkit_1.logger.error(`Unable to resolve package fallback main entry: ${chalk.bold(pkgJsonPath)}`);
}
return undefined;
}
exports.resolveFileToRunFromPackageJson = resolveFileToRunFromPackageJson;
//# sourceMappingURL=package-json-utils.js.map