minify: Does not work at all

Excuse me, I’m totally stuck.

That’s my main.jsx, it has to be converted to main.min.js:

import React, { Component } from "react";
import ReactDOM from "react-dom";
ReactDOM.render(<div>lala</div>,document.getElementById("root"));

That’s my build.js script:

var fs = require("fs");
var browserify = require("browserify");
browserify("./main.jsx")
  .transform("babelify",{
    presets: ["react","es2015","babili"]
  })
  .bundle()
  .pipe(fs.createWriteStream("./main.min.js"));

These are my package.json dependencies:

"babel-minify": "^0.1.12",
"babel-plugin-uglify": "^1.0.2",
"babel-preset-babili": "0.0.4",
"babel-preset-es2015": "^6.16.0",
"babel-preset-react": "^6.16.0",
"babelify": "^7.3.0",
"bootstrap-sass": "^3.3.7",
"browserify": "^13.1.0",
"classnames": "^2.2.5",
"extend": "^3.0.0",
"mobx": "^2.5.2",
"mobx-react": "^3.5.7",
"mobx-react-devtools": "^4.2.6",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"uglify": "^0.1.5"

The result is main.min.jsx with filesize ~ 730KB. If I cut off babili from presets, the result is the same.

What do I do wrong?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 3
  • Comments: 18 (3 by maintainers)

Most upvoted comments

For those who are using babili with webpack, you can try babili-webpack-plugin.

  • A webpack loader operates on single files and babili preset as a webpack loader is going to consider each file to be executed directly in the browser global scope (by default) and will not optimize some things in the toplevel scope. This is going to change and you can opt-in to optimize toplevel scope - #210, #203, …
  • When you exclude node_modules from being run through the babel-loader, babili optimizations are not applied to the excluded files as it doesn’t pass through the minifier.
  • When you use the babel-loader with webpack, the code generated by webpack for the module system doesn’t go through the loader and is not optimized by babili.
  • A webpack plugin can operate on the entire chunk/bundle output and can optimize the whole bundle and you can see some differences in minified output. But this will be a lot slower as the file size is usually really huge. So there is another idea where we can apply some optimizations as a part of the loader and some optimizations in a plugin (for webpack). For that, there is some more work that is done on babili’s side (#162).

Comments

Also, one more thing to note is that by default the preset doesn’t remove comments. So in your babelrc or babel config that you use for babili, you can set comments to false.

{
  presets: ["babili"],
  comments: false
}

I’ve not tried babili with browserify yet, so I can’t comment much there.

I was getting poor results too because it was only doing a per-file minification. The webpack edition matched the size of UglifyJS (thanks @boopathi)

Chart for the curious.

image

I have fallen down the same path. This does work for me, though: https://github.com/boopathi/babili-webpack-plugin.

I re-read the documentation I think the issue is that Babili isn’t operating the same way as UglifyJS or Closure Compiler. There’s only a small amount of minification due to Babili running per file rather than per bundle.

One issue with using Babili as a preset is that then Babili would only run per-file rather than on the whole bundle. Minification usually happens after bundling as with the “UglifyJsPlugin” in webpack. However, running it after bundling would lose the speed benefits (needs to be measured) of doing the transpiling/minification in the same step. Again, this is something we need to think about: how to intergrate or pass more information to the bundler.