-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build-system: Introduce CSS Cache 🚀 (#33313)
* Introduce cache for CSS. Reuses the same infrastructure built for the esbuild cache. * address raghu comments * add file extensions
- Loading branch information
Showing
8 changed files
with
234 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ build/ | |
.amp-dep-check | ||
extensions/**/dist/** | ||
c | ||
.css-cache/ | ||
/dist | ||
dist.3p | ||
dist.tools | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.babel-cache/** | ||
.css-cache/** | ||
.github/ISSUE_TEMPLATE/** | ||
**/package*.json | ||
**/node_modules/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* Copyright 2021 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const crypto = require('crypto'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Cache for storing transformed files on both memory and on disk. | ||
*/ | ||
class TransformCache { | ||
constructor(cacheDir, fileExtension) { | ||
/** @type {string} */ | ||
this.fileExtension = fileExtension; | ||
|
||
/** @type {string} */ | ||
this.cacheDir = cacheDir; | ||
fs.ensureDirSync(cacheDir); | ||
|
||
/** @type {Map<string, Promise<Buffer|string>>} */ | ||
this.transformMap = new Map(); | ||
|
||
/** @type {Set<string>} */ | ||
this.fsCache = new Set(fs.readdirSync(cacheDir)); | ||
} | ||
|
||
/** | ||
* @param {string} hash | ||
* @return {null|Promise<Buffer|string>} | ||
*/ | ||
get(hash) { | ||
const cached = this.transformMap.get(hash); | ||
if (cached) { | ||
return cached; | ||
} | ||
const filename = hash + this.fileExtension; | ||
if (this.fsCache.has(filename)) { | ||
const transformedPromise = fs.readFile( | ||
path.join(this.cacheDir, filename) | ||
); | ||
this.transformMap.set(hash, transformedPromise); | ||
return transformedPromise; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @param {string} hash | ||
* @param {Promise<string>} transformPromise | ||
*/ | ||
set(hash, transformPromise) { | ||
if (this.transformMap.has(hash)) { | ||
throw new Error( | ||
`Read race occured. Attempting to transform a file twice.` | ||
); | ||
} | ||
|
||
this.transformMap.set(hash, transformPromise); | ||
const filepath = path.join(this.cacheDir, hash) + this.fileExtension; | ||
transformPromise.then((contents) => { | ||
fs.outputFile(filepath, contents); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the md5 hash of provided args. | ||
* | ||
* @param {...(string|Buffer)} args | ||
* @return {string} | ||
*/ | ||
function md5(...args) { | ||
const hash = crypto.createHash('md5'); | ||
for (const a of args) { | ||
hash.update(a); | ||
} | ||
return hash.digest('hex'); | ||
} | ||
|
||
/** | ||
* Used to cache file reads, since some (esbuild) will have multiple | ||
* "loads" per file. This batches consecutive reads into a single, and then | ||
* clears its cache item for the next load. | ||
* @private @const {!Map<string, Promise<{hash: string, contents: string}>>} | ||
*/ | ||
const readCache = new Map(); | ||
|
||
/** | ||
* Returns the string contents and hash of the file at the specified path. | ||
* If multiple reads are requested for the same file before the first read has completed, | ||
* the result will be reused. | ||
* | ||
* @param {string} path | ||
* @param {string=} optionsHash | ||
* @return {{contents: string, hash: string}} | ||
*/ | ||
function batchedRead(path, optionsHash) { | ||
let read = readCache.get(path); | ||
if (!read) { | ||
read = fs.promises | ||
.readFile(path) | ||
.then((contents) => ({ | ||
contents, | ||
hash: md5(contents, optionsHash ?? ''), | ||
})) | ||
.finally(() => { | ||
readCache.delete(path); | ||
}); | ||
readCache.set(path, read); | ||
} | ||
|
||
return read; | ||
} | ||
|
||
module.exports = {TransformCache, batchedRead, md5}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.