forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
249 lines (236 loc) · 10.2 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var fs = require("fs"),
glob = require("glob"),
path = require("path-posix"),
merge = require("merge-stream"),
gulp = require("gulp"),
gulpif = require("gulp-if"),
print = require("gulp-print"),
debug = require("gulp-debug"),
newer = require("gulp-newer"),
plumber = require("gulp-plumber"),
sourcemaps = require("gulp-sourcemaps"),
less = require("gulp-less"),
scss = require("gulp-sass"),
cssnano = require("gulp-cssnano"),
typescript = require("gulp-typescript"),
uglify = require("gulp-uglify"),
rename = require("gulp-rename"),
concat = require("gulp-concat"),
header = require("gulp-header"),
eol = require("gulp-eol");
// For compat with older versions of Node.js.
require("es6-promise").polyfill();
// To suppress memory leak warning from gulp.watch().
require("events").EventEmitter.prototype._maxListeners = 100;
/*
** GULP TASKS
*/
// Incremental build (each asset group is built only if one or more inputs are newer than the output).
gulp.task("build", function () {
var assetGroupTasks = getAssetGroups().map(function (assetGroup) {
var doRebuild = false;
return createAssetGroupTask(assetGroup, doRebuild);
});
return merge(assetGroupTasks);
});
// Full rebuild (all assets groups are built regardless of timestamps).
gulp.task("rebuild", function () {
var assetGroupTasks = getAssetGroups().map(function (assetGroup) {
var doRebuild = true;
return createAssetGroupTask(assetGroup, doRebuild);
});
return merge(assetGroupTasks);
});
// Continuous watch (each asset group is built whenever one of its inputs changes).
gulp.task("watch", function () {
var pathWin32 = require("path");
getAssetGroups().forEach(function (assetGroup) {
var watchPaths = assetGroup.inputPaths.concat(assetGroup.watchPaths);
var inputWatcher;
function createWatcher() {
inputWatcher = gulp.watch(watchPaths, function (event) {
var isConcat = path.basename(assetGroup.outputFileName, path.extname(assetGroup.outputFileName)) !== "@";
if (isConcat)
console.log("Asset file '" + event.path + "' was " + event.type + ", rebuilding asset group with output '" + assetGroup.outputPath + "'.");
else
console.log("Asset file '" + event.path + "' was " + event.type + ", rebuilding asset group.");
var doRebuild = true;
var task = createAssetGroupTask(assetGroup, doRebuild);
});
}
createWatcher();
gulp.watch(assetGroup.manifestPath, function (event) {
console.log("Asset manifest file '" + event.path + "' was " + event.type + ", restarting watcher.");
inputWatcher.remove();
inputWatcher.end();
createWatcher();
});
});
});
/*
** ASSET GROUPS
*/
function getAssetGroups() {
var assetManifestPaths = glob.sync("./src/OrchardCore.{Modules,Themes}/*/Assets.json", {});
var assetGroups = [];
assetManifestPaths.forEach(function (assetManifestPath) {
var assetManifest = require("./" + assetManifestPath);
assetManifest.forEach(function (assetGroup) {
resolveAssetGroupPaths(assetGroup, assetManifestPath);
assetGroups.push(assetGroup);
});
});
return assetGroups;
}
function resolveAssetGroupPaths(assetGroup, assetManifestPath) {
assetGroup.manifestPath = assetManifestPath;
assetGroup.basePath = path.dirname(assetManifestPath);
assetGroup.inputPaths = assetGroup.inputs.map(function (inputPath) {
return path.resolve(path.join(assetGroup.basePath, inputPath));
});
assetGroup.watchPaths = [];
if (!!assetGroup.watch) {
assetGroup.watchPaths = assetGroup.watch.map(function (watchPath) {
return path.resolve(path.join(assetGroup.basePath, watchPath));
});
}
assetGroup.outputPath = path.resolve(path.join(assetGroup.basePath, assetGroup.output));
assetGroup.outputDir = path.dirname(assetGroup.outputPath);
assetGroup.outputFileName = path.basename(assetGroup.output);
// Uncomment to copy assets to wwwroot
//assetGroup.webroot = path.join("./src/OrchardCore.Cms.Web/wwwroot/", path.basename(assetGroup.basePath), path.dirname(assetGroup.output));
}
function createAssetGroupTask(assetGroup, doRebuild) {
var outputExt = path.extname(assetGroup.output).toLowerCase();
var doConcat = path.basename(assetGroup.outputFileName, outputExt) !== "@";
if (doConcat && !doRebuild) {
// Force a rebuild of this asset group is the asset manifest file itself is newer than the output.
var assetManifestStats = fs.statSync(assetGroup.manifestPath);
var outputStats = fs.existsSync(assetGroup.outputPath) ? fs.statSync(assetGroup.outputPath) : null;
doRebuild = !outputStats || assetManifestStats.mtime > outputStats.mtime;
}
switch (outputExt) {
case ".css":
return buildCssPipeline(assetGroup, doConcat, doRebuild);
case ".js":
return buildJsPipeline(assetGroup, doConcat, doRebuild);
}
}
/*
** PROCESSING PIPELINES
*/
function buildCssPipeline(assetGroup, doConcat, doRebuild) {
assetGroup.inputPaths.forEach(function (inputPath) {
var ext = path.extname(inputPath).toLowerCase();
if (ext !== ".scss" && ext !== ".less" && ext !== ".css")
throw "Input file '" + inputPath + "' is not of a valid type for output file '" + assetGroup.outputPath + "'.";
});
var generateSourceMaps = assetGroup.hasOwnProperty("generateSourceMaps") ? assetGroup.generateSourceMaps : true;
var containsLessOrScss = assetGroup.inputPaths.some(function (inputPath) {
var ext = path.extname(inputPath).toLowerCase();
return ext === ".less" || ext === ".scss";
});
// Source maps are useless if neither concatenating nor transforming.
if ((!doConcat || assetGroup.inputPaths.length < 2) && !containsLessOrScss)
generateSourceMaps = false;
var minifiedStream = gulp.src(assetGroup.inputPaths) // Minified output, source mapping completely disabled.
.pipe(gulpif(!doRebuild,
gulpif(doConcat,
newer(assetGroup.outputPath),
newer({
dest: assetGroup.outputDir,
ext: ".css"
}))))
.pipe(plumber())
.pipe(gulpif("*.less", less()))
.pipe(gulpif("*.scss", scss({
precision: 10
})))
.pipe(gulpif(doConcat, concat(assetGroup.outputFileName)))
.pipe(cssnano({
autoprefixer: { browsers: ["last 2 versions"] },
discardComments: { removeAll: true },
discardUnused: false,
mergeIdents: false,
reduceIdents: false,
zindex: false
}))
.pipe(rename({
suffix: ".min"
}))
.pipe(eol())
.pipe(gulp.dest(assetGroup.outputDir))
// Uncomment to copy assets to wwwroot
//.pipe(gulp.dest(assetGroup.webroot));
var devStream = gulp.src(assetGroup.inputPaths) // Non-minified output, with source mapping
.pipe(gulpif(!doRebuild,
gulpif(doConcat,
newer(assetGroup.outputPath),
newer({
dest: assetGroup.outputDir,
ext: ".css"
}))))
.pipe(plumber())
.pipe(gulpif(generateSourceMaps, sourcemaps.init()))
.pipe(gulpif("*.less", less()))
.pipe(gulpif("*.scss", scss({
precision: 10
})))
.pipe(gulpif(doConcat, concat(assetGroup.outputFileName)))
.pipe(header(
"/*\n" +
"** NOTE: This file is generated by Gulp and should not be edited directly!\n" +
"** Any changes made directly to this file will be overwritten next time its asset group is processed by Gulp.\n" +
"*/\n\n"))
.pipe(gulpif(generateSourceMaps, sourcemaps.write()))
.pipe(eol())
.pipe(gulp.dest(assetGroup.outputDir))
// Uncomment to copy assets to wwwroot
//.pipe(gulp.dest(assetGroup.webroot));
return merge([minifiedStream, devStream]);
}
function buildJsPipeline(assetGroup, doConcat, doRebuild) {
assetGroup.inputPaths.forEach(function (inputPath) {
var ext = path.extname(inputPath).toLowerCase();
if (ext !== ".ts" && ext !== ".js")
throw "Input file '" + inputPath + "' is not of a valid type for output file '" + assetGroup.outputPath + "'.";
});
var generateSourceMaps = assetGroup.hasOwnProperty("generateSourceMaps") ? assetGroup.generateSourceMaps : true;
// Source maps are useless if neither concatenating nor transforming.
if ((!doConcat || assetGroup.inputPaths.length < 2) && !assetGroup.inputPaths.some(function (inputPath) { return path.extname(inputPath).toLowerCase() === ".ts"; }))
generateSourceMaps = false;
return gulp.src(assetGroup.inputPaths)
.pipe(gulpif(!doRebuild,
gulpif(doConcat,
newer(assetGroup.outputPath),
newer({
dest: assetGroup.outputDir,
ext: ".js"
}))))
.pipe(plumber())
.pipe(gulpif(generateSourceMaps, sourcemaps.init()))
.pipe(gulpif("*.ts", typescript({
declaration: false,
noImplicitAny: true,
noEmitOnError: true,
sortOutput: true,
}).js))
.pipe(gulpif(doConcat, concat(assetGroup.outputFileName)))
.pipe(header(
"/*\n" +
"** NOTE: This file is generated by Gulp and should not be edited directly!\n" +
"** Any changes made directly to this file will be overwritten next time its asset group is processed by Gulp.\n" +
"*/\n\n"))
.pipe(gulpif(generateSourceMaps, sourcemaps.write()))
.pipe(gulp.dest(assetGroup.outputDir))
// Uncomment to copy assets to wwwroot
//.pipe(gulp.dest(assetGroup.webroot))
.pipe(uglify())
.pipe(rename({
suffix: ".min"
}))
.pipe(eol())
.pipe(gulp.dest(assetGroup.outputDir))
// Uncomment to copy assets to wwwroot
//.pipe(gulp.dest(assetGroup.webroot));
}