简体   繁体   中英

Missing code after uglifying JS IIFE with gulp-uglify

So I'm starting to use Gulp to automate my workflow,I was playing around with it but I ran into that when execute the task to concatenate and uglify multiple files some code that is inside in a IIFE is missing. To concatenate use gulp-usref .

  // a-file.js
  (function() {
    'use strict';
    console.log('a-file');
    var variableA = 'variable a';
  })();

  // anotherfile.js
  (function() {
    'use strict';
    console.log('another-file');
    var variableB = 'variable b';
    var myFunction = function() {
      return 'return function value';
    };
  })();

   // main.js
  (function() {
    'use strict';
    console.log('Hello main');
    var variableC = 'variable c';
  })();

  // gulpfile.js
  gulp.task('useref', function() {
     return gulp.src('app/*.jade')
       .pipe(jade({pretty: true}))
       .pipe(useref())
       .pipe(gulpIf('*.js', uglify()))
       .pipe(gulpIf('*.css', cssnano()))
       .pipe(gulp.dest('dist'))
  })

When I run the task the file is concatenated and uglified but variableA, variableB, variableC and myFunction don't appear, just the console.log's.

!function(){"use strict";console.log("a-file")}(),function(){"use strict";console.log("another-file")}(),function(){"use strict";console.log("Hello main")}();

I'm missing a step? or how I can solve this?

Uglifier(包括gulp-uglify)会删除未使用的代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM