Gulp-cdnizer - Not replacing source links

做~自己de王妃 提交于 2019-12-12 23:34:08

问题


I'm trying to get gulp-cdnizer to work but all it does is take in the file and spits out the file unprocessed to the destination folder. Perhaps my option settings are wrong or the gulp task isn't working. How do you configure the gulp-cdnizer to work with a custom bower_components path?

Gulp task:

gulp.task('makeCdn', function () {

    return gulp.src('./app/**/*.html')
        .pipe(cdnizer({
            bowerComponents: './vendor/bower_components',
            allowRev: true,
            allowMin: true,
                files: [
                    {
                        file: '/vendor/bower_components/angular/angular.js',
                        package: 'angular',
                        // angular has a bizarre version string inside bower, with extra information.
                        // using major.minor.patch directly ensures it works with the CDN
                        cdn: '//ajax.googleapis.com/ajax/libs/angularjs/${ major }.${ minor }.${ patch }/angular.min.js'
                    }
                ]
            })
            .pipe(gulp.dest('./dist/'))
        );

});

HTML file './app/test/test.html':

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>

        <script src="/vendor/bower_components/angular/angular.js"></script>
        <script src="/vendor/bower_components/bootstrap/dist/js/bootstrap.js"></script>

    </body>
</html>

Folder Structure:

What needs to be done to get gulp-cdnizer to work?


回答1:


Turns out you have a couple of typos in your gulpfile. You have everything wrapped inside the first gulp.src().pipe(), rather than being chained.

If you strip the arguments and whitespace, you can see what you have is this:

return gulp.src(...)
    .pipe(
        cdnizer(...).pipe(gulp.dest(...))
    );

When it should be:

return gulp.src(...)
   .pipe(cdnizer(...))
   .pipe(gulp.dest('./dist/'));

Honestly, I'm not sure why this failed the way it did, but the result of cdnizer() was being bypassed.

Simply fix your nesting / parentheses, like so, and you'll be all set.

gulp.task('makeCdn', function () {

    return gulp.src('./app/**/*.html')
        .pipe(cdnizer({
            bowerComponents: './vendor/bower_components',
            allowRev: true,
            allowMin: true,
                files: [
                    {
                        file: '/vendor/bower_components/angular/angular.js',
                        package: 'angular',
                        // angular has a bizarre version string inside bower, with extra information.
                        // using major.minor.patch directly ensures it works with the CDN
                        cdn: '//ajax.googleapis.com/ajax/libs/angularjs/${ major }.${ minor }.${ patch }/angular.min.js'
                    }
                ]
            }))
        .pipe(gulp.dest('./dist/'));
});

You can also eliminate the wrapper object and default options, if your .bowerrc is in the right place:

gulp.task('makeCdn', function () {

    return gulp.src('./app/**/*.html')
        .pipe(cdnizer([
                    {
                        file: '/vendor/bower_components/angular/angular.js',
                        package: 'angular',
                        // angular has a bizarre version string inside bower, with extra information.
                        // using major.minor.patch directly ensures it works with the CDN
                        cdn: '//ajax.googleapis.com/ajax/libs/angularjs/${ major }.${ minor }.${ patch }/angular.min.js'
                    }
                ]))
        .pipe(gulp.dest('./dist/'));
});


来源:https://stackoverflow.com/questions/23033657/gulp-cdnizer-not-replacing-source-links

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!