-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
159 lines (139 loc) · 5.26 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var path = require('path');
var gulp = require("gulp");
var ejs = require('gulp-ejs');
var uglify = require('gulp-uglify');
var less = require("gulp-less");
var cssMinify = require("gulp-minify-css");
var htmlmin = require('gulp-htmlmin');
var gulpSequence = require('gulp-sequence');
var ejsVar = require("./ejs_var.js");
var webpack = require('gulp-webpack');
var clean = require('gulp-clean');
var dataStore = require('nedb');
var dbName = './file_version.db';
global.nedbObj = new dataStore({ filename: dbName, autoload: true });
global.nedbObj.ensureIndex({ fieldName: 'fileName', unique: true }, function (err) {
})
var changePath = require('./custom_builder/change-path');
var genVersion = require('./custom_builder/generate-file-version');
var htmlInnerResVersionHandle = require('./custom_builder/add-file-version');
var ejsPatch = require('./custom_builder/ejs-patch');
var buildData = null;
var pathConfig = null;
var globalPathType = 'relative'; //'absolute'; //relative
var projectRootDir = process.cwd();
var config = require('./doumi_config');
var bPath = config.buildRootPathConfig;
var rPath = config.remoteRootPathConfig;
function pathHandle() {
for (var i in pathConfig.sourcePath) {
pathConfig.sourcePath[i] = path.join(projectRootDir, pathConfig.sourcePath[i]);
}
}
/**
* 处理 css
* 特点:非模块化文件
*/
gulp.task("css", function () {
return gulp.src(pathConfig.sourcePath.cssPath, {base : projectRootDir})
.pipe(changePath({projectRootPath: projectRootDir, pathType: globalPathType, accessRootPath: ''}))
.pipe(cssMinify())
.pipe(genVersion({rootPath : projectRootDir}))
.pipe(gulp.dest(pathConfig.distDir));
});
/**
* 处理less
* 特点:模块化文件、且是根模块
*/
gulp.task("lessEntryModule", function () {
return gulp.src(pathConfig.sourcePath.lessEntryModulePath, {base : projectRootDir})
.pipe(less({
//modifyVars : {'@pic_path' : '"'+ rPath.css +'"'},
paths : ['./', projectRootDir]
}))
.pipe(changePath({projectRootPath: projectRootDir, pathType: globalPathType}))
.pipe(cssMinify())
.pipe(genVersion({rootPath : projectRootDir}))
.pipe(gulp.dest(pathConfig.distDir));
});
/**
* 处理非模块化js文件
*/
gulp.task("jsWithoutModule", function () {
return gulp.src(pathConfig.sourcePath.jsWithoutModulePath, {base : projectRootDir})
.pipe(genVersion({rootPath : projectRootDir}))
.pipe(uglify())
.pipe(gulp.dest(pathConfig.distDir));
});
/**
* 处理根模块js文件
* 特点:模块化文件、根模块
*/
gulp.task("jsEntryModule", function () {
return gulp.src(pathConfig.sourcePath.jsEntryModulePath, {base : projectRootDir})
.pipe(webpack({
output: {
path : projectRootDir, //必须配置,否则js文件的根路径为当前构建器所在的根路径
filename: '[name]',
chunkFilename : '[name]?v=[hash]'
},
resolve : {
root : projectRootDir
},
resolveLoader: { root: [path.join(__dirname, "node_modules"), path.join(__dirname, "custom_loader")] }, //指定loader路径
module: {
loaders: [
{test: /\.html$/, loader: 'raw-loader!html-minify'},
{ test: /\.js$/, loader: 'path-handle!babel?presets[]=es2015'}
]
},
//keep comments for avalon ms-for
'html-minify-loader': {
comments: true
}
}))
.pipe(genVersion({rootPath : projectRootDir}))
.pipe(uglify())
.pipe(gulp.dest(pathConfig.distDir));
});
/**
* 解析入口html模板 ejs
*/
gulp.task("html", function () {
let ejsPathConfig = {root : projectRootDir, filename: ''};
return gulp.src(pathConfig.sourcePath.htmlPath, {base : projectRootDir})
.pipe(ejsPatch(ejsPathConfig))
.pipe(ejs(null, ejsPathConfig))
.pipe(changePath({projectRootPath: projectRootDir, pathType: globalPathType})) //changePath必须在htmlInnerResVersionHandle之前执行,否则css找不到版本号
.pipe(htmlInnerResVersionHandle({projectRootPath: projectRootDir,jsRootPath : rPath.js, cssRootPath : rPath.css}))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(pathConfig.distDir));
});
gulp.task('img', function() {
return gulp.src(pathConfig.sourcePath.imgPath, {base : projectRootDir})
.pipe(gulp.dest(pathConfig.distDir));
});
/**
* 直接拷贝,不用处理的资源。
* 如字体文件、swf、cache.manifest、favicon.png文件等
*/
gulp.task('others', function() {
return gulp.src(pathConfig.sourcePath.otherFilesPath, {base : projectRootDir}).pipe(gulp.dest(pathConfig.distDir));
});
/**
* @desc 清空构建目录
* @src destPath
* @deps none
* @dest destPath
*/
gulp.task("cleanDistDir", function () {
return gulp.src(pathConfig.distDir, {read: false})
.pipe(clean({force: true}));
});
//全量发布
gulp.task('default', gulpSequence('cleanDistDir', ['jsWithoutModule', 'jsEntryModule', 'css', 'lessEntryModule', 'img', 'others'], 'html'));
exports.setOptions = function(opt) {
buildData = opt.buildData;
pathConfig = opt.pathConfig;
pathHandle();
}