gulpfile.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //var wrench = require('wrench');
  2. var gulp = require('gulp');
  3. var del = require('del');
  4. var path = require('path');
  5. var fs = require('fs');
  6. var gutil = require('gulp-util');
  7. var minimist = require('minimist');
  8. var less = require('gulp-less');
  9. var minifyCss = require('gulp-minify-css');
  10. var uglify = require('gulp-uglify');
  11. var rename = require('gulp-rename');
  12. var footer = require('gulp-footer');
  13. var webpack = require('webpack-stream');
  14. var wb = require('webpack');
  15. var webserver = require('gulp-webserver');
  16. var replace = require('gulp-replace');
  17. var babel = require('gulp-babel');
  18. var es2015 = require('babel-preset-es2015');
  19. var gulpif = require('gulp-if');
  20. var basePath = process.env.PJ_PATH;
  21. var absPath = 'http://127.0.0.1:8080/efunbox-to-b/1.0.0';
  22. //var absPath = 'http://lj-web.api.ai160.com/build';
  23. //var absPath = 'http://2blj.api.ai160.com/build';
  24. //var absPath = 'http://114.215.119.26:80/efunbox-to-b/1.0.0';
  25. var pkg = {}
  26. //兼容
  27. if(fs.existsSync(path.join(basePath, 'src/package.json'))){
  28. pkg = require(path.join(basePath, 'src/package.json'))
  29. }else{
  30. pkg = require(path.join(basePath, 'package.json'))
  31. fs.writeFileSync(path.join(basePath, 'src/package.json'), fs.readFileSync(path.join(basePath, 'package.json'), 'utf-8'), 'utf-8');
  32. fs.unlinkSync(path.join(basePath, 'package.json'));
  33. }
  34. //moye配置
  35. var moyeConf = fs.existsSync(path.join(basePath, 'moye.json')) ? require(path.join(basePath, 'moye.json')) : {}
  36. //js uglify config
  37. var uglifyConf = {
  38. compress: {
  39. drop_console: true
  40. },
  41. output:{
  42. ascii_only:true
  43. }
  44. }
  45. //server端口号
  46. var port = minimist(process.argv.slice(2), {string: 'port'}).port;
  47. var serverWatch = eval(minimist(process.argv.slice(3), {string: 'watch'}).watch);
  48. var isUglify = eval(minimist(process.argv.slice(3), {string: 'uglify'}).uglify);
  49. //老版本兼容
  50. process.env.C_PATH = fs.existsSync(path.join(basePath, 'src/stage')) ? 'stage' : 'page'
  51. // 获取stage列表
  52. var stages = []
  53. fs.readdirSync(path.join(basePath, 'src/'+process.env.C_PATH)).forEach(function(item){
  54. if(fs.statSync(path.join(basePath, 'src/'+process.env.C_PATH+'/'+item)).isDirectory()) stages.push(item)
  55. });
  56. var entry = {};
  57. for (var i = 0; i < stages.length; i++) {
  58. var exists = fs.existsSync(path.join(basePath, 'src/'+process.env.C_PATH+'/' + stages[i] + '/index.js'));
  59. if (exists) {
  60. entry[stages[i]] = path.join(basePath, 'src/'+process.env.C_PATH+'/') + stages[i] + '/index.js';
  61. }
  62. }
  63. // 获取template列表
  64. var templates = []
  65. fs.readdirSync(path.join(basePath, 'src/res/tpl')).forEach(function(item){
  66. if(!fs.statSync(path.join(basePath, 'src/res/tpl/'+item)).isDirectory() && /(\.tpl)$|(\.jade)$|(\.juicer)$|(\.ejs)$/g.test(item)) templates.push(item)
  67. });
  68. var templates_entry = {};
  69. for (var i = 0; i < templates.length; i++) {
  70. var exists = fs.existsSync(path.join(basePath, 'src/res/tpl/' + templates[i]));
  71. if (exists) {
  72. entry[templates[i]] = path.join(basePath, 'src/res/tpl/') + templates[i];
  73. }
  74. }
  75. //webpack配置
  76. var webpackConf = function(entry, buidPath){
  77. var conf = {
  78. cache: true,
  79. entry: entry,
  80. output: {
  81. path: path.join(basePath, './build/' + buidPath),
  82. filename:'[name].js',
  83. chunkFilename: 'module.[name].js'
  84. },
  85. module: {
  86. loaders: [{
  87. test: /\.js$/,
  88. loader: 'babel'
  89. },{
  90. test: /\.jade$/,
  91. loader: 'jade-loader'
  92. },{
  93. test: /\.ejs$/,
  94. loader: 'ejs-loader'
  95. },{
  96. test: /\.tpl$/,
  97. loader: 'tpl-loader'
  98. },{
  99. test: /\.juicer$/,
  100. loader: 'juicer-loader'
  101. },{
  102. test: /\.json$/,
  103. loader: 'json-loader'
  104. }],
  105. },
  106. resolveLoader: {
  107. root:[
  108. path.resolve(__dirname, '../../../node_modules'),
  109. path.resolve(__dirname, '../')
  110. ]
  111. },
  112. devtool: 'inline-source-map',
  113. plugins: [new wb.optimize.DedupePlugin()]
  114. }
  115. if(moyeConf.online && moyeConf.online.basePath) conf.output.publicPath = moyeConf.online.basePath
  116. return conf
  117. }
  118. //模板预编译
  119. gulp.task('compile-template', function(){
  120. for (var i = 0; i < templates.length; i++) {
  121. var entry = {};
  122. entry[templates[i]] = path.join(basePath, 'src/res/tpl/' + templates[i])
  123. gulp.src(entry[templates[i]])
  124. .pipe(webpack(webpackConf(entry, '../build/res/tpl'),null,function (err,status) {
  125. //console.log('webpack end')
  126. }))
  127. .pipe(gulpif(isUglify, uglify(uglifyConf)))
  128. .pipe(replace('!function','module.exports=(function'))
  129. .pipe(replace('(0)}([function','(0)})([function'))
  130. //lang
  131. .pipe(replace('src="./assets/','src="'+absPath+'/stage/index/assets/'))
  132. .pipe(replace('src="./../../stage/','src="'+absPath+'/stage/'))
  133. .pipe(replace('src="assets/','src="'+absPath+'/stage/index/assets/'))
  134. .pipe(gulp.dest(path.join(basePath, 'build/res/tpl/')))
  135. }
  136. })
  137. //编译任务
  138. gulp.task('compile', function(){
  139. for (var i = 0; i < stages.length; i++) {
  140. var entry = {};
  141. entry['index'] = path.join(basePath, 'src/'+process.env.C_PATH+'/' + stages[i] + '/index.js')
  142. gulp.src(entry['index'])
  143. .pipe(webpack(webpackConf(entry, ''),null,function (err,status) {
  144. //console.log('webpack end')
  145. }))
  146. //lang
  147. .pipe(replace('src="./assets/','src="'+absPath+'/stage/index/assets/'))
  148. .pipe(replace('src="./../../stage/','src="'+absPath+'/stage/'))
  149. .pipe(replace('src="assets/','src="'+absPath+'/stage/index/assets/'))
  150. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'/' + stages[i] + '/')))
  151. .pipe(uglify(uglifyConf))
  152. .pipe(rename({
  153. suffix: '.min'
  154. }))
  155. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'/' + stages[i] + '/')))
  156. gulp.src(path.join(basePath, 'src/'+process.env.C_PATH+'/' + stages[i] + '/index.less'))
  157. .pipe(less())
  158. //lang
  159. .pipe(replace('assets/',''+absPath+'/stage/index/assets/'))
  160. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'/') + stages[i]))
  161. .pipe(minifyCss({
  162. compatibility: '-units.ch,-units.in,-units.pc,-units.pt,-units.vh,-units.vm,-units.vmax,-units.vmin'
  163. }))
  164. .pipe(rename({
  165. suffix: '.min'
  166. }))
  167. .pipe(footer('/*# sourceMappingURL=index.css.map */'))
  168. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'/') + stages[i]));
  169. gutil.log(gutil.colors.green('Build less: '+path.join(basePath, 'src/'+process.env.C_PATH+'/' + stages[i] + '/index.less')));
  170. }
  171. })
  172. gulp.task('copy', function(){
  173. gulp.src(path.join(basePath, 'src/'+process.env.C_PATH+'/**/*.html'))
  174. .pipe(replace('src="../../lib/','src="'+absPath+'/lib/'))
  175. .pipe(replace('./index.css?t=',''+absPath+'/stage/index/index.css?t='))
  176. .pipe(replace('./index.js?t=',''+absPath+'/stage/index/index.js?t='))
  177. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'')));
  178. gutil.log(gutil.colors.green('Copy libs: build/'+process.env.C_PATH+'/**/*.html'));
  179. gulp.src(path.join(basePath, 'src/'+process.env.C_PATH+'/**/assets/**/*'))
  180. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'/')));
  181. gutil.log(gutil.colors.green('Copy libs: build/'+process.env.C_PATH+'/**/assets'));
  182. gulp.src(path.join(basePath, 'src/lib/**/*'))
  183. .pipe(gulp.dest(path.join(basePath, 'build/lib')));
  184. gutil.log(gutil.colors.green('Copy libs: build/lib'));
  185. gulp.src(path.join(basePath, 'src/res/**/*'))
  186. //lang
  187. .pipe(replace('src="./assets/','src="'+absPath+'/stage/index/assets/'))
  188. .pipe(replace('src="./../../stage/','src="'+absPath+'/stage/'))
  189. .pipe(replace('src="assets/','src="'+absPath+'/stage/index/assets/'))
  190. .pipe(gulp.dest(path.join(basePath, 'build/res/')));
  191. gutil.log(gutil.colors.green('Copy res: build/res/values'));
  192. // gulp.src(path.join(basePath, 'src/res/img/**/*'))
  193. // .pipe(gulp.dest(path.join(basePath, 'build/res/img/')));
  194. // gutil.log(gutil.colors.green('Copy res: build/res/img'));
  195. gulp.src(path.join(basePath, 'src/service/**/*'))
  196. .pipe(gulp.dest(path.join(basePath, 'build/service/')));
  197. gutil.log(gutil.colors.green('Copy service: build/service'));
  198. })
  199. gulp.task('default', function() {
  200. //console.log(path.join(basePath, 'src/**/*'))
  201. var watcherJsLess = gulp.watch(path.join(basePath, 'src/**/*.{js,less,css}'),['compile'])
  202. var watcherTemplate = gulp.watch(path.join(basePath, 'src/**/*.{tpl,jade,ejs,juicer}'),['compile'])
  203. var watcherRes = gulp.watch(path.join(basePath, 'src/**/*'),['copy'])
  204. watcherRes.on('change', function(event) {
  205. gutil.log(gutil.colors.yellow('File ' + event.path + ' was ' + event.type));
  206. });
  207. });
  208. // 启动server
  209. gulp.task('server', function() {
  210. gulp.src(path.join(basePath, 'build/'))
  211. .pipe(webserver({
  212. path: '/' + pkg.name + '/' + pkg.version + '/',
  213. host: '0.0.0.0',
  214. port: port,
  215. livereload: true,
  216. directoryListing: {
  217. enable: true,
  218. path: path.join(basePath, 'build/')
  219. },
  220. middleware: function(req, res, next) {
  221. gutil.log('Request received: ' + req.url);
  222. next();
  223. }
  224. }));
  225. gutil.log(gutil.colors.green('http://127.0.0.1'+(parseInt(port) == 80 ? '' : ':'+port)+'/' + pkg.name + '/' + pkg.version + '/'));
  226. if(serverWatch){
  227. var watcherJsLess = gulp.watch(path.join(basePath, 'src/**/*.{js,less,css}'),['compile'])
  228. var watcherTemplate = gulp.watch(path.join(basePath, 'src/**/*.{tpl,jade,ejs,juicer}'),['compile'])
  229. var watcherRes = gulp.watch(path.join(basePath, 'src/**/*'),['copy'])
  230. watcherRes.on('change', function(event) {
  231. gutil.log(gutil.colors.yellow('File ' + event.path + ' was ' + event.type));
  232. });
  233. }
  234. });
  235. //service-worker编译任务
  236. gulp.task('sw-compile', function(){
  237. for (var i = 0; i < stages.length; i++) {
  238. var entry = {};
  239. entry[stages[i]] = path.join(basePath, 'src/'+process.env.C_PATH+'/' + stages[i] + '/service-worker/index.js')
  240. if(fs.existsSync(entry[stages[i]])){
  241. gulp.src(entry[stages[i]])
  242. .pipe(webpack(webpackConf(entry, 'service-worker/'),null,function (err,status) {
  243. //console.log('webpack end')
  244. }))
  245. .pipe(gulp.dest(path.join(basePath, 'build/'+process.env.C_PATH+'/' + stages[i] + '/service-worker/')))
  246. }
  247. }
  248. })
  249. // 编译打包cyclone本地app
  250. gulp.task('cyclone', function() {
  251. var cssSrc = [path.join(basePath, 'src/'+process.env.C_PATH+'/**/*.{less,css}')];
  252. var jsSrc = [path.join(basePath, 'src/**/*.js'), '!' + path.join(basePath, 'src/lib/**/*'), '!' + path.join(basePath, 'src/service/**/*.js')];
  253. // var copySrc = [
  254. // path.join(basePath, 'src/*'),
  255. // path.join(basePath, 'src/'+process.env.C_PATH+'/**/assets/**/*'),
  256. // path.join(basePath, 'src/'+process.env.C_PATH+'/**/*.html'),
  257. // path.join(basePath, 'src/res/**/*'),
  258. // path.join(basePath, 'src/lib/**/*'),
  259. // path.join(basePath, 'src/manifest.json'),
  260. // path.join(basePath, 'src/service/**/*.js'),
  261. // path.join(basePath, 'src/util/**/*'),
  262. // path.join(basePath, 'src/component/**/*'),
  263. // path.join(basePath, 'src/node_modules/**/*')
  264. // ];
  265. // var copyDest = [
  266. // path.join(basePath, 'buildLocal/'),
  267. // path.join(basePath, 'buildLocal/'+process.env.C_PATH+'/'),
  268. // path.join(basePath, 'buildLocal/'+process.env.C_PATH+'/'),
  269. // path.join(basePath, 'buildLocal/res/'),
  270. // path.join(basePath, 'buildLocal/lib/'),
  271. // path.join(basePath, 'buildLocal/'),
  272. // path.join(basePath, 'buildLocal/service/'),
  273. // path.join(basePath, 'buildLocal/util/'),
  274. // path.join(basePath, 'buildLocal/component/'),
  275. // path.join(basePath, 'buildLocal/node_modules/')
  276. // ];
  277. //for (var i = 0; i < copySrc.length; i++) {
  278. gulp.src(path.join(basePath, 'src/manifest.json'))
  279. .pipe(gulp.dest(path.join(basePath, 'buildLocal/')))
  280. gulp.src(path.join(basePath, 'src/**/*'))
  281. .pipe(gulp.dest(path.join(basePath, 'buildLocal/')))
  282. .on('end',function(){
  283. gulp.src(cssSrc)
  284. .pipe(less())
  285. .pipe(minifyCss())
  286. .pipe(gulp.dest(path.join(basePath, 'buildLocal/'+process.env.C_PATH)))
  287. gulp.src(jsSrc)
  288. .pipe(babel({
  289. presets: [es2015]
  290. }))
  291. .pipe(replace(/((\.tpl)|(\.jade)|(\.juicer)|(\.ejs))[\'\"]\)/g,'$1.js\'\)'))
  292. .pipe(gulpif(isUglify, uglify(uglifyConf)))
  293. .pipe(gulp.dest(path.join(basePath, 'buildLocal/')));
  294. for (var i = 0; i < templates.length; i++) {
  295. var entry = {};
  296. entry[templates[i]] = path.join(basePath, 'src/res/tpl/' + templates[i])
  297. gulp.src(entry[templates[i]])
  298. .pipe(webpack(webpackConf(entry, '../buildLocal/res/tpl'),null,function (err,status) {
  299. //console.log('webpack end')
  300. }))
  301. .pipe(uglify(uglifyConf))
  302. .pipe(replace('!function','module.exports=(function'))
  303. .pipe(replace('(0)}([function','(0)})([function'))
  304. .pipe(gulp.dest(path.join(basePath, 'buildLocal/res/tpl/')))
  305. }
  306. })
  307. //}
  308. });