UNPKG

1.37 kBJavaScriptView Raw
1/**
2 * Created by Rodey on 2015/11/5.
3 * gupack publish
4 * 项目发布,
5 * 如果在gulp-config中有设置hostname属性
6 * 则将项目中所有资源链接替换,添加hostname
7 */
8'use strict';
9
10const fs = require('fs'),
11 path = require('path'),
12 through2 = require('through2'),
13 PluginError = require('plugin-error'),
14 Statics = require('../config/statics');
15
16const PLUGIN_NAME = 'publish';
17
18//将压缩后的内容替换到html中
19function publish(options) {
20 let statics = new Statics(options);
21
22 return through2.obj(function(file, enc, next) {
23 if (file.isStream()) {
24 this.emit('error', new PluginError(PLUGIN_NAME, 'Stream content is not supported'));
25 return next(null, file);
26 }
27
28 if (file.isBuffer()) {
29 let extname = path.extname(file.path);
30 if (statics.isExecute === true && !!statics.hostname && statics.testExt.test(extname)) {
31 try {
32 let content = statics.execture(file) || file.contents;
33 file.contents = new Buffer(content);
34 } catch (err) {
35 this.emit('error', new PluginError(PLUGIN_NAME, err.message));
36 }
37 }
38 }
39 this.push(file);
40 return next();
41 });
42}
43
44module.exports = publish;