Skip to content

Commit

Permalink
Optimize babel hashing (#33370)
Browse files Browse the repository at this point in the history
* Avoid JSON stringifying unless absolutely necessary.

* Remove console logs
  • Loading branch information
jridgewell committed Mar 18, 2021
1 parent a309710 commit 28a7428
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions build-system/common/esbuild-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

const argv = require('minimist')(process.argv.slice(2));
const babel = require('@babel/core');
const crypto = require('crypto');
const fs = require('fs-extra');
Expand Down Expand Up @@ -109,11 +108,15 @@ function getEsbuildBabelPlugin(
preSetup = () => {},
postLoad = () => {}
) {
function md5(obj) {
function md5(...args) {
if (!enableCache) {
return '';
}
return crypto.createHash('md5').update(JSON.stringify(obj)).digest('hex');
const hash = crypto.createHash('md5');
for (const a of args) {
hash.update(a);
}
return hash.digest('hex');
}

/**
Expand All @@ -125,10 +128,10 @@ function getEsbuildBabelPlugin(
let read = readCache.get(path);
if (!read) {
read = fs.promises
.readFile(path, 'utf8')
.readFile(path)
.then((contents) => ({
contents,
hash: md5({contents, optionsHash}),
hash: md5(contents, optionsHash),
}))
.finally(() => {
readCache.delete(path);
Expand Down Expand Up @@ -168,7 +171,9 @@ function getEsbuildBabelPlugin(

const babelOptions =
babel.loadOptions({caller: {name: callerName}}) || {};
const optionsHash = md5({babelOptions, argv});
const optionsHash = md5(
JSON.stringify({babelOptions, argv: process.argv.slice(2)})
);

build.onLoad({filter: /\.[cm]?js$/, namespace: ''}, async (file) => {
const filename = file.path;
Expand Down

0 comments on commit 28a7428

Please sign in to comment.