Authored by xuqi

home build

@@ -5,7 +5,19 @@ @@ -5,7 +5,19 @@
5 */ 5 */
6 6
7 var gulp = require('gulp'), 7 var gulp = require('gulp'),
  8 + fs = require('fs'),
  9 + ftp = require('gulp-ftp'),
  10 + gutil = require('gulp-util'),
  11 + concat = require('gulp-concat'),
8 compass = require('gulp-compass'), 12 compass = require('gulp-compass'),
  13 + exec = require('child_process').exec,
  14 + mkdirp = require('mkdirp'),
  15 + uglify = require('gulp-uglify'),
  16 + Package = require('father').SpmPackage,
  17 + transport = require('gulp-spm'),
  18 + request = require('request'),
  19 + // md5 = require("gulp-md5"),
  20 + rmdir = require('rmdir')
9 server = require('gulp-develop-server'); 21 server = require('gulp-develop-server');
10 22
11 gulp.task('default', ['server', 'server:restart', 'compass-watch', 'compass']); 23 gulp.task('default', ['server', 'server:restart', 'compass-watch', 'compass']);
@@ -37,4 +49,142 @@ gulp.task('compass', function() { @@ -37,4 +49,142 @@ gulp.task('compass', function() {
37 sass: 'public/sass' 49 sass: 'public/sass'
38 }) 50 })
39 ) 51 )
40 -});  
  52 +});
  53 +
  54 +//PRODUCTION
  55 +//cdn
  56 +var config = JSON.parse(fs.readFileSync('./package.json').toString()),
  57 + distRoot = 'dist/yohobuy-mobile/',
  58 + distDir = {
  59 + js: distRoot + config.name + '/' + config.version,
  60 + css: distRoot + config.name + '/' + config.version,
  61 + image: distRoot + config.name + '/assets/images',
  62 + fonts: distRoot + config.name + '/assets/fonts'
  63 + },
  64 + cdnDomain = 'http://cdn.yoho.cn/',
  65 + ftpConfig = {
  66 + host: '58.213.133.26',
  67 + user: 'php',
  68 + pass: 'yoho9646'
  69 + };
  70 +
  71 +gulp.task('dist', function() {
  72 + var ftpstream = ftp(ftpConfig);
  73 + return gulp.src('dist/**/')
  74 + .pipe(ftpstream)
  75 + .pipe(gutil.noop());
  76 +});
  77 +
  78 +//拷贝fonts+images到发布目录
  79 +gulp.task('assets', function() {
  80 + gulp.src('public/img/**')
  81 + .pipe(gulp.dest(distDir.image));
  82 + gulp.src('public/fonts/*')
  83 + .pipe(gulp.dest(distDir.fonts));
  84 +});
  85 +
  86 +//compass整合所有css到index后发布到发布目录
  87 +gulp.task('compass-production', function() {
  88 + gulp.src('public/sass/*.scss')
  89 + .pipe(
  90 + compass({
  91 + css: distDir.css,
  92 + sass: 'public/sass',
  93 + image: distDir.image,
  94 + font: distDir.fonts,
  95 + http_path: '/',
  96 + style: 'compressed'
  97 + })
  98 + )
  99 + .on('error', function(error) {
  100 + console.log(error);
  101 + this.emit('end');
  102 + });
  103 +});
  104 +
  105 +
  106 +//JS build
  107 +gulp.task('build', ['libs-build', 'index-build']);
  108 +
  109 +gulp.task('libs-build', ['pre-libs', 'concat-libs', 'min-libs', 'clear-libs']);
  110 +
  111 +gulp.task('index-build', ['libs-build'], function() {
  112 + var pkg = new Package(__dirname);
  113 + return gulp.src(pkg.main)
  114 + .pipe(transport({
  115 + pkg: pkg
  116 + }))
  117 + .pipe(concat('index-debug.js'))
  118 + .pipe(gulp.dest(distDir.js))
  119 + .pipe(uglify())
  120 + .pipe(concat('index.js'))
  121 + .pipe(gulp.dest(distDir.js));
  122 +});
  123 +
  124 +//SPM 打包库文件
  125 +gulp.task('pre-libs', function() {
  126 + libPkgPre();
  127 + var pkg = new Package(__dirname);
  128 + return gulp.src(pkg.main)
  129 + .pipe(transport({
  130 + pkg: pkg
  131 + }))
  132 + .pipe(gulp.dest(distDir.js));
  133 +});
  134 +
  135 +//合并库文件
  136 +gulp.task('concat-libs', ['pre-libs'], function() {
  137 + return gulp.src(['public/sea.js', distDir.js + '/libs.js'])
  138 + .pipe(concat('libs-debug.js'))
  139 + .pipe(gulp.dest(distDir.js));
  140 +});
  141 +
  142 +//压缩库文件
  143 +gulp.task('min-libs', ['pre-libs'], function() {
  144 + return gulp.src(['public/sea.js', distDir.js + '/libs.js'])
  145 + .pipe(concat('libs.js'))
  146 + .pipe(uglify())
  147 + // .pipe(md5())
  148 + .pipe(gulp.dest(distDir.js));
  149 +
  150 +});
  151 +
  152 +//清除过程文件
  153 +gulp.task('clear-libs', ['concat-libs', 'min-libs'], function() {
  154 + fs.renameSync('package_bak.json', 'package.json'); //恢复原包配置文件
  155 + fs.unlinkSync('./libs.js'); //删除入口文件
  156 + // fs.unlinkSync(distDir.js + '/libs.js');
  157 +});
  158 +
  159 +
  160 +//库文件的入口文件和过程文件的生成
  161 +function libPkgPre() {
  162 + var obj = {
  163 + name: config.name,
  164 + version: config.version,
  165 + spm: config.spm
  166 + },
  167 + libCon, libsjs = '',
  168 + key,
  169 + libDir = distDir.js + '';
  170 + obj.spm.main = 'libs.js';
  171 + obj.spm.buildArgs = '--idleading {{}} --include all';
  172 +
  173 + libCon = JSON.stringify(obj);
  174 + fs.renameSync('package.json', 'package_bak.json');
  175 + fs.writeFileSync('package.json', libCon); //生成库文件pkg配置
  176 + var packages = [];
  177 + for (key in obj.spm.dependencies) {
  178 + if (obj.spm.inside && obj.spm.inside[key]) {
  179 + packages.push(obj.spm.inside[key]);
  180 + } else {
  181 + packages.push(key);
  182 + }
  183 + }
  184 + for (var i = 0; i < packages.length; i++) {
  185 + libsjs += "require('" + packages[i] + "');";
  186 + }
  187 +
  188 + fs.writeFileSync('libs.js', libsjs); //写入口文件
  189 + return libDir;
  190 +}
1 var yohobuyMobile; 1 var yohobuyMobile;
2 2
  3 +require('./public/js/public');
  4 +
3 module.exports = yohobuyMobile; 5 module.exports = yohobuyMobile;
@@ -34,6 +34,12 @@ @@ -34,6 +34,12 @@
34 "gulp-develop-server": "^0.4.2", 34 "gulp-develop-server": "^0.4.2",
35 "gulp-ftp": "^1.0.3", 35 "gulp-ftp": "^1.0.3",
36 "gulp-util": "^3.0.4", 36 "gulp-util": "^3.0.4",
37 - "hbs": "^3.0.1" 37 + "hbs": "^3.0.1",
  38 + "father": "^1.0.0",
  39 + "gulp-spm": "^0.11.3",
  40 + "gulp-uglify": "^1.2.0",
  41 + "mkdirp": "^0.5.1",
  42 + "request": "^2.58.0",
  43 + "rmdir": "^1.1.0"
38 } 44 }
39 } 45 }
  1 +/*! Sea.js 2.3.0 | seajs.org/LICENSE.md */
  2 +!function(a,b){function c(a){return function(b){return{}.toString.call(b)=="[object "+a+"]"}}function d(){return z++}function e(a){return a.match(C)[0]}function f(a){for(a=a.replace(D,"/"),a=a.replace(F,"$1/");a.match(E);)a=a.replace(E,"/");return a}function g(a){var b=a.length-1,c=a.charAt(b);return"#"===c?a.substring(0,b):".js"===a.substring(b-2)||a.indexOf("?")>0||"/"===c?a:a+".js"}function h(a){var b=u.alias;return b&&w(b[a])?b[a]:a}function i(a){var b=u.paths,c;return b&&(c=a.match(G))&&w(b[c[1]])&&(a=b[c[1]]+c[2]),a}function j(a){var b=u.vars;return b&&a.indexOf("{")>-1&&(a=a.replace(H,function(a,c){return w(b[c])?b[c]:a})),a}function k(a){var b=u.map,c=a;if(b)for(var d=0,e=b.length;e>d;d++){var f=b[d];if(c=y(f)?f(a)||a:a.replace(f[0],f[1]),c!==a)break}return c}function l(a,b){var c,d=a.charAt(0);if(I.test(a))c=a;else if("."===d)c=f((b?e(b):u.cwd)+a);else if("/"===d){var g=u.cwd.match(J);c=g?g[0]+a.substring(1):a}else c=u.base+a;return 0===c.indexOf("//")&&(c=location.protocol+c),c}function m(a,b){if(!a)return"";a=h(a),a=i(a),a=j(a),a=g(a);var c=l(a,b);return c=k(c)}function n(a){return a.hasAttribute?a.src:a.getAttribute("src",4)}function o(a,b,c){var d=K.createElement("script");if(c){var e=y(c)?c(a):c;e&&(d.charset=e)}p(d,b,a),d.async=!0,d.src=a,R=d,Q?P.insertBefore(d,Q):P.appendChild(d),R=null}function p(a,b,c){function d(){a.onload=a.onerror=a.onreadystatechange=null,u.debug||P.removeChild(a),a=null,b()}var e="onload"in a;e?(a.onload=d,a.onerror=function(){B("error",{uri:c,node:a}),d()}):a.onreadystatechange=function(){/loaded|complete/.test(a.readyState)&&d()}}function q(){if(R)return R;if(S&&"interactive"===S.readyState)return S;for(var a=P.getElementsByTagName("script"),b=a.length-1;b>=0;b--){var c=a[b];if("interactive"===c.readyState)return S=c}}function r(a){var b=[];return a.replace(U,"").replace(T,function(a,c,d){d&&b.push(d)}),b}function s(a,b){this.uri=a,this.dependencies=b||[],this.exports=null,this.status=0,this._waitings={},this._remain=0}if(!a.seajs){var t=a.seajs={version:"2.3.0"},u=t.data={},v=c("Object"),w=c("String"),x=Array.isArray||c("Array"),y=c("Function"),z=0,A=u.events={};t.on=function(a,b){var c=A[a]||(A[a]=[]);return c.push(b),t},t.off=function(a,b){if(!a&&!b)return A=u.events={},t;var c=A[a];if(c)if(b)for(var d=c.length-1;d>=0;d--)c[d]===b&&c.splice(d,1);else delete A[a];return t};var B=t.emit=function(a,b){var c=A[a],d;if(c){c=c.slice();for(var e=0,f=c.length;f>e;e++)c[e](b)}return t},C=/[^?#]*\//,D=/\/\.\//g,E=/\/[^/]+\/\.\.\//,F=/([^:/])\/+\//g,G=/^([^/:]+)(\/.+)$/,H=/{([^{]+)}/g,I=/^\/\/.|:\//,J=/^.*?\/\/.*?\//,K=document,L=location.href&&0!==location.href.indexOf("about:")?e(location.href):"",M=K.scripts,N=K.getElementById("seajsnode")||M[M.length-1],O=e(n(N)||L);t.resolve=m;var P=K.head||K.getElementsByTagName("head")[0]||K.documentElement,Q=P.getElementsByTagName("base")[0],R,S;t.request=o;var T=/"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\/\*[\S\s]*?\*\/|\/(?:\\\/|[^\/\r\n])+\/(?=[^\/])|\/\/.*|\.\s*require|(?:^|[^$])\brequire\s*\(\s*(["'])(.+?)\1\s*\)/g,U=/\\\\/g,V=t.cache={},W,X={},Y={},Z={},$=s.STATUS={FETCHING:1,SAVED:2,LOADING:3,LOADED:4,EXECUTING:5,EXECUTED:6};s.prototype.resolve=function(){for(var a=this,b=a.dependencies,c=[],d=0,e=b.length;e>d;d++)c[d]=s.resolve(b[d],a.uri);return c},s.prototype.load=function(){var a=this;if(!(a.status>=$.LOADING)){a.status=$.LOADING;var c=a.resolve();B("load",c);for(var d=a._remain=c.length,e,f=0;d>f;f++)e=s.get(c[f]),e.status<$.LOADED?e._waitings[a.uri]=(e._waitings[a.uri]||0)+1:a._remain--;if(0===a._remain)return a.onload(),b;var g={};for(f=0;d>f;f++)e=V[c[f]],e.status<$.FETCHING?e.fetch(g):e.status===$.SAVED&&e.load();for(var h in g)g.hasOwnProperty(h)&&g[h]()}},s.prototype.onload=function(){var a=this;a.status=$.LOADED,a.callback&&a.callback();var b=a._waitings,c,d;for(c in b)b.hasOwnProperty(c)&&(d=V[c],d._remain-=b[c],0===d._remain&&d.onload());delete a._waitings,delete a._remain},s.prototype.fetch=function(a){function c(){t.request(g.requestUri,g.onRequest,g.charset)}function d(){delete X[h],Y[h]=!0,W&&(s.save(f,W),W=null);var a,b=Z[h];for(delete Z[h];a=b.shift();)a.load()}var e=this,f=e.uri;e.status=$.FETCHING;var g={uri:f};B("fetch",g);var h=g.requestUri||f;return!h||Y[h]?(e.load(),b):X[h]?(Z[h].push(e),b):(X[h]=!0,Z[h]=[e],B("request",g={uri:f,requestUri:h,onRequest:d,charset:u.charset}),g.requested||(a?a[g.requestUri]=c:c()),b)},s.prototype.exec=function(){function a(b){return s.get(a.resolve(b)).exec()}var c=this;if(c.status>=$.EXECUTING)return c.exports;c.status=$.EXECUTING;var e=c.uri;a.resolve=function(a){return s.resolve(a,e)},a.async=function(b,c){return s.use(b,c,e+"_async_"+d()),a};var f=c.factory,g=y(f)?f(a,c.exports={},c):f;return g===b&&(g=c.exports),delete c.factory,c.exports=g,c.status=$.EXECUTED,B("exec",c),g},s.resolve=function(a,b){var c={id:a,refUri:b};return B("resolve",c),c.uri||t.resolve(c.id,b)},s.define=function(a,c,d){var e=arguments.length;1===e?(d=a,a=b):2===e&&(d=c,x(a)?(c=a,a=b):c=b),!x(c)&&y(d)&&(c=r(""+d));var f={id:a,uri:s.resolve(a),deps:c,factory:d};if(!f.uri&&K.attachEvent){var g=q();g&&(f.uri=g.src)}B("define",f),f.uri?s.save(f.uri,f):W=f},s.save=function(a,b){var c=s.get(a);c.status<$.SAVED&&(c.id=b.id||a,c.dependencies=b.deps||[],c.factory=b.factory,c.status=$.SAVED,B("save",c))},s.get=function(a,b){return V[a]||(V[a]=new s(a,b))},s.use=function(b,c,d){var e=s.get(d,x(b)?b:[b]);e.callback=function(){for(var b=[],d=e.resolve(),f=0,g=d.length;g>f;f++)b[f]=V[d[f]].exec();c&&c.apply(a,b),delete e.callback},e.load()},t.use=function(a,b){return s.use(a,b,u.cwd+"_use_"+d()),t},s.define.cmd={},a.define=s.define,t.Module=s,u.fetchedList=Y,u.cid=d,t.require=function(a){var b=s.get(s.resolve(a));return b.status<$.EXECUTING&&(b.onload(),b.exec()),b.exports},u.base=O,u.dir=O,u.cwd=L,u.charset="utf-8",t.config=function(a){for(var b in a){var c=a[b],d=u[b];if(d&&v(d))for(var e in c)d[e]=c[e];else x(d)?c=d.concat(c):"base"===b&&("/"!==c.slice(-1)&&(c+="/"),c=l(c)),u[b]=c}return B("config",a),t}}}(this);