UNPKG

ng-packagr

Version:

Compile and package Angular libraries in Angular Package Format (APF)

118 lines • 4.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createFileWatch = createFileWatch; exports.invalidateEntryPointsAndCacheOnFileChange = invalidateEntryPointsAndCacheOnFileChange; const chokidar_1 = require("chokidar"); const node_os_1 = require("node:os"); const node_path_1 = require("node:path"); const rxjs_1 = require("rxjs"); const node_1 = require("../graph/node"); const nodes_1 = require("../ng-package/nodes"); const log_1 = __importDefault(require("../utils/log")); const path_1 = require("../utils/path"); function createFileWatch(basePaths, ignoredPaths = [], poll) { log_1.default.debug(`Watching for changes: basePath: ${basePaths}, ignoredPaths: ${ignoredPaths}`); const watch = (0, chokidar_1.watch)([], { ignoreInitial: true, ignored: [ /\.map$/, /.tsbuildinfo$/, file => { const normalizedPath = (0, path_1.ensureUnixPath)(file); return ignoredPaths.some(f => normalizedPath.startsWith(f)); }, ], persistent: true, usePolling: typeof poll === 'number' ? true : false, interval: typeof poll === 'number' ? poll : undefined, }); const isLinux = (0, node_os_1.platform)() === 'linux'; const handleFileChange = (event, filePath, observer) => { log_1.default.debug(`Watch: Path changed. Event: ${event}, Path: ${filePath}`); if (isLinux) { // Workaround for Linux where chokidar will not handle future events // for files that were unlinked and immediately recreated. watch.unwatch(filePath); watch.add(filePath); } if (event === 'unlinkDir' || event === 'addDir') { // we don't need to trigger on directory removed or renamed as chokidar will fire the changes for each file return; } observer.next({ filePath: (0, path_1.ensureUnixPath)((0, node_path_1.resolve)(filePath)), event, }); }; return { watcher: watch, onFileChange: new rxjs_1.Observable(observer => { watch.on('all', (event, filePath) => handleFileChange(event, filePath, observer)); // eslint-disable-next-line @typescript-eslint/no-misused-promises return () => watch.close(); }), }; } /** * Invalidates entry points and cache when specified files change. * * @returns - Returns `true` if any entry point was invalidated, otherwise `false`. */ function invalidateEntryPointsAndCacheOnFileChange(graph, files, sourcesFileCache) { let invalidatedEntryPoint = false; const allNodesToClean = new Map(); for (const filePath of files) { const changedFileUrl = (0, nodes_1.fileUrl)(filePath); const nodeToClean = graph.find(node => changedFileUrl === node.url); if (!nodeToClean) { continue; } allNodesToClean.set(filePath, nodeToClean); } // delete node that changes const potentialStylesResources = new Set(); for (const [filePath, nodeToClean] of allNodesToClean) { sourcesFileCache.delete(filePath); if (filePath.endsWith('.ts')) { continue; } // if a non ts file changes we need to clean up its direct dependees // this is mainly done for resources such as html and css potentialStylesResources.add(filePath); for (const dependees of nodeToClean.dependees) { const filePath = (0, nodes_1.fileUrlPath)(dependees.url); if (!filePath) { continue; } allNodesToClean.set(filePath, dependees); if (!filePath.endsWith('.ts')) { potentialStylesResources.add(filePath); } } } const entryPoints = graph.filter(nodes_1.isEntryPoint); for (const entryPoint of entryPoints) { let isDirty = false; if (potentialStylesResources.size > 0) { isDirty = !!entryPoint.cache.stylesheetProcessor?.invalidate(potentialStylesResources)?.length; } for (const [filePath, dependent] of allNodesToClean) { if (!entryPoint.dependents.has(dependent)) { continue; } entryPoint.cache.angularDiagnosticCache.delete(filePath); entryPoint.cache.declarationDiagnosticCache?.delete(filePath); entryPoint.cache.analyseSourcesFileCache.delete(filePath); isDirty = true; } if (isDirty) { entryPoint.state = node_1.STATE_PENDING; invalidatedEntryPoint = true; } } return invalidatedEntryPoint; } //# sourceMappingURL=file-watcher.js.map