Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@babel/helper-annotate-as-pure
Advanced tools
Helper function to annotate paths and nodes with #__PURE__ comment
The @babel/helper-annotate-as-pure npm package is a utility within the Babel ecosystem designed to annotate call expressions with a /*#__PURE__*/ comment. This comment hints to JavaScript bundlers and minifiers, such as Terser, that the function call is pure. A pure function call's result can be safely removed if its result is not used, enabling more efficient final bundles by removing unused code.
Annotating a call expression as pure
This feature allows developers to mark specific call expressions as pure, enabling potential removal during the minification process if the result of the call is unused. This is particularly useful for library authors who want to ensure their library's unused parts can be tree-shaken in consumer applications.
import * as t from '@babel/types';
import annotateAsPure from '@babel/helper-annotate-as-pure';
const callExpression = t.callExpression(t.identifier('myFunction'), []);
annotateAsPure(callExpression);
// The callExpression node is now annotated with a /*#__PURE__*/ comment.
Similar to @babel/helper-annotate-as-pure, this package provides a Babel plugin to automatically annotate call expressions and new expressions with /*#__PURE__*/ comments. The key difference is that it operates at the plugin level, automatically applying the annotations across your codebase, rather than requiring manual annotation of each call.
While not a direct alternative, uglifyjs-webpack-plugin interfaces with UglifyJS to minify JavaScript bundles in Webpack projects. It can take advantage of the pure annotations added by @babel/helper-annotate-as-pure to perform tree shaking and dead code elimination, showcasing how such annotations can be utilized in the minification process.
declare export default annotateAsPure(nodeOrPath: Node | NodePath);
import traverse from "@babel/traverse";
import annotateAsPure from "@babel/helper-annotate-as-pure";
// ...
traverse(file, {
CallExpression(path) {
annotateAsPure(path);
},
});
@babel/helper-annotate-as-pure
will append any existing leading comments to the #__PURE__
annotation. Versions of UglifyJS prior to v3.1.0 will ignore these annotations, as they only check the last leading comment for the annotation.
For example, using the Usage
snippet above:
In
const four = /* foo */ add(2, 2);
Out
const four = /* #__PURE__ */ /* foo */ add(2, 2);
FAQs
Helper function to annotate paths and nodes with #__PURE__ comment
The npm package @babel/helper-annotate-as-pure receives a total of 26,830,035 weekly downloads. As such, @babel/helper-annotate-as-pure popularity was classified as popular.
We found that @babel/helper-annotate-as-pure demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.