Giter VIP home page Giter VIP logo

assets-squasher's Introduction

About

Assets squasher is exactly what it says on the tin: it takes an HTML file and replaces all included scripts with just one and the same for stylesheets.

One caveat here, it's meant to be used for single page applications. There you typically have one entry point HTML file which links all the necessary scripts and stylesheets. Like this one:

<!DOCTYPE html>

<html ng-app="app" ng-controller="MainController">
  <head>
    <script defer src="/bower_components/angular/angular.min.js"></script>
    <script defer src="/bower_components/angular-route/angular-route.min.js"></script>
    <script defer src="/bower_components/angular-animate/angular-animate.min.js"></script>
    <script defer src="/bower_components/angular-ui-bootstrap/dist/ui-bootstrap-tpls-0.11.0.min.js"></script>
    <script defer src="/app.js"></script>
    <script defer src="/services.js"></script>

    <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/app.css">
  </head>

  <body>
    <div ng-view>Loading ...</div>
  </body>
</html>

Now let's save it into app.html (I'm assuming that all the assets exist). When you run bundle exec assets-asquasher app.html build.html, you'll get build/build.min.js and its source map, build/build.min.css and also build.html which will look like this:

<!DOCTYPE html>

<html ng-app="app" ng-controller="MainController">
  <head>
    <script src="/build/build.min.js"></script>
    <link rel="stylesheet" href="/build/build.min.css">
  </head>

  <body>
    <div ng-view>Loading ...</div>
  </body>
</html>

Why Is It Useful?

On production you want to use a single minified JS file and a single minified CSS file. Why? Because speed has significant impact on conversions. Yes, it's all cached after the first request, but how many times have you closed a web page before it even loaded?

On the other hand in development you want to include each unminified script separately, so the backtraces make sense[1].

Ultimately this the simplest solution: no need to change your code like you'd have to with say RequireJS. No need to define dependencies in an extra file โ€“ it's already there!

Installation

# Assuming you have Node.js already installed.
npm -g uglify-js uglifycss
gem install assets-asquasher

TODO

Since RubyGems is a piece of crap and couldn't fucking help it but to create an idiotic wrapper around the original file, our DATA are fucking gone. Fuck you RubyGems, fuck you, fuck you, fuck you!

Footnotes

[1] Or alternatively you could use one build file with source maps, but then you have to user guard or something to constantly rebuild it.

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.