@nx/js
Version:
109 lines (108 loc) • 4.68 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.addBuildAndWatchDepsTargets = addBuildAndWatchDepsTargets;
exports.isValidPackageJsonBuildConfig = isValidPackageJsonBuildConfig;
const devkit_1 = require("@nx/devkit");
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const path_1 = require("path");
/**
* Allow uses that use incremental builds to run `nx watch-deps` to continuously build all dependencies.
*/
function addBuildAndWatchDepsTargets(workspaceRoot, projectRoot, targets, options, pmc) {
let projectName;
const projectJsonPath = (0, path_1.join)(workspaceRoot, projectRoot, 'project.json');
const packageJsonPath = (0, path_1.join)(workspaceRoot, projectRoot, 'package.json');
if ((0, node_fs_1.existsSync)(projectJsonPath)) {
const projectJson = (0, devkit_1.readJsonFile)(projectJsonPath);
projectName = projectJson.name;
}
else if ((0, node_fs_1.existsSync)(packageJsonPath)) {
const packageJson = (0, devkit_1.readJsonFile)(packageJsonPath);
projectName = packageJson.nx?.name ?? packageJson.name;
}
if (!projectName)
return;
if (projectName) {
const buildDepsTargetName = options.buildDepsTargetName ?? 'build-deps';
targets[buildDepsTargetName] = {
dependsOn: ['^build'],
};
targets[options.watchDepsTargetName ?? 'watch-deps'] = {
continuous: true,
dependsOn: [buildDepsTargetName],
command: `${pmc.exec} nx watch --projects ${projectName} --includeDependentProjects -- ${pmc.exec} nx ${buildDepsTargetName} ${projectName}`,
};
}
}
function isValidPackageJsonBuildConfig(tsConfig, workspaceRoot, projectRoot) {
const resolvedProjectPath = (0, node_path_1.isAbsolute)(projectRoot)
? (0, node_path_1.relative)(workspaceRoot, projectRoot)
: projectRoot;
const packageJsonPath = (0, path_1.join)(workspaceRoot, resolvedProjectPath, 'package.json');
if (!(0, node_fs_1.existsSync)(packageJsonPath)) {
// If the package.json file does not exist.
// Assume it's valid because it would be using `project.json` instead.
return true;
}
const packageJson = (0, devkit_1.readJsonFile)(packageJsonPath);
const outDir = tsConfig.options.outFile
? (0, node_path_1.dirname)(tsConfig.options.outFile)
: tsConfig.options.outDir;
const resolvedOutDir = outDir
? (0, node_path_1.resolve)(workspaceRoot, resolvedProjectPath, outDir)
: undefined;
const isPathSourceFile = (path) => {
if (resolvedOutDir) {
const pathToCheck = (0, node_path_1.resolve)(workspaceRoot, resolvedProjectPath, path);
return !pathToCheck.startsWith(resolvedOutDir);
}
const ext = (0, node_path_1.extname)(path);
// Check that the file extension is a TS file extension. As the source files are in the same directory as the output files.
return ['.ts', '.tsx', '.cts', '.mts'].includes(ext);
};
// Checks if the value is a path within the `src` directory.
const containsInvalidPath = (value) => {
if (typeof value === 'string') {
return isPathSourceFile(value);
}
else if (typeof value === 'object') {
return Object.entries(value).some(([currentKey, subValue]) => {
// Skip types and development conditions
if (currentKey === 'types' || currentKey === 'development') {
return false;
}
if (typeof subValue === 'string') {
return isPathSourceFile(subValue);
}
return false;
});
}
return false;
};
const exports = packageJson?.exports;
// Check the `.` export if `exports` is defined.
if (exports) {
if (typeof exports === 'string') {
return !isPathSourceFile(exports);
}
if (typeof exports === 'object' && '.' in exports) {
return !containsInvalidPath(exports['.']);
}
// Check other exports if `.` is not defined or valid.
for (const key in exports) {
if (key !== '.' && containsInvalidPath(exports[key])) {
return false;
}
}
return true;
}
// If `exports` is not defined, fallback to `main` and `module` fields.
const buildPaths = ['main', 'module'];
for (const field of buildPaths) {
if (packageJson[field] && isPathSourceFile(packageJson[field])) {
return false;
}
}
return true;
}
;