2017-05-15 13:59:14 +00:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-10-12 18:24:15 +00:00
|
|
|
const es = require("event-stream");
|
2019-09-09 15:16:33 +00:00
|
|
|
const fs = require("fs");
|
2018-10-12 18:24:15 +00:00
|
|
|
const gulp = require("gulp");
|
|
|
|
const concat = require("gulp-concat");
|
|
|
|
const minifyCSS = require("gulp-cssnano");
|
|
|
|
const filter = require("gulp-filter");
|
|
|
|
const flatmap = require("gulp-flatmap");
|
|
|
|
const sourcemaps = require("gulp-sourcemaps");
|
|
|
|
const uglify = require("gulp-uglify");
|
|
|
|
const composer = require("gulp-uglify/composer");
|
2019-02-05 21:21:05 +00:00
|
|
|
const fancyLog = require("fancy-log");
|
|
|
|
const ansiColors = require("ansi-colors");
|
2018-10-12 18:24:15 +00:00
|
|
|
const path = require("path");
|
|
|
|
const pump = require("pump");
|
2019-09-02 09:19:26 +00:00
|
|
|
const terser = require("terser");
|
2018-10-12 18:24:15 +00:00
|
|
|
const VinylFile = require("vinyl");
|
|
|
|
const bundle = require("./bundle");
|
|
|
|
const i18n_1 = require("./i18n");
|
|
|
|
const stats_1 = require("./stats");
|
|
|
|
const util = require("./util");
|
|
|
|
const REPO_ROOT_PATH = path.join(__dirname, '../..');
|
2017-05-15 13:59:14 +00:00
|
|
|
function log(prefix, message) {
|
2019-02-05 21:21:05 +00:00
|
|
|
fancyLog(ansiColors.cyan('[' + prefix + ']'), message);
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
|
|
|
function loaderConfig(emptyPaths) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const result = {
|
2017-05-15 13:59:14 +00:00
|
|
|
paths: {
|
|
|
|
'vs': 'out-build/vs',
|
|
|
|
'vscode': 'empty:'
|
|
|
|
},
|
|
|
|
nodeModules: emptyPaths || []
|
|
|
|
};
|
|
|
|
result['vs/css'] = { inlineResources: true };
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
exports.loaderConfig = loaderConfig;
|
2018-10-12 18:24:15 +00:00
|
|
|
const IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;
|
2018-07-20 08:38:56 +00:00
|
|
|
function loader(src, bundledFileHeader, bundleLoader) {
|
2018-10-12 18:24:15 +00:00
|
|
|
let sources = [
|
|
|
|
`${src}/vs/loader.js`
|
2017-05-15 13:59:14 +00:00
|
|
|
];
|
|
|
|
if (bundleLoader) {
|
|
|
|
sources = sources.concat([
|
2018-10-12 18:24:15 +00:00
|
|
|
`${src}/vs/css.js`,
|
|
|
|
`${src}/vs/nls.js`
|
2017-05-15 13:59:14 +00:00
|
|
|
]);
|
|
|
|
}
|
2018-10-12 18:24:15 +00:00
|
|
|
let isFirst = true;
|
2017-05-15 13:59:14 +00:00
|
|
|
return (gulp
|
2018-10-12 18:24:15 +00:00
|
|
|
.src(sources, { base: `${src}` })
|
2017-05-15 13:59:14 +00:00
|
|
|
.pipe(es.through(function (data) {
|
|
|
|
if (isFirst) {
|
|
|
|
isFirst = false;
|
|
|
|
this.emit('data', new VinylFile({
|
|
|
|
path: 'fake',
|
|
|
|
base: '',
|
2018-02-26 16:22:52 +00:00
|
|
|
contents: Buffer.from(bundledFileHeader)
|
2017-05-15 13:59:14 +00:00
|
|
|
}));
|
|
|
|
this.emit('data', data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.emit('data', data);
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.pipe(util.loadSourcemaps())
|
|
|
|
.pipe(concat('vs/loader.js'))
|
|
|
|
.pipe(es.mapSync(function (f) {
|
|
|
|
f.sourceMap.sourceRoot = util.toFileUri(path.join(REPO_ROOT_PATH, 'src'));
|
|
|
|
return f;
|
|
|
|
})));
|
|
|
|
}
|
2018-07-20 08:38:56 +00:00
|
|
|
function toConcatStream(src, bundledFileHeader, sources, dest) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest);
|
2017-05-15 13:59:14 +00:00
|
|
|
// 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
|
2018-10-12 18:24:15 +00:00
|
|
|
let containsOurCopyright = false;
|
|
|
|
for (let i = 0, len = sources.length; i < len; i++) {
|
|
|
|
const fileContents = sources[i].contents;
|
2017-05-15 13:59:14 +00:00
|
|
|
if (IS_OUR_COPYRIGHT_REGEXP.test(fileContents)) {
|
|
|
|
containsOurCopyright = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (containsOurCopyright) {
|
|
|
|
sources.unshift({
|
|
|
|
path: null,
|
|
|
|
contents: bundledFileHeader
|
|
|
|
});
|
|
|
|
}
|
2018-10-12 18:24:15 +00:00
|
|
|
const treatedSources = sources.map(function (source) {
|
|
|
|
const root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : '';
|
|
|
|
const base = source.path ? root + `/${src}` : '';
|
2017-05-15 13:59:14 +00:00
|
|
|
return new VinylFile({
|
|
|
|
path: source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake',
|
|
|
|
base: base,
|
2018-02-26 16:22:52 +00:00
|
|
|
contents: Buffer.from(source.contents)
|
2017-05-15 13:59:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return es.readArray(treatedSources)
|
|
|
|
.pipe(useSourcemaps ? util.loadSourcemaps() : es.through())
|
2018-08-31 11:28:24 +00:00
|
|
|
.pipe(concat(dest))
|
2018-09-03 09:51:26 +00:00
|
|
|
.pipe(stats_1.createStatsStream(dest));
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
2018-07-20 08:38:56 +00:00
|
|
|
function toBundleStream(src, bundledFileHeader, bundles) {
|
2017-05-15 13:59:14 +00:00
|
|
|
return es.merge(bundles.map(function (bundle) {
|
2018-07-20 08:38:56 +00:00
|
|
|
return toConcatStream(src, bundledFileHeader, bundle.sources, bundle.dest);
|
2017-05-15 13:59:14 +00:00
|
|
|
}));
|
|
|
|
}
|
2019-07-14 11:12:54 +00:00
|
|
|
const DEFAULT_FILE_HEADER = [
|
|
|
|
'/*!--------------------------------------------------------',
|
|
|
|
' * Copyright (C) Microsoft Corporation. All rights reserved.',
|
|
|
|
' *--------------------------------------------------------*/'
|
|
|
|
].join('\n');
|
2017-05-15 13:59:14 +00:00
|
|
|
function optimizeTask(opts) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const src = opts.src;
|
|
|
|
const entryPoints = opts.entryPoints;
|
|
|
|
const resources = opts.resources;
|
|
|
|
const loaderConfig = opts.loaderConfig;
|
2019-07-14 11:12:54 +00:00
|
|
|
const bundledFileHeader = opts.header || DEFAULT_FILE_HEADER;
|
2018-10-12 18:24:15 +00:00
|
|
|
const bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader);
|
|
|
|
const out = opts.out;
|
2017-05-15 13:59:14 +00:00
|
|
|
return function () {
|
2018-10-12 18:24:15 +00:00
|
|
|
const bundlesStream = es.through(); // this stream will contain the bundled files
|
|
|
|
const resourcesStream = es.through(); // this stream will contain the resources
|
|
|
|
const bundleInfoStream = es.through(); // this stream will contain bundleInfo.json
|
2017-05-15 13:59:14 +00:00
|
|
|
bundle.bundle(entryPoints, loaderConfig, function (err, result) {
|
2018-10-03 00:19:03 +00:00
|
|
|
if (err || !result) {
|
2017-05-15 13:59:14 +00:00
|
|
|
return bundlesStream.emit('error', JSON.stringify(err));
|
|
|
|
}
|
2019-09-09 15:16:33 +00:00
|
|
|
if (opts.inlineAmdImages) {
|
|
|
|
try {
|
|
|
|
result = inlineAmdImages(src, result);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return bundlesStream.emit('error', JSON.stringify(err));
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 08:38:56 +00:00
|
|
|
toBundleStream(src, bundledFileHeader, result.files).pipe(bundlesStream);
|
2017-05-15 13:59:14 +00:00
|
|
|
// Remove css inlined resources
|
2018-10-12 18:24:15 +00:00
|
|
|
const filteredResources = resources.slice();
|
2017-05-15 13:59:14 +00:00
|
|
|
result.cssInlinedResources.forEach(function (resource) {
|
|
|
|
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
|
|
|
log('optimizer', 'excluding inlined: ' + resource);
|
|
|
|
}
|
|
|
|
filteredResources.push('!' + resource);
|
|
|
|
});
|
2019-02-06 09:15:36 +00:00
|
|
|
gulp.src(filteredResources, { base: `${src}`, allowEmpty: true }).pipe(resourcesStream);
|
2018-10-12 18:24:15 +00:00
|
|
|
const bundleInfoArray = [];
|
2017-05-15 13:59:14 +00:00
|
|
|
if (opts.bundleInfo) {
|
|
|
|
bundleInfoArray.push(new VinylFile({
|
|
|
|
path: 'bundleInfo.json',
|
|
|
|
base: '.',
|
2018-02-26 16:22:52 +00:00
|
|
|
contents: Buffer.from(JSON.stringify(result.bundleData, null, '\t'))
|
2017-05-15 13:59:14 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
es.readArray(bundleInfoArray).pipe(bundleInfoStream);
|
|
|
|
});
|
2019-02-06 09:15:36 +00:00
|
|
|
const result = es.merge(loader(src, bundledFileHeader, bundleLoader), bundlesStream, resourcesStream, bundleInfoStream);
|
2017-05-15 13:59:14 +00:00
|
|
|
return result
|
|
|
|
.pipe(sourcemaps.write('./', {
|
2018-10-03 00:19:03 +00:00
|
|
|
sourceRoot: undefined,
|
2017-05-15 13:59:14 +00:00
|
|
|
addComment: true,
|
|
|
|
includeContent: true
|
|
|
|
}))
|
2018-08-10 07:27:47 +00:00
|
|
|
.pipe(opts.languages && opts.languages.length ? i18n_1.processNlsFiles({
|
|
|
|
fileHeader: bundledFileHeader,
|
|
|
|
languages: opts.languages
|
|
|
|
}) : es.through())
|
2017-05-15 13:59:14 +00:00
|
|
|
.pipe(gulp.dest(out));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.optimizeTask = optimizeTask;
|
2019-09-09 15:16:33 +00:00
|
|
|
function inlineAmdImages(src, result) {
|
|
|
|
for (const outputFile of result.files) {
|
|
|
|
for (const sourceFile of outputFile.sources) {
|
|
|
|
if (sourceFile.path && /\.js$/.test(sourceFile.path)) {
|
|
|
|
sourceFile.contents = sourceFile.contents.replace(/\([^.]+\.registerAndGetAmdImageURL\(([^)]+)\)\)/g, (_, m0) => {
|
|
|
|
let imagePath = m0;
|
|
|
|
// remove `` or ''
|
|
|
|
if ((imagePath.charAt(0) === '`' && imagePath.charAt(imagePath.length - 1) === '`')
|
|
|
|
|| (imagePath.charAt(0) === '\'' && imagePath.charAt(imagePath.length - 1) === '\'')) {
|
|
|
|
imagePath = imagePath.substr(1, imagePath.length - 2);
|
|
|
|
}
|
|
|
|
if (!/\.(png|svg)$/.test(imagePath)) {
|
|
|
|
console.log(`original: ${_}`);
|
|
|
|
return _;
|
|
|
|
}
|
|
|
|
const repoLocation = path.join(src, imagePath);
|
|
|
|
const absoluteLocation = path.join(REPO_ROOT_PATH, repoLocation);
|
|
|
|
if (!fs.existsSync(absoluteLocation)) {
|
2019-09-09 16:15:03 +00:00
|
|
|
const message = `Invalid amd image url in file ${sourceFile.path}: ${imagePath}`;
|
|
|
|
console.log(message);
|
|
|
|
throw new Error(message);
|
2019-09-09 15:16:33 +00:00
|
|
|
}
|
|
|
|
const fileContents = fs.readFileSync(absoluteLocation);
|
|
|
|
const mime = /\.svg$/.test(imagePath) ? 'image/svg+xml' : 'image/png';
|
|
|
|
// Mark the file as inlined so we don't ship it by itself
|
|
|
|
result.cssInlinedResources.push(repoLocation);
|
|
|
|
return `("data:${mime};base64,${fileContents.toString('base64')}")`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-05-15 13:59:14 +00:00
|
|
|
/**
|
|
|
|
* Wrap around uglify and allow the preserveComments function
|
|
|
|
* to have a file "context" to include our copyright only once per file.
|
|
|
|
*/
|
|
|
|
function uglifyWithCopyrights() {
|
2018-10-12 18:24:15 +00:00
|
|
|
const preserveComments = (f) => {
|
|
|
|
return (_node, comment) => {
|
|
|
|
const text = comment.value;
|
|
|
|
const type = comment.type;
|
2017-05-15 13:59:14 +00:00
|
|
|
if (/@minifier_do_not_preserve/.test(text)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-12 18:24:15 +00:00
|
|
|
const isOurCopyright = IS_OUR_COPYRIGHT_REGEXP.test(text);
|
2017-05-15 13:59:14 +00:00
|
|
|
if (isOurCopyright) {
|
|
|
|
if (f.__hasOurCopyright) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
f.__hasOurCopyright = 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;
|
|
|
|
};
|
|
|
|
};
|
2019-09-02 09:19:26 +00:00
|
|
|
const minify = composer(terser);
|
2018-10-12 18:24:15 +00:00
|
|
|
const input = es.through();
|
|
|
|
const output = input
|
|
|
|
.pipe(flatmap((stream, f) => {
|
2017-06-20 09:45:37 +00:00
|
|
|
return stream.pipe(minify({
|
2017-05-15 13:59:14 +00:00
|
|
|
output: {
|
2017-06-20 09:45:37 +00:00
|
|
|
comments: preserveComments(f),
|
2017-11-14 16:21:00 +00:00
|
|
|
max_line_len: 1024
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
return es.duplex(input, output);
|
|
|
|
}
|
|
|
|
function minifyTask(src, sourceMapBaseUrl) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const sourceMappingURL = sourceMapBaseUrl ? ((f) => `${sourceMapBaseUrl}/${f.relative}.map`) : undefined;
|
|
|
|
return cb => {
|
|
|
|
const jsFilter = filter('**/*.js', { restore: true });
|
|
|
|
const cssFilter = filter('**/*.css', { restore: true });
|
2019-02-20 02:05:48 +00:00
|
|
|
pump(gulp.src([src + '/**', '!' + src + '/**/*.map']), jsFilter, sourcemaps.init({ loadMaps: true }), uglifyWithCopyrights(), jsFilter.restore, cssFilter, minifyCSS({ reduceIdents: false }), cssFilter.restore, sourcemaps.mapSources((sourcePath) => {
|
|
|
|
if (sourcePath === 'bootstrap-fork.js') {
|
|
|
|
return 'bootstrap-fork.orig.js';
|
|
|
|
}
|
|
|
|
return sourcePath;
|
|
|
|
}), sourcemaps.write('./', {
|
2018-10-12 18:24:15 +00:00
|
|
|
sourceMappingURL,
|
2018-10-03 00:19:03 +00:00
|
|
|
sourceRoot: undefined,
|
2017-05-15 13:59:14 +00:00
|
|
|
includeContent: true,
|
|
|
|
addComment: true
|
2018-10-12 18:24:15 +00:00
|
|
|
}), gulp.dest(src + '-min'), (err) => {
|
2017-05-15 13:59:14 +00:00
|
|
|
if (err instanceof uglify.GulpUglifyError) {
|
2018-10-12 18:24:15 +00:00
|
|
|
console.error(`Uglify error in '${err.cause && err.cause.filename}'`);
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.minifyTask = minifyTask;
|