Giter VIP home page Giter VIP logo

babel-minify's Introduction

babel-minify

DISCONTINUE NOTICE

This project is discontinued here and I'm contributing here -

https://github.com/babel/babili

Thanks for your interest !!!


Some tools, babel plugins and presets to minify ES6+ code or whatever code babel understands.

Build Status

Try Online

https://boopathi.in/babel-minify/

๐Ÿšจ WARNING: EXPERIMENTAL

Track

Packages overview

import minify from 'gulp-babel-minify';

gulp.task('min', function() {
  return gulp.src('build/temp/app.bundle.js')
    .pipe(minify(opts))
    .pipe(gulp.dest('build/'));
})
  • Node API + CLI
import minify from 'babel-minify';
minify(inputCode, {
  conditionals: true,
  drop_debugger: true
});

More details here - https://github.com/boopathi/babel-minify/blob/master/packages/babel-minify

This is a preset that uses the default options of babel-minify

WARNING: This might cause some regression, depending on what other plugins and presets you use with this preset - because all the plugins are applied in one pass by default in babel. You can enable the passPerPreset option in babel, but then all the babel-minify plugins are still applied in one pass. So, consider using babel-minify NodeAPI or CLI or Gulp task with the options - plugins: [] and presets: [] to pass your other plugins and presets.

{
  "presets": ["min"],
  "comments": false,
  "compact": true,
  "minified": true
}

Sample App Usage

When you bundle your code, remember to split your bundle into multiple packages or at least vendor and your app code separately. Usually, the vendor code will be ES5 compatible and UglifyJS does a better job here. And all the code you write is mostly ES6. You may want to ship this ES6 code to browsers. So we can pass this ES6 code via babel using a specific set of plugins applied in some fashion and make it do the optimizations and minification for you.

webpack.config.js

// webpack.config.js
module.exports = {
  entry: {
    app: './src/app.js'
    vendor: ['react', 'react-router', 'lodash', 'my-polyfills']
  },
  output: {
    path: 'build/webpack',
    filename: '[name].bundle.js'
  }
  plugins: [
    new webpack.optimize.CommonsChunkPlugin('vendor')
  ]
}

So, this would generate two files - vendor.bundle.js & app.bundle.js

gulpfile.js

const uglify = require('gulp-uglify');
const minify = require('gulp-babel-minify');
const webpack = require('webpack');
const config = require('./webpack.config.js');

gulp.task('webpack', function(cb) {
  webpack(config, (err, stats) => {
    if (err) return cb(err);
    console.log(stats.toString());
    cb();
  });
});

gulp.task('minify-vendor', ['webpack'], function() {
  return gulp.src('build/webpack/vendor.bundle.js')
    .pipe(uglify())
    .pipe(gulp.dest('build/minified'));
});

gulp.task('minify-app', ['webpack'], function() {
  return gulp.src('build/webpack/app.bundle.js')
    .pipe(minify())
    .pipe(gulp.dest('build/minified'));
});

LICENSE

http://boopathi.mit-license.org

babel-minify's People

Contributors

boopathi avatar greenkeeperio-bot avatar vigneshshanmugam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jhen0409

babel-minify's Issues

[mangle] global var a

When a global var already exists, there is no binding, and mangler fails to check this.

var x = 1;
var y = a + 1;

and the output is

var a=1,b=a+1;

Perf

As an example, I tried unminified React code on Chrome Canary with the browser version of the minifier and the time taken was around 19s in the worker thread.

  • Calculate individual plugins perf ?

[mangle] vars in blocks

{
  var x = 5;
  function a() {}
}
var x = 6;

returns

{ var b = 5; function b() {} } var a = 6;

The function name a(or something that the mangler will assign) gives this error, doesn't happen for other function names.

not applied babel plugins

Hi, i have problem when i transpiling es6 with using babel-preset-min and plugins babel-add-module-exports

src/index.js

export default (tree, a = {}) => {
    console.log(a, tree);
    return () => {};
};

babel config

"babel": {
    "presets": [
      "es2015",
      "min"
    ],
    "plugins": [
      "add-module-exports"
    ],
    "comments": true,
    "compact": true,
    "minified": true
  }

after run $ babel src/ -d lib/ i get:

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.default=function(b){var c=arguments.length<=1||arguments[1]===void 0?{}:arguments[1];console.log(c,b);return function(){}};

see in end line

else run without presets min i get:

"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=function(tree){var a=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];console.log(a,tree);return function(){}};module.exports=exports["default"];

see in end line

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.