kibana/packages/kbn-babel-register/transforms/babel.js
Spencer 1ee97e1657
[kbn/babel-register] improve cache performance (#150261)
After https://github.com/elastic/kibana/pull/146212 it feels like the
babel-register cache is getting invalidated more frequently for some
reason. The current version of the cache only stores a single cache
entry for each file path, which shouldn't be too big of a problem but
with these changes several versions of a file will be cached. The
performance seems about equal, but because the cache contains multiple
versions of a single file we should spend less time transpiling files
when switching branches often.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
2023-02-06 19:50:42 -07:00

28 lines
809 B
JavaScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const { transformCode } = require('@kbn/babel-transform');
/** @type {import('./types').Transform} */
const babelTransform = (path, source, cache) => {
const key = cache.getKey(path, source);
const cached = cache.getCode(key);
if (cached) {
return cached;
}
const result = transformCode(path, source);
cache.update(key, {
code: result.code,
map: result.map,
});
return result.code;
};
module.exports = { babelTransform };