forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.common.js
225 lines (194 loc) · 6.12 KB
/
gulpfile.common.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var path = require('path');
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var filter = require('gulp-filter');
var minifyCSS = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var es = require('event-stream');
var concat = require('gulp-concat');
var File = require('vinyl');
var underscore = require('underscore');
var bundle = require('./build/lib/bundle');
var util = require('./build/lib/util');
var tsOptions = {
target: 'ES5',
module: 'amd',
verbose: true,
preserveConstEnums: true,
experimentalDecorators: true,
sourceMap: true,
rootDir: path.join(__dirname, 'src')
};
exports.loaderConfig = function (emptyPaths) {
var result = {
paths: {
'vs': 'out-build/vs',
'vs/extensions': 'extensions',
'vscode': 'empty:',
'lib': 'out-build/lib'
},
'vs/text': {
paths: {
'vs/extensions': 'extensions'
}
}
};
(emptyPaths || []).forEach(function(m) { result.paths[m] = 'empty:'; });
return result;
};
var IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;
function loader() {
return gulp.src([
'out-build/vs/loader.js',
'out-build/vs/css.js',
'out-build/vs/nls.js',
'out-build/vs/text.js'
], { base: 'out-build' })
.pipe(util.loadSourcemaps())
.pipe(concat('vs/loader.js'))
.pipe(es.mapSync(function (f) {
f.sourceMap.sourceRoot = util.toFileUri(tsOptions.rootDir);
return f;
}));
}
function toBundleStream(bundles) {
return es.merge(bundles.map(function(bundle) {
var useSourcemaps = /\.js$/.test(bundle.dest) && !/\.nls\.js$/.test(bundle.dest);
var sources = bundle.sources.map(function(source) {
var root = source.path ? __dirname.replace(/\\/g, '/') : '';
var base = source.path ? root + '/out-build' : '';
return new File({
path: source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake',
base: base,
contents: new Buffer(source.contents)
});
});
return es.readArray(sources)
.pipe(useSourcemaps ? util.loadSourcemaps() : es.through())
.pipe(concat(bundle.dest));
}));
}
exports.optimizeTask = function(entryPoints, resources, loaderConfig, out) {
return function() {
var bundles = es.through();
bundle.bundle(entryPoints, loaderConfig, function(err, result) {
if (err) { return bundles.emit('error', JSON.stringify(err)); }
// If a bundle ends up including in any of the sources our copyright, then
// insert a fake source at the beginning of each bundle with our copyright
result.forEach(function(b) {
var containsOurCopyright = false;
for (var i = 0, len = b.sources.length; i < len; i++) {
var fileContents = b.sources[i].contents;
if (IS_OUR_COPYRIGHT_REGEXP.test(fileContents)) {
containsOurCopyright = true;
break;
}
}
if (containsOurCopyright) {
b.sources.unshift({
path: null,
contents: [
'/*!--------------------------------------------------------',
' * Copyright (C) Microsoft Corporation. All rights reserved.',
' *--------------------------------------------------------*/'
].join('\r\n')
});
}
});
var bundleInformation = result.map(function (b) {
return {
dest: b.dest,
sources: b.sources.filter(function (s) {
return !!s.path;
}).map(function (s) {
return path.relative('out-build', s.path);
})
}
});
var info = es.readArray([new File({
path: 'bundles.json',
contents: new Buffer(JSON.stringify(bundleInformation), 'utf8')
})]);
es.merge(toBundleStream(result), info).pipe(bundles);
});
var result = es.merge(
loader(),
bundles,
gulp.src(resources, { base: 'out-build' })
);
return result
.pipe(sourcemaps.write('./', {
sourceRoot: null,
addComment: false,
includeContent: true
}))
.pipe(gulp.dest(out));
};
};
/**
* wrap around uglify and allow the preserveComments function
* to have a file "context" to include our copyright only once per file
*/
function uglifyWithCopyrights() {
var currentFileHasOurCopyright = false;
var onNewFile = function() {
currentFileHasOurCopyright = false;
};
var preserveComments = function(node, comment) {
var text = comment.value;
var type = comment.type;
if (/@minifier_do_not_preserve/.test(text)) {
return false;
}
var isOurCopyright = IS_OUR_COPYRIGHT_REGEXP.test(text);
if (isOurCopyright) {
if (currentFileHasOurCopyright) {
return false;
}
currentFileHasOurCopyright = true;
return true;
}
if ('comment2' === type) {
// check for /*!. Note that text doesn't contain leading /*
return (text.length > 0 && text[0] === '!') || /@preserve|license|@cc_on|copyright/i.test(text);
} else if ('comment1' === type) {
return /license|copyright/i.test(text);
}
return false;
};
var uglifyStream = uglify({ preserveComments: preserveComments });
return es.through(function write(data) {
var _this = this;
onNewFile();
uglifyStream.once('data', function(data) {
_this.emit('data', data);
})
uglifyStream.write(data);
}, function end() {
this.emit('end')
});
}
exports.minifyTask = function (src) {
return function() {
var jsFilter = filter('**/*.js', { restore: true });
var cssFilter = filter('**/*.css', { restore: true });
return gulp.src([src + '/**', '!' + src + '/**/*.map'])
.pipe(jsFilter)
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglifyWithCopyrights())
.pipe(jsFilter.restore)
.pipe(cssFilter)
.pipe(minifyCSS())
.pipe(cssFilter.restore)
.pipe(sourcemaps.write('./', {
sourceRoot: null,
includeContent: true,
addComment: false
}))
.pipe(gulp.dest(src + '-min'));
};
};