mirror-jetstream/startup-helper/BabelCacheBuster.mjs
Camillo Bruni 97f348ec9d Merge main
- Merge upstream main branch
- Partially merge https://github.com/WebKit/JetStream/pull/167/ to
  display sub-times

Change-Id: I9f30772ff2d6d068b09e43df3cb57da35b231ca2
Reviewed-on: https://chromium-review.googlesource.com/c/external/github.com/WebKit/JetStream/+/6911418
Reviewed-by: Victor Gomes <victorgomes@chromium.org>
2025-09-03 04:51:51 -07:00

27 lines
954 B
JavaScript

// Babel plugin that adds CACHE_BUST_COMMENT to every function body.
const CACHE_BUST_COMMENT = "ThouShaltNotCache";
export default function({ types: t }) {
return {
visitor: {
Function(path) {
const bodyPath = path.get("body");
// Handle arrow functions: () => "value"
// Convert them to block statements: () => { return "value"; }
if (!bodyPath.isBlockStatement()) {
const newBody = t.blockStatement([t.returnStatement(bodyPath.node)]);
path.set("body", newBody);
}
// Handle empty function bodies: function foo() {}
// Add an empty statement so we have a first node to attach the comment to.
if (path.get("body.body").length === 0) {
path.get("body").pushContainer("body", t.emptyStatement());
}
const firstNode = path.node.body.body[0];
t.addComment(firstNode, "leading", CACHE_BUST_COMMENT);
},
},
};
};