jonkemp / gulp-useref

Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.
MIT License
705 stars 93 forks source link

Does not work properly with lazypipe, gulp-if and gulp-uglify #202

Closed tsarapke closed 8 years ago

tsarapke commented 8 years ago

I have following html code:

<!-- build:js js/vendor.js -->
<script src="js/vendor/jquery-2.2.1.min.js"></script>
<script src="js/vendor/material.min.js"></script>
<script src="js/vendor/SmoothScroll.js"></script>
<script src="js/vendor/jquery.touchSwipe.min.js"></script>
<script src="js/vendor/jquery.smooth-scroll.js"></script>
<!-- endbuild -->

<!-- build:js js/app.js -->
<script src="js/app/main.js"></script>
<!-- endbuild -->

And following task:

function minCondition(type) {

    return function (file) {

        var fileData = path.parse(file.path),
            fileExt = fileData.ext.replace('.', ''),
            minRegexp = new RegExp('\.min$');

        return !!(fileExt === type && !minRegexp.test(fileData.name));
    };
}

gulp.task('html', function() {
    return gulp.src(path.join(config.root.src, config.html.src, config.html.files))
        .pipe(gulpSwig({data: data}))
        .pipe(gulpUseref({}, lazypipe().pipe( gulpIf, minCondition('js'), gulpUglify() ) ))
        .pipe(gulp.dest(config.root.dist));
});

Actually, it's works well, but with one minus: when I've got results as vendor.js and app.js, i have a copy of app.js inside vendor.js before other scripts.

Or, if i change task to follow:

gulp.task('html', function() {
    return gulp.src(path.join(config.root.src, config.html.src, config.html.files))
        .pipe(gulpSwig({data: data}))
        .pipe(gulpUseref({}, lazypipe().pipe( gulpIf, '*.js', gulpUglify() ) ))
        .pipe(gulp.dest(config.root.dist));
});

Then i will have a concatenated content of app.js with vendor.js inside app.js and vendor.js (i.e. duplicating)

Have you any suggestion? Thanks.

tsarapke commented 8 years ago

Hmm... I found solution, but I don't understand why previous solution not works properly. Here what I have done:

gulp.task('html', function() {
    return gulp.src(path.join(config.root.src, config.html.src, config.html.files))
        .pipe(gulpSwig({data: data}))
        .pipe(gulpUseref({},
            lazypipe().pipe(
                function() {
                    return gulpIf(minCondition('js'), gulpUglify());
                }
            ))
        )
        .pipe(gulp.dest(config.root.dist));

I hope that it will help for people who met a similar problem and they couldn't find solution.

jonkemp commented 8 years ago

Closing.