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