Socket
Socket
Sign inDemoInstall

@angular-devkit/build-optimizer

Package Overview
Dependencies
3
Maintainers
3
Versions
481
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@angular-devkit/build-optimizer

Angular Build Optimizer


Version published
Weekly downloads
362K
increased by5.43%
Maintainers
3
Install size
60.3 MB
Created
Weekly downloads
 

Package description

What is @angular-devkit/build-optimizer?

@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.

What are @angular-devkit/build-optimizer's main functionalities?

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);

Other packages similar to @angular-devkit/build-optimizer

Readme

Source

Angular Build Optimizer

Angular Build Optimizer contains Angular optimizations applicable to JavaScript code as a TypeScript transform pipeline.

This package is deprecated and should not be used. It has always been experimental (never hit 1.0.0) and was an internal package for the Angular CLI. All the relevant functionality has been moved into @angular-devkit/build-angular.

Available optimizations

Transformations applied depend on file content:

Some of these optimizations add /*@__PURE__*/ comments. These are used by JS optimization tools to identify pure functions that can potentially be dropped.

Class fold

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;
})();

Scrub file

Angular decorators, property decorators and constructor parameters are removed, while leaving non-Angular ones intact.

// input
import { Injectable, Input, Component } from '@angular/core';
import { NotInjectable, NotComponent, NotInput } 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 }];
};
var ComponentClazz = (function () {
  function ComponentClazz() {}
  __decorate([Input(), __metadata('design:type', Object)], Clazz.prototype, 'selected', void 0);
  __decorate(
    [NotInput(), __metadata('design:type', Object)],
    Clazz.prototype,
    'notSelected',
    void 0,
  );
  ComponentClazz = __decorate(
    [
      NotComponent(),
      Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css'],
      }),
    ],
    ComponentClazz,
  );
  return ComponentClazz;
})();

// output
import { Injectable, Input, Component } from '@angular/core';
import { NotInjectable, NotComponent } from 'another-lib';
var Clazz = (function () {
  function Clazz() {}
  return Clazz;
})();
Clazz.decorators = [{ type: NotInjectable }];
var ComponentClazz = (function () {
  function ComponentClazz() {}
  __decorate(
    [NotInput(), __metadata('design:type', Object)],
    Clazz.prototype,
    'notSelected',
    void 0,
  );
  ComponentClazz = __decorate([NotComponent()], ComponentClazz);
  return ComponentClazz;
})();

Prefix functions

Adds /*@__PURE__*/ comments to top level downleveled class declarations and instantiation.

Warning: this transform assumes the file is a pure module. It should not be used with unpure modules.

// 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();

Prefix Classes

Adds /*@__PURE__*/ to downleveled TypeScript classes.

// input
var ReplayEvent = (function () {
  function ReplayEvent(time, value) {
    this.time = time;
    this.value = value;
  }
  return ReplayEvent;
})();

// output
var ReplayEvent = /*@__PURE__*/ (function () {
  function ReplayEvent(time, value) {
    this.time = time;
    this.value = value;
  }
  return ReplayEvent;
})();

Import tslib

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';

Wrap enums

Wrap downleveled TypeScript enums in a function, and adds /*@__PURE__*/ comment.

// input
var ChangeDetectionStrategy;
(function (ChangeDetectionStrategy) {
  ChangeDetectionStrategy[(ChangeDetectionStrategy['OnPush'] = 0)] = 'OnPush';
  ChangeDetectionStrategy[(ChangeDetectionStrategy['Default'] = 1)] = 'Default';
})(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));

// output
var ChangeDetectionStrategy = /*@__PURE__*/ (function () {
  var ChangeDetectionStrategy = {};
  ChangeDetectionStrategy[(ChangeDetectionStrategy['OnPush'] = 0)] = 'OnPush';
  ChangeDetectionStrategy[(ChangeDetectionStrategy['Default'] = 1)] = 'Default';
  return ChangeDetectionStrategy;
})();

Library Usage

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;
  isSideEffectFree?: boolean;
}

Webpack loader usage:

import { BuildOptimizerWebpackPlugin } from '@angular-devkit/build-optimizer';

module.exports = {
  plugins: [
    new BuildOptimizerWebpackPlugin(),
  ]
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: '@angular-devkit/build-optimizer/webpack-loader',
        options: {
          sourceMap: false
        }
      }
    ]
  }
}

CLI usage

build-optimizer input.js
build-optimizer input.js output.js

Keywords

FAQs

Last updated on 01 Dec 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc