|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { createHash } from 'crypto'; |
| 9 | +import * as findCacheDirectory from 'find-cache-dir'; |
| 10 | +import * as fs from 'fs'; |
| 11 | +import { manglingDisabled } from '../utils/mangle-options'; |
| 12 | +import { CacheKey, ProcessBundleOptions, ProcessBundleResult } from '../utils/process-bundle'; |
| 13 | + |
| 14 | +const cacache = require('cacache'); |
| 15 | +const cacheDownlevelPath = findCacheDirectory({ name: 'angular-build-dl' }); |
| 16 | +const packageVersion = require('../../package.json').version; |
| 17 | + |
| 18 | +// Workaround Node.js issue prior to 10.16 with copyFile on macOS |
| 19 | +// https://github.com/angular/angular-cli/issues/15544 & https://github.com/nodejs/node/pull/27241 |
| 20 | +let copyFileWorkaround = false; |
| 21 | +if (process.platform === 'darwin') { |
| 22 | + const version = process.versions.node.split('.').map(part => Number(part)); |
| 23 | + if (version[0] < 10 || version[0] === 11 || (version[0] === 10 && version[1] < 16)) { |
| 24 | + copyFileWorkaround = true; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +export interface CacheEntry { |
| 29 | + path: string; |
| 30 | + size: number; |
| 31 | + integrity?: string; |
| 32 | +} |
| 33 | + |
| 34 | +export class BundleActionCache { |
| 35 | + constructor(private readonly integrityAlgorithm?: string) {} |
| 36 | + |
| 37 | + static copyEntryContent(entry: CacheEntry | string, dest: fs.PathLike): void { |
| 38 | + if (copyFileWorkaround) { |
| 39 | + try { |
| 40 | + fs.unlinkSync(dest); |
| 41 | + } catch {} |
| 42 | + } |
| 43 | + |
| 44 | + fs.copyFileSync( |
| 45 | + typeof entry === 'string' ? entry : entry.path, |
| 46 | + dest, |
| 47 | + fs.constants.COPYFILE_FICLONE, |
| 48 | + ); |
| 49 | + if (process.platform !== 'win32') { |
| 50 | + // The cache writes entries as readonly and when using copyFile the permissions will also be copied. |
| 51 | + // See: https://github.com/npm/cacache/blob/073fbe1a9f789ba42d9a41de7b8429c93cf61579/lib/util/move-file.js#L36 |
| 52 | + fs.chmodSync(dest, 0o644); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + generateBaseCacheKey(content: string): string { |
| 57 | + // Create base cache key with elements: |
| 58 | + // * package version - different build-angular versions cause different final outputs |
| 59 | + // * code length/hash - ensure cached version matches the same input code |
| 60 | + const algorithm = this.integrityAlgorithm || 'sha1'; |
| 61 | + const codeHash = createHash(algorithm) |
| 62 | + .update(content) |
| 63 | + .digest('base64'); |
| 64 | + let baseCacheKey = `${packageVersion}|${content.length}|${algorithm}-${codeHash}`; |
| 65 | + if (manglingDisabled) { |
| 66 | + baseCacheKey += '|MD'; |
| 67 | + } |
| 68 | + |
| 69 | + return baseCacheKey; |
| 70 | + } |
| 71 | + |
| 72 | + generateCacheKeys(action: ProcessBundleOptions): string[] { |
| 73 | + const baseCacheKey = this.generateBaseCacheKey(action.code); |
| 74 | + |
| 75 | + // Postfix added to sourcemap cache keys when vendor sourcemaps are present |
| 76 | + // Allows non-destructive caching of both variants |
| 77 | + const SourceMapVendorPostfix = !!action.sourceMaps && action.vendorSourceMaps ? '|vendor' : ''; |
| 78 | + |
| 79 | + // Determine cache entries required based on build settings |
| 80 | + const cacheKeys = []; |
| 81 | + |
| 82 | + // If optimizing and the original is not ignored, add original as required |
| 83 | + if ((action.optimize || action.optimizeOnly) && !action.ignoreOriginal) { |
| 84 | + cacheKeys[CacheKey.OriginalCode] = baseCacheKey + '|orig'; |
| 85 | + |
| 86 | + // If sourcemaps are enabled, add original sourcemap as required |
| 87 | + if (action.sourceMaps) { |
| 88 | + cacheKeys[CacheKey.OriginalMap] = baseCacheKey + SourceMapVendorPostfix + '|orig-map'; |
| 89 | + } |
| 90 | + } |
| 91 | + // If not only optimizing, add downlevel as required |
| 92 | + if (!action.optimizeOnly) { |
| 93 | + cacheKeys[CacheKey.DownlevelCode] = baseCacheKey + '|dl'; |
| 94 | + |
| 95 | + // If sourcemaps are enabled, add downlevel sourcemap as required |
| 96 | + if (action.sourceMaps) { |
| 97 | + cacheKeys[CacheKey.DownlevelMap] = baseCacheKey + SourceMapVendorPostfix + '|dl-map'; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return cacheKeys; |
| 102 | + } |
| 103 | + |
| 104 | + async getCacheEntries(cacheKeys: (string | null)[]): Promise<(CacheEntry | null)[] | false> { |
| 105 | + // Attempt to get required cache entries |
| 106 | + const cacheEntries = []; |
| 107 | + for (const key of cacheKeys) { |
| 108 | + if (key) { |
| 109 | + const entry = await cacache.get.info(cacheDownlevelPath, key); |
| 110 | + if (!entry) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + cacheEntries.push({ |
| 114 | + path: entry.path, |
| 115 | + size: entry.size, |
| 116 | + integrity: entry.metadata && entry.metadata.integrity, |
| 117 | + }); |
| 118 | + } else { |
| 119 | + cacheEntries.push(null); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return cacheEntries; |
| 124 | + } |
| 125 | + |
| 126 | + async getCachedBundleResult(action: ProcessBundleOptions): Promise<ProcessBundleResult | null> { |
| 127 | + const entries = action.cacheKeys && await this.getCacheEntries(action.cacheKeys); |
| 128 | + if (!entries) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + const result: ProcessBundleResult = { name: action.name }; |
| 133 | + |
| 134 | + let cacheEntry = entries[CacheKey.OriginalCode]; |
| 135 | + if (cacheEntry) { |
| 136 | + result.original = { |
| 137 | + filename: action.filename, |
| 138 | + size: cacheEntry.size, |
| 139 | + integrity: cacheEntry.integrity, |
| 140 | + }; |
| 141 | + |
| 142 | + BundleActionCache.copyEntryContent(cacheEntry, result.original.filename); |
| 143 | + |
| 144 | + cacheEntry = entries[CacheKey.OriginalMap]; |
| 145 | + if (cacheEntry) { |
| 146 | + result.original.map = { |
| 147 | + filename: action.filename + '.map', |
| 148 | + size: cacheEntry.size, |
| 149 | + }; |
| 150 | + |
| 151 | + BundleActionCache.copyEntryContent(cacheEntry, result.original.filename + '.map'); |
| 152 | + } |
| 153 | + } else if (!action.ignoreOriginal) { |
| 154 | + // If the original wasn't processed (and therefore not cached), add info |
| 155 | + result.original = { |
| 156 | + filename: action.filename, |
| 157 | + size: Buffer.byteLength(action.code, 'utf8'), |
| 158 | + map: |
| 159 | + action.map === undefined |
| 160 | + ? undefined |
| 161 | + : { |
| 162 | + filename: action.filename + '.map', |
| 163 | + size: Buffer.byteLength(action.map, 'utf8'), |
| 164 | + }, |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + cacheEntry = entries[CacheKey.DownlevelCode]; |
| 169 | + if (cacheEntry) { |
| 170 | + result.downlevel = { |
| 171 | + filename: action.filename.replace('es2015', 'es5'), |
| 172 | + size: cacheEntry.size, |
| 173 | + integrity: cacheEntry.integrity, |
| 174 | + }; |
| 175 | + |
| 176 | + BundleActionCache.copyEntryContent(cacheEntry, result.downlevel.filename); |
| 177 | + |
| 178 | + cacheEntry = entries[CacheKey.DownlevelMap]; |
| 179 | + if (cacheEntry) { |
| 180 | + result.downlevel.map = { |
| 181 | + filename: action.filename.replace('es2015', 'es5') + '.map', |
| 182 | + size: cacheEntry.size, |
| 183 | + }; |
| 184 | + |
| 185 | + BundleActionCache.copyEntryContent(cacheEntry, result.downlevel.filename + '.map'); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return result; |
| 190 | + } |
| 191 | +} |
0 commit comments