-
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.
🏗 Reorganize browserify caching code (#32297)
Co-authored-by: Jake Fried <samouri@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
79 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* 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 browserifyPersistFs = require('browserify-persist-fs'); | ||
const crypto = require('crypto'); | ||
const fs = require('fs-extra'); | ||
const globby = require('globby'); | ||
const {dotWrappingWidth} = require('./logging'); | ||
|
||
/** | ||
* Used for persistent babel caching during tests. Created using a hash object | ||
* that includes the repo package lockfile and various parts of the build-system | ||
* so that the cache is invalidated if any of them changes, and files are | ||
* retransformed. | ||
* @return {function} | ||
*/ | ||
function getPersistentBrowserifyCache() { | ||
let wrapCounter = 0; | ||
const createHash = (input) => | ||
crypto.createHash('sha1').update(input).digest('hex'); | ||
const hashObject = { | ||
deps: createHash(fs.readFileSync('./package-lock.json')), | ||
build: globby | ||
.sync([ | ||
'build-system/**/*.js', | ||
'!build-system/eslint-rules', | ||
'!**/test/**', | ||
]) | ||
.map((f) => createHash(fs.readFileSync(f))), | ||
}; | ||
const logger = () => { | ||
process.stdout.write('.'); | ||
if (++wrapCounter >= dotWrappingWidth) { | ||
wrapCounter = 0; | ||
process.stdout.write('\n'); | ||
} | ||
}; | ||
const cache = browserifyPersistFs('.karma-cache', hashObject, logger); | ||
cache.gc( | ||
{maxAge: 1000 * 60 * 60 * 24 * 7}, // Refresh cache if more than a week old | ||
() => {} // swallow errors | ||
); | ||
return cache; | ||
} | ||
|
||
module.exports = { | ||
getPersistentBrowserifyCache, | ||
}; |
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
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