How do I uglify my AngularJS 2 Typescript app file

2020-07-24 03:58发布

问题:

How do I uglify my AngularJS 2 Typescript app files and wire up for a production release using Gulp?

I can uglify the js files but then I have no idea how to use them / wire it up. Currently my code below just cache busts the css and injects it to the index file head.

I used typescript for my angular 2 app and I noticed that when I viewed my website in chrome I could see all the source files for all the .ts and .js code that I have done.

I'm looking to uglify that and make it more secure.

I could do this in angular 1 but in angular 2 I have no idea how to do this.

Thank you in advance.

This is my current index file:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Pool Cover Dev</title>
    <base href="/"></base>

    <!-- inject:css -->    
       <link rel="stylesheet" href="/css/styles.4e04da1a.css"> 
    <!-- endinject -->

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>           
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.34.2/es6-shim.min.js"></script>    
    <script type="text/javascript" src="/safariPolyFix.js"></script>    
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.18.4/system.src.js"></script>   

    <script>
        System.config({
            transpiler: 'typescript',
            defaultJSExtensions: true,  
            typescriptOptions: {
                emitDecoratorMetadata: true,
            },          
            packages: {
                'angular2-google-maps': {
                  defaultExtension: 'js'
                }
            }
        });       
    </script>

    <script type="text/javascript"src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
    <script type="text/javascript"src="https://code.angularjs.org/tools/typescript.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>

    <script type="text/javascript" src="https://code.angularjs.org/2.0.0-beta.0/angular2.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/2.0.0-beta.0/router.js"></script>
    <script type="text/javascript" src="https://code.angularjs.org/2.0.0-beta.0/http.js"></script>  

    <script type="text/javascript" src="/firebase/firebaseNew.js"></script>

  </head>

  <body id="container">

    <app></app>

    <script type="text/javascript">  
      System.import('/app/app.component.js');
    </script>

  </body>
</html>

This is my current gulp file:

var gulp = require('gulp');
var sass = require('gulp-sass');
var inject = require('gulp-inject');
var del = require('delete');
var minifyCss = require('gulp-minify-css');
var uglify = require('gulp-uglify');

var CacheBuster = require('gulp-cachebust');
var cachebust = new CacheBuster();

//1. Delete styles.css
gulp.task('deleteStyle', function() {

    setTimeout(function () {
        del.promise(['src/css/styles.*.css'])
          .then(function() {            
                console.log("Deleted original styles.css");         
                return true;
          });      
    }, 1000);  

});

//2. Make new styles.css
gulp.task('addStyles', function() {

    setTimeout(function () {

        gulp.src('src/sass/styles.scss')
            .pipe(sass().on('error', sass.logError))
            .pipe(minifyCss({compatibility: 'ie8'}))
            .pipe(cachebust.resources())
            .pipe(gulp.dest('src/css/')) 

    }, 3000); 

});

//3. Inject new style.css to index.html file
gulp.task('injectStyle', function() {

    setTimeout(function () {
          var target = gulp.src('src/index.html');
          var sources = gulp.src(['src/css/styles.*.css'], {read: false});

          return target.pipe(inject(sources))
            .pipe(gulp.dest('./src'));

    }, 5000); 

});

//4. Get all js files into folder js
gulp.task('getAllJsFiles', function() {

    setTimeout(function () {

        gulp.src('src/app/**/**/*.js')
            .pipe(cachebust.resources())
            .pipe(gulp.dest('src/app/js/'))

    }, 8000); 

});

gulp.task('default', ['deleteStyle', 'addStyles', 'injectStyle', 'getAllJsFiles']);

回答1:

There is a very nice sample angular2 application that does exactly what you are looking for: angular 2 seed. Start looking of what is done when the following command is run:

npm run build.prod

and follow logic of the corresponding gulp task to see how its done.

Hope this helps.