Security News
Python Overtakes JavaScript as Top Programming Language on GitHub
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
@angular-devkit/build-optimizer
Advanced tools
@angular-devkit/build-optimizer is a tool designed to optimize Angular applications by performing various transformations and optimizations on the build output. It helps in reducing the size of the final bundle and improving the performance of the application.
Tree Shaking
Tree shaking is a feature that removes unused code from the final bundle. The code sample demonstrates how to use the buildOptimizer function to perform tree shaking on a given input code.
const { buildOptimizer } = require('@angular-devkit/build-optimizer');
const inputCode = 'import { Component } from "@angular/core";';
const result = buildOptimizer({ content: inputCode, inputFilePath: 'app.component.ts' });
console.log(result.content);
Inlining
Inlining is a feature that replaces external resources (like templates and styles) with inline content. The code sample shows how to use the buildOptimizer function to inline a template string in the input code.
const { buildOptimizer } = require('@angular-devkit/build-optimizer');
const inputCode = 'const template = `<div>{{title}}</div>`;';
const result = buildOptimizer({ content: inputCode, inputFilePath: 'app.component.ts' });
console.log(result.content);
Dead Code Elimination
Dead code elimination is a feature that removes code that is never used or executed. The code sample demonstrates how to use the buildOptimizer function to eliminate dead code from the input code.
const { buildOptimizer } = require('@angular-devkit/build-optimizer');
const inputCode = 'function unusedFunction() { return "I am not used"; }';
const result = buildOptimizer({ content: inputCode, inputFilePath: 'app.component.ts' });
console.log(result.content);
Webpack is a popular module bundler for JavaScript applications. It offers a wide range of optimization features, including tree shaking, code splitting, and minification. Compared to @angular-devkit/build-optimizer, Webpack provides a more comprehensive and flexible set of tools for optimizing and bundling applications.
Rollup is a module bundler that focuses on producing smaller and more efficient bundles. It supports tree shaking and other optimizations similar to @angular-devkit/build-optimizer. Rollup is often used for libraries and smaller projects, whereas @angular-devkit/build-optimizer is specifically tailored for Angular applications.
Terser is a JavaScript parser and mangler/compressor toolkit for ES6+. It is used to minify JavaScript code, reducing the size of the final bundle. While @angular-devkit/build-optimizer includes some minification capabilities, Terser is a dedicated tool for this purpose and can be used in conjunction with other bundlers like Webpack or Rollup.
Angular Build Optimizer contains Angular optimizations applicable to JavaScript code as a TypeScript transform pipeline.
Transformations applied depend on file content:
Non-transform optimizations are applied via the Purify Plugin.
Some of these optimizations add /*@__PURE__*/
comments.
These are used by UglifyJS to identify pure functions that can potentially be dropped.
Static properties are folded into ES5 classes:
// input
var Clazz = (function () { function Clazz() { } return Clazz; }());
Clazz.prop = 1;
// output
var Clazz = (function () { function Clazz() { } Clazz.prop = 1; return Clazz; }());
Angular decorators, property decorators and constructor parameters are removed, while leaving non-Angular ones intact.
// input
import { Injectable, Input } from '@angular/core';
import { NotInjectable } from 'another-lib';
var Clazz = (function () { function Clazz() { } return Clazz; }());
Clazz.decorators = [{ type: Injectable }, { type: NotInjectable }];
Clazz.propDecorators = { 'ngIf': [{ type: Input }] };
Clazz.ctorParameters = function () { return [{type: Injector}]; };
// output
import { Injectable, Input } from '@angular/core';
import { NotInjectable } from 'another-lib';
var Clazz = (function () { function Clazz() { } return Clazz; }());
Clazz.decorators = [{ type: NotInjectable }];
Adds /*@__PURE__*/
comments to top level downleveled class declarations and instantiation.
Webpack library imports are also marked as /*@__PURE__*/
when used with Purify Plugin.
// input
var Clazz = (function () { function Clazz() { } return Clazz; }());
var newClazz = new Clazz();
var newClazzTwo = Clazz();
// output
var Clazz = /*@__PURE__*/ (function () { function Clazz() { } return Clazz; }());
var newClazz = /*@__PURE__*/ new Clazz();
var newClazzTwo = /*@__PURE__*/ Clazz();
TypeScript helpers (__extends/__decorate/__metadata/__param
) are replaced with tslib
imports whenever found.
// input
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
// output
import { __extends } from "tslib";
Performs regex based replacements on all files that add /*@__PURE__*/
comments to downleveled classes, TypeScript
enums and webpack imports (used with Prefix functions)
import { buildOptimizer } from '@angular-devkit/build-optimizer';
const transpiledContent = buildOptimizer({ content: input }).content;
Available options:
export interface BuildOptimizerOptions {
content?: string;
inputFilePath?: string;
outputFilePath?: string;
emitSourceMap?: boolean;
strict?: boolean;
}
const PurifyPlugin = require('@angular-devkit/build-optimizer').PurifyPlugin;
module.exports = {
module: {
rules: [
{
test: /\.js$/,
loader: '@angular-devkit/build-optimizer/webpack-loader'
options: {
sourceMap: false
}
}
]
},
plugins: [
new PurifyPlugin()
]
}
build-optimizer input.js
build-optimizer input.js output.js
purify input.js
purify input.js output.js
FAQs
Angular Build Optimizer
We found that @angular-devkit/build-optimizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
Python becomes GitHub's top language in 2024, driven by AI and data science projects, while AI-powered security tools are gaining adoption.
Security News
Dutch National Police and FBI dismantle Redline and Meta infostealer malware-as-a-service operations in Operation Magnus, seizing servers and source code.
Research
Security News
Socket is tracking a new trend where malicious actors are now exploiting the popularity of LLM research to spread malware through seemingly useful open source packages.