|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { statSync } from 'node:fs'; |
10 | | -import path from 'node:path'; |
11 | | -import picomatch from 'picomatch'; |
12 | 9 | import { toPosixPath } from '../../../utils/path'; |
13 | | -import { DEFAULT_ASSET_IGNORE, resolveAssets } from '../../../utils/resolve-assets'; |
| 10 | +import { resolveAssets } from '../../../utils/resolve-assets'; |
14 | 11 | import type { NormalizedLibraryOptions } from '../types'; |
15 | 12 | import { type DiskOutputFile, createDiskOutputFile } from './utils'; |
16 | 13 |
|
17 | 14 | /** |
18 | | - * Resolves and collects configured library assets to be emitted to disk, |
19 | | - * and registers their source paths with the watch set. |
| 15 | + * Resolves and collects configured library assets to be emitted to disk. |
20 | 16 | * |
21 | 17 | * @param assets The normalized asset patterns. |
22 | 18 | * @param workspaceRoot The workspace root directory path. |
23 | | - * @param allWatchedFiles Set collecting all watched file paths for watch mode. |
24 | 19 | * @param modifiedFiles Optional set of modified file paths for incremental copying in watch mode. |
25 | 20 | * @returns An array of disk file emission descriptors. |
26 | 21 | */ |
27 | 22 | export async function collectAssetsToEmit( |
28 | 23 | assets: NormalizedLibraryOptions['assets'], |
29 | 24 | workspaceRoot: string, |
30 | | - allWatchedFiles: Set<string>, |
31 | 25 | modifiedFiles?: ReadonlySet<string>, |
32 | 26 | ): Promise<DiskOutputFile[]> { |
33 | | - if (assets.length === 0) { |
| 27 | + if (assets.length === 0 || modifiedFiles?.size === 0) { |
34 | 28 | return []; |
35 | 29 | } |
36 | 30 |
|
37 | | - if (modifiedFiles) { |
38 | | - if (modifiedFiles.size === 0) { |
39 | | - return []; |
40 | | - } |
41 | | - |
42 | | - const matchers = createAssetMatchers(assets, workspaceRoot); |
43 | | - const filesToEmit: DiskOutputFile[] = []; |
44 | | - |
45 | | - for (const file of modifiedFiles) { |
46 | | - const resolvedFile = path.isAbsolute(file) ? file : path.resolve(workspaceRoot, file); |
47 | | - const posixFile = toPosixPath(resolvedFile); |
48 | | - |
49 | | - for (const { asset, posixInputPrefix, isMatch } of matchers) { |
50 | | - if (!posixFile.startsWith(posixInputPrefix)) { |
51 | | - continue; |
52 | | - } |
53 | | - |
54 | | - const relative = posixFile.slice(posixInputPrefix.length); |
55 | | - if (!isMatch(relative)) { |
56 | | - continue; |
57 | | - } |
58 | | - |
59 | | - if (statSync(resolvedFile, { throwIfNoEntry: false })?.isFile()) { |
60 | | - filesToEmit.push(createDiskOutputFile(resolvedFile, path.join(asset.output, relative))); |
61 | | - allWatchedFiles.add(posixFile); |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - return filesToEmit; |
67 | | - } |
68 | | - |
69 | 31 | const resolvedAssets = await resolveAssets(assets, workspaceRoot); |
70 | 32 | const filesToEmit: DiskOutputFile[] = []; |
71 | 33 |
|
72 | 34 | for (const { source, destination } of resolvedAssets) { |
73 | | - filesToEmit.push(createDiskOutputFile(source, destination)); |
74 | | - allWatchedFiles.add(toPosixPath(source)); |
75 | | - } |
76 | | - |
77 | | - return filesToEmit; |
78 | | -} |
79 | | - |
80 | | -/** |
81 | | - * Checks whether any configured library assets were modified. |
82 | | - * |
83 | | - * @param assets The normalized asset patterns. |
84 | | - * @param workspaceRoot The workspace root directory path. |
85 | | - * @param changedFiles Set of changed file paths. |
86 | | - * @returns True if any asset file was modified. |
87 | | - */ |
88 | | -export function checkAssetChanges( |
89 | | - assets: NormalizedLibraryOptions['assets'], |
90 | | - workspaceRoot: string, |
91 | | - changedFiles: ReadonlySet<string>, |
92 | | -): boolean { |
93 | | - if (assets.length === 0 || changedFiles.size === 0) { |
94 | | - return false; |
95 | | - } |
96 | | - |
97 | | - const matchers = createAssetMatchers(assets, workspaceRoot); |
98 | | - |
99 | | - for (const file of changedFiles) { |
100 | | - const resolvedFile = path.isAbsolute(file) ? file : path.resolve(workspaceRoot, file); |
101 | | - const posixFile = toPosixPath(resolvedFile); |
102 | | - |
103 | | - for (const { posixInputPrefix, isMatch } of matchers) { |
104 | | - if (posixFile.startsWith(posixInputPrefix)) { |
105 | | - const relative = posixFile.slice(posixInputPrefix.length); |
106 | | - if (isMatch(relative)) { |
107 | | - return true; |
108 | | - } |
109 | | - } |
| 35 | + if (!modifiedFiles || modifiedFiles.has(toPosixPath(source))) { |
| 36 | + filesToEmit.push(createDiskOutputFile(source, destination)); |
110 | 37 | } |
111 | 38 | } |
112 | 39 |
|
113 | | - return false; |
114 | | -} |
115 | | - |
116 | | -function createAssetMatchers(assets: NormalizedLibraryOptions['assets'], workspaceRoot: string) { |
117 | | - return assets.map((asset) => { |
118 | | - const absInput = path.resolve(workspaceRoot, asset.input); |
119 | | - const posixInput = toPosixPath(absInput).replace(/\/+$/, ''); |
120 | | - const isMatch = picomatch(asset.glob, { |
121 | | - dot: true, |
122 | | - ignore: [...DEFAULT_ASSET_IGNORE, ...(asset.ignore ?? [])], |
123 | | - }); |
124 | | - |
125 | | - return { asset, posixInputPrefix: `${posixInput}/`, isMatch }; |
126 | | - }); |
| 40 | + return filesToEmit; |
127 | 41 | } |
0 commit comments