简体   繁体   English

在gulp-inject中包含CDN源

[英]Include CDN sources in gulp-inject

I am trying to get CDN and other HTTP resources into a script that is modified by gulp-inject . 我试图将CDN和其他HTTP资源放入由gulp-inject修改的脚本中。

I created a corresponding issue at the repository. 我在存储库中创建了一个相应的问题

The gist is that I would like something along these lines to work: 要点是我希望沿着这些方向发挥作用:

var sources = [
  "http://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js",
  "./spec/test.js"
]

gulp.task('source', function () {
   gulp.src("src/my.html")
       .pipe(inject(sources))
       .dest("dest/")
})

With that result being the following included in dest/my.html : 该结果包含在dest/my.html

<script src='http://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.js'>
</script>
<script src='/spec/test.js'></script>

Anyone have any thoughts? 有人有什么想法?

I wrote a plugin, gulp-cdnizer , specifically to help with this situation. 我写了一个插件gulp-cdnizer ,专门用来帮助解决这个问题。

It's designed to allow you to keep all your CDN sources local during development, then replace the local path with a CDN path when you build your distribution. 它旨在允许您在开发期间将所有CDN源保持在本地,然后在构建分发时将本地路径替换为CDN路径。

Basically, you install your vendor scripts using bower or just copy-and-paste, and inject them into your HTML using the local path. 基本上,您使用bower安装供应商脚本或只是复制粘贴,并使用本地路径将它们注入HTML。 Then, pipe the results of gulp-inject into gulp-cdnizer , and it will replace the local paths with the CDN path. 然后, gulp-inject gulp-cdnizer gulp-inject的结果gulp-cdnizer ,它将用CDN路径替换本地路径。

gulp.task('source', function () {
   return gulp.src("src/my.html")
       .pipe(inject(sources)) // only local sources
       .pipe(cdnizer([
           {
               package: 'jasmine',
               file: 'bower_components/jasmine/jasmine.js',
               cdn: 'http://cdnjs.cloudflare.com/ajax/libs/jasmine/${version}/jasmine.js'
           }
       ])
       .dest("dest/")
});

I like doing it this way a lot better, because you can still develop offline. 我喜欢这样做得更好,因为你仍然可以离线开发。 The cdnizer library can also handle local fallbacks, which makes sure your page still works if the CDN fails (for whatever reason). cdnizer库还可以处理本地回退,这可以确保在CDN失败时(无论出于何种原因)您的页面仍然有效。

I used gulp-replace for a similar use case: 我使用gulp-replace来获得类似的用例:

html: HTML:

<!-- replace:google-places -->

gulp: 一饮而尽:

return gulp.src(path.join(conf.paths.src, '/*.html'))
    .pipe($.inject(injectStyles, injectOptions))
    .pipe($.inject(injectScripts, injectOptions))
    .pipe($.replace('<!-- replace:google-places -->', replacePlaces))  // <-- gulp-replace
    .pipe(wiredep(_.extend({}, conf.wiredep)))
    .pipe(gulp.dest(path.join(conf.paths.tmp, '/serve')));

replacePlaces: replacePlaces:

const replacePlaces = match => {
    switch (process.env.NODE_ENV){
      case 'dev':
        return '<script src="https://maps.googleapis.com/maps/api/js....."></script>';
      case 'production':
        return '<script src="https://maps.googleapis.com/maps/api/js......"></script>';
      default:
        return match;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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