@harves/nx-node-esm-plugin
Version:
Node executor including ESM module resolution for buildable libraries within Nx workspaces.
91 lines • 4.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileToRunCorrectPath = exports.getFileToRun = exports.getRelativeDirectoryToProjectRoot = void 0;
const devkit_1 = require("@nx/devkit");
const utils_1 = require("nx/src/tasks-runner/utils");
const path = require("path");
const path_1 = require("path");
const chalk = require("chalk");
const fileutils_1 = require("nx/src/utils/fileutils");
/**
*
* @param file
* @param projectRoot
* @returns
*
* @see https://github.com/nrwl/nx/blob/b9e190d22b03ca792fbd1a9665d3344167ccaf03/packages/js/src/utils/get-main-file-dir.ts#L4-L11
*/
function getRelativeDirectoryToProjectRoot(file, projectRoot) {
const dir = (0, path_1.dirname)(file);
const relativeDir = (0, devkit_1.normalizePath)((0, path_1.relative)(projectRoot, dir));
return relativeDir === '' ? `./` : `./${relativeDir}/`;
}
exports.getRelativeDirectoryToProjectRoot = getRelativeDirectoryToProjectRoot;
/**
*
* @param context
* @param project
* @param buildOptions
* @param buildTargetExecutor
* @returns
*
* @see https://github.com/nrwl/nx/blob/b9e190d22b03ca792fbd1a9665d3344167ccaf03/packages/js/src/executors/node/node.impl.ts#L360C1-L425C2
*/
function getFileToRun(context, project,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
buildOptions, buildTargetExecutor) {
var _a, _b;
// If using run-commands or another custom executor, then user should set
// outputFileName, but we can try the default value that we use.
if (!(buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.outputPath) && !(buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.outputFileName)) {
// If we are using crystal for infering the target, we can use the output path from the target.
// Since the output path has a token for the project name, we need to interpolate it.
// {workspaceRoot}/dist/{projectRoot} -> dist/my-app
const outputPath = (_b = (_a = project.data.targets[buildOptions.target]) === null || _a === void 0 ? void 0 : _a.outputs) === null || _b === void 0 ? void 0 : _b[0];
if (outputPath) {
const outputFilePath = (0, utils_1.interpolate)(outputPath, {
projectName: project.name,
projectRoot: project.data.root,
workspaceRoot: '',
});
return path.join(outputFilePath, 'main.js');
}
const fallbackFile = path.join('dist', project.data.root, 'main.js');
devkit_1.logger.warn(`Build option ${chalk.bold('outputFileName')} not set for ${chalk.bold(project.name)}. Using fallback value of ${chalk.bold(fallbackFile)}.`);
return (0, path_1.join)(context.root, fallbackFile);
}
let outputFileName = buildOptions.outputFileName;
if (!outputFileName) {
const fileName = `${path.parse(buildOptions.main).name}.js`;
if (buildTargetExecutor === '@nx/js:tsc' ||
buildTargetExecutor === '@nx/js:swc') {
outputFileName = path.join(getRelativeDirectoryToProjectRoot(buildOptions.main, project.data.root), fileName);
}
else {
outputFileName = fileName;
}
}
return (0, path_1.join)(context.root, buildOptions.outputPath, outputFileName);
}
exports.getFileToRun = getFileToRun;
/**
*
* @param fileToRun
* @returns
*
* @see https://github.com/nrwl/nx/blob/b9e190d22b03ca792fbd1a9665d3344167ccaf03/packages/js/src/executors/node/node.impl.ts#L412-L425
*/
function fileToRunCorrectPath(fileToRun) {
if ((0, fileutils_1.fileExists)(fileToRun))
return fileToRun;
// const extensionsToTry = ['.cjs', '.mjs', '.cjs.js', '.esm.js'];
const extensionsToTry = ['.mjs', '.esm.js', '.cjs', '.cjs.js']; // Prefer ESM
for (const ext of extensionsToTry) {
const file = fileToRun.replace(/\.js$/, ext);
if ((0, fileutils_1.fileExists)(file))
return file;
}
throw new Error(`Could not find ${fileToRun}. Make sure your build succeeded.`);
}
exports.fileToRunCorrectPath = fileToRunCorrectPath;
//# sourceMappingURL=node-utils.js.map