@nxworker/workspace
Version:
Nx plugin providing generators for managing workspace files, including the move-file generator for safely moving files between projects while updating all imports
71 lines (70 loc) • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "updateRelativeImportsToAliasInMovedFile", {
enumerable: true,
get: function() {
return updateRelativeImportsToAliasInMovedFile;
}
});
const _devkit = require("@nx/devkit");
const _nodepath = require("node:path");
const _treecache = require("../tree-cache");
const _jscodeshiftutils = require("../jscodeshift-utils");
const _removesourcefileextension = require("../path-utils/remove-source-file-extension");
const _escaperegex = require("../security-utils/escape-regex");
const _getprojectentrypointpaths = require("../project-analysis/get-project-entry-point-paths");
/**
* Checks if a file is exported from the project's entrypoint
*/ function isFileExported(tree, project, file, cachedTreeExistsFn) {
const indexPaths = (0, _getprojectentrypointpaths.getProjectEntryPointPaths)(tree, project);
const fileWithoutExt = (0, _removesourcefileextension.removeSourceFileExtension)(file);
const escapedFile = (0, _escaperegex.escapeRegex)(fileWithoutExt);
return indexPaths.some((indexPath)=>{
if (!cachedTreeExistsFn(tree, indexPath)) {
return false;
}
const content = _treecache.treeReadCache.read(tree, indexPath, 'utf-8');
if (!content) {
return false;
}
// Support: export ... from "path"
// Support: export * from "path"
// Support: export { Something } from "path"
const exportPattern = new RegExp(`export\\s+(?:\\*|\\{[^}]+\\}|.+)\\s+from\\s+['"]\\.?\\.?/.*${escapedFile}['"]`);
return exportPattern.test(content);
});
}
function updateRelativeImportsToAliasInMovedFile(tree, normalizedSource, normalizedTarget, sourceProject, sourceImportPath, cachedTreeExistsFn) {
const content = _treecache.treeReadCache.read(tree, normalizedTarget, 'utf-8');
if (!content) {
return;
}
_devkit.logger.verbose(`Updating relative imports in moved file to use alias imports to source project`);
const sourceRoot = sourceProject.sourceRoot || sourceProject.root;
// Use jscodeshift to update relative imports to alias
(0, _jscodeshiftutils.updateImportSpecifierPattern)(tree, normalizedTarget, (specifier)=>{
// Only process relative imports
if (!specifier.startsWith('.')) {
return false;
}
// Resolve the import path relative to the ORIGINAL (source) file location
const sourceDir = _nodepath.posix.dirname(normalizedSource);
const resolvedPath = _nodepath.posix.join(sourceDir, specifier);
// Check if this import points to a file in the source project
return resolvedPath.startsWith(sourceRoot + '/');
}, (importPath)=>{
// Resolve the import path relative to the ORIGINAL (source) file location
const sourceDir = _nodepath.posix.dirname(normalizedSource);
const resolvedPath = _nodepath.posix.join(sourceDir, importPath);
// Check if the resolved file is exported from the source project's entrypoint
const relativeFilePathInSource = _nodepath.posix.relative(sourceRoot, resolvedPath);
const isExported = isFileExported(tree, sourceProject, relativeFilePathInSource, cachedTreeExistsFn);
if (!isExported) {
_devkit.logger.warn(`Import '${importPath}' in ${normalizedTarget} is being converted to '${sourceImportPath}', but the imported file is not exported from the source project's entrypoint. This may result in an invalid import.`);
}
return sourceImportPath;
});
}
//# sourceMappingURL=update-relative-imports-to-alias-in-moved-file.js.map