userMod2 userMod2 - 6 months ago 87
AngularJS Question

Angular - Best practice for merging all JS files in index.html

I have my Angular app in which my index.html lists all my services and controllers JS files.

e.g. A lot of these lines

<script src="services/loginService.js"></script>


For going into production I'd like to simplify this and just reference one js file (which is a minified version).

What tools have people found useful for doing this?

I'm guessing gulp?

Any advice would be appreciated.

Thanks.

Answer

In gulp, you can do it as follows:

Here is a sample gulpfile I have set up locally. You can use the scripts task or the useref task. Useref allows you to specify which files should be minified.

// Include gulp
var gulp = require('gulp');

// Include Our Plugins
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var gulpIf = require('gulp-if');
var rename = require('gulp-rename');
var babel = require('gulp-babel');
var webserver = require('gulp-webserver');
var connect = require('gulp-connect');
var gulpIgnore = require('gulp-ignore');
var useref = require('gulp-useref');
var minifyCSS = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
var rename = require('gulp-rename');
var cssnano = require('gulp-cssnano');
var imagemin = require('gulp-imagemin');

var outputDir = 'dist';
var jsLintIgnore = 'app/js/bootstrap-3.3.6/**/*.js';

// run a local webserver from app directory
gulp.task('webserver', function() {
  gulp.src('app')
    .pipe(webserver({
      livereload: true,
      fallback: './src/index.html',
      //port: 8000, // Default is 8000
      //directoryListing: true,
      open: true
    }));
});

// Lint Task - checks any JavaScript file in our js/ directory and makes sure there are no errors in our code.
gulp.task('lint', function() {
    return gulp.src('app/js/**/*.js')
        .pipe(jshint())
        //.pipe(gulpIgnore.include(jsLintIgnore))
        .pipe(jshint.reporter('default'));
});


gulp.task('useref', function(){
  return gulp.src('app/*.html')
    .pipe(useref())
    // Minifies only if it's a JavaScript file
    .pipe(gulpIf('js/**/*.js', uglify()))
    .pipe(gulpIf('css/**/*.css', cssnano()))
    .pipe(gulp.dest('dist'))
});

// Compile Our Sass - compiles any of our Sass files in our scss/ directory into CSS and saves the compiled CSS file in our dist/css directory.
gulp.task('sass', function() {
    return gulp.src('app/scss/**/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('app/css'));
});

gulp.task('css', function(){
    gulp.src('app/css/**/*.css')
        .pipe(minifyCSS())
        .pipe(rename('style.min.css'))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9'))
        .pipe(gulp.dest('dist/css'))
});

gulp.task('images', function(){
  return gulp.src('app/images/**/*.+(png|jpg|gif|svg)')
  .pipe(imagemin())
  .pipe(gulp.dest('dist/images'))
});

// Concatenate & Minify JS - concatenates all JavaScript files in our js/ directory and saves the ouput to our dist/js directory. Then gulp takes 
//   that concatenated file, minifies it, renames it and saves it to the dist/js directory alongside the concatenated file.
gulp.task('scripts', function() {
    return gulp.src('app/js/**/*.js')
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist'))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'));
});

// Watch Files For Changes - run tasks as we make changes to our files.
gulp.task('watch', function() {
    gulp.watch('app/js/**/*.js', ['lint', 'scripts']);
    gulp.watch('app/scss/**/*.scss', ['sass']);
});

// Default Task - used as a grouped reference to our other tasks. This will be the task that is ran upon entering gulp into the command line without any additional parameters.
gulp.task('default', ['webserver', 'lint', 'sass', 'useref','images', 'watch']);

In your html you would wrap the js files you wanted minifed with the following:

<!--build:js js/main.min.js -->

<script src="/js/script1.js"></script> 
<script src="/js/script2.js"></script> 
<script src="/js/script3.js"></script> 

<!-- endbuild -->

This would create a minified file called: js/main.min.js in your dist folder. As you can see I'm doing the same with my css files. You can wrap them in tags as well to target certain or all css files:

  <!--build:css css/styles.min.css-->
  <link rel="stylesheet" href="/css/styles1.css">
  <link rel="stylesheet" href="/css/styles2.css">
  <link rel="stylesheet" href="/css/styles3.css">  
  <!--endbuild-->   

This gulp file assumes the following directory structure, but you can change it to suit your environment:

- Root Folder
  - /app (development environment)
    - /css
    - /images
    - /js
    - /scss
    - index.html
  - /dist (production version of site here)
    - /css
    - /images
    - /js 
    - index.html
  - /node_modules
  - bower.json
  - gulpfile.js
  - package.json

Here is a great YouTube series on getting started with gulp. https://www.youtube.com/watch?v=dwSLFai8ovQ

Now having said all that, I prefer webpack. It has a bit of a steeper learning curve though so Gulp may be a good place to start.