Coder Social home page Coder Social logo

eslint-plugin-reduce-bundle-size's Introduction

eslint-plugin-mangling-friendly

ESLint plugin for more mangling friendly code, for less bundle size

Feature ✨

  • ESLint plugin for more mangling friendly code
  • some mangling process with safe needs coding rule
  • This plugin provides a restrict to accomplish the above coding rule

Setup

npm install eslint-plugin-mangling-friendly -D # TBD

write your eslint config file

// .eslintrc.js
module.exports = {
    ...,
    parser: '@typescript-eslint/parser',
    plugins: [..., 'mangling-friendly'],
    rules: [
        ...,
        'mangling-friendly/ban-underscore-prefix-on-public-field': 'warn',
        'mangling-friendly/enforce-underscore-prefix-on-private-field': 'warn',
    ]
}

Rules

ban-underscore-prefix-on-public-field

This rule bans underscore prefix on public field. Mangling object field is effective for reducing bundle size, but this is unsafe process. Mangling only private field is the way to reduce bundle size safely. However, current mangling tool(such as terser) cannot use the type information, so it cannot know whether private or not a property is, and Hard privating with (# sharp) will provide increased bundle size because this should be transpiled for more browser support, so we should add prefix _ on private field and should not do that on the other field.

examples

class Hoge {
    private _hoge: string; // not throw warning
    public hoge: string; // not throw warning
    public _bar: string; // throw warning
    public fuga() {} // not throw warning
    public _hello() {} // throw warning
}

const obj = {
    fuga: "hoge" // not throw warning
    _bar: "hoge" // throw warning
}

for more examples, plz see the test.

enforce-underscore-prefix-on-private-field

This rule enforces underscore prefix on private field. Mangling object field is effective for reducing bundle size, but this is unsafe process. Mangling only private field is the way to reduce bundle size safely. However, current mangling tool(such as terser) cannot use the type information, so it cannot know whether private or not a property is, and Hard privating with (# sharp) will provide increased bundle size because this should be transpiled for more browser support, so we should add prefix _ on private field and should not do that on the other field.

examples

class Hoge {
    private _hoge: string; // not throw warning
    public hoge: string; // not throw warning
    private bar: string; // throw warning
    private fuga() {} // throw warning
}

for more examples, plz see the test.

I strongly recommend that ban-underscore-prefix-on-public-field and enforce-underscore-prefix-on-private-field are used together, and mangled with the above mangling rule.

mangle: {
    properties: {
        regex: /^_/,
    }
},

Further optimization is possible by using the ban-computed-property-access rule (described below) to prohibit computed property access, and by making all object properties subject to minify.

ban-computed-property-access

This rule bans computed property access. A computed property access can break minified property.

// mangler can not minify such a fuga property as a default behavior

hoge.fuga = "fuga";
console.log(hoge.fuga);

// into

a.fuga = "fuga";
console.log(a.fuga);

// because such a output code will break with property minifying.

const hogeProp = getRandomlyHogeProperty() // return fuga or bar;
hoge.fuga = "fuga";
hoge.bar = "bar";
console.log(hoge[hogeProp]);

examples

hoge[fuga] // throw warnings
hoge.fuga // not throw warnings

eslint-plugin-reduce-bundle-size's People

Contributors

dependabot[bot] avatar shinyaigeek avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.