UNPKG

@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

54 lines (53 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "removeFileExport", { enumerable: true, get: function() { return removeFileExport; } }); const _devkit = require("@nx/devkit"); const _getprojectentrypointpaths = require("../project-analysis/get-project-entry-point-paths"); const _removesourcefileextension = require("../path-utils/remove-source-file-extension"); const _escaperegex = require("../security-utils/escape-regex"); const _treecache = require("../tree-cache"); const _astcache = require("../ast-cache"); function removeFileExport(tree, project, file, cachedTreeExists) { const indexPaths = (0, _getprojectentrypointpaths.getProjectEntryPointPaths)(tree, project); // Find existing index files indexPaths.forEach((indexPath)=>{ if (!cachedTreeExists(tree, indexPath)) { return; } const content = _treecache.treeReadCache.read(tree, indexPath, 'utf-8'); if (!content) { return; } // Remove export for the file const fileWithoutExt = (0, _removesourcefileextension.removeSourceFileExtension)(file); const escapedFile = (0, _escaperegex.escapeRegex)(fileWithoutExt); // Match various export patterns const exportPatterns = [ new RegExp(`export\\s+\\*\\s+from\\s+['"]\\.\\.?/${escapedFile}['"];?\\s*\\n?`, 'g'), new RegExp(`export\\s+\\{[^}]+\\}\\s+from\\s+['"]\\.\\.?/${escapedFile}['"];?\\s*\\n?`, 'g') ]; let updatedContent = content; exportPatterns.forEach((pattern)=>{ updatedContent = updatedContent.replace(pattern, ''); }); if (updatedContent !== content) { // If the file becomes empty or whitespace-only, add export {} // to prevent runtime errors when importing from the package if (updatedContent.trim() === '') { updatedContent = 'export {};\n'; } tree.write(indexPath, updatedContent); _treecache.treeReadCache.invalidateFile(indexPath); _astcache.astCache.invalidate(indexPath); _devkit.logger.verbose(`Removed export from ${indexPath}`); } }); } //# sourceMappingURL=remove-file-export.js.map