@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
42 lines (41 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "updateImportPathsToPackageAlias", {
enumerable: true,
get: function() {
return updateImportPathsToPackageAlias;
}
});
const _devkit = require("@nx/devkit");
const _nodepath = require("node:path");
const _removesourcefileextension = require("../path-utils/remove-source-file-extension");
const _jscodeshiftutils = require("../jscodeshift-utils");
function updateImportPathsToPackageAlias(tree, project, sourceFilePath, targetPackageAlias, excludeFilePaths, getProjectSourceFilesFn) {
const normalizedSourceWithoutExt = (0, _devkit.normalizePath)((0, _removesourcefileextension.removeSourceFileExtension)(sourceFilePath));
const excludeSet = new Set([
sourceFilePath,
...excludeFilePaths
]);
const sourceFiles = getProjectSourceFilesFn(tree, project.root);
for (const normalizedFilePath of sourceFiles){
if (excludeSet.has(normalizedFilePath)) {
continue;
}
// Use jscodeshift to update imports that reference the source file
(0, _jscodeshiftutils.updateImportSpecifierPattern)(tree, normalizedFilePath, (specifier)=>{
// Match relative imports that reference the source file
if (!specifier.startsWith('.')) {
return false;
}
// Resolve the import specifier to an absolute path
const importerDir = _nodepath.posix.dirname(normalizedFilePath);
const resolvedImport = _nodepath.posix.join(importerDir, specifier);
// Normalize and compare with source file (both without extension)
const normalizedResolvedImport = (0, _devkit.normalizePath)((0, _removesourcefileextension.removeSourceFileExtension)(resolvedImport));
return normalizedResolvedImport === normalizedSourceWithoutExt;
}, ()=>targetPackageAlias);
}
}
//# sourceMappingURL=update-import-paths-to-package-alias.js.map