2015-07-13 53 views
3

我的一個.html文件進口/socket.io/socket.io.js的,我想這硫化文件,但忽略了腳本代碼,進口socket.io如何讓gulp-vulcanize忽略socket.io.js?

我寫了下面gulp任務:

// vulcanize HTML files 
var vulcanizeHtmlSrc = 'views/**/*.html', 
    vulcanizeHtmlDst = 'build/views'; 
gulp.task('vulcanize', function() { 
gulp.src(vulcanizeHtmlSrc) 
    .pipe(vulcanize({ 
     excludes: ['//fonts.googleapis.com/*', '/socket.io/socket.io.js'], 
     stripExcludes: false 
    })) 
    .pipe(gulp.dest(vulcanizeHtmlDst));  
}); 

我仍然得到以下錯誤:

ERROR finding /socket.io/socket.io.js 

我在做什麼錯?

回答

2
// vulcanize HTML files 
var vulcanizeHtmlSrc = 'views/**/*.html', 
vulcanizeHtmlDst = 'build/views'; 
gulp.task('vulcanize', function() { 
gulp.src(vulcanizeHtmlSrc) 
    .pipe(vulcanize({ 
     excludes: ['//fonts.googleapis.com/*', 
      './bower_components/polymer/polymer.html' 
     ], 
     stripExcludes: false, 
     inlineScripts: true, 
     inlineCss: true, 
     implicitStrip: true, 
     stripComments: true 
    })) 
    // pipe to injectString to add script tags that cause an issue with vulcanize 
    // e.g. <script src="/socket.io/socket.io.js"> 
    // Error caused if the script is added in index.html itself 
    // ERROR finding /socket.io/socket.io.js 
    .pipe(injectString.before('<script class="usesSocket.io">', '<script src="/socket.io/socket.io.js"></script>\n')) 
    .pipe(gulp.dest(vulcanizeHtmlDst)); 
}); 

只是一個類添加到script標籤需要socket.io.js和使用gulp-inject-string模塊硫化後插入socket.io.js。這有點哈克。無論哪種方式硫化仍然會導致很多錯誤,我建議人們避開聚合物(如果應用程序正在開發用於生產),直到它完全穩定並具有更好的文檔。

1

如果您希望將socket.io腳本保留在原始源代碼中,您還可以刪除這些腳本,然後像@Torcellite所示的那樣注入腳本。我使用開始和結束註釋來標記HTML中的該塊。

HTML

<!-- gulp:remove --> 
<script src="/socket.io/socket.io.js"></script> 
<!-- gulp:endremove --> 

gulp.task

// 1 - remove server scripts for vulcanization 
    var start_comment = "gulp:remove", 
     end_comment = "gulp:endremove", 
     pattern = new RegExp("(\\<!--\\s" + start_comment + "\\s--\\>)(.*\\n)*(\\<!--\\s" + end_comment + "\\s--\\>)", "g"); 
    .pipe(require('gulp-tap')(function(file) { 
     file.contents = new Buffer(String(file.contents).replace(pattern, "")); 
    })) 
    // 2 - pipe vulcanize... 
    // 3 - pipe injectString back in... 
+0

管道硫化未硫化從抽頭該文件。 – 1Mayur