mirror of https://github.com/microsoft/vscode.git
👷 update build files
This commit is contained in:
parent
92ee0d1e06
commit
a85a6a22de
|
@ -1,452 +1,451 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var vm = require("vm");
|
||||
/**
|
||||
* Bundle `entryPoints` given config `config`.
|
||||
*/
|
||||
function bundle(entryPoints, config, callback) {
|
||||
var entryPointsMap = {};
|
||||
entryPoints.forEach(function (module) {
|
||||
entryPointsMap[module.name] = module;
|
||||
});
|
||||
var allMentionedModulesMap = {};
|
||||
entryPoints.forEach(function (module) {
|
||||
allMentionedModulesMap[module.name] = true;
|
||||
(module.include || []).forEach(function (includedModule) {
|
||||
allMentionedModulesMap[includedModule] = true;
|
||||
});
|
||||
(module.exclude || []).forEach(function (excludedModule) {
|
||||
allMentionedModulesMap[excludedModule] = true;
|
||||
});
|
||||
});
|
||||
var code = require('fs').readFileSync(path.join(__dirname, '../../src/vs/loader.js'));
|
||||
var r = vm.runInThisContext('(function(require, module, exports) { ' + code + '\n});');
|
||||
var loaderModule = { exports: {} };
|
||||
r.call({}, require, loaderModule, loaderModule.exports);
|
||||
var loader = loaderModule.exports;
|
||||
config.isBuild = true;
|
||||
loader.config(config);
|
||||
loader(['require'], function (localRequire) {
|
||||
var resolvePath = function (path) {
|
||||
var r = localRequire.toUrl(path);
|
||||
if (!/\.js/.test(r)) {
|
||||
return r + '.js';
|
||||
}
|
||||
return r;
|
||||
};
|
||||
for (var moduleId in entryPointsMap) {
|
||||
var entryPoint = entryPointsMap[moduleId];
|
||||
if (entryPoint.append) {
|
||||
entryPoint.append = entryPoint.append.map(resolvePath);
|
||||
}
|
||||
if (entryPoint.prepend) {
|
||||
entryPoint.prepend = entryPoint.prepend.map(resolvePath);
|
||||
}
|
||||
}
|
||||
});
|
||||
loader(Object.keys(allMentionedModulesMap), function () {
|
||||
var modules = loader.getBuildInfo();
|
||||
var partialResult = emitEntryPoints(modules, entryPointsMap);
|
||||
var cssInlinedResources = loader('vs/css').getInlinedResources();
|
||||
callback(null, {
|
||||
files: partialResult.files,
|
||||
cssInlinedResources: cssInlinedResources,
|
||||
bundleData: partialResult.bundleData
|
||||
});
|
||||
}, function (err) { return callback(err, null); });
|
||||
}
|
||||
exports.bundle = bundle;
|
||||
function emitEntryPoints(modules, entryPoints) {
|
||||
var modulesMap = {};
|
||||
modules.forEach(function (m) {
|
||||
modulesMap[m.id] = m;
|
||||
});
|
||||
var modulesGraph = {};
|
||||
modules.forEach(function (m) {
|
||||
modulesGraph[m.id] = m.dependencies;
|
||||
});
|
||||
var sortedModules = topologicalSort(modulesGraph);
|
||||
var result = [];
|
||||
var usedPlugins = {};
|
||||
var bundleData = {
|
||||
graph: modulesGraph,
|
||||
bundles: {}
|
||||
};
|
||||
Object.keys(entryPoints).forEach(function (moduleToBundle) {
|
||||
var info = entryPoints[moduleToBundle];
|
||||
var rootNodes = [moduleToBundle].concat(info.include || []);
|
||||
var allDependencies = visit(rootNodes, modulesGraph);
|
||||
var excludes = ['require', 'exports', 'module'].concat(info.exclude || []);
|
||||
excludes.forEach(function (excludeRoot) {
|
||||
var allExcludes = visit([excludeRoot], modulesGraph);
|
||||
Object.keys(allExcludes).forEach(function (exclude) {
|
||||
delete allDependencies[exclude];
|
||||
});
|
||||
});
|
||||
var includedModules = sortedModules.filter(function (module) {
|
||||
return allDependencies[module];
|
||||
});
|
||||
bundleData.bundles[moduleToBundle] = includedModules;
|
||||
var res = emitEntryPoint(modulesMap, modulesGraph, moduleToBundle, includedModules, info.prepend, info.append, info.dest);
|
||||
result = result.concat(res.files);
|
||||
for (var pluginName in res.usedPlugins) {
|
||||
usedPlugins[pluginName] = usedPlugins[pluginName] || res.usedPlugins[pluginName];
|
||||
}
|
||||
});
|
||||
Object.keys(usedPlugins).forEach(function (pluginName) {
|
||||
var plugin = usedPlugins[pluginName];
|
||||
if (typeof plugin.finishBuild === 'function') {
|
||||
var write = function (filename, contents) {
|
||||
result.push({
|
||||
dest: filename,
|
||||
sources: [{
|
||||
path: null,
|
||||
contents: contents
|
||||
}]
|
||||
});
|
||||
};
|
||||
plugin.finishBuild(write);
|
||||
}
|
||||
});
|
||||
return {
|
||||
files: extractStrings(removeDuplicateTSBoilerplate(result)),
|
||||
bundleData: bundleData
|
||||
};
|
||||
}
|
||||
function extractStrings(destFiles) {
|
||||
var parseDefineCall = function (moduleMatch, depsMatch) {
|
||||
var module = moduleMatch.replace(/^"|"$/g, '');
|
||||
var deps = depsMatch.split(',');
|
||||
deps = deps.map(function (dep) {
|
||||
dep = dep.trim();
|
||||
dep = dep.replace(/^"|"$/g, '');
|
||||
dep = dep.replace(/^'|'$/g, '');
|
||||
var prefix = null;
|
||||
var _path = null;
|
||||
var pieces = dep.split('!');
|
||||
if (pieces.length > 1) {
|
||||
prefix = pieces[0] + '!';
|
||||
_path = pieces[1];
|
||||
}
|
||||
else {
|
||||
prefix = '';
|
||||
_path = pieces[0];
|
||||
}
|
||||
if (/^\.\//.test(_path) || /^\.\.\//.test(_path)) {
|
||||
var res = path.join(path.dirname(module), _path).replace(/\\/g, '/');
|
||||
return prefix + res;
|
||||
}
|
||||
return prefix + _path;
|
||||
});
|
||||
return {
|
||||
module: module,
|
||||
deps: deps
|
||||
};
|
||||
};
|
||||
destFiles.forEach(function (destFile, index) {
|
||||
if (!/\.js$/.test(destFile.dest)) {
|
||||
return;
|
||||
}
|
||||
if (/\.nls\.js$/.test(destFile.dest)) {
|
||||
return;
|
||||
}
|
||||
// Do one pass to record the usage counts for each module id
|
||||
var useCounts = {};
|
||||
destFile.sources.forEach(function (source) {
|
||||
var matches = source.contents.match(/define\(("[^"]+"),\s*\[(((, )?("|')[^"']+("|'))+)\]/);
|
||||
if (!matches) {
|
||||
return;
|
||||
}
|
||||
var defineCall = parseDefineCall(matches[1], matches[2]);
|
||||
useCounts[defineCall.module] = (useCounts[defineCall.module] || 0) + 1;
|
||||
defineCall.deps.forEach(function (dep) {
|
||||
useCounts[dep] = (useCounts[dep] || 0) + 1;
|
||||
});
|
||||
});
|
||||
var sortedByUseModules = Object.keys(useCounts);
|
||||
sortedByUseModules.sort(function (a, b) {
|
||||
return useCounts[b] - useCounts[a];
|
||||
});
|
||||
var replacementMap = {};
|
||||
sortedByUseModules.forEach(function (module, index) {
|
||||
replacementMap[module] = index;
|
||||
});
|
||||
destFile.sources.forEach(function (source) {
|
||||
source.contents = source.contents.replace(/define\(("[^"]+"),\s*\[(((, )?("|')[^"']+("|'))+)\]/, function (_, moduleMatch, depsMatch) {
|
||||
var defineCall = parseDefineCall(moduleMatch, depsMatch);
|
||||
return "define(__m[" + replacementMap[defineCall.module] + "/*" + defineCall.module + "*/], __M([" + defineCall.deps.map(function (dep) { return replacementMap[dep] + '/*' + dep + '*/'; }).join(',') + "])";
|
||||
});
|
||||
});
|
||||
destFile.sources.unshift({
|
||||
path: null,
|
||||
contents: [
|
||||
'(function() {',
|
||||
"var __m = " + JSON.stringify(sortedByUseModules) + ";",
|
||||
"var __M = function(deps) {",
|
||||
" var result = [];",
|
||||
" for (var i = 0, len = deps.length; i < len; i++) {",
|
||||
" result[i] = __m[deps[i]];",
|
||||
" }",
|
||||
" return result;",
|
||||
"};"
|
||||
].join('\n')
|
||||
});
|
||||
destFile.sources.push({
|
||||
path: null,
|
||||
contents: '}).call(this);'
|
||||
});
|
||||
});
|
||||
return destFiles;
|
||||
}
|
||||
function removeDuplicateTSBoilerplate(destFiles) {
|
||||
// Taken from typescript compiler => emitFiles
|
||||
var BOILERPLATE = [
|
||||
{ start: /^var __extends/, end: /^};$/ },
|
||||
{ start: /^var __assign/, end: /^};$/ },
|
||||
{ start: /^var __decorate/, end: /^};$/ },
|
||||
{ start: /^var __metadata/, end: /^};$/ },
|
||||
{ start: /^var __param/, end: /^};$/ },
|
||||
{ start: /^var __awaiter/, end: /^};$/ },
|
||||
];
|
||||
destFiles.forEach(function (destFile) {
|
||||
var SEEN_BOILERPLATE = [];
|
||||
destFile.sources.forEach(function (source) {
|
||||
var lines = source.contents.split(/\r\n|\n|\r/);
|
||||
var newLines = [];
|
||||
var IS_REMOVING_BOILERPLATE = false, END_BOILERPLATE;
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var line = lines[i];
|
||||
if (IS_REMOVING_BOILERPLATE) {
|
||||
newLines.push('');
|
||||
if (END_BOILERPLATE.test(line)) {
|
||||
IS_REMOVING_BOILERPLATE = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (var j = 0; j < BOILERPLATE.length; j++) {
|
||||
var boilerplate = BOILERPLATE[j];
|
||||
if (boilerplate.start.test(line)) {
|
||||
if (SEEN_BOILERPLATE[j]) {
|
||||
IS_REMOVING_BOILERPLATE = true;
|
||||
END_BOILERPLATE = boilerplate.end;
|
||||
}
|
||||
else {
|
||||
SEEN_BOILERPLATE[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (IS_REMOVING_BOILERPLATE) {
|
||||
newLines.push('');
|
||||
}
|
||||
else {
|
||||
newLines.push(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
source.contents = newLines.join('\n');
|
||||
});
|
||||
});
|
||||
return destFiles;
|
||||
}
|
||||
function emitEntryPoint(modulesMap, deps, entryPoint, includedModules, prepend, append, dest) {
|
||||
if (!dest) {
|
||||
dest = entryPoint + '.js';
|
||||
}
|
||||
var mainResult = {
|
||||
sources: [],
|
||||
dest: dest
|
||||
}, results = [mainResult];
|
||||
var usedPlugins = {};
|
||||
var getLoaderPlugin = function (pluginName) {
|
||||
if (!usedPlugins[pluginName]) {
|
||||
usedPlugins[pluginName] = modulesMap[pluginName].exports;
|
||||
}
|
||||
return usedPlugins[pluginName];
|
||||
};
|
||||
includedModules.forEach(function (c) {
|
||||
var bangIndex = c.indexOf('!');
|
||||
if (bangIndex >= 0) {
|
||||
var pluginName = c.substr(0, bangIndex);
|
||||
var plugin = getLoaderPlugin(pluginName);
|
||||
mainResult.sources.push(emitPlugin(entryPoint, plugin, pluginName, c.substr(bangIndex + 1)));
|
||||
return;
|
||||
}
|
||||
var module = modulesMap[c];
|
||||
if (module.path === 'empty:') {
|
||||
return;
|
||||
}
|
||||
var contents = readFileAndRemoveBOM(module.path);
|
||||
if (module.shim) {
|
||||
mainResult.sources.push(emitShimmedModule(c, deps[c], module.shim, module.path, contents));
|
||||
}
|
||||
else {
|
||||
mainResult.sources.push(emitNamedModule(c, deps[c], module.defineLocation, module.path, contents));
|
||||
}
|
||||
});
|
||||
Object.keys(usedPlugins).forEach(function (pluginName) {
|
||||
var plugin = usedPlugins[pluginName];
|
||||
if (typeof plugin.writeFile === 'function') {
|
||||
var req = (function () {
|
||||
throw new Error('no-no!');
|
||||
});
|
||||
req.toUrl = function (something) { return something; };
|
||||
var write = function (filename, contents) {
|
||||
results.push({
|
||||
dest: filename,
|
||||
sources: [{
|
||||
path: null,
|
||||
contents: contents
|
||||
}]
|
||||
});
|
||||
};
|
||||
plugin.writeFile(pluginName, entryPoint, req, write, {});
|
||||
}
|
||||
});
|
||||
var toIFile = function (path) {
|
||||
var contents = readFileAndRemoveBOM(path);
|
||||
return {
|
||||
path: path,
|
||||
contents: contents
|
||||
};
|
||||
};
|
||||
var toPrepend = (prepend || []).map(toIFile);
|
||||
var toAppend = (append || []).map(toIFile);
|
||||
mainResult.sources = toPrepend.concat(mainResult.sources).concat(toAppend);
|
||||
return {
|
||||
files: results,
|
||||
usedPlugins: usedPlugins
|
||||
};
|
||||
}
|
||||
function readFileAndRemoveBOM(path) {
|
||||
var BOM_CHAR_CODE = 65279;
|
||||
var contents = fs.readFileSync(path, 'utf8');
|
||||
// Remove BOM
|
||||
if (contents.charCodeAt(0) === BOM_CHAR_CODE) {
|
||||
contents = contents.substring(1);
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
function emitPlugin(entryPoint, plugin, pluginName, moduleName) {
|
||||
var result = '';
|
||||
if (typeof plugin.write === 'function') {
|
||||
var write = (function (what) {
|
||||
result += what;
|
||||
});
|
||||
write.getEntryPoint = function () {
|
||||
return entryPoint;
|
||||
};
|
||||
write.asModule = function (moduleId, code) {
|
||||
code = code.replace(/^define\(/, 'define("' + moduleId + '",');
|
||||
result += code;
|
||||
};
|
||||
plugin.write(pluginName, moduleName, write);
|
||||
}
|
||||
return {
|
||||
path: null,
|
||||
contents: result
|
||||
};
|
||||
}
|
||||
function emitNamedModule(moduleId, myDeps, defineCallPosition, path, contents) {
|
||||
// `defineCallPosition` is the position in code: |define()
|
||||
var defineCallOffset = positionToOffset(contents, defineCallPosition.line, defineCallPosition.col);
|
||||
// `parensOffset` is the position in code: define|()
|
||||
var parensOffset = contents.indexOf('(', defineCallOffset);
|
||||
var insertStr = '"' + moduleId + '", ';
|
||||
return {
|
||||
path: path,
|
||||
contents: contents.substr(0, parensOffset + 1) + insertStr + contents.substr(parensOffset + 1)
|
||||
};
|
||||
}
|
||||
function emitShimmedModule(moduleId, myDeps, factory, path, contents) {
|
||||
var strDeps = (myDeps.length > 0 ? '"' + myDeps.join('", "') + '"' : '');
|
||||
var strDefine = 'define("' + moduleId + '", [' + strDeps + '], ' + factory + ');';
|
||||
return {
|
||||
path: path,
|
||||
contents: contents + '\n;\n' + strDefine
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Convert a position (line:col) to (offset) in string `str`
|
||||
*/
|
||||
function positionToOffset(str, desiredLine, desiredCol) {
|
||||
if (desiredLine === 1) {
|
||||
return desiredCol - 1;
|
||||
}
|
||||
var line = 1, lastNewLineOffset = -1;
|
||||
do {
|
||||
if (desiredLine === line) {
|
||||
return lastNewLineOffset + 1 + desiredCol - 1;
|
||||
}
|
||||
lastNewLineOffset = str.indexOf('\n', lastNewLineOffset + 1);
|
||||
line++;
|
||||
} while (lastNewLineOffset >= 0);
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
* Return a set of reachable nodes in `graph` starting from `rootNodes`
|
||||
*/
|
||||
function visit(rootNodes, graph) {
|
||||
var result = {}, queue = rootNodes;
|
||||
rootNodes.forEach(function (node) {
|
||||
result[node] = true;
|
||||
});
|
||||
while (queue.length > 0) {
|
||||
var el = queue.shift();
|
||||
var myEdges = graph[el] || [];
|
||||
myEdges.forEach(function (toNode) {
|
||||
if (!result[toNode]) {
|
||||
result[toNode] = true;
|
||||
queue.push(toNode);
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Perform a topological sort on `graph`
|
||||
*/
|
||||
function topologicalSort(graph) {
|
||||
var allNodes = {}, outgoingEdgeCount = {}, inverseEdges = {};
|
||||
Object.keys(graph).forEach(function (fromNode) {
|
||||
allNodes[fromNode] = true;
|
||||
outgoingEdgeCount[fromNode] = graph[fromNode].length;
|
||||
graph[fromNode].forEach(function (toNode) {
|
||||
allNodes[toNode] = true;
|
||||
outgoingEdgeCount[toNode] = outgoingEdgeCount[toNode] || 0;
|
||||
inverseEdges[toNode] = inverseEdges[toNode] || [];
|
||||
inverseEdges[toNode].push(fromNode);
|
||||
});
|
||||
});
|
||||
// https://en.wikipedia.org/wiki/Topological_sorting
|
||||
var S = [], L = [];
|
||||
Object.keys(allNodes).forEach(function (node) {
|
||||
if (outgoingEdgeCount[node] === 0) {
|
||||
delete outgoingEdgeCount[node];
|
||||
S.push(node);
|
||||
}
|
||||
});
|
||||
while (S.length > 0) {
|
||||
// Ensure the exact same order all the time with the same inputs
|
||||
S.sort();
|
||||
var n = S.shift();
|
||||
L.push(n);
|
||||
var myInverseEdges = inverseEdges[n] || [];
|
||||
myInverseEdges.forEach(function (m) {
|
||||
outgoingEdgeCount[m]--;
|
||||
if (outgoingEdgeCount[m] === 0) {
|
||||
delete outgoingEdgeCount[m];
|
||||
S.push(m);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (Object.keys(outgoingEdgeCount).length > 0) {
|
||||
throw new Error('Cannot do topological sort on cyclic graph, remaining nodes: ' + Object.keys(outgoingEdgeCount));
|
||||
}
|
||||
return L;
|
||||
}
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var vm = require("vm");
|
||||
/**
|
||||
* Bundle `entryPoints` given config `config`.
|
||||
*/
|
||||
function bundle(entryPoints, config, callback) {
|
||||
var entryPointsMap = {};
|
||||
entryPoints.forEach(function (module) {
|
||||
entryPointsMap[module.name] = module;
|
||||
});
|
||||
var allMentionedModulesMap = {};
|
||||
entryPoints.forEach(function (module) {
|
||||
allMentionedModulesMap[module.name] = true;
|
||||
(module.include || []).forEach(function (includedModule) {
|
||||
allMentionedModulesMap[includedModule] = true;
|
||||
});
|
||||
(module.exclude || []).forEach(function (excludedModule) {
|
||||
allMentionedModulesMap[excludedModule] = true;
|
||||
});
|
||||
});
|
||||
var code = require('fs').readFileSync(path.join(__dirname, '../../src/vs/loader.js'));
|
||||
var r = vm.runInThisContext('(function(require, module, exports) { ' + code + '\n});');
|
||||
var loaderModule = { exports: {} };
|
||||
r.call({}, require, loaderModule, loaderModule.exports);
|
||||
var loader = loaderModule.exports;
|
||||
config.isBuild = true;
|
||||
loader.config(config);
|
||||
loader(['require'], function (localRequire) {
|
||||
var resolvePath = function (path) {
|
||||
var r = localRequire.toUrl(path);
|
||||
if (!/\.js/.test(r)) {
|
||||
return r + '.js';
|
||||
}
|
||||
return r;
|
||||
};
|
||||
for (var moduleId in entryPointsMap) {
|
||||
var entryPoint = entryPointsMap[moduleId];
|
||||
if (entryPoint.append) {
|
||||
entryPoint.append = entryPoint.append.map(resolvePath);
|
||||
}
|
||||
if (entryPoint.prepend) {
|
||||
entryPoint.prepend = entryPoint.prepend.map(resolvePath);
|
||||
}
|
||||
}
|
||||
});
|
||||
loader(Object.keys(allMentionedModulesMap), function () {
|
||||
var modules = loader.getBuildInfo();
|
||||
var partialResult = emitEntryPoints(modules, entryPointsMap);
|
||||
var cssInlinedResources = loader('vs/css').getInlinedResources();
|
||||
callback(null, {
|
||||
files: partialResult.files,
|
||||
cssInlinedResources: cssInlinedResources,
|
||||
bundleData: partialResult.bundleData
|
||||
});
|
||||
}, function (err) { return callback(err, null); });
|
||||
}
|
||||
exports.bundle = bundle;
|
||||
function emitEntryPoints(modules, entryPoints) {
|
||||
var modulesMap = {};
|
||||
modules.forEach(function (m) {
|
||||
modulesMap[m.id] = m;
|
||||
});
|
||||
var modulesGraph = {};
|
||||
modules.forEach(function (m) {
|
||||
modulesGraph[m.id] = m.dependencies;
|
||||
});
|
||||
var sortedModules = topologicalSort(modulesGraph);
|
||||
var result = [];
|
||||
var usedPlugins = {};
|
||||
var bundleData = {
|
||||
graph: modulesGraph,
|
||||
bundles: {}
|
||||
};
|
||||
Object.keys(entryPoints).forEach(function (moduleToBundle) {
|
||||
var info = entryPoints[moduleToBundle];
|
||||
var rootNodes = [moduleToBundle].concat(info.include || []);
|
||||
var allDependencies = visit(rootNodes, modulesGraph);
|
||||
var excludes = ['require', 'exports', 'module'].concat(info.exclude || []);
|
||||
excludes.forEach(function (excludeRoot) {
|
||||
var allExcludes = visit([excludeRoot], modulesGraph);
|
||||
Object.keys(allExcludes).forEach(function (exclude) {
|
||||
delete allDependencies[exclude];
|
||||
});
|
||||
});
|
||||
var includedModules = sortedModules.filter(function (module) {
|
||||
return allDependencies[module];
|
||||
});
|
||||
bundleData.bundles[moduleToBundle] = includedModules;
|
||||
var res = emitEntryPoint(modulesMap, modulesGraph, moduleToBundle, includedModules, info.prepend, info.append, info.dest);
|
||||
result = result.concat(res.files);
|
||||
for (var pluginName in res.usedPlugins) {
|
||||
usedPlugins[pluginName] = usedPlugins[pluginName] || res.usedPlugins[pluginName];
|
||||
}
|
||||
});
|
||||
Object.keys(usedPlugins).forEach(function (pluginName) {
|
||||
var plugin = usedPlugins[pluginName];
|
||||
if (typeof plugin.finishBuild === 'function') {
|
||||
var write = function (filename, contents) {
|
||||
result.push({
|
||||
dest: filename,
|
||||
sources: [{
|
||||
path: null,
|
||||
contents: contents
|
||||
}]
|
||||
});
|
||||
};
|
||||
plugin.finishBuild(write);
|
||||
}
|
||||
});
|
||||
return {
|
||||
files: extractStrings(removeDuplicateTSBoilerplate(result)),
|
||||
bundleData: bundleData
|
||||
};
|
||||
}
|
||||
function extractStrings(destFiles) {
|
||||
var parseDefineCall = function (moduleMatch, depsMatch) {
|
||||
var module = moduleMatch.replace(/^"|"$/g, '');
|
||||
var deps = depsMatch.split(',');
|
||||
deps = deps.map(function (dep) {
|
||||
dep = dep.trim();
|
||||
dep = dep.replace(/^"|"$/g, '');
|
||||
dep = dep.replace(/^'|'$/g, '');
|
||||
var prefix = null;
|
||||
var _path = null;
|
||||
var pieces = dep.split('!');
|
||||
if (pieces.length > 1) {
|
||||
prefix = pieces[0] + '!';
|
||||
_path = pieces[1];
|
||||
}
|
||||
else {
|
||||
prefix = '';
|
||||
_path = pieces[0];
|
||||
}
|
||||
if (/^\.\//.test(_path) || /^\.\.\//.test(_path)) {
|
||||
var res = path.join(path.dirname(module), _path).replace(/\\/g, '/');
|
||||
return prefix + res;
|
||||
}
|
||||
return prefix + _path;
|
||||
});
|
||||
return {
|
||||
module: module,
|
||||
deps: deps
|
||||
};
|
||||
};
|
||||
destFiles.forEach(function (destFile, index) {
|
||||
if (!/\.js$/.test(destFile.dest)) {
|
||||
return;
|
||||
}
|
||||
if (/\.nls\.js$/.test(destFile.dest)) {
|
||||
return;
|
||||
}
|
||||
// Do one pass to record the usage counts for each module id
|
||||
var useCounts = {};
|
||||
destFile.sources.forEach(function (source) {
|
||||
var matches = source.contents.match(/define\(("[^"]+"),\s*\[(((, )?("|')[^"']+("|'))+)\]/);
|
||||
if (!matches) {
|
||||
return;
|
||||
}
|
||||
var defineCall = parseDefineCall(matches[1], matches[2]);
|
||||
useCounts[defineCall.module] = (useCounts[defineCall.module] || 0) + 1;
|
||||
defineCall.deps.forEach(function (dep) {
|
||||
useCounts[dep] = (useCounts[dep] || 0) + 1;
|
||||
});
|
||||
});
|
||||
var sortedByUseModules = Object.keys(useCounts);
|
||||
sortedByUseModules.sort(function (a, b) {
|
||||
return useCounts[b] - useCounts[a];
|
||||
});
|
||||
var replacementMap = {};
|
||||
sortedByUseModules.forEach(function (module, index) {
|
||||
replacementMap[module] = index;
|
||||
});
|
||||
destFile.sources.forEach(function (source) {
|
||||
source.contents = source.contents.replace(/define\(("[^"]+"),\s*\[(((, )?("|')[^"']+("|'))+)\]/, function (_, moduleMatch, depsMatch) {
|
||||
var defineCall = parseDefineCall(moduleMatch, depsMatch);
|
||||
return "define(__m[" + replacementMap[defineCall.module] + "/*" + defineCall.module + "*/], __M([" + defineCall.deps.map(function (dep) { return replacementMap[dep] + '/*' + dep + '*/'; }).join(',') + "])";
|
||||
});
|
||||
});
|
||||
destFile.sources.unshift({
|
||||
path: null,
|
||||
contents: [
|
||||
'(function() {',
|
||||
"var __m = " + JSON.stringify(sortedByUseModules) + ";",
|
||||
"var __M = function(deps) {",
|
||||
" var result = [];",
|
||||
" for (var i = 0, len = deps.length; i < len; i++) {",
|
||||
" result[i] = __m[deps[i]];",
|
||||
" }",
|
||||
" return result;",
|
||||
"};"
|
||||
].join('\n')
|
||||
});
|
||||
destFile.sources.push({
|
||||
path: null,
|
||||
contents: '}).call(this);'
|
||||
});
|
||||
});
|
||||
return destFiles;
|
||||
}
|
||||
function removeDuplicateTSBoilerplate(destFiles) {
|
||||
// Taken from typescript compiler => emitFiles
|
||||
var BOILERPLATE = [
|
||||
{ start: /^var __extends/, end: /^};$/ },
|
||||
{ start: /^var __assign/, end: /^};$/ },
|
||||
{ start: /^var __decorate/, end: /^};$/ },
|
||||
{ start: /^var __metadata/, end: /^};$/ },
|
||||
{ start: /^var __param/, end: /^};$/ },
|
||||
{ start: /^var __awaiter/, end: /^};$/ },
|
||||
];
|
||||
destFiles.forEach(function (destFile) {
|
||||
var SEEN_BOILERPLATE = [];
|
||||
destFile.sources.forEach(function (source) {
|
||||
var lines = source.contents.split(/\r\n|\n|\r/);
|
||||
var newLines = [];
|
||||
var IS_REMOVING_BOILERPLATE = false, END_BOILERPLATE;
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
var line = lines[i];
|
||||
if (IS_REMOVING_BOILERPLATE) {
|
||||
newLines.push('');
|
||||
if (END_BOILERPLATE.test(line)) {
|
||||
IS_REMOVING_BOILERPLATE = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (var j = 0; j < BOILERPLATE.length; j++) {
|
||||
var boilerplate = BOILERPLATE[j];
|
||||
if (boilerplate.start.test(line)) {
|
||||
if (SEEN_BOILERPLATE[j]) {
|
||||
IS_REMOVING_BOILERPLATE = true;
|
||||
END_BOILERPLATE = boilerplate.end;
|
||||
}
|
||||
else {
|
||||
SEEN_BOILERPLATE[j] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (IS_REMOVING_BOILERPLATE) {
|
||||
newLines.push('');
|
||||
}
|
||||
else {
|
||||
newLines.push(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
source.contents = newLines.join('\n');
|
||||
});
|
||||
});
|
||||
return destFiles;
|
||||
}
|
||||
function emitEntryPoint(modulesMap, deps, entryPoint, includedModules, prepend, append, dest) {
|
||||
if (!dest) {
|
||||
dest = entryPoint + '.js';
|
||||
}
|
||||
var mainResult = {
|
||||
sources: [],
|
||||
dest: dest
|
||||
}, results = [mainResult];
|
||||
var usedPlugins = {};
|
||||
var getLoaderPlugin = function (pluginName) {
|
||||
if (!usedPlugins[pluginName]) {
|
||||
usedPlugins[pluginName] = modulesMap[pluginName].exports;
|
||||
}
|
||||
return usedPlugins[pluginName];
|
||||
};
|
||||
includedModules.forEach(function (c) {
|
||||
var bangIndex = c.indexOf('!');
|
||||
if (bangIndex >= 0) {
|
||||
var pluginName = c.substr(0, bangIndex);
|
||||
var plugin = getLoaderPlugin(pluginName);
|
||||
mainResult.sources.push(emitPlugin(entryPoint, plugin, pluginName, c.substr(bangIndex + 1)));
|
||||
return;
|
||||
}
|
||||
var module = modulesMap[c];
|
||||
if (module.path === 'empty:') {
|
||||
return;
|
||||
}
|
||||
var contents = readFileAndRemoveBOM(module.path);
|
||||
if (module.shim) {
|
||||
mainResult.sources.push(emitShimmedModule(c, deps[c], module.shim, module.path, contents));
|
||||
}
|
||||
else {
|
||||
mainResult.sources.push(emitNamedModule(c, deps[c], module.defineLocation, module.path, contents));
|
||||
}
|
||||
});
|
||||
Object.keys(usedPlugins).forEach(function (pluginName) {
|
||||
var plugin = usedPlugins[pluginName];
|
||||
if (typeof plugin.writeFile === 'function') {
|
||||
var req = (function () {
|
||||
throw new Error('no-no!');
|
||||
});
|
||||
req.toUrl = function (something) { return something; };
|
||||
var write = function (filename, contents) {
|
||||
results.push({
|
||||
dest: filename,
|
||||
sources: [{
|
||||
path: null,
|
||||
contents: contents
|
||||
}]
|
||||
});
|
||||
};
|
||||
plugin.writeFile(pluginName, entryPoint, req, write, {});
|
||||
}
|
||||
});
|
||||
var toIFile = function (path) {
|
||||
var contents = readFileAndRemoveBOM(path);
|
||||
return {
|
||||
path: path,
|
||||
contents: contents
|
||||
};
|
||||
};
|
||||
var toPrepend = (prepend || []).map(toIFile);
|
||||
var toAppend = (append || []).map(toIFile);
|
||||
mainResult.sources = toPrepend.concat(mainResult.sources).concat(toAppend);
|
||||
return {
|
||||
files: results,
|
||||
usedPlugins: usedPlugins
|
||||
};
|
||||
}
|
||||
function readFileAndRemoveBOM(path) {
|
||||
var BOM_CHAR_CODE = 65279;
|
||||
var contents = fs.readFileSync(path, 'utf8');
|
||||
// Remove BOM
|
||||
if (contents.charCodeAt(0) === BOM_CHAR_CODE) {
|
||||
contents = contents.substring(1);
|
||||
}
|
||||
return contents;
|
||||
}
|
||||
function emitPlugin(entryPoint, plugin, pluginName, moduleName) {
|
||||
var result = '';
|
||||
if (typeof plugin.write === 'function') {
|
||||
var write = (function (what) {
|
||||
result += what;
|
||||
});
|
||||
write.getEntryPoint = function () {
|
||||
return entryPoint;
|
||||
};
|
||||
write.asModule = function (moduleId, code) {
|
||||
code = code.replace(/^define\(/, 'define("' + moduleId + '",');
|
||||
result += code;
|
||||
};
|
||||
plugin.write(pluginName, moduleName, write);
|
||||
}
|
||||
return {
|
||||
path: null,
|
||||
contents: result
|
||||
};
|
||||
}
|
||||
function emitNamedModule(moduleId, myDeps, defineCallPosition, path, contents) {
|
||||
// `defineCallPosition` is the position in code: |define()
|
||||
var defineCallOffset = positionToOffset(contents, defineCallPosition.line, defineCallPosition.col);
|
||||
// `parensOffset` is the position in code: define|()
|
||||
var parensOffset = contents.indexOf('(', defineCallOffset);
|
||||
var insertStr = '"' + moduleId + '", ';
|
||||
return {
|
||||
path: path,
|
||||
contents: contents.substr(0, parensOffset + 1) + insertStr + contents.substr(parensOffset + 1)
|
||||
};
|
||||
}
|
||||
function emitShimmedModule(moduleId, myDeps, factory, path, contents) {
|
||||
var strDeps = (myDeps.length > 0 ? '"' + myDeps.join('", "') + '"' : '');
|
||||
var strDefine = 'define("' + moduleId + '", [' + strDeps + '], ' + factory + ');';
|
||||
return {
|
||||
path: path,
|
||||
contents: contents + '\n;\n' + strDefine
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Convert a position (line:col) to (offset) in string `str`
|
||||
*/
|
||||
function positionToOffset(str, desiredLine, desiredCol) {
|
||||
if (desiredLine === 1) {
|
||||
return desiredCol - 1;
|
||||
}
|
||||
var line = 1, lastNewLineOffset = -1;
|
||||
do {
|
||||
if (desiredLine === line) {
|
||||
return lastNewLineOffset + 1 + desiredCol - 1;
|
||||
}
|
||||
lastNewLineOffset = str.indexOf('\n', lastNewLineOffset + 1);
|
||||
line++;
|
||||
} while (lastNewLineOffset >= 0);
|
||||
return -1;
|
||||
}
|
||||
/**
|
||||
* Return a set of reachable nodes in `graph` starting from `rootNodes`
|
||||
*/
|
||||
function visit(rootNodes, graph) {
|
||||
var result = {}, queue = rootNodes;
|
||||
rootNodes.forEach(function (node) {
|
||||
result[node] = true;
|
||||
});
|
||||
while (queue.length > 0) {
|
||||
var el = queue.shift();
|
||||
var myEdges = graph[el] || [];
|
||||
myEdges.forEach(function (toNode) {
|
||||
if (!result[toNode]) {
|
||||
result[toNode] = true;
|
||||
queue.push(toNode);
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Perform a topological sort on `graph`
|
||||
*/
|
||||
function topologicalSort(graph) {
|
||||
var allNodes = {}, outgoingEdgeCount = {}, inverseEdges = {};
|
||||
Object.keys(graph).forEach(function (fromNode) {
|
||||
allNodes[fromNode] = true;
|
||||
outgoingEdgeCount[fromNode] = graph[fromNode].length;
|
||||
graph[fromNode].forEach(function (toNode) {
|
||||
allNodes[toNode] = true;
|
||||
outgoingEdgeCount[toNode] = outgoingEdgeCount[toNode] || 0;
|
||||
inverseEdges[toNode] = inverseEdges[toNode] || [];
|
||||
inverseEdges[toNode].push(fromNode);
|
||||
});
|
||||
});
|
||||
// https://en.wikipedia.org/wiki/Topological_sorting
|
||||
var S = [], L = [];
|
||||
Object.keys(allNodes).forEach(function (node) {
|
||||
if (outgoingEdgeCount[node] === 0) {
|
||||
delete outgoingEdgeCount[node];
|
||||
S.push(node);
|
||||
}
|
||||
});
|
||||
while (S.length > 0) {
|
||||
// Ensure the exact same order all the time with the same inputs
|
||||
S.sort();
|
||||
var n = S.shift();
|
||||
L.push(n);
|
||||
var myInverseEdges = inverseEdges[n] || [];
|
||||
myInverseEdges.forEach(function (m) {
|
||||
outgoingEdgeCount[m]--;
|
||||
if (outgoingEdgeCount[m] === 0) {
|
||||
delete outgoingEdgeCount[m];
|
||||
S.push(m);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (Object.keys(outgoingEdgeCount).length > 0) {
|
||||
throw new Error('Cannot do topological sort on cyclic graph, remaining nodes: ' + Object.keys(outgoingEdgeCount));
|
||||
}
|
||||
return L;
|
||||
}
|
||||
|
|
|
@ -1,171 +1,170 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var gulp = require("gulp");
|
||||
var tsb = require("gulp-tsb");
|
||||
var es = require("event-stream");
|
||||
var watch = require('./watch');
|
||||
var nls = require("./nls");
|
||||
var util = require("./util");
|
||||
var reporter_1 = require("./reporter");
|
||||
var path = require("path");
|
||||
var bom = require("gulp-bom");
|
||||
var sourcemaps = require("gulp-sourcemaps");
|
||||
var _ = require("underscore");
|
||||
var monacodts = require("../monaco/api");
|
||||
var fs = require("fs");
|
||||
var reporter = reporter_1.createReporter();
|
||||
var rootDir = path.join(__dirname, '../../src');
|
||||
var options = require('../../src/tsconfig.json').compilerOptions;
|
||||
options.verbose = false;
|
||||
options.sourceMap = true;
|
||||
options.rootDir = rootDir;
|
||||
options.sourceRoot = util.toFileUri(rootDir);
|
||||
function createCompile(build, emitError) {
|
||||
var opts = _.clone(options);
|
||||
opts.inlineSources = !!build;
|
||||
opts.noFilesystemLookup = true;
|
||||
var ts = tsb.create(opts, null, null, function (err) { return reporter(err.toString()); });
|
||||
return function (token) {
|
||||
var utf8Filter = util.filter(function (data) { return /(\/|\\)test(\/|\\).*utf8/.test(data.path); });
|
||||
var tsFilter = util.filter(function (data) { return /\.ts$/.test(data.path); });
|
||||
var noDeclarationsFilter = util.filter(function (data) { return !(/\.d\.ts$/.test(data.path)); });
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(utf8Filter)
|
||||
.pipe(bom())
|
||||
.pipe(utf8Filter.restore)
|
||||
.pipe(tsFilter)
|
||||
.pipe(util.loadSourcemaps())
|
||||
.pipe(ts(token))
|
||||
.pipe(build ? reloadTypeScriptNodeModule() : es.through())
|
||||
.pipe(noDeclarationsFilter)
|
||||
.pipe(build ? nls() : es.through())
|
||||
.pipe(noDeclarationsFilter.restore)
|
||||
.pipe(sourcemaps.write('.', {
|
||||
addComment: false,
|
||||
includeContent: !!build,
|
||||
sourceRoot: options.sourceRoot
|
||||
}))
|
||||
.pipe(tsFilter.restore)
|
||||
.pipe(reporter.end(emitError));
|
||||
return es.duplex(input, output);
|
||||
};
|
||||
}
|
||||
function compileTask(out, build) {
|
||||
return function () {
|
||||
var compile = createCompile(build, true);
|
||||
var src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src('node_modules/typescript/lib/lib.d.ts'), gulp.src('node_modules/@types/**/index.d.ts'));
|
||||
return src
|
||||
.pipe(compile())
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, false));
|
||||
};
|
||||
}
|
||||
exports.compileTask = compileTask;
|
||||
function watchTask(out, build) {
|
||||
return function () {
|
||||
var compile = createCompile(build);
|
||||
var src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src('node_modules/typescript/lib/lib.d.ts'), gulp.src('node_modules/@types/**/index.d.ts'));
|
||||
var watchSrc = watch('src/**', { base: 'src' });
|
||||
return watchSrc
|
||||
.pipe(util.incremental(compile, src, true))
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, true));
|
||||
};
|
||||
}
|
||||
exports.watchTask = watchTask;
|
||||
function reloadTypeScriptNodeModule() {
|
||||
var util = require('gulp-util');
|
||||
function log(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log.apply(util, [util.colors.cyan('[memory watch dog]'), message].concat(rest));
|
||||
}
|
||||
function heapUsed() {
|
||||
return (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2) + ' MB';
|
||||
}
|
||||
return es.through(function (data) {
|
||||
this.emit('data', data);
|
||||
}, function () {
|
||||
log('memory usage after compilation finished: ' + heapUsed());
|
||||
// It appears we are running into some variant of
|
||||
// https://bugs.chromium.org/p/v8/issues/detail?id=2073
|
||||
//
|
||||
// Even though all references are dropped, some
|
||||
// optimized methods in the TS compiler end up holding references
|
||||
// to the entire TypeScript language host (>600MB)
|
||||
//
|
||||
// The idea is to force v8 to drop references to these
|
||||
// optimized methods, by "reloading" the typescript node module
|
||||
log('Reloading typescript node module...');
|
||||
var resolvedName = require.resolve('typescript');
|
||||
var originalModule = require.cache[resolvedName];
|
||||
delete require.cache[resolvedName];
|
||||
var newExports = require('typescript');
|
||||
require.cache[resolvedName] = originalModule;
|
||||
for (var prop in newExports) {
|
||||
if (newExports.hasOwnProperty(prop)) {
|
||||
originalModule.exports[prop] = newExports[prop];
|
||||
}
|
||||
}
|
||||
log('typescript node module reloaded.');
|
||||
this.emit('end');
|
||||
});
|
||||
}
|
||||
function monacodtsTask(out, isWatch) {
|
||||
var neededFiles = {};
|
||||
monacodts.getFilesToWatch(out).forEach(function (filePath) {
|
||||
filePath = path.normalize(filePath);
|
||||
neededFiles[filePath] = true;
|
||||
});
|
||||
var inputFiles = {};
|
||||
for (var filePath in neededFiles) {
|
||||
if (/\bsrc(\/|\\)vs\b/.test(filePath)) {
|
||||
// This file is needed from source => simply read it now
|
||||
inputFiles[filePath] = fs.readFileSync(filePath).toString();
|
||||
}
|
||||
}
|
||||
var setInputFile = function (filePath, contents) {
|
||||
if (inputFiles[filePath] === contents) {
|
||||
// no change
|
||||
return;
|
||||
}
|
||||
inputFiles[filePath] = contents;
|
||||
var neededInputFilesCount = Object.keys(neededFiles).length;
|
||||
var availableInputFilesCount = Object.keys(inputFiles).length;
|
||||
if (neededInputFilesCount === availableInputFilesCount) {
|
||||
run();
|
||||
}
|
||||
};
|
||||
var run = function () {
|
||||
var result = monacodts.run(out, inputFiles);
|
||||
if (!result.isTheSame) {
|
||||
if (isWatch) {
|
||||
fs.writeFileSync(result.filePath, result.content);
|
||||
}
|
||||
else {
|
||||
resultStream.emit('error', 'monaco.d.ts is no longer up to date. Please run gulp watch and commit the new file.');
|
||||
}
|
||||
}
|
||||
};
|
||||
var resultStream;
|
||||
if (isWatch) {
|
||||
watch('build/monaco/*').pipe(es.through(function () {
|
||||
run();
|
||||
}));
|
||||
}
|
||||
resultStream = es.through(function (data) {
|
||||
var filePath = path.normalize(data.path);
|
||||
if (neededFiles[filePath]) {
|
||||
setInputFile(filePath, data.contents.toString());
|
||||
}
|
||||
this.emit('data', data);
|
||||
});
|
||||
return resultStream;
|
||||
}
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
var gulp = require("gulp");
|
||||
var tsb = require("gulp-tsb");
|
||||
var es = require("event-stream");
|
||||
var watch = require('./watch');
|
||||
var nls = require("./nls");
|
||||
var util = require("./util");
|
||||
var reporter_1 = require("./reporter");
|
||||
var path = require("path");
|
||||
var bom = require("gulp-bom");
|
||||
var sourcemaps = require("gulp-sourcemaps");
|
||||
var _ = require("underscore");
|
||||
var monacodts = require("../monaco/api");
|
||||
var fs = require("fs");
|
||||
var reporter = reporter_1.createReporter();
|
||||
var rootDir = path.join(__dirname, '../../src');
|
||||
var options = require('../../src/tsconfig.json').compilerOptions;
|
||||
options.verbose = false;
|
||||
options.sourceMap = true;
|
||||
options.rootDir = rootDir;
|
||||
options.sourceRoot = util.toFileUri(rootDir);
|
||||
function createCompile(build, emitError) {
|
||||
var opts = _.clone(options);
|
||||
opts.inlineSources = !!build;
|
||||
opts.noFilesystemLookup = true;
|
||||
var ts = tsb.create(opts, null, null, function (err) { return reporter(err.toString()); });
|
||||
return function (token) {
|
||||
var utf8Filter = util.filter(function (data) { return /(\/|\\)test(\/|\\).*utf8/.test(data.path); });
|
||||
var tsFilter = util.filter(function (data) { return /\.ts$/.test(data.path); });
|
||||
var noDeclarationsFilter = util.filter(function (data) { return !(/\.d\.ts$/.test(data.path)); });
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(utf8Filter)
|
||||
.pipe(bom())
|
||||
.pipe(utf8Filter.restore)
|
||||
.pipe(tsFilter)
|
||||
.pipe(util.loadSourcemaps())
|
||||
.pipe(ts(token))
|
||||
.pipe(build ? reloadTypeScriptNodeModule() : es.through())
|
||||
.pipe(noDeclarationsFilter)
|
||||
.pipe(build ? nls() : es.through())
|
||||
.pipe(noDeclarationsFilter.restore)
|
||||
.pipe(sourcemaps.write('.', {
|
||||
addComment: false,
|
||||
includeContent: !!build,
|
||||
sourceRoot: options.sourceRoot
|
||||
}))
|
||||
.pipe(tsFilter.restore)
|
||||
.pipe(reporter.end(emitError));
|
||||
return es.duplex(input, output);
|
||||
};
|
||||
}
|
||||
function compileTask(out, build) {
|
||||
return function () {
|
||||
var compile = createCompile(build, true);
|
||||
var src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src('node_modules/typescript/lib/lib.d.ts'), gulp.src('node_modules/@types/**/index.d.ts'));
|
||||
return src
|
||||
.pipe(compile())
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, false));
|
||||
};
|
||||
}
|
||||
exports.compileTask = compileTask;
|
||||
function watchTask(out, build) {
|
||||
return function () {
|
||||
var compile = createCompile(build);
|
||||
var src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src('node_modules/typescript/lib/lib.d.ts'), gulp.src('node_modules/@types/**/index.d.ts'));
|
||||
var watchSrc = watch('src/**', { base: 'src' });
|
||||
return watchSrc
|
||||
.pipe(util.incremental(compile, src, true))
|
||||
.pipe(gulp.dest(out))
|
||||
.pipe(monacodtsTask(out, true));
|
||||
};
|
||||
}
|
||||
exports.watchTask = watchTask;
|
||||
function reloadTypeScriptNodeModule() {
|
||||
var util = require('gulp-util');
|
||||
function log(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log.apply(util, [util.colors.cyan('[memory watch dog]'), message].concat(rest));
|
||||
}
|
||||
function heapUsed() {
|
||||
return (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2) + ' MB';
|
||||
}
|
||||
return es.through(function (data) {
|
||||
this.emit('data', data);
|
||||
}, function () {
|
||||
log('memory usage after compilation finished: ' + heapUsed());
|
||||
// It appears we are running into some variant of
|
||||
// https://bugs.chromium.org/p/v8/issues/detail?id=2073
|
||||
//
|
||||
// Even though all references are dropped, some
|
||||
// optimized methods in the TS compiler end up holding references
|
||||
// to the entire TypeScript language host (>600MB)
|
||||
//
|
||||
// The idea is to force v8 to drop references to these
|
||||
// optimized methods, by "reloading" the typescript node module
|
||||
log('Reloading typescript node module...');
|
||||
var resolvedName = require.resolve('typescript');
|
||||
var originalModule = require.cache[resolvedName];
|
||||
delete require.cache[resolvedName];
|
||||
var newExports = require('typescript');
|
||||
require.cache[resolvedName] = originalModule;
|
||||
for (var prop in newExports) {
|
||||
if (newExports.hasOwnProperty(prop)) {
|
||||
originalModule.exports[prop] = newExports[prop];
|
||||
}
|
||||
}
|
||||
log('typescript node module reloaded.');
|
||||
this.emit('end');
|
||||
});
|
||||
}
|
||||
function monacodtsTask(out, isWatch) {
|
||||
var neededFiles = {};
|
||||
monacodts.getFilesToWatch(out).forEach(function (filePath) {
|
||||
filePath = path.normalize(filePath);
|
||||
neededFiles[filePath] = true;
|
||||
});
|
||||
var inputFiles = {};
|
||||
for (var filePath in neededFiles) {
|
||||
if (/\bsrc(\/|\\)vs\b/.test(filePath)) {
|
||||
// This file is needed from source => simply read it now
|
||||
inputFiles[filePath] = fs.readFileSync(filePath).toString();
|
||||
}
|
||||
}
|
||||
var setInputFile = function (filePath, contents) {
|
||||
if (inputFiles[filePath] === contents) {
|
||||
// no change
|
||||
return;
|
||||
}
|
||||
inputFiles[filePath] = contents;
|
||||
var neededInputFilesCount = Object.keys(neededFiles).length;
|
||||
var availableInputFilesCount = Object.keys(inputFiles).length;
|
||||
if (neededInputFilesCount === availableInputFilesCount) {
|
||||
run();
|
||||
}
|
||||
};
|
||||
var run = function () {
|
||||
var result = monacodts.run(out, inputFiles);
|
||||
if (!result.isTheSame) {
|
||||
if (isWatch) {
|
||||
fs.writeFileSync(result.filePath, result.content);
|
||||
}
|
||||
else {
|
||||
resultStream.emit('error', 'monaco.d.ts is no longer up to date. Please run gulp watch and commit the new file.');
|
||||
}
|
||||
}
|
||||
};
|
||||
var resultStream;
|
||||
if (isWatch) {
|
||||
watch('build/monaco/*').pipe(es.through(function () {
|
||||
run();
|
||||
}));
|
||||
}
|
||||
resultStream = es.through(function (data) {
|
||||
var filePath = path.normalize(data.path);
|
||||
if (neededFiles[filePath]) {
|
||||
setInputFile(filePath, data.contents.toString());
|
||||
}
|
||||
this.emit('data', data);
|
||||
});
|
||||
return resultStream;
|
||||
}
|
||||
|
|
|
@ -1,96 +1,95 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var event_stream_1 = require("event-stream");
|
||||
var assign = require("object-assign");
|
||||
var remote = require("gulp-remote-src");
|
||||
var flatmap = require('gulp-flatmap');
|
||||
var vzip = require('gulp-vinyl-zip');
|
||||
var filter = require('gulp-filter');
|
||||
var rename = require('gulp-rename');
|
||||
var util = require('gulp-util');
|
||||
var buffer = require('gulp-buffer');
|
||||
var json = require('gulp-json-editor');
|
||||
function error(err) {
|
||||
var result = event_stream_1.through();
|
||||
setTimeout(function () { return result.emit('error', err); });
|
||||
return result;
|
||||
}
|
||||
var baseHeaders = {
|
||||
'X-Market-Client-Id': 'VSCode Build',
|
||||
'User-Agent': 'VSCode Build',
|
||||
};
|
||||
function src(extensionName, version) {
|
||||
var filterType = 7;
|
||||
var value = extensionName;
|
||||
var criterium = { filterType: filterType, value: value };
|
||||
var criteria = [criterium];
|
||||
var pageNumber = 1;
|
||||
var pageSize = 1;
|
||||
var sortBy = 0;
|
||||
var sortOrder = 0;
|
||||
var flags = 0x1 | 0x2 | 0x80;
|
||||
var assetTypes = ['Microsoft.VisualStudio.Services.VSIXPackage'];
|
||||
var filters = [{ criteria: criteria, pageNumber: pageNumber, pageSize: pageSize, sortBy: sortBy, sortOrder: sortOrder }];
|
||||
var body = JSON.stringify({ filters: filters, assetTypes: assetTypes, flags: flags });
|
||||
var headers = assign({}, baseHeaders, {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json;api-version=3.0-preview.1',
|
||||
'Content-Length': body.length
|
||||
});
|
||||
var options = {
|
||||
base: 'https://marketplace.visualstudio.com/_apis/public/gallery',
|
||||
requestOptions: {
|
||||
method: 'POST',
|
||||
gzip: true,
|
||||
headers: headers,
|
||||
body: body
|
||||
}
|
||||
};
|
||||
return remote('/extensionquery', options)
|
||||
.pipe(flatmap(function (stream, f) {
|
||||
var rawResult = f.contents.toString('utf8');
|
||||
var result = JSON.parse(rawResult);
|
||||
var extension = result.results[0].extensions[0];
|
||||
if (!extension) {
|
||||
return error("No such extension: " + extension);
|
||||
}
|
||||
var metadata = {
|
||||
id: extension.extensionId,
|
||||
publisherId: extension.publisher,
|
||||
publisherDisplayName: extension.publisher.displayName
|
||||
};
|
||||
var extensionVersion = extension.versions.filter(function (v) { return v.version === version; })[0];
|
||||
if (!extensionVersion) {
|
||||
return error("No such extension version: " + extensionName + " @ " + version);
|
||||
}
|
||||
var asset = extensionVersion.files.filter(function (f) { return f.assetType === 'Microsoft.VisualStudio.Services.VSIXPackage'; })[0];
|
||||
if (!asset) {
|
||||
return error("No VSIX found for extension version: " + extensionName + " @ " + version);
|
||||
}
|
||||
util.log('Downloading extension:', util.colors.yellow(extensionName + "@" + version), '...');
|
||||
var options = {
|
||||
base: asset.source,
|
||||
requestOptions: {
|
||||
gzip: true,
|
||||
headers: baseHeaders
|
||||
}
|
||||
};
|
||||
return remote('', options)
|
||||
.pipe(flatmap(function (stream) {
|
||||
var packageJsonFilter = filter('package.json', { restore: true });
|
||||
return stream
|
||||
.pipe(vzip.src())
|
||||
.pipe(filter('extension/**'))
|
||||
.pipe(rename(function (p) { return p.dirname = p.dirname.replace(/^extension\/?/, ''); }))
|
||||
.pipe(packageJsonFilter)
|
||||
.pipe(buffer())
|
||||
.pipe(json({ __metadata: metadata }))
|
||||
.pipe(packageJsonFilter.restore);
|
||||
}));
|
||||
}));
|
||||
}
|
||||
exports.src = src;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var event_stream_1 = require("event-stream");
|
||||
var assign = require("object-assign");
|
||||
var remote = require("gulp-remote-src");
|
||||
var flatmap = require('gulp-flatmap');
|
||||
var vzip = require('gulp-vinyl-zip');
|
||||
var filter = require('gulp-filter');
|
||||
var rename = require('gulp-rename');
|
||||
var util = require('gulp-util');
|
||||
var buffer = require('gulp-buffer');
|
||||
var json = require('gulp-json-editor');
|
||||
function error(err) {
|
||||
var result = event_stream_1.through();
|
||||
setTimeout(function () { return result.emit('error', err); });
|
||||
return result;
|
||||
}
|
||||
var baseHeaders = {
|
||||
'X-Market-Client-Id': 'VSCode Build',
|
||||
'User-Agent': 'VSCode Build',
|
||||
};
|
||||
function src(extensionName, version) {
|
||||
var filterType = 7;
|
||||
var value = extensionName;
|
||||
var criterium = { filterType: filterType, value: value };
|
||||
var criteria = [criterium];
|
||||
var pageNumber = 1;
|
||||
var pageSize = 1;
|
||||
var sortBy = 0;
|
||||
var sortOrder = 0;
|
||||
var flags = 0x1 | 0x2 | 0x80;
|
||||
var assetTypes = ['Microsoft.VisualStudio.Services.VSIXPackage'];
|
||||
var filters = [{ criteria: criteria, pageNumber: pageNumber, pageSize: pageSize, sortBy: sortBy, sortOrder: sortOrder }];
|
||||
var body = JSON.stringify({ filters: filters, assetTypes: assetTypes, flags: flags });
|
||||
var headers = assign({}, baseHeaders, {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json;api-version=3.0-preview.1',
|
||||
'Content-Length': body.length
|
||||
});
|
||||
var options = {
|
||||
base: 'https://marketplace.visualstudio.com/_apis/public/gallery',
|
||||
requestOptions: {
|
||||
method: 'POST',
|
||||
gzip: true,
|
||||
headers: headers,
|
||||
body: body
|
||||
}
|
||||
};
|
||||
return remote('/extensionquery', options)
|
||||
.pipe(flatmap(function (stream, f) {
|
||||
var rawResult = f.contents.toString('utf8');
|
||||
var result = JSON.parse(rawResult);
|
||||
var extension = result.results[0].extensions[0];
|
||||
if (!extension) {
|
||||
return error("No such extension: " + extension);
|
||||
}
|
||||
var metadata = {
|
||||
id: extension.extensionId,
|
||||
publisherId: extension.publisher,
|
||||
publisherDisplayName: extension.publisher.displayName
|
||||
};
|
||||
var extensionVersion = extension.versions.filter(function (v) { return v.version === version; })[0];
|
||||
if (!extensionVersion) {
|
||||
return error("No such extension version: " + extensionName + " @ " + version);
|
||||
}
|
||||
var asset = extensionVersion.files.filter(function (f) { return f.assetType === 'Microsoft.VisualStudio.Services.VSIXPackage'; })[0];
|
||||
if (!asset) {
|
||||
return error("No VSIX found for extension version: " + extensionName + " @ " + version);
|
||||
}
|
||||
util.log('Downloading extension:', util.colors.yellow(extensionName + "@" + version), '...');
|
||||
var options = {
|
||||
base: asset.source,
|
||||
requestOptions: {
|
||||
gzip: true,
|
||||
headers: baseHeaders
|
||||
}
|
||||
};
|
||||
return remote('', options)
|
||||
.pipe(flatmap(function (stream) {
|
||||
var packageJsonFilter = filter('package.json', { restore: true });
|
||||
return stream
|
||||
.pipe(vzip.src())
|
||||
.pipe(filter('extension/**'))
|
||||
.pipe(rename(function (p) { return p.dirname = p.dirname.replace(/^extension\/?/, ''); }))
|
||||
.pipe(packageJsonFilter)
|
||||
.pipe(buffer())
|
||||
.pipe(json({ __metadata: metadata }))
|
||||
.pipe(packageJsonFilter.restore);
|
||||
}));
|
||||
}));
|
||||
}
|
||||
exports.src = src;
|
||||
|
|
104
build/lib/git.js
104
build/lib/git.js
|
@ -1,53 +1,51 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
/**
|
||||
* Returns the sha1 commit version of a repository or undefined in case of failure.
|
||||
*/
|
||||
function getVersion(repo) {
|
||||
var git = path.join(repo, '.git');
|
||||
var headPath = path.join(git, 'HEAD');
|
||||
var head;
|
||||
try {
|
||||
head = fs.readFileSync(headPath, 'utf8').trim();
|
||||
}
|
||||
catch (e) {
|
||||
return void 0;
|
||||
}
|
||||
if (/^[0-9a-f]{40}$/i.test(head)) {
|
||||
return head;
|
||||
}
|
||||
var refMatch = /^ref: (.*)$/.exec(head);
|
||||
if (!refMatch) {
|
||||
return void 0;
|
||||
}
|
||||
var ref = refMatch[1];
|
||||
var refPath = path.join(git, ref);
|
||||
try {
|
||||
return fs.readFileSync(refPath, 'utf8').trim();
|
||||
}
|
||||
catch (e) {
|
||||
// noop
|
||||
}
|
||||
var packedRefsPath = path.join(git, 'packed-refs');
|
||||
var refsRaw;
|
||||
try {
|
||||
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
|
||||
}
|
||||
catch (e) {
|
||||
return void 0;
|
||||
}
|
||||
var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
|
||||
var refsMatch;
|
||||
var refs = {};
|
||||
while (refsMatch = refsRegex.exec(refsRaw)) {
|
||||
refs[refsMatch[2]] = refsMatch[1];
|
||||
}
|
||||
return refs[ref];
|
||||
}
|
||||
exports.getVersion = getVersion;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
/**
|
||||
* Returns the sha1 commit version of a repository or undefined in case of failure.
|
||||
*/
|
||||
function getVersion(repo) {
|
||||
var git = path.join(repo, '.git');
|
||||
var headPath = path.join(git, 'HEAD');
|
||||
var head;
|
||||
try {
|
||||
head = fs.readFileSync(headPath, 'utf8').trim();
|
||||
}
|
||||
catch (e) {
|
||||
return void 0;
|
||||
}
|
||||
if (/^[0-9a-f]{40}$/i.test(head)) {
|
||||
return head;
|
||||
}
|
||||
var refMatch = /^ref: (.*)$/.exec(head);
|
||||
if (!refMatch) {
|
||||
return void 0;
|
||||
}
|
||||
var ref = refMatch[1];
|
||||
var refPath = path.join(git, ref);
|
||||
try {
|
||||
return fs.readFileSync(refPath, 'utf8').trim();
|
||||
}
|
||||
catch (e) {
|
||||
}
|
||||
var packedRefsPath = path.join(git, 'packed-refs');
|
||||
var refsRaw;
|
||||
try {
|
||||
refsRaw = fs.readFileSync(packedRefsPath, 'utf8').trim();
|
||||
}
|
||||
catch (e) {
|
||||
return void 0;
|
||||
}
|
||||
var refsRegex = /^([0-9a-f]{40})\s+(.+)$/gm;
|
||||
var refsMatch;
|
||||
var refs = {};
|
||||
while (refsMatch = refsRegex.exec(refsRaw)) {
|
||||
refs[refsMatch[2]] = refsMatch[1];
|
||||
}
|
||||
return refs[ref];
|
||||
}
|
||||
exports.getVersion = getVersion;
|
||||
|
|
|
@ -1,290 +1,289 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var event_stream_1 = require("event-stream");
|
||||
var File = require("vinyl");
|
||||
var Is = require("is");
|
||||
var util = require('gulp-util');
|
||||
function log(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log.apply(util, [util.colors.green('[i18n]'), message].concat(rest));
|
||||
}
|
||||
var LocalizeInfo;
|
||||
(function (LocalizeInfo) {
|
||||
function is(value) {
|
||||
var candidate = value;
|
||||
return Is.defined(candidate) && Is.string(candidate.key) && (Is.undef(candidate.comment) || (Is.array(candidate.comment) && candidate.comment.every(function (element) { return Is.string(element); })));
|
||||
}
|
||||
LocalizeInfo.is = is;
|
||||
})(LocalizeInfo || (LocalizeInfo = {}));
|
||||
var BundledFormat;
|
||||
(function (BundledFormat) {
|
||||
function is(value) {
|
||||
if (Is.undef(value)) {
|
||||
return false;
|
||||
}
|
||||
var candidate = value;
|
||||
var length = Object.keys(value).length;
|
||||
return length === 3 && Is.defined(candidate.keys) && Is.defined(candidate.messages) && Is.defined(candidate.bundles);
|
||||
}
|
||||
BundledFormat.is = is;
|
||||
})(BundledFormat || (BundledFormat = {}));
|
||||
var vscodeLanguages = [
|
||||
'chs',
|
||||
'cht',
|
||||
'jpn',
|
||||
'kor',
|
||||
'deu',
|
||||
'fra',
|
||||
'esn',
|
||||
'rus',
|
||||
'ita'
|
||||
];
|
||||
var iso639_3_to_2 = {
|
||||
'chs': 'zh-cn',
|
||||
'cht': 'zh-tw',
|
||||
'csy': 'cs-cz',
|
||||
'deu': 'de',
|
||||
'enu': 'en',
|
||||
'esn': 'es',
|
||||
'fra': 'fr',
|
||||
'hun': 'hu',
|
||||
'ita': 'it',
|
||||
'jpn': 'ja',
|
||||
'kor': 'ko',
|
||||
'nld': 'nl',
|
||||
'plk': 'pl',
|
||||
'ptb': 'pt-br',
|
||||
'ptg': 'pt',
|
||||
'rus': 'ru',
|
||||
'sve': 'sv-se',
|
||||
'trk': 'tr'
|
||||
};
|
||||
function sortLanguages(directoryNames) {
|
||||
return directoryNames.map(function (dirName) {
|
||||
var lower = dirName.toLowerCase();
|
||||
return {
|
||||
name: lower,
|
||||
iso639_2: iso639_3_to_2[lower]
|
||||
};
|
||||
}).sort(function (a, b) {
|
||||
if (!a.iso639_2 && !b.iso639_2) {
|
||||
return 0;
|
||||
}
|
||||
if (!a.iso639_2) {
|
||||
return -1;
|
||||
}
|
||||
if (!b.iso639_2) {
|
||||
return 1;
|
||||
}
|
||||
return a.iso639_2 < b.iso639_2 ? -1 : (a.iso639_2 > b.iso639_2 ? 1 : 0);
|
||||
});
|
||||
}
|
||||
function stripComments(content) {
|
||||
/**
|
||||
* First capturing group matches double quoted string
|
||||
* Second matches single quotes string
|
||||
* Third matches block comments
|
||||
* Fourth matches line comments
|
||||
*/
|
||||
var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
|
||||
var result = content.replace(regexp, function (match, m1, m2, m3, m4) {
|
||||
// Only one of m1, m2, m3, m4 matches
|
||||
if (m3) {
|
||||
// A block comment. Replace with nothing
|
||||
return '';
|
||||
}
|
||||
else if (m4) {
|
||||
// A line comment. If it ends in \r?\n then keep it.
|
||||
var length_1 = m4.length;
|
||||
if (length_1 > 2 && m4[length_1 - 1] === '\n') {
|
||||
return m4[length_1 - 2] === '\r' ? '\r\n' : '\n';
|
||||
}
|
||||
else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
else {
|
||||
// We match a string
|
||||
return match;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
;
|
||||
function escapeCharacters(value) {
|
||||
var result = [];
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
var ch = value.charAt(i);
|
||||
switch (ch) {
|
||||
case '\'':
|
||||
result.push('\\\'');
|
||||
break;
|
||||
case '"':
|
||||
result.push('\\"');
|
||||
break;
|
||||
case '\\':
|
||||
result.push('\\\\');
|
||||
break;
|
||||
case '\n':
|
||||
result.push('\\n');
|
||||
break;
|
||||
case '\r':
|
||||
result.push('\\r');
|
||||
break;
|
||||
case '\t':
|
||||
result.push('\\t');
|
||||
break;
|
||||
case '\b':
|
||||
result.push('\\b');
|
||||
break;
|
||||
case '\f':
|
||||
result.push('\\f');
|
||||
break;
|
||||
default:
|
||||
result.push(ch);
|
||||
}
|
||||
}
|
||||
return result.join('');
|
||||
}
|
||||
function processCoreBundleFormat(fileHeader, json, emitter) {
|
||||
var keysSection = json.keys;
|
||||
var messageSection = json.messages;
|
||||
var bundleSection = json.bundles;
|
||||
var statistics = Object.create(null);
|
||||
var total = 0;
|
||||
var defaultMessages = Object.create(null);
|
||||
var modules = Object.keys(keysSection);
|
||||
modules.forEach(function (module) {
|
||||
var keys = keysSection[module];
|
||||
var messages = messageSection[module];
|
||||
if (!messages || keys.length !== messages.length) {
|
||||
emitter.emit('error', "Message for module " + module + " corrupted. Mismatch in number of keys and messages.");
|
||||
return;
|
||||
}
|
||||
var messageMap = Object.create(null);
|
||||
defaultMessages[module] = messageMap;
|
||||
keys.map(function (key, i) {
|
||||
total++;
|
||||
if (Is.string(key)) {
|
||||
messageMap[key] = messages[i];
|
||||
}
|
||||
else {
|
||||
messageMap[key.key] = messages[i];
|
||||
}
|
||||
});
|
||||
});
|
||||
var languageDirectory = path.join(__dirname, '..', '..', 'i18n');
|
||||
var languages = sortLanguages(fs.readdirSync(languageDirectory).filter(function (item) { return fs.statSync(path.join(languageDirectory, item)).isDirectory(); }));
|
||||
languages.forEach(function (language) {
|
||||
if (!language.iso639_2) {
|
||||
return;
|
||||
}
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log("Generating nls bundles for: " + language.iso639_2);
|
||||
}
|
||||
statistics[language.iso639_2] = 0;
|
||||
var localizedModules = Object.create(null);
|
||||
var cwd = path.join(languageDirectory, language.name, 'src');
|
||||
modules.forEach(function (module) {
|
||||
var order = keysSection[module];
|
||||
var i18nFile = path.join(cwd, module) + '.i18n.json';
|
||||
var messages = null;
|
||||
if (fs.existsSync(i18nFile)) {
|
||||
var content = stripComments(fs.readFileSync(i18nFile, 'utf8'));
|
||||
messages = JSON.parse(content);
|
||||
}
|
||||
else {
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log("No localized messages found for module " + module + ". Using default messages.");
|
||||
}
|
||||
messages = defaultMessages[module];
|
||||
statistics[language.iso639_2] = statistics[language.iso639_2] + Object.keys(messages).length;
|
||||
}
|
||||
var localizedMessages = [];
|
||||
order.forEach(function (keyInfo) {
|
||||
var key = null;
|
||||
if (Is.string(keyInfo)) {
|
||||
key = keyInfo;
|
||||
}
|
||||
else {
|
||||
key = keyInfo.key;
|
||||
}
|
||||
var message = messages[key];
|
||||
if (!message) {
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log("No localized message found for key " + key + " in module " + module + ". Using default message.");
|
||||
}
|
||||
message = defaultMessages[module][key];
|
||||
statistics[language.iso639_2] = statistics[language.iso639_2] + 1;
|
||||
}
|
||||
localizedMessages.push(message);
|
||||
});
|
||||
localizedModules[module] = localizedMessages;
|
||||
});
|
||||
Object.keys(bundleSection).forEach(function (bundle) {
|
||||
var modules = bundleSection[bundle];
|
||||
var contents = [
|
||||
fileHeader,
|
||||
"define(\"" + bundle + ".nls." + language.iso639_2 + "\", {"
|
||||
];
|
||||
modules.forEach(function (module, index) {
|
||||
contents.push("\t\"" + module + "\": [");
|
||||
var messages = localizedModules[module];
|
||||
if (!messages) {
|
||||
emitter.emit('error', "Didn't find messages for module " + module + ".");
|
||||
return;
|
||||
}
|
||||
messages.forEach(function (message, index) {
|
||||
contents.push("\t\t\"" + escapeCharacters(message) + (index < messages.length ? '",' : '"'));
|
||||
});
|
||||
contents.push(index < modules.length - 1 ? '\t],' : '\t]');
|
||||
});
|
||||
contents.push('});');
|
||||
emitter.emit('data', new File({ path: bundle + '.nls.' + language.iso639_2 + '.js', contents: new Buffer(contents.join('\n'), 'utf-8') }));
|
||||
});
|
||||
});
|
||||
Object.keys(statistics).forEach(function (key) {
|
||||
var value = statistics[key];
|
||||
log(key + " has " + value + " untranslated strings.");
|
||||
});
|
||||
vscodeLanguages.forEach(function (language) {
|
||||
var iso639_2 = iso639_3_to_2[language];
|
||||
if (!iso639_2) {
|
||||
log("\tCouldn't find iso639 2 mapping for language " + language + ". Using default language instead.");
|
||||
}
|
||||
else {
|
||||
var stats = statistics[iso639_2];
|
||||
if (Is.undef(stats)) {
|
||||
log("\tNo translations found for language " + language + ". Using default language instead.");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
function processNlsFiles(opts) {
|
||||
return event_stream_1.through(function (file) {
|
||||
var fileName = path.basename(file.path);
|
||||
if (fileName === 'nls.metadata.json') {
|
||||
var json = null;
|
||||
if (file.isBuffer()) {
|
||||
json = JSON.parse(file.contents.toString('utf8'));
|
||||
}
|
||||
else {
|
||||
this.emit('error', "Failed to read component file: " + file.relative);
|
||||
}
|
||||
if (BundledFormat.is(json)) {
|
||||
processCoreBundleFormat(opts.fileHeader, json, this);
|
||||
}
|
||||
}
|
||||
this.emit('data', file);
|
||||
});
|
||||
}
|
||||
exports.processNlsFiles = processNlsFiles;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var event_stream_1 = require("event-stream");
|
||||
var File = require("vinyl");
|
||||
var Is = require("is");
|
||||
var util = require('gulp-util');
|
||||
function log(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log.apply(util, [util.colors.green('[i18n]'), message].concat(rest));
|
||||
}
|
||||
var LocalizeInfo;
|
||||
(function (LocalizeInfo) {
|
||||
function is(value) {
|
||||
var candidate = value;
|
||||
return Is.defined(candidate) && Is.string(candidate.key) && (Is.undef(candidate.comment) || (Is.array(candidate.comment) && candidate.comment.every(function (element) { return Is.string(element); })));
|
||||
}
|
||||
LocalizeInfo.is = is;
|
||||
})(LocalizeInfo || (LocalizeInfo = {}));
|
||||
var BundledFormat;
|
||||
(function (BundledFormat) {
|
||||
function is(value) {
|
||||
if (Is.undef(value)) {
|
||||
return false;
|
||||
}
|
||||
var candidate = value;
|
||||
var length = Object.keys(value).length;
|
||||
return length === 3 && Is.defined(candidate.keys) && Is.defined(candidate.messages) && Is.defined(candidate.bundles);
|
||||
}
|
||||
BundledFormat.is = is;
|
||||
})(BundledFormat || (BundledFormat = {}));
|
||||
var vscodeLanguages = [
|
||||
'chs',
|
||||
'cht',
|
||||
'jpn',
|
||||
'kor',
|
||||
'deu',
|
||||
'fra',
|
||||
'esn',
|
||||
'rus',
|
||||
'ita'
|
||||
];
|
||||
var iso639_3_to_2 = {
|
||||
'chs': 'zh-cn',
|
||||
'cht': 'zh-tw',
|
||||
'csy': 'cs-cz',
|
||||
'deu': 'de',
|
||||
'enu': 'en',
|
||||
'esn': 'es',
|
||||
'fra': 'fr',
|
||||
'hun': 'hu',
|
||||
'ita': 'it',
|
||||
'jpn': 'ja',
|
||||
'kor': 'ko',
|
||||
'nld': 'nl',
|
||||
'plk': 'pl',
|
||||
'ptb': 'pt-br',
|
||||
'ptg': 'pt',
|
||||
'rus': 'ru',
|
||||
'sve': 'sv-se',
|
||||
'trk': 'tr'
|
||||
};
|
||||
function sortLanguages(directoryNames) {
|
||||
return directoryNames.map(function (dirName) {
|
||||
var lower = dirName.toLowerCase();
|
||||
return {
|
||||
name: lower,
|
||||
iso639_2: iso639_3_to_2[lower]
|
||||
};
|
||||
}).sort(function (a, b) {
|
||||
if (!a.iso639_2 && !b.iso639_2) {
|
||||
return 0;
|
||||
}
|
||||
if (!a.iso639_2) {
|
||||
return -1;
|
||||
}
|
||||
if (!b.iso639_2) {
|
||||
return 1;
|
||||
}
|
||||
return a.iso639_2 < b.iso639_2 ? -1 : (a.iso639_2 > b.iso639_2 ? 1 : 0);
|
||||
});
|
||||
}
|
||||
function stripComments(content) {
|
||||
/**
|
||||
* First capturing group matches double quoted string
|
||||
* Second matches single quotes string
|
||||
* Third matches block comments
|
||||
* Fourth matches line comments
|
||||
*/
|
||||
var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
|
||||
var result = content.replace(regexp, function (match, m1, m2, m3, m4) {
|
||||
// Only one of m1, m2, m3, m4 matches
|
||||
if (m3) {
|
||||
// A block comment. Replace with nothing
|
||||
return '';
|
||||
}
|
||||
else if (m4) {
|
||||
// A line comment. If it ends in \r?\n then keep it.
|
||||
var length_1 = m4.length;
|
||||
if (length_1 > 2 && m4[length_1 - 1] === '\n') {
|
||||
return m4[length_1 - 2] === '\r' ? '\r\n' : '\n';
|
||||
}
|
||||
else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
else {
|
||||
// We match a string
|
||||
return match;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
;
|
||||
function escapeCharacters(value) {
|
||||
var result = [];
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
var ch = value.charAt(i);
|
||||
switch (ch) {
|
||||
case '\'':
|
||||
result.push('\\\'');
|
||||
break;
|
||||
case '"':
|
||||
result.push('\\"');
|
||||
break;
|
||||
case '\\':
|
||||
result.push('\\\\');
|
||||
break;
|
||||
case '\n':
|
||||
result.push('\\n');
|
||||
break;
|
||||
case '\r':
|
||||
result.push('\\r');
|
||||
break;
|
||||
case '\t':
|
||||
result.push('\\t');
|
||||
break;
|
||||
case '\b':
|
||||
result.push('\\b');
|
||||
break;
|
||||
case '\f':
|
||||
result.push('\\f');
|
||||
break;
|
||||
default:
|
||||
result.push(ch);
|
||||
}
|
||||
}
|
||||
return result.join('');
|
||||
}
|
||||
function processCoreBundleFormat(fileHeader, json, emitter) {
|
||||
var keysSection = json.keys;
|
||||
var messageSection = json.messages;
|
||||
var bundleSection = json.bundles;
|
||||
var statistics = Object.create(null);
|
||||
var total = 0;
|
||||
var defaultMessages = Object.create(null);
|
||||
var modules = Object.keys(keysSection);
|
||||
modules.forEach(function (module) {
|
||||
var keys = keysSection[module];
|
||||
var messages = messageSection[module];
|
||||
if (!messages || keys.length !== messages.length) {
|
||||
emitter.emit('error', "Message for module " + module + " corrupted. Mismatch in number of keys and messages.");
|
||||
return;
|
||||
}
|
||||
var messageMap = Object.create(null);
|
||||
defaultMessages[module] = messageMap;
|
||||
keys.map(function (key, i) {
|
||||
total++;
|
||||
if (Is.string(key)) {
|
||||
messageMap[key] = messages[i];
|
||||
}
|
||||
else {
|
||||
messageMap[key.key] = messages[i];
|
||||
}
|
||||
});
|
||||
});
|
||||
var languageDirectory = path.join(__dirname, '..', '..', 'i18n');
|
||||
var languages = sortLanguages(fs.readdirSync(languageDirectory).filter(function (item) { return fs.statSync(path.join(languageDirectory, item)).isDirectory(); }));
|
||||
languages.forEach(function (language) {
|
||||
if (!language.iso639_2) {
|
||||
return;
|
||||
}
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log("Generating nls bundles for: " + language.iso639_2);
|
||||
}
|
||||
statistics[language.iso639_2] = 0;
|
||||
var localizedModules = Object.create(null);
|
||||
var cwd = path.join(languageDirectory, language.name, 'src');
|
||||
modules.forEach(function (module) {
|
||||
var order = keysSection[module];
|
||||
var i18nFile = path.join(cwd, module) + '.i18n.json';
|
||||
var messages = null;
|
||||
if (fs.existsSync(i18nFile)) {
|
||||
var content = stripComments(fs.readFileSync(i18nFile, 'utf8'));
|
||||
messages = JSON.parse(content);
|
||||
}
|
||||
else {
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log("No localized messages found for module " + module + ". Using default messages.");
|
||||
}
|
||||
messages = defaultMessages[module];
|
||||
statistics[language.iso639_2] = statistics[language.iso639_2] + Object.keys(messages).length;
|
||||
}
|
||||
var localizedMessages = [];
|
||||
order.forEach(function (keyInfo) {
|
||||
var key = null;
|
||||
if (Is.string(keyInfo)) {
|
||||
key = keyInfo;
|
||||
}
|
||||
else {
|
||||
key = keyInfo.key;
|
||||
}
|
||||
var message = messages[key];
|
||||
if (!message) {
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log("No localized message found for key " + key + " in module " + module + ". Using default message.");
|
||||
}
|
||||
message = defaultMessages[module][key];
|
||||
statistics[language.iso639_2] = statistics[language.iso639_2] + 1;
|
||||
}
|
||||
localizedMessages.push(message);
|
||||
});
|
||||
localizedModules[module] = localizedMessages;
|
||||
});
|
||||
Object.keys(bundleSection).forEach(function (bundle) {
|
||||
var modules = bundleSection[bundle];
|
||||
var contents = [
|
||||
fileHeader,
|
||||
"define(\"" + bundle + ".nls." + language.iso639_2 + "\", {"
|
||||
];
|
||||
modules.forEach(function (module, index) {
|
||||
contents.push("\t\"" + module + "\": [");
|
||||
var messages = localizedModules[module];
|
||||
if (!messages) {
|
||||
emitter.emit('error', "Didn't find messages for module " + module + ".");
|
||||
return;
|
||||
}
|
||||
messages.forEach(function (message, index) {
|
||||
contents.push("\t\t\"" + escapeCharacters(message) + (index < messages.length ? '",' : '"'));
|
||||
});
|
||||
contents.push(index < modules.length - 1 ? '\t],' : '\t]');
|
||||
});
|
||||
contents.push('});');
|
||||
emitter.emit('data', new File({ path: bundle + '.nls.' + language.iso639_2 + '.js', contents: new Buffer(contents.join('\n'), 'utf-8') }));
|
||||
});
|
||||
});
|
||||
Object.keys(statistics).forEach(function (key) {
|
||||
var value = statistics[key];
|
||||
log(key + " has " + value + " untranslated strings.");
|
||||
});
|
||||
vscodeLanguages.forEach(function (language) {
|
||||
var iso639_2 = iso639_3_to_2[language];
|
||||
if (!iso639_2) {
|
||||
log("\tCouldn't find iso639 2 mapping for language " + language + ". Using default language instead.");
|
||||
}
|
||||
else {
|
||||
var stats = statistics[iso639_2];
|
||||
if (Is.undef(stats)) {
|
||||
log("\tNo translations found for language " + language + ". Using default language instead.");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
function processNlsFiles(opts) {
|
||||
return event_stream_1.through(function (file) {
|
||||
var fileName = path.basename(file.path);
|
||||
if (fileName === 'nls.metadata.json') {
|
||||
var json = null;
|
||||
if (file.isBuffer()) {
|
||||
json = JSON.parse(file.contents.toString('utf8'));
|
||||
}
|
||||
else {
|
||||
this.emit('error', "Failed to read component file: " + file.relative);
|
||||
}
|
||||
if (BundledFormat.is(json)) {
|
||||
processCoreBundleFormat(opts.fileHeader, json, this);
|
||||
}
|
||||
}
|
||||
this.emit('data', file);
|
||||
});
|
||||
}
|
||||
exports.processNlsFiles = processNlsFiles;
|
||||
|
|
710
build/lib/nls.js
710
build/lib/nls.js
|
@ -1,355 +1,355 @@
|
|||
"use strict";
|
||||
var ts = require("./typescript/typescriptServices");
|
||||
var lazy = require("lazy.js");
|
||||
var event_stream_1 = require("event-stream");
|
||||
var File = require("vinyl");
|
||||
var sm = require("source-map");
|
||||
var assign = require("object-assign");
|
||||
var path = require("path");
|
||||
var CollectStepResult;
|
||||
(function (CollectStepResult) {
|
||||
CollectStepResult[CollectStepResult["Yes"] = 0] = "Yes";
|
||||
CollectStepResult[CollectStepResult["YesAndRecurse"] = 1] = "YesAndRecurse";
|
||||
CollectStepResult[CollectStepResult["No"] = 2] = "No";
|
||||
CollectStepResult[CollectStepResult["NoAndRecurse"] = 3] = "NoAndRecurse";
|
||||
})(CollectStepResult || (CollectStepResult = {}));
|
||||
function collect(node, fn) {
|
||||
var result = [];
|
||||
function loop(node) {
|
||||
var stepResult = fn(node);
|
||||
if (stepResult === CollectStepResult.Yes || stepResult === CollectStepResult.YesAndRecurse) {
|
||||
result.push(node);
|
||||
}
|
||||
if (stepResult === CollectStepResult.YesAndRecurse || stepResult === CollectStepResult.NoAndRecurse) {
|
||||
ts.forEachChild(node, loop);
|
||||
}
|
||||
}
|
||||
loop(node);
|
||||
return result;
|
||||
}
|
||||
function clone(object) {
|
||||
var result = {};
|
||||
for (var id in object) {
|
||||
result[id] = object[id];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function template(lines) {
|
||||
var indent = '', wrap = '';
|
||||
if (lines.length > 1) {
|
||||
indent = '\t';
|
||||
wrap = '\n';
|
||||
}
|
||||
return "/*---------------------------------------------------------\n * Copyright (C) Microsoft Corporation. All rights reserved.\n *--------------------------------------------------------*/\ndefine([], [" + (wrap + lines.map(function (l) { return indent + l; }).join(',\n') + wrap) + "]);";
|
||||
}
|
||||
/**
|
||||
* Returns a stream containing the patched JavaScript and source maps.
|
||||
*/
|
||||
function nls() {
|
||||
var input = event_stream_1.through();
|
||||
var output = input.pipe(event_stream_1.through(function (f) {
|
||||
var _this = this;
|
||||
if (!f.sourceMap) {
|
||||
return this.emit('error', new Error("File " + f.relative + " does not have sourcemaps."));
|
||||
}
|
||||
var source = f.sourceMap.sources[0];
|
||||
if (!source) {
|
||||
return this.emit('error', new Error("File " + f.relative + " does not have a source in the source map."));
|
||||
}
|
||||
var root = f.sourceMap.sourceRoot;
|
||||
if (root) {
|
||||
source = path.join(root, source);
|
||||
}
|
||||
var typescript = f.sourceMap.sourcesContent[0];
|
||||
if (!typescript) {
|
||||
return this.emit('error', new Error("File " + f.relative + " does not have the original content in the source map."));
|
||||
}
|
||||
nls.patchFiles(f, typescript).forEach(function (f) { return _this.emit('data', f); });
|
||||
}));
|
||||
return event_stream_1.duplex(input, output);
|
||||
}
|
||||
function isImportNode(node) {
|
||||
return node.kind === 212 /* ImportDeclaration */ || node.kind === 211 /* ImportEqualsDeclaration */;
|
||||
}
|
||||
(function (nls_1) {
|
||||
function fileFrom(file, contents, path) {
|
||||
if (path === void 0) { path = file.path; }
|
||||
return new File({
|
||||
contents: new Buffer(contents),
|
||||
base: file.base,
|
||||
cwd: file.cwd,
|
||||
path: path
|
||||
});
|
||||
}
|
||||
nls_1.fileFrom = fileFrom;
|
||||
function mappedPositionFrom(source, lc) {
|
||||
return { source: source, line: lc.line + 1, column: lc.character };
|
||||
}
|
||||
nls_1.mappedPositionFrom = mappedPositionFrom;
|
||||
function lcFrom(position) {
|
||||
return { line: position.line - 1, character: position.column };
|
||||
}
|
||||
nls_1.lcFrom = lcFrom;
|
||||
var SingleFileServiceHost = (function () {
|
||||
function SingleFileServiceHost(options, filename, contents) {
|
||||
var _this = this;
|
||||
this.options = options;
|
||||
this.filename = filename;
|
||||
this.getCompilationSettings = function () { return _this.options; };
|
||||
this.getScriptFileNames = function () { return [_this.filename]; };
|
||||
this.getScriptVersion = function () { return '1'; };
|
||||
this.getScriptSnapshot = function (name) { return name === _this.filename ? _this.file : _this.lib; };
|
||||
this.getCurrentDirectory = function () { return ''; };
|
||||
this.getDefaultLibFileName = function () { return 'lib.d.ts'; };
|
||||
this.file = ts.ScriptSnapshot.fromString(contents);
|
||||
this.lib = ts.ScriptSnapshot.fromString('');
|
||||
}
|
||||
return SingleFileServiceHost;
|
||||
}());
|
||||
nls_1.SingleFileServiceHost = SingleFileServiceHost;
|
||||
function isCallExpressionWithinTextSpanCollectStep(textSpan, node) {
|
||||
if (!ts.textSpanContainsTextSpan({ start: node.pos, length: node.end - node.pos }, textSpan)) {
|
||||
return CollectStepResult.No;
|
||||
}
|
||||
return node.kind === 160 /* CallExpression */ ? CollectStepResult.YesAndRecurse : CollectStepResult.NoAndRecurse;
|
||||
}
|
||||
function analyze(contents, options) {
|
||||
if (options === void 0) { options = {}; }
|
||||
var filename = 'file.ts';
|
||||
var serviceHost = new SingleFileServiceHost(assign(clone(options), { noResolve: true }), filename, contents);
|
||||
var service = ts.createLanguageService(serviceHost);
|
||||
var sourceFile = service.getSourceFile(filename);
|
||||
// all imports
|
||||
var imports = lazy(collect(sourceFile, function (n) { return isImportNode(n) ? CollectStepResult.YesAndRecurse : CollectStepResult.NoAndRecurse; }));
|
||||
// import nls = require('vs/nls');
|
||||
var importEqualsDeclarations = imports
|
||||
.filter(function (n) { return n.kind === 211 /* ImportEqualsDeclaration */; })
|
||||
.map(function (n) { return n; })
|
||||
.filter(function (d) { return d.moduleReference.kind === 222 /* ExternalModuleReference */; })
|
||||
.filter(function (d) { return d.moduleReference.expression.getText() === '\'vs/nls\''; });
|
||||
// import ... from 'vs/nls';
|
||||
var importDeclarations = imports
|
||||
.filter(function (n) { return n.kind === 212 /* ImportDeclaration */; })
|
||||
.map(function (n) { return n; })
|
||||
.filter(function (d) { return d.moduleSpecifier.kind === 8 /* StringLiteral */; })
|
||||
.filter(function (d) { return d.moduleSpecifier.getText() === '\'vs/nls\''; })
|
||||
.filter(function (d) { return !!d.importClause && !!d.importClause.namedBindings; });
|
||||
var nlsExpressions = importEqualsDeclarations
|
||||
.map(function (d) { return d.moduleReference.expression; })
|
||||
.concat(importDeclarations.map(function (d) { return d.moduleSpecifier; }))
|
||||
.map(function (d) { return ({
|
||||
start: ts.getLineAndCharacterOfPosition(sourceFile, d.getStart()),
|
||||
end: ts.getLineAndCharacterOfPosition(sourceFile, d.getEnd())
|
||||
}); });
|
||||
// `nls.localize(...)` calls
|
||||
var nlsLocalizeCallExpressions = importDeclarations
|
||||
.filter(function (d) { return d.importClause.namedBindings.kind === 214 /* NamespaceImport */; })
|
||||
.map(function (d) { return d.importClause.namedBindings.name; })
|
||||
.concat(importEqualsDeclarations.map(function (d) { return d.name; }))
|
||||
.map(function (n) { return service.getReferencesAtPosition(filename, n.pos + 1); })
|
||||
.flatten()
|
||||
.filter(function (r) { return !r.isWriteAccess; })
|
||||
.map(function (r) { return collect(sourceFile, function (n) { return isCallExpressionWithinTextSpanCollectStep(r.textSpan, n); }); })
|
||||
.map(function (a) { return lazy(a).last(); })
|
||||
.filter(function (n) { return !!n; })
|
||||
.map(function (n) { return n; })
|
||||
.filter(function (n) { return n.expression.kind === 158 /* PropertyAccessExpression */ && n.expression.name.getText() === 'localize'; });
|
||||
// `localize` named imports
|
||||
var allLocalizeImportDeclarations = importDeclarations
|
||||
.filter(function (d) { return d.importClause.namedBindings.kind === 215 /* NamedImports */; })
|
||||
.map(function (d) { return d.importClause.namedBindings.elements; })
|
||||
.flatten();
|
||||
// `localize` read-only references
|
||||
var localizeReferences = allLocalizeImportDeclarations
|
||||
.filter(function (d) { return d.name.getText() === 'localize'; })
|
||||
.map(function (n) { return service.getReferencesAtPosition(filename, n.pos + 1); })
|
||||
.flatten()
|
||||
.filter(function (r) { return !r.isWriteAccess; });
|
||||
// custom named `localize` read-only references
|
||||
var namedLocalizeReferences = allLocalizeImportDeclarations
|
||||
.filter(function (d) { return d.propertyName && d.propertyName.getText() === 'localize'; })
|
||||
.map(function (n) { return service.getReferencesAtPosition(filename, n.name.pos + 1); })
|
||||
.flatten()
|
||||
.filter(function (r) { return !r.isWriteAccess; });
|
||||
// find the deepest call expressions AST nodes that contain those references
|
||||
var localizeCallExpressions = localizeReferences
|
||||
.concat(namedLocalizeReferences)
|
||||
.map(function (r) { return collect(sourceFile, function (n) { return isCallExpressionWithinTextSpanCollectStep(r.textSpan, n); }); })
|
||||
.map(function (a) { return lazy(a).last(); })
|
||||
.filter(function (n) { return !!n; })
|
||||
.map(function (n) { return n; });
|
||||
// collect everything
|
||||
var localizeCalls = nlsLocalizeCallExpressions
|
||||
.concat(localizeCallExpressions)
|
||||
.map(function (e) { return e.arguments; })
|
||||
.filter(function (a) { return a.length > 1; })
|
||||
.sort(function (a, b) { return a[0].getStart() - b[0].getStart(); })
|
||||
.map(function (a) { return ({
|
||||
keySpan: { start: ts.getLineAndCharacterOfPosition(sourceFile, a[0].getStart()), end: ts.getLineAndCharacterOfPosition(sourceFile, a[0].getEnd()) },
|
||||
key: a[0].getText(),
|
||||
valueSpan: { start: ts.getLineAndCharacterOfPosition(sourceFile, a[1].getStart()), end: ts.getLineAndCharacterOfPosition(sourceFile, a[1].getEnd()) },
|
||||
value: a[1].getText()
|
||||
}); });
|
||||
return {
|
||||
localizeCalls: localizeCalls.toArray(),
|
||||
nlsExpressions: nlsExpressions.toArray()
|
||||
};
|
||||
}
|
||||
nls_1.analyze = analyze;
|
||||
var TextModel = (function () {
|
||||
function TextModel(contents) {
|
||||
var regex = /\r\n|\r|\n/g;
|
||||
var index = 0;
|
||||
var match;
|
||||
this.lines = [];
|
||||
this.lineEndings = [];
|
||||
while (match = regex.exec(contents)) {
|
||||
this.lines.push(contents.substring(index, match.index));
|
||||
this.lineEndings.push(match[0]);
|
||||
index = regex.lastIndex;
|
||||
}
|
||||
if (contents.length > 0) {
|
||||
this.lines.push(contents.substring(index, contents.length));
|
||||
this.lineEndings.push('');
|
||||
}
|
||||
}
|
||||
TextModel.prototype.get = function (index) {
|
||||
return this.lines[index];
|
||||
};
|
||||
TextModel.prototype.set = function (index, line) {
|
||||
this.lines[index] = line;
|
||||
};
|
||||
Object.defineProperty(TextModel.prototype, "lineCount", {
|
||||
get: function () {
|
||||
return this.lines.length;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
/**
|
||||
* Applies patch(es) to the model.
|
||||
* Multiple patches must be ordered.
|
||||
* Does not support patches spanning multiple lines.
|
||||
*/
|
||||
TextModel.prototype.apply = function (patch) {
|
||||
var startLineNumber = patch.span.start.line;
|
||||
var endLineNumber = patch.span.end.line;
|
||||
var startLine = this.lines[startLineNumber] || '';
|
||||
var endLine = this.lines[endLineNumber] || '';
|
||||
this.lines[startLineNumber] = [
|
||||
startLine.substring(0, patch.span.start.character),
|
||||
patch.content,
|
||||
endLine.substring(patch.span.end.character)
|
||||
].join('');
|
||||
for (var i = startLineNumber + 1; i <= endLineNumber; i++) {
|
||||
this.lines[i] = '';
|
||||
}
|
||||
};
|
||||
TextModel.prototype.toString = function () {
|
||||
return lazy(this.lines).zip(this.lineEndings)
|
||||
.flatten().toArray().join('');
|
||||
};
|
||||
return TextModel;
|
||||
}());
|
||||
nls_1.TextModel = TextModel;
|
||||
function patchJavascript(patches, contents, moduleId) {
|
||||
var model = new nls.TextModel(contents);
|
||||
// patch the localize calls
|
||||
lazy(patches).reverse().each(function (p) { return model.apply(p); });
|
||||
// patch the 'vs/nls' imports
|
||||
var firstLine = model.get(0);
|
||||
var patchedFirstLine = firstLine.replace(/(['"])vs\/nls\1/g, "$1vs/nls!" + moduleId + "$1");
|
||||
model.set(0, patchedFirstLine);
|
||||
return model.toString();
|
||||
}
|
||||
nls_1.patchJavascript = patchJavascript;
|
||||
function patchSourcemap(patches, rsm, smc) {
|
||||
var smg = new sm.SourceMapGenerator({
|
||||
file: rsm.file,
|
||||
sourceRoot: rsm.sourceRoot
|
||||
});
|
||||
patches = patches.reverse();
|
||||
var currentLine = -1;
|
||||
var currentLineDiff = 0;
|
||||
var source = null;
|
||||
smc.eachMapping(function (m) {
|
||||
var patch = patches[patches.length - 1];
|
||||
var original = { line: m.originalLine, column: m.originalColumn };
|
||||
var generated = { line: m.generatedLine, column: m.generatedColumn };
|
||||
if (currentLine !== generated.line) {
|
||||
currentLineDiff = 0;
|
||||
}
|
||||
currentLine = generated.line;
|
||||
generated.column += currentLineDiff;
|
||||
if (patch && m.generatedLine - 1 === patch.span.end.line && m.generatedColumn === patch.span.end.character) {
|
||||
var originalLength = patch.span.end.character - patch.span.start.character;
|
||||
var modifiedLength = patch.content.length;
|
||||
var lengthDiff = modifiedLength - originalLength;
|
||||
currentLineDiff += lengthDiff;
|
||||
generated.column += lengthDiff;
|
||||
patches.pop();
|
||||
}
|
||||
source = rsm.sourceRoot ? path.relative(rsm.sourceRoot, m.source) : m.source;
|
||||
source = source.replace(/\\/g, '/');
|
||||
smg.addMapping({ source: source, name: m.name, original: original, generated: generated });
|
||||
}, null, sm.SourceMapConsumer.GENERATED_ORDER);
|
||||
if (source) {
|
||||
smg.setSourceContent(source, smc.sourceContentFor(source));
|
||||
}
|
||||
return JSON.parse(smg.toString());
|
||||
}
|
||||
nls_1.patchSourcemap = patchSourcemap;
|
||||
function patch(moduleId, typescript, javascript, sourcemap) {
|
||||
var _a = analyze(typescript), localizeCalls = _a.localizeCalls, nlsExpressions = _a.nlsExpressions;
|
||||
if (localizeCalls.length === 0) {
|
||||
return { javascript: javascript, sourcemap: sourcemap };
|
||||
}
|
||||
var nlsKeys = template(localizeCalls.map(function (lc) { return lc.key; }));
|
||||
var nls = template(localizeCalls.map(function (lc) { return lc.value; }));
|
||||
var smc = new sm.SourceMapConsumer(sourcemap);
|
||||
var positionFrom = mappedPositionFrom.bind(null, sourcemap.sources[0]);
|
||||
var i = 0;
|
||||
// build patches
|
||||
var patches = lazy(localizeCalls)
|
||||
.map(function (lc) { return ([
|
||||
{ range: lc.keySpan, content: '' + (i++) },
|
||||
{ range: lc.valueSpan, content: 'null' }
|
||||
]); })
|
||||
.flatten()
|
||||
.map(function (c) {
|
||||
var start = lcFrom(smc.generatedPositionFor(positionFrom(c.range.start)));
|
||||
var end = lcFrom(smc.generatedPositionFor(positionFrom(c.range.end)));
|
||||
return { span: { start: start, end: end }, content: c.content };
|
||||
})
|
||||
.toArray();
|
||||
javascript = patchJavascript(patches, javascript, moduleId);
|
||||
// since imports are not within the sourcemap information,
|
||||
// we must do this MacGyver style
|
||||
if (nlsExpressions.length) {
|
||||
javascript = javascript.replace(/^define\(.*$/m, function (line) {
|
||||
return line.replace(/(['"])vs\/nls\1/g, "$1vs/nls!" + moduleId + "$1");
|
||||
});
|
||||
}
|
||||
sourcemap = patchSourcemap(patches, sourcemap, smc);
|
||||
return { javascript: javascript, sourcemap: sourcemap, nlsKeys: nlsKeys, nls: nls };
|
||||
}
|
||||
nls_1.patch = patch;
|
||||
function patchFiles(javascriptFile, typescript) {
|
||||
// hack?
|
||||
var moduleId = javascriptFile.relative
|
||||
.replace(/\.js$/, '')
|
||||
.replace(/\\/g, '/');
|
||||
var _a = patch(moduleId, typescript, javascriptFile.contents.toString(), javascriptFile.sourceMap), javascript = _a.javascript, sourcemap = _a.sourcemap, nlsKeys = _a.nlsKeys, nls = _a.nls;
|
||||
var result = [fileFrom(javascriptFile, javascript)];
|
||||
result[0].sourceMap = sourcemap;
|
||||
if (nlsKeys) {
|
||||
result.push(fileFrom(javascriptFile, nlsKeys, javascriptFile.path.replace(/\.js$/, '.nls.keys.js')));
|
||||
}
|
||||
if (nls) {
|
||||
result.push(fileFrom(javascriptFile, nls, javascriptFile.path.replace(/\.js$/, '.nls.js')));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
nls_1.patchFiles = patchFiles;
|
||||
})(nls || (nls = {}));
|
||||
module.exports = nls;
|
||||
"use strict";
|
||||
var ts = require("./typescript/typescriptServices");
|
||||
var lazy = require("lazy.js");
|
||||
var event_stream_1 = require("event-stream");
|
||||
var File = require("vinyl");
|
||||
var sm = require("source-map");
|
||||
var assign = require("object-assign");
|
||||
var path = require("path");
|
||||
var CollectStepResult;
|
||||
(function (CollectStepResult) {
|
||||
CollectStepResult[CollectStepResult["Yes"] = 0] = "Yes";
|
||||
CollectStepResult[CollectStepResult["YesAndRecurse"] = 1] = "YesAndRecurse";
|
||||
CollectStepResult[CollectStepResult["No"] = 2] = "No";
|
||||
CollectStepResult[CollectStepResult["NoAndRecurse"] = 3] = "NoAndRecurse";
|
||||
})(CollectStepResult || (CollectStepResult = {}));
|
||||
function collect(node, fn) {
|
||||
var result = [];
|
||||
function loop(node) {
|
||||
var stepResult = fn(node);
|
||||
if (stepResult === CollectStepResult.Yes || stepResult === CollectStepResult.YesAndRecurse) {
|
||||
result.push(node);
|
||||
}
|
||||
if (stepResult === CollectStepResult.YesAndRecurse || stepResult === CollectStepResult.NoAndRecurse) {
|
||||
ts.forEachChild(node, loop);
|
||||
}
|
||||
}
|
||||
loop(node);
|
||||
return result;
|
||||
}
|
||||
function clone(object) {
|
||||
var result = {};
|
||||
for (var id in object) {
|
||||
result[id] = object[id];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function template(lines) {
|
||||
var indent = '', wrap = '';
|
||||
if (lines.length > 1) {
|
||||
indent = '\t';
|
||||
wrap = '\n';
|
||||
}
|
||||
return "/*---------------------------------------------------------\n * Copyright (C) Microsoft Corporation. All rights reserved.\n *--------------------------------------------------------*/\ndefine([], [" + (wrap + lines.map(function (l) { return indent + l; }).join(',\n') + wrap) + "]);";
|
||||
}
|
||||
/**
|
||||
* Returns a stream containing the patched JavaScript and source maps.
|
||||
*/
|
||||
function nls() {
|
||||
var input = event_stream_1.through();
|
||||
var output = input.pipe(event_stream_1.through(function (f) {
|
||||
var _this = this;
|
||||
if (!f.sourceMap) {
|
||||
return this.emit('error', new Error("File " + f.relative + " does not have sourcemaps."));
|
||||
}
|
||||
var source = f.sourceMap.sources[0];
|
||||
if (!source) {
|
||||
return this.emit('error', new Error("File " + f.relative + " does not have a source in the source map."));
|
||||
}
|
||||
var root = f.sourceMap.sourceRoot;
|
||||
if (root) {
|
||||
source = path.join(root, source);
|
||||
}
|
||||
var typescript = f.sourceMap.sourcesContent[0];
|
||||
if (!typescript) {
|
||||
return this.emit('error', new Error("File " + f.relative + " does not have the original content in the source map."));
|
||||
}
|
||||
nls.patchFiles(f, typescript).forEach(function (f) { return _this.emit('data', f); });
|
||||
}));
|
||||
return event_stream_1.duplex(input, output);
|
||||
}
|
||||
function isImportNode(node) {
|
||||
return node.kind === 212 /* ImportDeclaration */ || node.kind === 211 /* ImportEqualsDeclaration */;
|
||||
}
|
||||
(function (nls_1) {
|
||||
function fileFrom(file, contents, path) {
|
||||
if (path === void 0) { path = file.path; }
|
||||
return new File({
|
||||
contents: new Buffer(contents),
|
||||
base: file.base,
|
||||
cwd: file.cwd,
|
||||
path: path
|
||||
});
|
||||
}
|
||||
nls_1.fileFrom = fileFrom;
|
||||
function mappedPositionFrom(source, lc) {
|
||||
return { source: source, line: lc.line + 1, column: lc.character };
|
||||
}
|
||||
nls_1.mappedPositionFrom = mappedPositionFrom;
|
||||
function lcFrom(position) {
|
||||
return { line: position.line - 1, character: position.column };
|
||||
}
|
||||
nls_1.lcFrom = lcFrom;
|
||||
var SingleFileServiceHost = (function () {
|
||||
function SingleFileServiceHost(options, filename, contents) {
|
||||
var _this = this;
|
||||
this.options = options;
|
||||
this.filename = filename;
|
||||
this.getCompilationSettings = function () { return _this.options; };
|
||||
this.getScriptFileNames = function () { return [_this.filename]; };
|
||||
this.getScriptVersion = function () { return '1'; };
|
||||
this.getScriptSnapshot = function (name) { return name === _this.filename ? _this.file : _this.lib; };
|
||||
this.getCurrentDirectory = function () { return ''; };
|
||||
this.getDefaultLibFileName = function () { return 'lib.d.ts'; };
|
||||
this.file = ts.ScriptSnapshot.fromString(contents);
|
||||
this.lib = ts.ScriptSnapshot.fromString('');
|
||||
}
|
||||
return SingleFileServiceHost;
|
||||
}());
|
||||
nls_1.SingleFileServiceHost = SingleFileServiceHost;
|
||||
function isCallExpressionWithinTextSpanCollectStep(textSpan, node) {
|
||||
if (!ts.textSpanContainsTextSpan({ start: node.pos, length: node.end - node.pos }, textSpan)) {
|
||||
return CollectStepResult.No;
|
||||
}
|
||||
return node.kind === 160 /* CallExpression */ ? CollectStepResult.YesAndRecurse : CollectStepResult.NoAndRecurse;
|
||||
}
|
||||
function analyze(contents, options) {
|
||||
if (options === void 0) { options = {}; }
|
||||
var filename = 'file.ts';
|
||||
var serviceHost = new SingleFileServiceHost(assign(clone(options), { noResolve: true }), filename, contents);
|
||||
var service = ts.createLanguageService(serviceHost);
|
||||
var sourceFile = service.getSourceFile(filename);
|
||||
// all imports
|
||||
var imports = lazy(collect(sourceFile, function (n) { return isImportNode(n) ? CollectStepResult.YesAndRecurse : CollectStepResult.NoAndRecurse; }));
|
||||
// import nls = require('vs/nls');
|
||||
var importEqualsDeclarations = imports
|
||||
.filter(function (n) { return n.kind === 211 /* ImportEqualsDeclaration */; })
|
||||
.map(function (n) { return n; })
|
||||
.filter(function (d) { return d.moduleReference.kind === 222 /* ExternalModuleReference */; })
|
||||
.filter(function (d) { return d.moduleReference.expression.getText() === '\'vs/nls\''; });
|
||||
// import ... from 'vs/nls';
|
||||
var importDeclarations = imports
|
||||
.filter(function (n) { return n.kind === 212 /* ImportDeclaration */; })
|
||||
.map(function (n) { return n; })
|
||||
.filter(function (d) { return d.moduleSpecifier.kind === 8 /* StringLiteral */; })
|
||||
.filter(function (d) { return d.moduleSpecifier.getText() === '\'vs/nls\''; })
|
||||
.filter(function (d) { return !!d.importClause && !!d.importClause.namedBindings; });
|
||||
var nlsExpressions = importEqualsDeclarations
|
||||
.map(function (d) { return d.moduleReference.expression; })
|
||||
.concat(importDeclarations.map(function (d) { return d.moduleSpecifier; }))
|
||||
.map(function (d) { return ({
|
||||
start: ts.getLineAndCharacterOfPosition(sourceFile, d.getStart()),
|
||||
end: ts.getLineAndCharacterOfPosition(sourceFile, d.getEnd())
|
||||
}); });
|
||||
// `nls.localize(...)` calls
|
||||
var nlsLocalizeCallExpressions = importDeclarations
|
||||
.filter(function (d) { return d.importClause.namedBindings.kind === 214 /* NamespaceImport */; })
|
||||
.map(function (d) { return d.importClause.namedBindings.name; })
|
||||
.concat(importEqualsDeclarations.map(function (d) { return d.name; }))
|
||||
.map(function (n) { return service.getReferencesAtPosition(filename, n.pos + 1); })
|
||||
.flatten()
|
||||
.filter(function (r) { return !r.isWriteAccess; })
|
||||
.map(function (r) { return collect(sourceFile, function (n) { return isCallExpressionWithinTextSpanCollectStep(r.textSpan, n); }); })
|
||||
.map(function (a) { return lazy(a).last(); })
|
||||
.filter(function (n) { return !!n; })
|
||||
.map(function (n) { return n; })
|
||||
.filter(function (n) { return n.expression.kind === 158 /* PropertyAccessExpression */ && n.expression.name.getText() === 'localize'; });
|
||||
// `localize` named imports
|
||||
var allLocalizeImportDeclarations = importDeclarations
|
||||
.filter(function (d) { return d.importClause.namedBindings.kind === 215 /* NamedImports */; })
|
||||
.map(function (d) { return d.importClause.namedBindings.elements; })
|
||||
.flatten();
|
||||
// `localize` read-only references
|
||||
var localizeReferences = allLocalizeImportDeclarations
|
||||
.filter(function (d) { return d.name.getText() === 'localize'; })
|
||||
.map(function (n) { return service.getReferencesAtPosition(filename, n.pos + 1); })
|
||||
.flatten()
|
||||
.filter(function (r) { return !r.isWriteAccess; });
|
||||
// custom named `localize` read-only references
|
||||
var namedLocalizeReferences = allLocalizeImportDeclarations
|
||||
.filter(function (d) { return d.propertyName && d.propertyName.getText() === 'localize'; })
|
||||
.map(function (n) { return service.getReferencesAtPosition(filename, n.name.pos + 1); })
|
||||
.flatten()
|
||||
.filter(function (r) { return !r.isWriteAccess; });
|
||||
// find the deepest call expressions AST nodes that contain those references
|
||||
var localizeCallExpressions = localizeReferences
|
||||
.concat(namedLocalizeReferences)
|
||||
.map(function (r) { return collect(sourceFile, function (n) { return isCallExpressionWithinTextSpanCollectStep(r.textSpan, n); }); })
|
||||
.map(function (a) { return lazy(a).last(); })
|
||||
.filter(function (n) { return !!n; })
|
||||
.map(function (n) { return n; });
|
||||
// collect everything
|
||||
var localizeCalls = nlsLocalizeCallExpressions
|
||||
.concat(localizeCallExpressions)
|
||||
.map(function (e) { return e.arguments; })
|
||||
.filter(function (a) { return a.length > 1; })
|
||||
.sort(function (a, b) { return a[0].getStart() - b[0].getStart(); })
|
||||
.map(function (a) { return ({
|
||||
keySpan: { start: ts.getLineAndCharacterOfPosition(sourceFile, a[0].getStart()), end: ts.getLineAndCharacterOfPosition(sourceFile, a[0].getEnd()) },
|
||||
key: a[0].getText(),
|
||||
valueSpan: { start: ts.getLineAndCharacterOfPosition(sourceFile, a[1].getStart()), end: ts.getLineAndCharacterOfPosition(sourceFile, a[1].getEnd()) },
|
||||
value: a[1].getText()
|
||||
}); });
|
||||
return {
|
||||
localizeCalls: localizeCalls.toArray(),
|
||||
nlsExpressions: nlsExpressions.toArray()
|
||||
};
|
||||
}
|
||||
nls_1.analyze = analyze;
|
||||
var TextModel = (function () {
|
||||
function TextModel(contents) {
|
||||
var regex = /\r\n|\r|\n/g;
|
||||
var index = 0;
|
||||
var match;
|
||||
this.lines = [];
|
||||
this.lineEndings = [];
|
||||
while (match = regex.exec(contents)) {
|
||||
this.lines.push(contents.substring(index, match.index));
|
||||
this.lineEndings.push(match[0]);
|
||||
index = regex.lastIndex;
|
||||
}
|
||||
if (contents.length > 0) {
|
||||
this.lines.push(contents.substring(index, contents.length));
|
||||
this.lineEndings.push('');
|
||||
}
|
||||
}
|
||||
TextModel.prototype.get = function (index) {
|
||||
return this.lines[index];
|
||||
};
|
||||
TextModel.prototype.set = function (index, line) {
|
||||
this.lines[index] = line;
|
||||
};
|
||||
Object.defineProperty(TextModel.prototype, "lineCount", {
|
||||
get: function () {
|
||||
return this.lines.length;
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
/**
|
||||
* Applies patch(es) to the model.
|
||||
* Multiple patches must be ordered.
|
||||
* Does not support patches spanning multiple lines.
|
||||
*/
|
||||
TextModel.prototype.apply = function (patch) {
|
||||
var startLineNumber = patch.span.start.line;
|
||||
var endLineNumber = patch.span.end.line;
|
||||
var startLine = this.lines[startLineNumber] || '';
|
||||
var endLine = this.lines[endLineNumber] || '';
|
||||
this.lines[startLineNumber] = [
|
||||
startLine.substring(0, patch.span.start.character),
|
||||
patch.content,
|
||||
endLine.substring(patch.span.end.character)
|
||||
].join('');
|
||||
for (var i = startLineNumber + 1; i <= endLineNumber; i++) {
|
||||
this.lines[i] = '';
|
||||
}
|
||||
};
|
||||
TextModel.prototype.toString = function () {
|
||||
return lazy(this.lines).zip(this.lineEndings)
|
||||
.flatten().toArray().join('');
|
||||
};
|
||||
return TextModel;
|
||||
}());
|
||||
nls_1.TextModel = TextModel;
|
||||
function patchJavascript(patches, contents, moduleId) {
|
||||
var model = new nls.TextModel(contents);
|
||||
// patch the localize calls
|
||||
lazy(patches).reverse().each(function (p) { return model.apply(p); });
|
||||
// patch the 'vs/nls' imports
|
||||
var firstLine = model.get(0);
|
||||
var patchedFirstLine = firstLine.replace(/(['"])vs\/nls\1/g, "$1vs/nls!" + moduleId + "$1");
|
||||
model.set(0, patchedFirstLine);
|
||||
return model.toString();
|
||||
}
|
||||
nls_1.patchJavascript = patchJavascript;
|
||||
function patchSourcemap(patches, rsm, smc) {
|
||||
var smg = new sm.SourceMapGenerator({
|
||||
file: rsm.file,
|
||||
sourceRoot: rsm.sourceRoot
|
||||
});
|
||||
patches = patches.reverse();
|
||||
var currentLine = -1;
|
||||
var currentLineDiff = 0;
|
||||
var source = null;
|
||||
smc.eachMapping(function (m) {
|
||||
var patch = patches[patches.length - 1];
|
||||
var original = { line: m.originalLine, column: m.originalColumn };
|
||||
var generated = { line: m.generatedLine, column: m.generatedColumn };
|
||||
if (currentLine !== generated.line) {
|
||||
currentLineDiff = 0;
|
||||
}
|
||||
currentLine = generated.line;
|
||||
generated.column += currentLineDiff;
|
||||
if (patch && m.generatedLine - 1 === patch.span.end.line && m.generatedColumn === patch.span.end.character) {
|
||||
var originalLength = patch.span.end.character - patch.span.start.character;
|
||||
var modifiedLength = patch.content.length;
|
||||
var lengthDiff = modifiedLength - originalLength;
|
||||
currentLineDiff += lengthDiff;
|
||||
generated.column += lengthDiff;
|
||||
patches.pop();
|
||||
}
|
||||
source = rsm.sourceRoot ? path.relative(rsm.sourceRoot, m.source) : m.source;
|
||||
source = source.replace(/\\/g, '/');
|
||||
smg.addMapping({ source: source, name: m.name, original: original, generated: generated });
|
||||
}, null, sm.SourceMapConsumer.GENERATED_ORDER);
|
||||
if (source) {
|
||||
smg.setSourceContent(source, smc.sourceContentFor(source));
|
||||
}
|
||||
return JSON.parse(smg.toString());
|
||||
}
|
||||
nls_1.patchSourcemap = patchSourcemap;
|
||||
function patch(moduleId, typescript, javascript, sourcemap) {
|
||||
var _a = analyze(typescript), localizeCalls = _a.localizeCalls, nlsExpressions = _a.nlsExpressions;
|
||||
if (localizeCalls.length === 0) {
|
||||
return { javascript: javascript, sourcemap: sourcemap };
|
||||
}
|
||||
var nlsKeys = template(localizeCalls.map(function (lc) { return lc.key; }));
|
||||
var nls = template(localizeCalls.map(function (lc) { return lc.value; }));
|
||||
var smc = new sm.SourceMapConsumer(sourcemap);
|
||||
var positionFrom = mappedPositionFrom.bind(null, sourcemap.sources[0]);
|
||||
var i = 0;
|
||||
// build patches
|
||||
var patches = lazy(localizeCalls)
|
||||
.map(function (lc) { return ([
|
||||
{ range: lc.keySpan, content: '' + (i++) },
|
||||
{ range: lc.valueSpan, content: 'null' }
|
||||
]); })
|
||||
.flatten()
|
||||
.map(function (c) {
|
||||
var start = lcFrom(smc.generatedPositionFor(positionFrom(c.range.start)));
|
||||
var end = lcFrom(smc.generatedPositionFor(positionFrom(c.range.end)));
|
||||
return { span: { start: start, end: end }, content: c.content };
|
||||
})
|
||||
.toArray();
|
||||
javascript = patchJavascript(patches, javascript, moduleId);
|
||||
// since imports are not within the sourcemap information,
|
||||
// we must do this MacGyver style
|
||||
if (nlsExpressions.length) {
|
||||
javascript = javascript.replace(/^define\(.*$/m, function (line) {
|
||||
return line.replace(/(['"])vs\/nls\1/g, "$1vs/nls!" + moduleId + "$1");
|
||||
});
|
||||
}
|
||||
sourcemap = patchSourcemap(patches, sourcemap, smc);
|
||||
return { javascript: javascript, sourcemap: sourcemap, nlsKeys: nlsKeys, nls: nls };
|
||||
}
|
||||
nls_1.patch = patch;
|
||||
function patchFiles(javascriptFile, typescript) {
|
||||
// hack?
|
||||
var moduleId = javascriptFile.relative
|
||||
.replace(/\.js$/, '')
|
||||
.replace(/\\/g, '/');
|
||||
var _a = patch(moduleId, typescript, javascriptFile.contents.toString(), javascriptFile.sourceMap), javascript = _a.javascript, sourcemap = _a.sourcemap, nlsKeys = _a.nlsKeys, nls = _a.nls;
|
||||
var result = [fileFrom(javascriptFile, javascript)];
|
||||
result[0].sourceMap = sourcemap;
|
||||
if (nlsKeys) {
|
||||
result.push(fileFrom(javascriptFile, nlsKeys, javascriptFile.path.replace(/\.js$/, '.nls.keys.js')));
|
||||
}
|
||||
if (nls) {
|
||||
result.push(fileFrom(javascriptFile, nls, javascriptFile.path.replace(/\.js$/, '.nls.js')));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
nls_1.patchFiles = patchFiles;
|
||||
})(nls || (nls = {}));
|
||||
module.exports = nls;
|
||||
|
|
|
@ -1,230 +1,229 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var path = require("path");
|
||||
var gulp = require("gulp");
|
||||
var sourcemaps = require("gulp-sourcemaps");
|
||||
var filter = require("gulp-filter");
|
||||
var minifyCSS = require("gulp-cssnano");
|
||||
var uglify = require("gulp-uglify");
|
||||
var es = require("event-stream");
|
||||
var concat = require("gulp-concat");
|
||||
var VinylFile = require("vinyl");
|
||||
var bundle = require("./bundle");
|
||||
var util = require("./util");
|
||||
var i18n = require("./i18n");
|
||||
var gulpUtil = require("gulp-util");
|
||||
var flatmap = require("gulp-flatmap");
|
||||
var pump = require("pump");
|
||||
var REPO_ROOT_PATH = path.join(__dirname, '../..');
|
||||
function log(prefix, message) {
|
||||
gulpUtil.log(gulpUtil.colors.cyan('[' + prefix + ']'), message);
|
||||
}
|
||||
function loaderConfig(emptyPaths) {
|
||||
var result = {
|
||||
paths: {
|
||||
'vs': 'out-build/vs',
|
||||
'vscode': 'empty:'
|
||||
},
|
||||
nodeModules: emptyPaths || []
|
||||
};
|
||||
result['vs/css'] = { inlineResources: true };
|
||||
return result;
|
||||
}
|
||||
exports.loaderConfig = loaderConfig;
|
||||
var IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;
|
||||
function loader(bundledFileHeader, bundleLoader) {
|
||||
var sources = [
|
||||
'out-build/vs/loader.js'
|
||||
];
|
||||
if (bundleLoader) {
|
||||
sources = sources.concat([
|
||||
'out-build/vs/css.js',
|
||||
'out-build/vs/nls.js'
|
||||
]);
|
||||
}
|
||||
var isFirst = true;
|
||||
return (gulp
|
||||
.src(sources, { base: 'out-build' })
|
||||
.pipe(es.through(function (data) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
this.emit('data', new VinylFile({
|
||||
path: 'fake',
|
||||
base: '',
|
||||
contents: new Buffer(bundledFileHeader)
|
||||
}));
|
||||
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;
|
||||
})));
|
||||
}
|
||||
function toConcatStream(bundledFileHeader, sources, dest) {
|
||||
var useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest);
|
||||
// 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
|
||||
var containsOurCopyright = false;
|
||||
for (var i = 0, len = sources.length; i < len; i++) {
|
||||
var fileContents = sources[i].contents;
|
||||
if (IS_OUR_COPYRIGHT_REGEXP.test(fileContents)) {
|
||||
containsOurCopyright = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (containsOurCopyright) {
|
||||
sources.unshift({
|
||||
path: null,
|
||||
contents: bundledFileHeader
|
||||
});
|
||||
}
|
||||
var treatedSources = sources.map(function (source) {
|
||||
var root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : '';
|
||||
var base = source.path ? root + '/out-build' : '';
|
||||
return new VinylFile({
|
||||
path: source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake',
|
||||
base: base,
|
||||
contents: new Buffer(source.contents)
|
||||
});
|
||||
});
|
||||
return es.readArray(treatedSources)
|
||||
.pipe(useSourcemaps ? util.loadSourcemaps() : es.through())
|
||||
.pipe(concat(dest));
|
||||
}
|
||||
function toBundleStream(bundledFileHeader, bundles) {
|
||||
return es.merge(bundles.map(function (bundle) {
|
||||
return toConcatStream(bundledFileHeader, bundle.sources, bundle.dest);
|
||||
}));
|
||||
}
|
||||
function optimizeTask(opts) {
|
||||
var entryPoints = opts.entryPoints;
|
||||
var otherSources = opts.otherSources;
|
||||
var resources = opts.resources;
|
||||
var loaderConfig = opts.loaderConfig;
|
||||
var bundledFileHeader = opts.header;
|
||||
var bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader);
|
||||
var out = opts.out;
|
||||
return function () {
|
||||
var bundlesStream = es.through(); // this stream will contain the bundled files
|
||||
var resourcesStream = es.through(); // this stream will contain the resources
|
||||
var bundleInfoStream = es.through(); // this stream will contain bundleInfo.json
|
||||
bundle.bundle(entryPoints, loaderConfig, function (err, result) {
|
||||
if (err) {
|
||||
return bundlesStream.emit('error', JSON.stringify(err));
|
||||
}
|
||||
toBundleStream(bundledFileHeader, result.files).pipe(bundlesStream);
|
||||
// Remove css inlined resources
|
||||
var filteredResources = resources.slice();
|
||||
result.cssInlinedResources.forEach(function (resource) {
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log('optimizer', 'excluding inlined: ' + resource);
|
||||
}
|
||||
filteredResources.push('!' + resource);
|
||||
});
|
||||
gulp.src(filteredResources, { base: 'out-build' }).pipe(resourcesStream);
|
||||
var bundleInfoArray = [];
|
||||
if (opts.bundleInfo) {
|
||||
bundleInfoArray.push(new VinylFile({
|
||||
path: 'bundleInfo.json',
|
||||
base: '.',
|
||||
contents: new Buffer(JSON.stringify(result.bundleData, null, '\t'))
|
||||
}));
|
||||
}
|
||||
es.readArray(bundleInfoArray).pipe(bundleInfoStream);
|
||||
});
|
||||
var otherSourcesStream = es.through();
|
||||
var otherSourcesStreamArr = [];
|
||||
gulp.src(otherSources, { base: 'out-build' })
|
||||
.pipe(es.through(function (data) {
|
||||
otherSourcesStreamArr.push(toConcatStream(bundledFileHeader, [data], data.relative));
|
||||
}, function () {
|
||||
if (!otherSourcesStreamArr.length) {
|
||||
setTimeout(function () { otherSourcesStream.emit('end'); }, 0);
|
||||
}
|
||||
else {
|
||||
es.merge(otherSourcesStreamArr).pipe(otherSourcesStream);
|
||||
}
|
||||
}));
|
||||
var result = es.merge(loader(bundledFileHeader, bundleLoader), bundlesStream, otherSourcesStream, resourcesStream, bundleInfoStream);
|
||||
return result
|
||||
.pipe(sourcemaps.write('./', {
|
||||
sourceRoot: null,
|
||||
addComment: true,
|
||||
includeContent: true
|
||||
}))
|
||||
.pipe(i18n.processNlsFiles({
|
||||
fileHeader: bundledFileHeader
|
||||
}))
|
||||
.pipe(gulp.dest(out));
|
||||
};
|
||||
}
|
||||
exports.optimizeTask = optimizeTask;
|
||||
;
|
||||
/**
|
||||
* Wrap around uglify and allow the preserveComments function
|
||||
* to have a file "context" to include our copyright only once per file.
|
||||
*/
|
||||
function uglifyWithCopyrights() {
|
||||
var preserveComments = function (f) {
|
||||
return 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 (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;
|
||||
};
|
||||
};
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(flatmap(function (stream, f) {
|
||||
return stream
|
||||
.pipe(uglify({ preserveComments: preserveComments(f) }));
|
||||
}));
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
function minifyTask(src, sourceMapBaseUrl) {
|
||||
var sourceMappingURL = sourceMapBaseUrl && (function (f) { return sourceMapBaseUrl + "/" + f.relative + ".map"; });
|
||||
return function (cb) {
|
||||
var jsFilter = filter('**/*.js', { restore: true });
|
||||
var cssFilter = filter('**/*.css', { restore: true });
|
||||
pump(gulp.src([src + '/**', '!' + src + '/**/*.map']), jsFilter, sourcemaps.init({ loadMaps: true }), uglifyWithCopyrights(), jsFilter.restore, cssFilter, minifyCSS({ reduceIdents: false }), cssFilter.restore, sourcemaps.write('./', {
|
||||
sourceMappingURL: sourceMappingURL,
|
||||
sourceRoot: null,
|
||||
includeContent: true,
|
||||
addComment: true
|
||||
}), gulp.dest(src + '-min'), function (err) {
|
||||
if (err instanceof uglify.GulpUglifyError) {
|
||||
console.error("Uglify error in '" + (err.cause && err.cause.filename) + "'");
|
||||
}
|
||||
cb(err);
|
||||
});
|
||||
};
|
||||
}
|
||||
exports.minifyTask = minifyTask;
|
||||
;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
var path = require("path");
|
||||
var gulp = require("gulp");
|
||||
var sourcemaps = require("gulp-sourcemaps");
|
||||
var filter = require("gulp-filter");
|
||||
var minifyCSS = require("gulp-cssnano");
|
||||
var uglify = require("gulp-uglify");
|
||||
var es = require("event-stream");
|
||||
var concat = require("gulp-concat");
|
||||
var VinylFile = require("vinyl");
|
||||
var bundle = require("./bundle");
|
||||
var util = require("./util");
|
||||
var i18n = require("./i18n");
|
||||
var gulpUtil = require("gulp-util");
|
||||
var flatmap = require("gulp-flatmap");
|
||||
var pump = require("pump");
|
||||
var REPO_ROOT_PATH = path.join(__dirname, '../..');
|
||||
function log(prefix, message) {
|
||||
gulpUtil.log(gulpUtil.colors.cyan('[' + prefix + ']'), message);
|
||||
}
|
||||
function loaderConfig(emptyPaths) {
|
||||
var result = {
|
||||
paths: {
|
||||
'vs': 'out-build/vs',
|
||||
'vscode': 'empty:'
|
||||
},
|
||||
nodeModules: emptyPaths || []
|
||||
};
|
||||
result['vs/css'] = { inlineResources: true };
|
||||
return result;
|
||||
}
|
||||
exports.loaderConfig = loaderConfig;
|
||||
var IS_OUR_COPYRIGHT_REGEXP = /Copyright \(C\) Microsoft Corporation/i;
|
||||
function loader(bundledFileHeader, bundleLoader) {
|
||||
var sources = [
|
||||
'out-build/vs/loader.js'
|
||||
];
|
||||
if (bundleLoader) {
|
||||
sources = sources.concat([
|
||||
'out-build/vs/css.js',
|
||||
'out-build/vs/nls.js'
|
||||
]);
|
||||
}
|
||||
var isFirst = true;
|
||||
return (gulp
|
||||
.src(sources, { base: 'out-build' })
|
||||
.pipe(es.through(function (data) {
|
||||
if (isFirst) {
|
||||
isFirst = false;
|
||||
this.emit('data', new VinylFile({
|
||||
path: 'fake',
|
||||
base: '',
|
||||
contents: new Buffer(bundledFileHeader)
|
||||
}));
|
||||
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;
|
||||
})));
|
||||
}
|
||||
function toConcatStream(bundledFileHeader, sources, dest) {
|
||||
var useSourcemaps = /\.js$/.test(dest) && !/\.nls\.js$/.test(dest);
|
||||
// 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
|
||||
var containsOurCopyright = false;
|
||||
for (var i = 0, len = sources.length; i < len; i++) {
|
||||
var fileContents = sources[i].contents;
|
||||
if (IS_OUR_COPYRIGHT_REGEXP.test(fileContents)) {
|
||||
containsOurCopyright = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (containsOurCopyright) {
|
||||
sources.unshift({
|
||||
path: null,
|
||||
contents: bundledFileHeader
|
||||
});
|
||||
}
|
||||
var treatedSources = sources.map(function (source) {
|
||||
var root = source.path ? REPO_ROOT_PATH.replace(/\\/g, '/') : '';
|
||||
var base = source.path ? root + '/out-build' : '';
|
||||
return new VinylFile({
|
||||
path: source.path ? root + '/' + source.path.replace(/\\/g, '/') : 'fake',
|
||||
base: base,
|
||||
contents: new Buffer(source.contents)
|
||||
});
|
||||
});
|
||||
return es.readArray(treatedSources)
|
||||
.pipe(useSourcemaps ? util.loadSourcemaps() : es.through())
|
||||
.pipe(concat(dest));
|
||||
}
|
||||
function toBundleStream(bundledFileHeader, bundles) {
|
||||
return es.merge(bundles.map(function (bundle) {
|
||||
return toConcatStream(bundledFileHeader, bundle.sources, bundle.dest);
|
||||
}));
|
||||
}
|
||||
function optimizeTask(opts) {
|
||||
var entryPoints = opts.entryPoints;
|
||||
var otherSources = opts.otherSources;
|
||||
var resources = opts.resources;
|
||||
var loaderConfig = opts.loaderConfig;
|
||||
var bundledFileHeader = opts.header;
|
||||
var bundleLoader = (typeof opts.bundleLoader === 'undefined' ? true : opts.bundleLoader);
|
||||
var out = opts.out;
|
||||
return function () {
|
||||
var bundlesStream = es.through(); // this stream will contain the bundled files
|
||||
var resourcesStream = es.through(); // this stream will contain the resources
|
||||
var bundleInfoStream = es.through(); // this stream will contain bundleInfo.json
|
||||
bundle.bundle(entryPoints, loaderConfig, function (err, result) {
|
||||
if (err) {
|
||||
return bundlesStream.emit('error', JSON.stringify(err));
|
||||
}
|
||||
toBundleStream(bundledFileHeader, result.files).pipe(bundlesStream);
|
||||
// Remove css inlined resources
|
||||
var filteredResources = resources.slice();
|
||||
result.cssInlinedResources.forEach(function (resource) {
|
||||
if (process.env['VSCODE_BUILD_VERBOSE']) {
|
||||
log('optimizer', 'excluding inlined: ' + resource);
|
||||
}
|
||||
filteredResources.push('!' + resource);
|
||||
});
|
||||
gulp.src(filteredResources, { base: 'out-build' }).pipe(resourcesStream);
|
||||
var bundleInfoArray = [];
|
||||
if (opts.bundleInfo) {
|
||||
bundleInfoArray.push(new VinylFile({
|
||||
path: 'bundleInfo.json',
|
||||
base: '.',
|
||||
contents: new Buffer(JSON.stringify(result.bundleData, null, '\t'))
|
||||
}));
|
||||
}
|
||||
es.readArray(bundleInfoArray).pipe(bundleInfoStream);
|
||||
});
|
||||
var otherSourcesStream = es.through();
|
||||
var otherSourcesStreamArr = [];
|
||||
gulp.src(otherSources, { base: 'out-build' })
|
||||
.pipe(es.through(function (data) {
|
||||
otherSourcesStreamArr.push(toConcatStream(bundledFileHeader, [data], data.relative));
|
||||
}, function () {
|
||||
if (!otherSourcesStreamArr.length) {
|
||||
setTimeout(function () { otherSourcesStream.emit('end'); }, 0);
|
||||
}
|
||||
else {
|
||||
es.merge(otherSourcesStreamArr).pipe(otherSourcesStream);
|
||||
}
|
||||
}));
|
||||
var result = es.merge(loader(bundledFileHeader, bundleLoader), bundlesStream, otherSourcesStream, resourcesStream, bundleInfoStream);
|
||||
return result
|
||||
.pipe(sourcemaps.write('./', {
|
||||
sourceRoot: null,
|
||||
addComment: true,
|
||||
includeContent: true
|
||||
}))
|
||||
.pipe(i18n.processNlsFiles({
|
||||
fileHeader: bundledFileHeader
|
||||
}))
|
||||
.pipe(gulp.dest(out));
|
||||
};
|
||||
}
|
||||
exports.optimizeTask = optimizeTask;
|
||||
;
|
||||
/**
|
||||
* Wrap around uglify and allow the preserveComments function
|
||||
* to have a file "context" to include our copyright only once per file.
|
||||
*/
|
||||
function uglifyWithCopyrights() {
|
||||
var preserveComments = function (f) {
|
||||
return 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 (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;
|
||||
};
|
||||
};
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(flatmap(function (stream, f) {
|
||||
return stream
|
||||
.pipe(uglify({ preserveComments: preserveComments(f) }));
|
||||
}));
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
function minifyTask(src, sourceMapBaseUrl) {
|
||||
var sourceMappingURL = sourceMapBaseUrl && (function (f) { return sourceMapBaseUrl + "/" + f.relative + ".map"; });
|
||||
return function (cb) {
|
||||
var jsFilter = filter('**/*.js', { restore: true });
|
||||
var cssFilter = filter('**/*.css', { restore: true });
|
||||
pump(gulp.src([src + '/**', '!' + src + '/**/*.map']), jsFilter, sourcemaps.init({ loadMaps: true }), uglifyWithCopyrights(), jsFilter.restore, cssFilter, minifyCSS({ reduceIdents: false }), cssFilter.restore, sourcemaps.write('./', {
|
||||
sourceMappingURL: sourceMappingURL,
|
||||
sourceRoot: null,
|
||||
includeContent: true,
|
||||
addComment: true
|
||||
}), gulp.dest(src + '-min'), function (err) {
|
||||
if (err instanceof uglify.GulpUglifyError) {
|
||||
console.error("Uglify error in '" + (err.cause && err.cause.filename) + "'");
|
||||
}
|
||||
cb(err);
|
||||
});
|
||||
};
|
||||
}
|
||||
exports.minifyTask = minifyTask;
|
||||
;
|
||||
|
|
|
@ -1,83 +1,80 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var es = require("event-stream");
|
||||
var _ = require("underscore");
|
||||
var util = require("gulp-util");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var allErrors = [];
|
||||
var startTime = null;
|
||||
var count = 0;
|
||||
function onStart() {
|
||||
if (count++ > 0) {
|
||||
return;
|
||||
}
|
||||
startTime = new Date().getTime();
|
||||
util.log("Starting " + util.colors.green('compilation') + "...");
|
||||
}
|
||||
function onEnd() {
|
||||
if (--count > 0) {
|
||||
return;
|
||||
}
|
||||
log();
|
||||
}
|
||||
var buildLogPath = path.join(path.dirname(path.dirname(__dirname)), '.build', 'log');
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(buildLogPath));
|
||||
}
|
||||
catch (err) {
|
||||
// ignore
|
||||
}
|
||||
function log() {
|
||||
var errors = _.flatten(allErrors);
|
||||
errors.map(function (err) { return util.log(util.colors.red('Error') + ": " + err); });
|
||||
var regex = /^([^(]+)\((\d+),(\d+)\): (.*)$/;
|
||||
var messages = errors
|
||||
.map(function (err) { return regex.exec(err); })
|
||||
.filter(function (match) { return !!match; })
|
||||
.map(function (_a) {
|
||||
var path = _a[1], line = _a[2], column = _a[3], message = _a[4];
|
||||
return ({ path: path, line: Number.parseInt(line), column: Number.parseInt(column), message: message });
|
||||
});
|
||||
try {
|
||||
fs.writeFileSync(buildLogPath, JSON.stringify(messages));
|
||||
}
|
||||
catch (err) {
|
||||
//noop
|
||||
}
|
||||
util.log("Finished " + util.colors.green('compilation') + " with " + errors.length + " errors after " + util.colors.magenta((new Date().getTime() - startTime) + ' ms'));
|
||||
}
|
||||
function createReporter() {
|
||||
var errors = [];
|
||||
allErrors.push(errors);
|
||||
var ReportFunc = (function () {
|
||||
function ReportFunc(err) {
|
||||
errors.push(err);
|
||||
}
|
||||
ReportFunc.hasErrors = function () {
|
||||
return errors.length > 0;
|
||||
};
|
||||
ReportFunc.end = function (emitError) {
|
||||
errors.length = 0;
|
||||
onStart();
|
||||
return es.through(null, function () {
|
||||
onEnd();
|
||||
if (emitError && errors.length > 0) {
|
||||
log();
|
||||
this.emit('error');
|
||||
}
|
||||
else {
|
||||
this.emit('end');
|
||||
}
|
||||
});
|
||||
};
|
||||
return ReportFunc;
|
||||
}());
|
||||
return ReportFunc;
|
||||
}
|
||||
exports.createReporter = createReporter;
|
||||
;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
var es = require("event-stream");
|
||||
var _ = require("underscore");
|
||||
var util = require("gulp-util");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
var allErrors = [];
|
||||
var startTime = null;
|
||||
var count = 0;
|
||||
function onStart() {
|
||||
if (count++ > 0) {
|
||||
return;
|
||||
}
|
||||
startTime = new Date().getTime();
|
||||
util.log("Starting " + util.colors.green('compilation') + "...");
|
||||
}
|
||||
function onEnd() {
|
||||
if (--count > 0) {
|
||||
return;
|
||||
}
|
||||
log();
|
||||
}
|
||||
var buildLogPath = path.join(path.dirname(path.dirname(__dirname)), '.build', 'log');
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(buildLogPath));
|
||||
}
|
||||
catch (err) {
|
||||
}
|
||||
function log() {
|
||||
var errors = _.flatten(allErrors);
|
||||
errors.map(function (err) { return util.log(util.colors.red('Error') + ": " + err); });
|
||||
var regex = /^([^(]+)\((\d+),(\d+)\): (.*)$/;
|
||||
var messages = errors
|
||||
.map(function (err) { return regex.exec(err); })
|
||||
.filter(function (match) { return !!match; })
|
||||
.map(function (_a) {
|
||||
var path = _a[1], line = _a[2], column = _a[3], message = _a[4];
|
||||
return ({ path: path, line: Number.parseInt(line), column: Number.parseInt(column), message: message });
|
||||
});
|
||||
try {
|
||||
fs.writeFileSync(buildLogPath, JSON.stringify(messages));
|
||||
}
|
||||
catch (err) {
|
||||
}
|
||||
util.log("Finished " + util.colors.green('compilation') + " with " + errors.length + " errors after " + util.colors.magenta((new Date().getTime() - startTime) + ' ms'));
|
||||
}
|
||||
function createReporter() {
|
||||
var errors = [];
|
||||
allErrors.push(errors);
|
||||
var ReportFunc = (function () {
|
||||
function ReportFunc(err) {
|
||||
errors.push(err);
|
||||
}
|
||||
ReportFunc.hasErrors = function () {
|
||||
return errors.length > 0;
|
||||
};
|
||||
ReportFunc.end = function (emitError) {
|
||||
errors.length = 0;
|
||||
onStart();
|
||||
return es.through(null, function () {
|
||||
onEnd();
|
||||
if (emitError && errors.length > 0) {
|
||||
log();
|
||||
this.emit('error');
|
||||
}
|
||||
else {
|
||||
this.emit('end');
|
||||
}
|
||||
});
|
||||
};
|
||||
return ReportFunc;
|
||||
}());
|
||||
return ReportFunc;
|
||||
}
|
||||
exports.createReporter = createReporter;
|
||||
;
|
||||
|
|
|
@ -1,50 +1,44 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var path_1 = require("path");
|
||||
var Lint = require("tslint");
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions()));
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var ImportPatterns = (function (_super) {
|
||||
__extends(ImportPatterns, _super);
|
||||
function ImportPatterns(file, opts) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this.imports = Object.create(null);
|
||||
return _this;
|
||||
}
|
||||
ImportPatterns.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
if (path[0] === '.') {
|
||||
path = path_1.join(path_1.dirname(node.getSourceFile().fileName), path);
|
||||
}
|
||||
if (this.imports[path]) {
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Duplicate imports for '" + path + "'."));
|
||||
}
|
||||
this.imports[path] = true;
|
||||
};
|
||||
return ImportPatterns;
|
||||
}(Lint.RuleWalker));
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var path_1 = require("path");
|
||||
var Lint = require("tslint");
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions()));
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var ImportPatterns = (function (_super) {
|
||||
__extends(ImportPatterns, _super);
|
||||
function ImportPatterns(file, opts) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this.imports = Object.create(null);
|
||||
return _this;
|
||||
}
|
||||
ImportPatterns.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
if (path[0] === '.') {
|
||||
path = path_1.join(path_1.dirname(node.getSourceFile().fileName), path);
|
||||
}
|
||||
if (this.imports[path]) {
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Duplicate imports for '" + path + "'."));
|
||||
}
|
||||
this.imports[path] = true;
|
||||
};
|
||||
return ImportPatterns;
|
||||
}(Lint.RuleWalker));
|
||||
|
|
|
@ -1,57 +1,51 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Lint = require("tslint");
|
||||
var minimatch = require("minimatch");
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
var configs = this.getOptions().ruleArguments;
|
||||
for (var _i = 0, configs_1 = configs; _i < configs_1.length; _i++) {
|
||||
var config = configs_1[_i];
|
||||
if (minimatch(sourceFile.fileName, config.target)) {
|
||||
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var ImportPatterns = (function (_super) {
|
||||
__extends(ImportPatterns, _super);
|
||||
function ImportPatterns(file, opts, _config) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this._config = _config;
|
||||
return _this;
|
||||
}
|
||||
ImportPatterns.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
// ignore relative paths
|
||||
if (path[0] === '.') {
|
||||
return;
|
||||
}
|
||||
if (!minimatch(path, this._config.restrictions)) {
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Imports violates '" + this._config.restrictions + "'-restriction."));
|
||||
}
|
||||
};
|
||||
return ImportPatterns;
|
||||
}(Lint.RuleWalker));
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var Lint = require("tslint");
|
||||
var minimatch = require("minimatch");
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
var configs = this.getOptions().ruleArguments;
|
||||
for (var _i = 0, configs_1 = configs; _i < configs_1.length; _i++) {
|
||||
var config = configs_1[_i];
|
||||
if (minimatch(sourceFile.fileName, config.target)) {
|
||||
return this.applyWithWalker(new ImportPatterns(sourceFile, this.getOptions(), config));
|
||||
}
|
||||
}
|
||||
return [];
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var ImportPatterns = (function (_super) {
|
||||
__extends(ImportPatterns, _super);
|
||||
function ImportPatterns(file, opts, _config) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this._config = _config;
|
||||
return _this;
|
||||
}
|
||||
ImportPatterns.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
// ignore relative paths
|
||||
if (path[0] === '.') {
|
||||
return;
|
||||
}
|
||||
if (!minimatch(path, this._config.restrictions)) {
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Imports violates '" + this._config.restrictions + "'-restriction."));
|
||||
}
|
||||
};
|
||||
return ImportPatterns;
|
||||
}(Lint.RuleWalker));
|
||||
|
|
|
@ -1,85 +1,79 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var Lint = require("tslint");
|
||||
var path_1 = require("path");
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
var parts = path_1.dirname(sourceFile.fileName).split(/\\|\//);
|
||||
var ruleArgs = this.getOptions().ruleArguments[0];
|
||||
var config;
|
||||
for (var i = parts.length - 1; i >= 0; i--) {
|
||||
if (ruleArgs[parts[i]]) {
|
||||
config = {
|
||||
allowed: new Set(ruleArgs[parts[i]]).add(parts[i]),
|
||||
disallowed: new Set()
|
||||
};
|
||||
Object.keys(ruleArgs).forEach(function (key) {
|
||||
if (!config.allowed.has(key)) {
|
||||
config.disallowed.add(key);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!config) {
|
||||
return [];
|
||||
}
|
||||
return this.applyWithWalker(new LayeringRule(sourceFile, config, this.getOptions()));
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var LayeringRule = (function (_super) {
|
||||
__extends(LayeringRule, _super);
|
||||
function LayeringRule(file, config, opts) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this._config = config;
|
||||
return _this;
|
||||
}
|
||||
LayeringRule.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
if (path[0] === '.') {
|
||||
path = path_1.join(path_1.dirname(node.getSourceFile().fileName), path);
|
||||
}
|
||||
var parts = path_1.dirname(path).split(/\\|\//);
|
||||
for (var i = parts.length - 1; i >= 0; i--) {
|
||||
var part = parts[i];
|
||||
if (this._config.allowed.has(part)) {
|
||||
// GOOD - same layer
|
||||
return;
|
||||
}
|
||||
if (this._config.disallowed.has(part)) {
|
||||
// BAD - wrong layer
|
||||
var message = "Bad layering. You are not allowed to access '" + part + "' from here, allowed layers are: [" + LayeringRule._print(this._config.allowed) + "]";
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
LayeringRule._print = function (set) {
|
||||
var r = [];
|
||||
set.forEach(function (e) { return r.push(e); });
|
||||
return r.join(', ');
|
||||
};
|
||||
return LayeringRule;
|
||||
}(Lint.RuleWalker));
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var Lint = require("tslint");
|
||||
var path_1 = require("path");
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
var parts = path_1.dirname(sourceFile.fileName).split(/\\|\//);
|
||||
var ruleArgs = this.getOptions().ruleArguments[0];
|
||||
var config;
|
||||
for (var i = parts.length - 1; i >= 0; i--) {
|
||||
if (ruleArgs[parts[i]]) {
|
||||
config = {
|
||||
allowed: new Set(ruleArgs[parts[i]]).add(parts[i]),
|
||||
disallowed: new Set()
|
||||
};
|
||||
Object.keys(ruleArgs).forEach(function (key) {
|
||||
if (!config.allowed.has(key)) {
|
||||
config.disallowed.add(key);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!config) {
|
||||
return [];
|
||||
}
|
||||
return this.applyWithWalker(new LayeringRule(sourceFile, config, this.getOptions()));
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
var LayeringRule = (function (_super) {
|
||||
__extends(LayeringRule, _super);
|
||||
function LayeringRule(file, config, opts) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this._config = config;
|
||||
return _this;
|
||||
}
|
||||
LayeringRule.prototype.visitImportDeclaration = function (node) {
|
||||
var path = node.moduleSpecifier.getText();
|
||||
// remove quotes
|
||||
path = path.slice(1, -1);
|
||||
if (path[0] === '.') {
|
||||
path = path_1.join(path_1.dirname(node.getSourceFile().fileName), path);
|
||||
}
|
||||
var parts = path_1.dirname(path).split(/\\|\//);
|
||||
for (var i = parts.length - 1; i >= 0; i--) {
|
||||
var part = parts[i];
|
||||
if (this._config.allowed.has(part)) {
|
||||
// GOOD - same layer
|
||||
return;
|
||||
}
|
||||
if (this._config.disallowed.has(part)) {
|
||||
// BAD - wrong layer
|
||||
var message = "Bad layering. You are not allowed to access '" + part + "' from here, allowed layers are: [" + LayeringRule._print(this._config.allowed) + "]";
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), message));
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
LayeringRule._print = function (set) {
|
||||
var r = [];
|
||||
set.forEach(function (e) { return r.push(e); });
|
||||
return r.join(', ');
|
||||
};
|
||||
return LayeringRule;
|
||||
}(Lint.RuleWalker));
|
||||
|
|
|
@ -1,177 +1,171 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var ts = require("typescript");
|
||||
var Lint = require("tslint");
|
||||
/**
|
||||
* Implementation of the no-unexternalized-strings rule.
|
||||
*/
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
return this.applyWithWalker(new NoUnexternalizedStringsRuleWalker(sourceFile, this.getOptions()));
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
function isStringLiteral(node) {
|
||||
return node && node.kind === ts.SyntaxKind.StringLiteral;
|
||||
}
|
||||
function isObjectLiteral(node) {
|
||||
return node && node.kind === ts.SyntaxKind.ObjectLiteralExpression;
|
||||
}
|
||||
function isPropertyAssignment(node) {
|
||||
return node && node.kind === ts.SyntaxKind.PropertyAssignment;
|
||||
}
|
||||
var NoUnexternalizedStringsRuleWalker = (function (_super) {
|
||||
__extends(NoUnexternalizedStringsRuleWalker, _super);
|
||||
function NoUnexternalizedStringsRuleWalker(file, opts) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this.signatures = Object.create(null);
|
||||
_this.ignores = Object.create(null);
|
||||
_this.messageIndex = undefined;
|
||||
_this.keyIndex = undefined;
|
||||
_this.usedKeys = Object.create(null);
|
||||
var options = _this.getOptions();
|
||||
var first = options && options.length > 0 ? options[0] : null;
|
||||
if (first) {
|
||||
if (Array.isArray(first.signatures)) {
|
||||
first.signatures.forEach(function (signature) { return _this.signatures[signature] = true; });
|
||||
}
|
||||
if (Array.isArray(first.ignores)) {
|
||||
first.ignores.forEach(function (ignore) { return _this.ignores[ignore] = true; });
|
||||
}
|
||||
if (typeof first.messageIndex !== 'undefined') {
|
||||
_this.messageIndex = first.messageIndex;
|
||||
}
|
||||
if (typeof first.keyIndex !== 'undefined') {
|
||||
_this.keyIndex = first.keyIndex;
|
||||
}
|
||||
}
|
||||
return _this;
|
||||
}
|
||||
NoUnexternalizedStringsRuleWalker.prototype.visitSourceFile = function (node) {
|
||||
var _this = this;
|
||||
_super.prototype.visitSourceFile.call(this, node);
|
||||
Object.keys(this.usedKeys).forEach(function (key) {
|
||||
var occurences = _this.usedKeys[key];
|
||||
if (occurences.length > 1) {
|
||||
occurences.forEach(function (occurence) {
|
||||
_this.addFailure((_this.createFailure(occurence.key.getStart(), occurence.key.getWidth(), "Duplicate key " + occurence.key.getText() + " with different message value.")));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.visitStringLiteral = function (node) {
|
||||
this.checkStringLiteral(node);
|
||||
_super.prototype.visitStringLiteral.call(this, node);
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.checkStringLiteral = function (node) {
|
||||
var text = node.getText();
|
||||
var doubleQuoted = text.length >= 2 && text[0] === NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE && text[text.length - 1] === NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE;
|
||||
var info = this.findDescribingParent(node);
|
||||
// Ignore strings in import and export nodes.
|
||||
if (info && info.ignoreUsage) {
|
||||
return;
|
||||
}
|
||||
var callInfo = info ? info.callInfo : null;
|
||||
var functionName = callInfo ? callInfo.callExpression.expression.getText() : null;
|
||||
if (functionName && this.ignores[functionName]) {
|
||||
return;
|
||||
}
|
||||
if (doubleQuoted && (!callInfo || callInfo.argIndex === -1 || !this.signatures[functionName])) {
|
||||
var s = node.getText();
|
||||
var replacement = new Lint.Replacement(node.getStart(), node.getWidth(), "nls.localize('KEY-" + s.substring(1, s.length - 1) + "', " + s + ")");
|
||||
var fix = new Lint.Fix('Unexternalitzed string', [replacement]);
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Unexternalized string found: " + node.getText(), fix));
|
||||
return;
|
||||
}
|
||||
// We have a single quoted string outside a localize function name.
|
||||
if (!doubleQuoted && !this.signatures[functionName]) {
|
||||
return;
|
||||
}
|
||||
// We have a string that is a direct argument into the localize call.
|
||||
var keyArg = callInfo.argIndex === this.keyIndex
|
||||
? callInfo.callExpression.arguments[this.keyIndex]
|
||||
: null;
|
||||
if (keyArg) {
|
||||
if (isStringLiteral(keyArg)) {
|
||||
this.recordKey(keyArg, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined);
|
||||
}
|
||||
else if (isObjectLiteral(keyArg)) {
|
||||
for (var i = 0; i < keyArg.properties.length; i++) {
|
||||
var property = keyArg.properties[i];
|
||||
if (isPropertyAssignment(property)) {
|
||||
var name_1 = property.name.getText();
|
||||
if (name_1 === 'key') {
|
||||
var initializer = property.initializer;
|
||||
if (isStringLiteral(initializer)) {
|
||||
this.recordKey(initializer, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var messageArg = callInfo.argIndex === this.messageIndex
|
||||
? callInfo.callExpression.arguments[this.messageIndex]
|
||||
: null;
|
||||
if (messageArg && messageArg !== node) {
|
||||
this.addFailure(this.createFailure(messageArg.getStart(), messageArg.getWidth(), "Message argument to '" + callInfo.callExpression.expression.getText() + "' must be a string literal."));
|
||||
return;
|
||||
}
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.recordKey = function (keyNode, messageNode) {
|
||||
var text = keyNode.getText();
|
||||
var occurences = this.usedKeys[text];
|
||||
if (!occurences) {
|
||||
occurences = [];
|
||||
this.usedKeys[text] = occurences;
|
||||
}
|
||||
if (messageNode) {
|
||||
if (occurences.some(function (pair) { return pair.message ? pair.message.getText() === messageNode.getText() : false; })) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
occurences.push({ key: keyNode, message: messageNode });
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.findDescribingParent = function (node) {
|
||||
var parent;
|
||||
while ((parent = node.parent)) {
|
||||
var kind = parent.kind;
|
||||
if (kind === ts.SyntaxKind.CallExpression) {
|
||||
var callExpression = parent;
|
||||
return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(node) } };
|
||||
}
|
||||
else if (kind === ts.SyntaxKind.ImportEqualsDeclaration || kind === ts.SyntaxKind.ImportDeclaration || kind === ts.SyntaxKind.ExportDeclaration) {
|
||||
return { ignoreUsage: true };
|
||||
}
|
||||
else if (kind === ts.SyntaxKind.VariableDeclaration || kind === ts.SyntaxKind.FunctionDeclaration || kind === ts.SyntaxKind.PropertyDeclaration
|
||||
|| kind === ts.SyntaxKind.MethodDeclaration || kind === ts.SyntaxKind.VariableDeclarationList || kind === ts.SyntaxKind.InterfaceDeclaration
|
||||
|| kind === ts.SyntaxKind.ClassDeclaration || kind === ts.SyntaxKind.EnumDeclaration || kind === ts.SyntaxKind.ModuleDeclaration
|
||||
|| kind === ts.SyntaxKind.TypeAliasDeclaration || kind === ts.SyntaxKind.SourceFile) {
|
||||
return null;
|
||||
}
|
||||
node = parent;
|
||||
}
|
||||
};
|
||||
return NoUnexternalizedStringsRuleWalker;
|
||||
}(Lint.RuleWalker));
|
||||
NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE = '"';
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var ts = require("typescript");
|
||||
var Lint = require("tslint");
|
||||
/**
|
||||
* Implementation of the no-unexternalized-strings rule.
|
||||
*/
|
||||
var Rule = (function (_super) {
|
||||
__extends(Rule, _super);
|
||||
function Rule() {
|
||||
return _super !== null && _super.apply(this, arguments) || this;
|
||||
}
|
||||
Rule.prototype.apply = function (sourceFile) {
|
||||
return this.applyWithWalker(new NoUnexternalizedStringsRuleWalker(sourceFile, this.getOptions()));
|
||||
};
|
||||
return Rule;
|
||||
}(Lint.Rules.AbstractRule));
|
||||
exports.Rule = Rule;
|
||||
function isStringLiteral(node) {
|
||||
return node && node.kind === ts.SyntaxKind.StringLiteral;
|
||||
}
|
||||
function isObjectLiteral(node) {
|
||||
return node && node.kind === ts.SyntaxKind.ObjectLiteralExpression;
|
||||
}
|
||||
function isPropertyAssignment(node) {
|
||||
return node && node.kind === ts.SyntaxKind.PropertyAssignment;
|
||||
}
|
||||
var NoUnexternalizedStringsRuleWalker = (function (_super) {
|
||||
__extends(NoUnexternalizedStringsRuleWalker, _super);
|
||||
function NoUnexternalizedStringsRuleWalker(file, opts) {
|
||||
var _this = _super.call(this, file, opts) || this;
|
||||
_this.signatures = Object.create(null);
|
||||
_this.ignores = Object.create(null);
|
||||
_this.messageIndex = undefined;
|
||||
_this.keyIndex = undefined;
|
||||
_this.usedKeys = Object.create(null);
|
||||
var options = _this.getOptions();
|
||||
var first = options && options.length > 0 ? options[0] : null;
|
||||
if (first) {
|
||||
if (Array.isArray(first.signatures)) {
|
||||
first.signatures.forEach(function (signature) { return _this.signatures[signature] = true; });
|
||||
}
|
||||
if (Array.isArray(first.ignores)) {
|
||||
first.ignores.forEach(function (ignore) { return _this.ignores[ignore] = true; });
|
||||
}
|
||||
if (typeof first.messageIndex !== 'undefined') {
|
||||
_this.messageIndex = first.messageIndex;
|
||||
}
|
||||
if (typeof first.keyIndex !== 'undefined') {
|
||||
_this.keyIndex = first.keyIndex;
|
||||
}
|
||||
}
|
||||
return _this;
|
||||
}
|
||||
NoUnexternalizedStringsRuleWalker.prototype.visitSourceFile = function (node) {
|
||||
var _this = this;
|
||||
_super.prototype.visitSourceFile.call(this, node);
|
||||
Object.keys(this.usedKeys).forEach(function (key) {
|
||||
var occurences = _this.usedKeys[key];
|
||||
if (occurences.length > 1) {
|
||||
occurences.forEach(function (occurence) {
|
||||
_this.addFailure((_this.createFailure(occurence.key.getStart(), occurence.key.getWidth(), "Duplicate key " + occurence.key.getText() + " with different message value.")));
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.visitStringLiteral = function (node) {
|
||||
this.checkStringLiteral(node);
|
||||
_super.prototype.visitStringLiteral.call(this, node);
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.checkStringLiteral = function (node) {
|
||||
var text = node.getText();
|
||||
var doubleQuoted = text.length >= 2 && text[0] === NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE && text[text.length - 1] === NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE;
|
||||
var info = this.findDescribingParent(node);
|
||||
// Ignore strings in import and export nodes.
|
||||
if (info && info.ignoreUsage) {
|
||||
return;
|
||||
}
|
||||
var callInfo = info ? info.callInfo : null;
|
||||
var functionName = callInfo ? callInfo.callExpression.expression.getText() : null;
|
||||
if (functionName && this.ignores[functionName]) {
|
||||
return;
|
||||
}
|
||||
if (doubleQuoted && (!callInfo || callInfo.argIndex === -1 || !this.signatures[functionName])) {
|
||||
var s = node.getText();
|
||||
var replacement = new Lint.Replacement(node.getStart(), node.getWidth(), "nls.localize('KEY-" + s.substring(1, s.length - 1) + "', " + s + ")");
|
||||
var fix = new Lint.Fix('Unexternalitzed string', [replacement]);
|
||||
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), "Unexternalized string found: " + node.getText(), fix));
|
||||
return;
|
||||
}
|
||||
// We have a single quoted string outside a localize function name.
|
||||
if (!doubleQuoted && !this.signatures[functionName]) {
|
||||
return;
|
||||
}
|
||||
// We have a string that is a direct argument into the localize call.
|
||||
var keyArg = callInfo.argIndex === this.keyIndex
|
||||
? callInfo.callExpression.arguments[this.keyIndex]
|
||||
: null;
|
||||
if (keyArg) {
|
||||
if (isStringLiteral(keyArg)) {
|
||||
this.recordKey(keyArg, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined);
|
||||
}
|
||||
else if (isObjectLiteral(keyArg)) {
|
||||
for (var i = 0; i < keyArg.properties.length; i++) {
|
||||
var property = keyArg.properties[i];
|
||||
if (isPropertyAssignment(property)) {
|
||||
var name_1 = property.name.getText();
|
||||
if (name_1 === 'key') {
|
||||
var initializer = property.initializer;
|
||||
if (isStringLiteral(initializer)) {
|
||||
this.recordKey(initializer, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var messageArg = callInfo.argIndex === this.messageIndex
|
||||
? callInfo.callExpression.arguments[this.messageIndex]
|
||||
: null;
|
||||
if (messageArg && messageArg !== node) {
|
||||
this.addFailure(this.createFailure(messageArg.getStart(), messageArg.getWidth(), "Message argument to '" + callInfo.callExpression.expression.getText() + "' must be a string literal."));
|
||||
return;
|
||||
}
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.recordKey = function (keyNode, messageNode) {
|
||||
var text = keyNode.getText();
|
||||
var occurences = this.usedKeys[text];
|
||||
if (!occurences) {
|
||||
occurences = [];
|
||||
this.usedKeys[text] = occurences;
|
||||
}
|
||||
if (messageNode) {
|
||||
if (occurences.some(function (pair) { return pair.message ? pair.message.getText() === messageNode.getText() : false; })) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
occurences.push({ key: keyNode, message: messageNode });
|
||||
};
|
||||
NoUnexternalizedStringsRuleWalker.prototype.findDescribingParent = function (node) {
|
||||
var parent;
|
||||
while ((parent = node.parent)) {
|
||||
var kind = parent.kind;
|
||||
if (kind === ts.SyntaxKind.CallExpression) {
|
||||
var callExpression = parent;
|
||||
return { callInfo: { callExpression: callExpression, argIndex: callExpression.arguments.indexOf(node) } };
|
||||
}
|
||||
else if (kind === ts.SyntaxKind.ImportEqualsDeclaration || kind === ts.SyntaxKind.ImportDeclaration || kind === ts.SyntaxKind.ExportDeclaration) {
|
||||
return { ignoreUsage: true };
|
||||
}
|
||||
else if (kind === ts.SyntaxKind.VariableDeclaration || kind === ts.SyntaxKind.FunctionDeclaration || kind === ts.SyntaxKind.PropertyDeclaration
|
||||
|| kind === ts.SyntaxKind.MethodDeclaration || kind === ts.SyntaxKind.VariableDeclarationList || kind === ts.SyntaxKind.InterfaceDeclaration
|
||||
|| kind === ts.SyntaxKind.ClassDeclaration || kind === ts.SyntaxKind.EnumDeclaration || kind === ts.SyntaxKind.ModuleDeclaration
|
||||
|| kind === ts.SyntaxKind.TypeAliasDeclaration || kind === ts.SyntaxKind.SourceFile) {
|
||||
return null;
|
||||
}
|
||||
node = parent;
|
||||
}
|
||||
};
|
||||
return NoUnexternalizedStringsRuleWalker;
|
||||
}(Lint.RuleWalker));
|
||||
NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE = '"';
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"lib": [
|
||||
"es2015"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -1,213 +1,212 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var es = require("event-stream");
|
||||
var debounce = require("debounce");
|
||||
var _filter = require("gulp-filter");
|
||||
var rename = require("gulp-rename");
|
||||
var _ = require("underscore");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _rimraf = require("rimraf");
|
||||
var git = require("./git");
|
||||
var VinylFile = require("vinyl");
|
||||
var NoCancellationToken = { isCancellationRequested: function () { return false; } };
|
||||
function incremental(streamProvider, initial, supportsCancellation) {
|
||||
var input = es.through();
|
||||
var output = es.through();
|
||||
var state = 'idle';
|
||||
var buffer = Object.create(null);
|
||||
var token = !supportsCancellation ? null : { isCancellationRequested: function () { return Object.keys(buffer).length > 0; } };
|
||||
var run = function (input, isCancellable) {
|
||||
state = 'running';
|
||||
var stream = !supportsCancellation ? streamProvider() : streamProvider(isCancellable ? token : NoCancellationToken);
|
||||
input
|
||||
.pipe(stream)
|
||||
.pipe(es.through(null, function () {
|
||||
state = 'idle';
|
||||
eventuallyRun();
|
||||
}))
|
||||
.pipe(output);
|
||||
};
|
||||
if (initial) {
|
||||
run(initial, false);
|
||||
}
|
||||
var eventuallyRun = debounce(function () {
|
||||
var paths = Object.keys(buffer);
|
||||
if (paths.length === 0) {
|
||||
return;
|
||||
}
|
||||
var data = paths.map(function (path) { return buffer[path]; });
|
||||
buffer = Object.create(null);
|
||||
run(es.readArray(data), true);
|
||||
}, 500);
|
||||
input.on('data', function (f) {
|
||||
buffer[f.path] = f;
|
||||
if (state === 'idle') {
|
||||
eventuallyRun();
|
||||
}
|
||||
});
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.incremental = incremental;
|
||||
function fixWin32DirectoryPermissions() {
|
||||
if (!/win32/.test(process.platform)) {
|
||||
return es.through();
|
||||
}
|
||||
return es.mapSync(function (f) {
|
||||
if (f.stat && f.stat.isDirectory && f.stat.isDirectory()) {
|
||||
f.stat.mode = 16877;
|
||||
}
|
||||
return f;
|
||||
});
|
||||
}
|
||||
exports.fixWin32DirectoryPermissions = fixWin32DirectoryPermissions;
|
||||
function setExecutableBit(pattern) {
|
||||
var setBit = es.mapSync(function (f) {
|
||||
f.stat.mode = 33261;
|
||||
return f;
|
||||
});
|
||||
if (!pattern) {
|
||||
return setBit;
|
||||
}
|
||||
var input = es.through();
|
||||
var filter = _filter(pattern, { restore: true });
|
||||
var output = input
|
||||
.pipe(filter)
|
||||
.pipe(setBit)
|
||||
.pipe(filter.restore);
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.setExecutableBit = setExecutableBit;
|
||||
function toFileUri(filePath) {
|
||||
var match = filePath.match(/^([a-z])\:(.*)$/i);
|
||||
if (match) {
|
||||
filePath = '/' + match[1].toUpperCase() + ':' + match[2];
|
||||
}
|
||||
return 'file://' + filePath.replace(/\\/g, '/');
|
||||
}
|
||||
exports.toFileUri = toFileUri;
|
||||
function skipDirectories() {
|
||||
return es.mapSync(function (f) {
|
||||
if (!f.isDirectory()) {
|
||||
return f;
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.skipDirectories = skipDirectories;
|
||||
function cleanNodeModule(name, excludes, includes) {
|
||||
var toGlob = function (path) { return '**/node_modules/' + name + (path ? '/' + path : ''); };
|
||||
var negate = function (str) { return '!' + str; };
|
||||
var allFilter = _filter(toGlob('**'), { restore: true });
|
||||
var globs = [toGlob('**')].concat(excludes.map(_.compose(negate, toGlob)));
|
||||
var input = es.through();
|
||||
var nodeModuleInput = input.pipe(allFilter);
|
||||
var output = nodeModuleInput.pipe(_filter(globs));
|
||||
if (includes) {
|
||||
var includeGlobs = includes.map(toGlob);
|
||||
output = es.merge(output, nodeModuleInput.pipe(_filter(includeGlobs)));
|
||||
}
|
||||
output = output.pipe(allFilter.restore);
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.cleanNodeModule = cleanNodeModule;
|
||||
function loadSourcemaps() {
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(es.map(function (f, cb) {
|
||||
if (f.sourceMap) {
|
||||
cb(null, f);
|
||||
return;
|
||||
}
|
||||
if (!f.contents) {
|
||||
cb(new Error('empty file'));
|
||||
return;
|
||||
}
|
||||
var contents = f.contents.toString('utf8');
|
||||
var reg = /\/\/# sourceMappingURL=(.*)$/g;
|
||||
var lastMatch = null, match = null;
|
||||
while (match = reg.exec(contents)) {
|
||||
lastMatch = match;
|
||||
}
|
||||
if (!lastMatch) {
|
||||
f.sourceMap = {
|
||||
version: 3,
|
||||
names: [],
|
||||
mappings: '',
|
||||
sources: [f.relative.replace(/\//g, '/')],
|
||||
sourcesContent: [contents]
|
||||
};
|
||||
cb(null, f);
|
||||
return;
|
||||
}
|
||||
f.contents = new Buffer(contents.replace(/\/\/# sourceMappingURL=(.*)$/g, ''), 'utf8');
|
||||
fs.readFile(path.join(path.dirname(f.path), lastMatch[1]), 'utf8', function (err, contents) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
f.sourceMap = JSON.parse(contents);
|
||||
cb(null, f);
|
||||
});
|
||||
}));
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.loadSourcemaps = loadSourcemaps;
|
||||
function stripSourceMappingURL() {
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(es.mapSync(function (f) {
|
||||
var contents = f.contents.toString('utf8');
|
||||
f.contents = new Buffer(contents.replace(/\n\/\/# sourceMappingURL=(.*)$/gm, ''), 'utf8');
|
||||
return f;
|
||||
}));
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.stripSourceMappingURL = stripSourceMappingURL;
|
||||
function rimraf(dir) {
|
||||
var retries = 0;
|
||||
var retry = function (cb) {
|
||||
_rimraf(dir, { maxBusyTries: 1 }, function (err) {
|
||||
if (!err) {
|
||||
return cb();
|
||||
}
|
||||
;
|
||||
if (err.code === 'ENOTEMPTY' && ++retries < 5) {
|
||||
return setTimeout(function () { return retry(cb); }, 10);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
return function (cb) { return retry(cb); };
|
||||
}
|
||||
exports.rimraf = rimraf;
|
||||
function getVersion(root) {
|
||||
var version = process.env['BUILD_SOURCEVERSION'];
|
||||
if (!version || !/^[0-9a-f]{40}$/i.test(version)) {
|
||||
version = git.getVersion(root);
|
||||
}
|
||||
return version;
|
||||
}
|
||||
exports.getVersion = getVersion;
|
||||
function rebase(count) {
|
||||
return rename(function (f) {
|
||||
var parts = f.dirname.split(/[\/\\]/);
|
||||
f.dirname = parts.slice(count).join(path.sep);
|
||||
});
|
||||
}
|
||||
exports.rebase = rebase;
|
||||
function filter(fn) {
|
||||
var result = es.through(function (data) {
|
||||
if (fn(data)) {
|
||||
this.emit('data', data);
|
||||
}
|
||||
else {
|
||||
result.restore.push(data);
|
||||
}
|
||||
});
|
||||
result.restore = es.through();
|
||||
return result;
|
||||
}
|
||||
exports.filter = filter;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
var es = require("event-stream");
|
||||
var debounce = require("debounce");
|
||||
var _filter = require("gulp-filter");
|
||||
var rename = require("gulp-rename");
|
||||
var _ = require("underscore");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
var _rimraf = require("rimraf");
|
||||
var git = require("./git");
|
||||
var VinylFile = require("vinyl");
|
||||
var NoCancellationToken = { isCancellationRequested: function () { return false; } };
|
||||
function incremental(streamProvider, initial, supportsCancellation) {
|
||||
var input = es.through();
|
||||
var output = es.through();
|
||||
var state = 'idle';
|
||||
var buffer = Object.create(null);
|
||||
var token = !supportsCancellation ? null : { isCancellationRequested: function () { return Object.keys(buffer).length > 0; } };
|
||||
var run = function (input, isCancellable) {
|
||||
state = 'running';
|
||||
var stream = !supportsCancellation ? streamProvider() : streamProvider(isCancellable ? token : NoCancellationToken);
|
||||
input
|
||||
.pipe(stream)
|
||||
.pipe(es.through(null, function () {
|
||||
state = 'idle';
|
||||
eventuallyRun();
|
||||
}))
|
||||
.pipe(output);
|
||||
};
|
||||
if (initial) {
|
||||
run(initial, false);
|
||||
}
|
||||
var eventuallyRun = debounce(function () {
|
||||
var paths = Object.keys(buffer);
|
||||
if (paths.length === 0) {
|
||||
return;
|
||||
}
|
||||
var data = paths.map(function (path) { return buffer[path]; });
|
||||
buffer = Object.create(null);
|
||||
run(es.readArray(data), true);
|
||||
}, 500);
|
||||
input.on('data', function (f) {
|
||||
buffer[f.path] = f;
|
||||
if (state === 'idle') {
|
||||
eventuallyRun();
|
||||
}
|
||||
});
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.incremental = incremental;
|
||||
function fixWin32DirectoryPermissions() {
|
||||
if (!/win32/.test(process.platform)) {
|
||||
return es.through();
|
||||
}
|
||||
return es.mapSync(function (f) {
|
||||
if (f.stat && f.stat.isDirectory && f.stat.isDirectory()) {
|
||||
f.stat.mode = 16877;
|
||||
}
|
||||
return f;
|
||||
});
|
||||
}
|
||||
exports.fixWin32DirectoryPermissions = fixWin32DirectoryPermissions;
|
||||
function setExecutableBit(pattern) {
|
||||
var setBit = es.mapSync(function (f) {
|
||||
f.stat.mode = 33261;
|
||||
return f;
|
||||
});
|
||||
if (!pattern) {
|
||||
return setBit;
|
||||
}
|
||||
var input = es.through();
|
||||
var filter = _filter(pattern, { restore: true });
|
||||
var output = input
|
||||
.pipe(filter)
|
||||
.pipe(setBit)
|
||||
.pipe(filter.restore);
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.setExecutableBit = setExecutableBit;
|
||||
function toFileUri(filePath) {
|
||||
var match = filePath.match(/^([a-z])\:(.*)$/i);
|
||||
if (match) {
|
||||
filePath = '/' + match[1].toUpperCase() + ':' + match[2];
|
||||
}
|
||||
return 'file://' + filePath.replace(/\\/g, '/');
|
||||
}
|
||||
exports.toFileUri = toFileUri;
|
||||
function skipDirectories() {
|
||||
return es.mapSync(function (f) {
|
||||
if (!f.isDirectory()) {
|
||||
return f;
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.skipDirectories = skipDirectories;
|
||||
function cleanNodeModule(name, excludes, includes) {
|
||||
var toGlob = function (path) { return '**/node_modules/' + name + (path ? '/' + path : ''); };
|
||||
var negate = function (str) { return '!' + str; };
|
||||
var allFilter = _filter(toGlob('**'), { restore: true });
|
||||
var globs = [toGlob('**')].concat(excludes.map(_.compose(negate, toGlob)));
|
||||
var input = es.through();
|
||||
var nodeModuleInput = input.pipe(allFilter);
|
||||
var output = nodeModuleInput.pipe(_filter(globs));
|
||||
if (includes) {
|
||||
var includeGlobs = includes.map(toGlob);
|
||||
output = es.merge(output, nodeModuleInput.pipe(_filter(includeGlobs)));
|
||||
}
|
||||
output = output.pipe(allFilter.restore);
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.cleanNodeModule = cleanNodeModule;
|
||||
function loadSourcemaps() {
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(es.map(function (f, cb) {
|
||||
if (f.sourceMap) {
|
||||
cb(null, f);
|
||||
return;
|
||||
}
|
||||
if (!f.contents) {
|
||||
cb(new Error('empty file'));
|
||||
return;
|
||||
}
|
||||
var contents = f.contents.toString('utf8');
|
||||
var reg = /\/\/# sourceMappingURL=(.*)$/g;
|
||||
var lastMatch = null, match = null;
|
||||
while (match = reg.exec(contents)) {
|
||||
lastMatch = match;
|
||||
}
|
||||
if (!lastMatch) {
|
||||
f.sourceMap = {
|
||||
version: 3,
|
||||
names: [],
|
||||
mappings: '',
|
||||
sources: [f.relative.replace(/\//g, '/')],
|
||||
sourcesContent: [contents]
|
||||
};
|
||||
cb(null, f);
|
||||
return;
|
||||
}
|
||||
f.contents = new Buffer(contents.replace(/\/\/# sourceMappingURL=(.*)$/g, ''), 'utf8');
|
||||
fs.readFile(path.join(path.dirname(f.path), lastMatch[1]), 'utf8', function (err, contents) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
f.sourceMap = JSON.parse(contents);
|
||||
cb(null, f);
|
||||
});
|
||||
}));
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.loadSourcemaps = loadSourcemaps;
|
||||
function stripSourceMappingURL() {
|
||||
var input = es.through();
|
||||
var output = input
|
||||
.pipe(es.mapSync(function (f) {
|
||||
var contents = f.contents.toString('utf8');
|
||||
f.contents = new Buffer(contents.replace(/\n\/\/# sourceMappingURL=(.*)$/gm, ''), 'utf8');
|
||||
return f;
|
||||
}));
|
||||
return es.duplex(input, output);
|
||||
}
|
||||
exports.stripSourceMappingURL = stripSourceMappingURL;
|
||||
function rimraf(dir) {
|
||||
var retries = 0;
|
||||
var retry = function (cb) {
|
||||
_rimraf(dir, { maxBusyTries: 1 }, function (err) {
|
||||
if (!err) {
|
||||
return cb();
|
||||
}
|
||||
;
|
||||
if (err.code === 'ENOTEMPTY' && ++retries < 5) {
|
||||
return setTimeout(function () { return retry(cb); }, 10);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
return function (cb) { return retry(cb); };
|
||||
}
|
||||
exports.rimraf = rimraf;
|
||||
function getVersion(root) {
|
||||
var version = process.env['BUILD_SOURCEVERSION'];
|
||||
if (!version || !/^[0-9a-f]{40}$/i.test(version)) {
|
||||
version = git.getVersion(root);
|
||||
}
|
||||
return version;
|
||||
}
|
||||
exports.getVersion = getVersion;
|
||||
function rebase(count) {
|
||||
return rename(function (f) {
|
||||
var parts = f.dirname.split(/[\/\\]/);
|
||||
f.dirname = parts.slice(count).join(path.sep);
|
||||
});
|
||||
}
|
||||
exports.rebase = rebase;
|
||||
function filter(fn) {
|
||||
var result = es.through(function (data) {
|
||||
if (fn(data)) {
|
||||
this.emit('data', data);
|
||||
}
|
||||
else {
|
||||
result.restore.push(data);
|
||||
}
|
||||
});
|
||||
result.restore = es.through();
|
||||
return result;
|
||||
}
|
||||
exports.filter = filter;
|
||||
|
|
|
@ -1,346 +1,343 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* 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 });
|
||||
var fs = require("fs");
|
||||
var ts = require("typescript");
|
||||
var path = require("path");
|
||||
var util = require('gulp-util');
|
||||
function log(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log.apply(util, [util.colors.cyan('[monaco.d.ts]'), message].concat(rest));
|
||||
}
|
||||
var SRC = path.join(__dirname, '../../src');
|
||||
var OUT_ROOT = path.join(__dirname, '../../');
|
||||
var RECIPE_PATH = path.join(__dirname, './monaco.d.ts.recipe');
|
||||
var DECLARATION_PATH = path.join(__dirname, '../../src/vs/monaco.d.ts');
|
||||
var CURRENT_PROCESSING_RULE = '';
|
||||
function logErr(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log(util.colors.red('[monaco.d.ts]'), 'WHILE HANDLING RULE: ', CURRENT_PROCESSING_RULE);
|
||||
util.log.apply(util, [util.colors.red('[monaco.d.ts]'), message].concat(rest));
|
||||
}
|
||||
function moduleIdToPath(out, moduleId) {
|
||||
if (/\.d\.ts/.test(moduleId)) {
|
||||
return path.join(SRC, moduleId);
|
||||
}
|
||||
return path.join(OUT_ROOT, out, moduleId) + '.d.ts';
|
||||
}
|
||||
var SOURCE_FILE_MAP = {};
|
||||
function getSourceFile(out, inputFiles, moduleId) {
|
||||
if (!SOURCE_FILE_MAP[moduleId]) {
|
||||
var filePath = path.normalize(moduleIdToPath(out, moduleId));
|
||||
if (!inputFiles.hasOwnProperty(filePath)) {
|
||||
logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp');
|
||||
return null;
|
||||
}
|
||||
var fileContents = inputFiles[filePath];
|
||||
var sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5);
|
||||
SOURCE_FILE_MAP[moduleId] = sourceFile;
|
||||
}
|
||||
return SOURCE_FILE_MAP[moduleId];
|
||||
}
|
||||
function isDeclaration(a) {
|
||||
return (a.kind === ts.SyntaxKind.InterfaceDeclaration
|
||||
|| a.kind === ts.SyntaxKind.EnumDeclaration
|
||||
|| a.kind === ts.SyntaxKind.ClassDeclaration
|
||||
|| a.kind === ts.SyntaxKind.TypeAliasDeclaration
|
||||
|| a.kind === ts.SyntaxKind.FunctionDeclaration
|
||||
|| a.kind === ts.SyntaxKind.ModuleDeclaration);
|
||||
}
|
||||
function visitTopLevelDeclarations(sourceFile, visitor) {
|
||||
var stop = false;
|
||||
var visit = function (node) {
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
switch (node.kind) {
|
||||
case ts.SyntaxKind.InterfaceDeclaration:
|
||||
case ts.SyntaxKind.EnumDeclaration:
|
||||
case ts.SyntaxKind.ClassDeclaration:
|
||||
case ts.SyntaxKind.VariableStatement:
|
||||
case ts.SyntaxKind.TypeAliasDeclaration:
|
||||
case ts.SyntaxKind.FunctionDeclaration:
|
||||
case ts.SyntaxKind.ModuleDeclaration:
|
||||
stop = visitor(node);
|
||||
}
|
||||
// if (node.kind !== ts.SyntaxKind.SourceFile) {
|
||||
// if (getNodeText(sourceFile, node).indexOf('SymbolKind') >= 0) {
|
||||
// console.log('FOUND TEXT IN NODE: ' + ts.SyntaxKind[node.kind]);
|
||||
// console.log(getNodeText(sourceFile, node));
|
||||
// }
|
||||
// }
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(sourceFile);
|
||||
}
|
||||
function getAllTopLevelDeclarations(sourceFile) {
|
||||
var all = [];
|
||||
visitTopLevelDeclarations(sourceFile, function (node) {
|
||||
if (node.kind === ts.SyntaxKind.InterfaceDeclaration || node.kind === ts.SyntaxKind.ClassDeclaration || node.kind === ts.SyntaxKind.ModuleDeclaration) {
|
||||
var interfaceDeclaration = node;
|
||||
var triviaStart = interfaceDeclaration.pos;
|
||||
var triviaEnd = interfaceDeclaration.name.pos;
|
||||
var triviaText = getNodeText(sourceFile, { pos: triviaStart, end: triviaEnd });
|
||||
// // let nodeText = getNodeText(sourceFile, node);
|
||||
// if (getNodeText(sourceFile, node).indexOf('SymbolKind') >= 0) {
|
||||
// console.log('TRIVIA: ', triviaText);
|
||||
// }
|
||||
if (triviaText.indexOf('@internal') === -1) {
|
||||
all.push(node);
|
||||
}
|
||||
}
|
||||
else {
|
||||
var nodeText = getNodeText(sourceFile, node);
|
||||
if (nodeText.indexOf('@internal') === -1) {
|
||||
all.push(node);
|
||||
}
|
||||
}
|
||||
return false /*continue*/;
|
||||
});
|
||||
return all;
|
||||
}
|
||||
function getTopLevelDeclaration(sourceFile, typeName) {
|
||||
var result = null;
|
||||
visitTopLevelDeclarations(sourceFile, function (node) {
|
||||
if (isDeclaration(node)) {
|
||||
if (node.name.text === typeName) {
|
||||
result = node;
|
||||
return true /*stop*/;
|
||||
}
|
||||
return false /*continue*/;
|
||||
}
|
||||
// node is ts.VariableStatement
|
||||
if (getNodeText(sourceFile, node).indexOf(typeName) >= 0) {
|
||||
result = node;
|
||||
return true /*stop*/;
|
||||
}
|
||||
return false /*continue*/;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
function getNodeText(sourceFile, node) {
|
||||
return sourceFile.getFullText().substring(node.pos, node.end);
|
||||
}
|
||||
function getMassagedTopLevelDeclarationText(sourceFile, declaration) {
|
||||
var result = getNodeText(sourceFile, declaration);
|
||||
// if (result.indexOf('MonacoWorker') >= 0) {
|
||||
// console.log('here!');
|
||||
// // console.log(ts.SyntaxKind[declaration.kind]);
|
||||
// }
|
||||
if (declaration.kind === ts.SyntaxKind.InterfaceDeclaration || declaration.kind === ts.SyntaxKind.ClassDeclaration) {
|
||||
var interfaceDeclaration = declaration;
|
||||
var members = interfaceDeclaration.members;
|
||||
members.forEach(function (member) {
|
||||
try {
|
||||
var memberText = getNodeText(sourceFile, member);
|
||||
if (memberText.indexOf('@internal') >= 0 || memberText.indexOf('private') >= 0) {
|
||||
// console.log('BEFORE: ', result);
|
||||
result = result.replace(memberText, '');
|
||||
// console.log('AFTER: ', result);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
// life..
|
||||
}
|
||||
});
|
||||
}
|
||||
result = result.replace(/export default/g, 'export');
|
||||
result = result.replace(/export declare/g, 'export');
|
||||
return result;
|
||||
}
|
||||
function format(text) {
|
||||
var options = getDefaultOptions();
|
||||
// Parse the source text
|
||||
var sourceFile = ts.createSourceFile('file.ts', text, ts.ScriptTarget.Latest, /*setParentPointers*/ true);
|
||||
// Get the formatting edits on the input sources
|
||||
var edits = ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options);
|
||||
// Apply the edits on the input code
|
||||
return applyEdits(text, edits);
|
||||
function getRuleProvider(options) {
|
||||
// Share this between multiple formatters using the same options.
|
||||
// This represents the bulk of the space the formatter uses.
|
||||
var ruleProvider = new ts.formatting.RulesProvider();
|
||||
ruleProvider.ensureUpToDate(options);
|
||||
return ruleProvider;
|
||||
}
|
||||
function applyEdits(text, edits) {
|
||||
// Apply edits in reverse on the existing text
|
||||
var result = text;
|
||||
for (var i = edits.length - 1; i >= 0; i--) {
|
||||
var change = edits[i];
|
||||
var head = result.slice(0, change.span.start);
|
||||
var tail = result.slice(change.span.start + change.span.length);
|
||||
result = head + change.newText + tail;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function getDefaultOptions() {
|
||||
return {
|
||||
indentSize: 4,
|
||||
tabSize: 4,
|
||||
newLineCharacter: '\r\n',
|
||||
convertTabsToSpaces: true,
|
||||
indentStyle: ts.IndentStyle.Block,
|
||||
insertSpaceAfterCommaDelimiter: true,
|
||||
insertSpaceAfterSemicolonInForStatements: true,
|
||||
insertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: true,
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: true,
|
||||
placeOpenBraceOnNewLineForFunctions: false,
|
||||
placeOpenBraceOnNewLineForControlBlocks: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
function createReplacer(data) {
|
||||
data = data || '';
|
||||
var rawDirectives = data.split(';');
|
||||
var directives = [];
|
||||
rawDirectives.forEach(function (rawDirective) {
|
||||
if (rawDirective.length === 0) {
|
||||
return;
|
||||
}
|
||||
var pieces = rawDirective.split('=>');
|
||||
var findStr = pieces[0];
|
||||
var replaceStr = pieces[1];
|
||||
findStr = findStr.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&');
|
||||
findStr = '\\b' + findStr + '\\b';
|
||||
directives.push([new RegExp(findStr, 'g'), replaceStr]);
|
||||
});
|
||||
return function (str) {
|
||||
for (var i = 0; i < directives.length; i++) {
|
||||
str = str.replace(directives[i][0], directives[i][1]);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
}
|
||||
function generateDeclarationFile(out, inputFiles, recipe) {
|
||||
var lines = recipe.split(/\r\n|\n|\r/);
|
||||
var result = [];
|
||||
lines.forEach(function (line) {
|
||||
var m1 = line.match(/^\s*#include\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m1) {
|
||||
CURRENT_PROCESSING_RULE = line;
|
||||
var moduleId = m1[1];
|
||||
var sourceFile_1 = getSourceFile(out, inputFiles, moduleId);
|
||||
if (!sourceFile_1) {
|
||||
return;
|
||||
}
|
||||
var replacer_1 = createReplacer(m1[2]);
|
||||
var typeNames = m1[3].split(/,/);
|
||||
typeNames.forEach(function (typeName) {
|
||||
typeName = typeName.trim();
|
||||
if (typeName.length === 0) {
|
||||
return;
|
||||
}
|
||||
var declaration = getTopLevelDeclaration(sourceFile_1, typeName);
|
||||
if (!declaration) {
|
||||
logErr('Cannot find type ' + typeName);
|
||||
return;
|
||||
}
|
||||
result.push(replacer_1(getMassagedTopLevelDeclarationText(sourceFile_1, declaration)));
|
||||
});
|
||||
return;
|
||||
}
|
||||
var m2 = line.match(/^\s*#includeAll\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m2) {
|
||||
CURRENT_PROCESSING_RULE = line;
|
||||
var moduleId = m2[1];
|
||||
var sourceFile_2 = getSourceFile(out, inputFiles, moduleId);
|
||||
if (!sourceFile_2) {
|
||||
return;
|
||||
}
|
||||
var replacer_2 = createReplacer(m2[2]);
|
||||
var typeNames = m2[3].split(/,/);
|
||||
var typesToExcludeMap_1 = {};
|
||||
var typesToExcludeArr_1 = [];
|
||||
typeNames.forEach(function (typeName) {
|
||||
typeName = typeName.trim();
|
||||
if (typeName.length === 0) {
|
||||
return;
|
||||
}
|
||||
typesToExcludeMap_1[typeName] = true;
|
||||
typesToExcludeArr_1.push(typeName);
|
||||
});
|
||||
getAllTopLevelDeclarations(sourceFile_2).forEach(function (declaration) {
|
||||
if (isDeclaration(declaration)) {
|
||||
if (typesToExcludeMap_1[declaration.name.text]) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// node is ts.VariableStatement
|
||||
var nodeText = getNodeText(sourceFile_2, declaration);
|
||||
for (var i = 0; i < typesToExcludeArr_1.length; i++) {
|
||||
if (nodeText.indexOf(typesToExcludeArr_1[i]) >= 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
result.push(replacer_2(getMassagedTopLevelDeclarationText(sourceFile_2, declaration)));
|
||||
});
|
||||
return;
|
||||
}
|
||||
result.push(line);
|
||||
});
|
||||
var resultTxt = result.join('\n');
|
||||
resultTxt = resultTxt.replace(/\bURI\b/g, 'Uri');
|
||||
resultTxt = resultTxt.replace(/\bEvent</g, 'IEvent<');
|
||||
resultTxt = resultTxt.replace(/\bTPromise</g, 'Promise<');
|
||||
resultTxt = format(resultTxt);
|
||||
resultTxt = resultTxt.replace(/\r\n/g, '\n');
|
||||
return resultTxt;
|
||||
}
|
||||
function getFilesToWatch(out) {
|
||||
var recipe = fs.readFileSync(RECIPE_PATH).toString();
|
||||
var lines = recipe.split(/\r\n|\n|\r/);
|
||||
var result = [];
|
||||
lines.forEach(function (line) {
|
||||
var m1 = line.match(/^\s*#include\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m1) {
|
||||
var moduleId = m1[1];
|
||||
result.push(moduleIdToPath(out, moduleId));
|
||||
return;
|
||||
}
|
||||
var m2 = line.match(/^\s*#includeAll\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m2) {
|
||||
var moduleId = m2[1];
|
||||
result.push(moduleIdToPath(out, moduleId));
|
||||
return;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
exports.getFilesToWatch = getFilesToWatch;
|
||||
function run(out, inputFiles) {
|
||||
log('Starting monaco.d.ts generation');
|
||||
SOURCE_FILE_MAP = {};
|
||||
var recipe = fs.readFileSync(RECIPE_PATH).toString();
|
||||
var result = generateDeclarationFile(out, inputFiles, recipe);
|
||||
var currentContent = fs.readFileSync(DECLARATION_PATH).toString();
|
||||
log('Finished monaco.d.ts generation');
|
||||
return {
|
||||
content: result,
|
||||
filePath: DECLARATION_PATH,
|
||||
isTheSame: currentContent === result
|
||||
};
|
||||
}
|
||||
exports.run = run;
|
||||
function complainErrors() {
|
||||
logErr('Not running monaco.d.ts generation due to compile errors');
|
||||
}
|
||||
exports.complainErrors = complainErrors;
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
"use strict";
|
||||
var fs = require("fs");
|
||||
var ts = require("typescript");
|
||||
var path = require("path");
|
||||
var util = require('gulp-util');
|
||||
function log(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log.apply(util, [util.colors.cyan('[monaco.d.ts]'), message].concat(rest));
|
||||
}
|
||||
var SRC = path.join(__dirname, '../../src');
|
||||
var OUT_ROOT = path.join(__dirname, '../../');
|
||||
var RECIPE_PATH = path.join(__dirname, './monaco.d.ts.recipe');
|
||||
var DECLARATION_PATH = path.join(__dirname, '../../src/vs/monaco.d.ts');
|
||||
var CURRENT_PROCESSING_RULE = '';
|
||||
function logErr(message) {
|
||||
var rest = [];
|
||||
for (var _i = 1; _i < arguments.length; _i++) {
|
||||
rest[_i - 1] = arguments[_i];
|
||||
}
|
||||
util.log(util.colors.red('[monaco.d.ts]'), 'WHILE HANDLING RULE: ', CURRENT_PROCESSING_RULE);
|
||||
util.log.apply(util, [util.colors.red('[monaco.d.ts]'), message].concat(rest));
|
||||
}
|
||||
function moduleIdToPath(out, moduleId) {
|
||||
if (/\.d\.ts/.test(moduleId)) {
|
||||
return path.join(SRC, moduleId);
|
||||
}
|
||||
return path.join(OUT_ROOT, out, moduleId) + '.d.ts';
|
||||
}
|
||||
var SOURCE_FILE_MAP = {};
|
||||
function getSourceFile(out, inputFiles, moduleId) {
|
||||
if (!SOURCE_FILE_MAP[moduleId]) {
|
||||
var filePath = path.normalize(moduleIdToPath(out, moduleId));
|
||||
if (!inputFiles.hasOwnProperty(filePath)) {
|
||||
logErr('CANNOT FIND FILE ' + filePath + '. YOU MIGHT NEED TO RESTART gulp');
|
||||
return null;
|
||||
}
|
||||
var fileContents = inputFiles[filePath];
|
||||
var sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ES5);
|
||||
SOURCE_FILE_MAP[moduleId] = sourceFile;
|
||||
}
|
||||
return SOURCE_FILE_MAP[moduleId];
|
||||
}
|
||||
function isDeclaration(a) {
|
||||
return (a.kind === ts.SyntaxKind.InterfaceDeclaration
|
||||
|| a.kind === ts.SyntaxKind.EnumDeclaration
|
||||
|| a.kind === ts.SyntaxKind.ClassDeclaration
|
||||
|| a.kind === ts.SyntaxKind.TypeAliasDeclaration
|
||||
|| a.kind === ts.SyntaxKind.FunctionDeclaration
|
||||
|| a.kind === ts.SyntaxKind.ModuleDeclaration);
|
||||
}
|
||||
function visitTopLevelDeclarations(sourceFile, visitor) {
|
||||
var stop = false;
|
||||
var visit = function (node) {
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
switch (node.kind) {
|
||||
case ts.SyntaxKind.InterfaceDeclaration:
|
||||
case ts.SyntaxKind.EnumDeclaration:
|
||||
case ts.SyntaxKind.ClassDeclaration:
|
||||
case ts.SyntaxKind.VariableStatement:
|
||||
case ts.SyntaxKind.TypeAliasDeclaration:
|
||||
case ts.SyntaxKind.FunctionDeclaration:
|
||||
case ts.SyntaxKind.ModuleDeclaration:
|
||||
stop = visitor(node);
|
||||
}
|
||||
// if (node.kind !== ts.SyntaxKind.SourceFile) {
|
||||
// if (getNodeText(sourceFile, node).indexOf('SymbolKind') >= 0) {
|
||||
// console.log('FOUND TEXT IN NODE: ' + ts.SyntaxKind[node.kind]);
|
||||
// console.log(getNodeText(sourceFile, node));
|
||||
// }
|
||||
// }
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(sourceFile);
|
||||
}
|
||||
function getAllTopLevelDeclarations(sourceFile) {
|
||||
var all = [];
|
||||
visitTopLevelDeclarations(sourceFile, function (node) {
|
||||
if (node.kind === ts.SyntaxKind.InterfaceDeclaration || node.kind === ts.SyntaxKind.ClassDeclaration || node.kind === ts.SyntaxKind.ModuleDeclaration) {
|
||||
var interfaceDeclaration = node;
|
||||
var triviaStart = interfaceDeclaration.pos;
|
||||
var triviaEnd = interfaceDeclaration.name.pos;
|
||||
var triviaText = getNodeText(sourceFile, { pos: triviaStart, end: triviaEnd });
|
||||
// // let nodeText = getNodeText(sourceFile, node);
|
||||
// if (getNodeText(sourceFile, node).indexOf('SymbolKind') >= 0) {
|
||||
// console.log('TRIVIA: ', triviaText);
|
||||
// }
|
||||
if (triviaText.indexOf('@internal') === -1) {
|
||||
all.push(node);
|
||||
}
|
||||
}
|
||||
else {
|
||||
var nodeText = getNodeText(sourceFile, node);
|
||||
if (nodeText.indexOf('@internal') === -1) {
|
||||
all.push(node);
|
||||
}
|
||||
}
|
||||
return false /*continue*/;
|
||||
});
|
||||
return all;
|
||||
}
|
||||
function getTopLevelDeclaration(sourceFile, typeName) {
|
||||
var result = null;
|
||||
visitTopLevelDeclarations(sourceFile, function (node) {
|
||||
if (isDeclaration(node)) {
|
||||
if (node.name.text === typeName) {
|
||||
result = node;
|
||||
return true /*stop*/;
|
||||
}
|
||||
return false /*continue*/;
|
||||
}
|
||||
// node is ts.VariableStatement
|
||||
if (getNodeText(sourceFile, node).indexOf(typeName) >= 0) {
|
||||
result = node;
|
||||
return true /*stop*/;
|
||||
}
|
||||
return false /*continue*/;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
function getNodeText(sourceFile, node) {
|
||||
return sourceFile.getFullText().substring(node.pos, node.end);
|
||||
}
|
||||
function getMassagedTopLevelDeclarationText(sourceFile, declaration) {
|
||||
var result = getNodeText(sourceFile, declaration);
|
||||
// if (result.indexOf('MonacoWorker') >= 0) {
|
||||
// console.log('here!');
|
||||
// // console.log(ts.SyntaxKind[declaration.kind]);
|
||||
// }
|
||||
if (declaration.kind === ts.SyntaxKind.InterfaceDeclaration || declaration.kind === ts.SyntaxKind.ClassDeclaration) {
|
||||
var interfaceDeclaration = declaration;
|
||||
var members = interfaceDeclaration.members;
|
||||
members.forEach(function (member) {
|
||||
try {
|
||||
var memberText = getNodeText(sourceFile, member);
|
||||
if (memberText.indexOf('@internal') >= 0 || memberText.indexOf('private') >= 0) {
|
||||
// console.log('BEFORE: ', result);
|
||||
result = result.replace(memberText, '');
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
}
|
||||
});
|
||||
}
|
||||
result = result.replace(/export default/g, 'export');
|
||||
result = result.replace(/export declare/g, 'export');
|
||||
return result;
|
||||
}
|
||||
function format(text) {
|
||||
var options = getDefaultOptions();
|
||||
// Parse the source text
|
||||
var sourceFile = ts.createSourceFile('file.ts', text, ts.ScriptTarget.Latest, /*setParentPointers*/ true);
|
||||
// Get the formatting edits on the input sources
|
||||
var edits = ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options);
|
||||
// Apply the edits on the input code
|
||||
return applyEdits(text, edits);
|
||||
function getRuleProvider(options) {
|
||||
// Share this between multiple formatters using the same options.
|
||||
// This represents the bulk of the space the formatter uses.
|
||||
var ruleProvider = new ts.formatting.RulesProvider();
|
||||
ruleProvider.ensureUpToDate(options);
|
||||
return ruleProvider;
|
||||
}
|
||||
function applyEdits(text, edits) {
|
||||
// Apply edits in reverse on the existing text
|
||||
var result = text;
|
||||
for (var i = edits.length - 1; i >= 0; i--) {
|
||||
var change = edits[i];
|
||||
var head = result.slice(0, change.span.start);
|
||||
var tail = result.slice(change.span.start + change.span.length);
|
||||
result = head + change.newText + tail;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function getDefaultOptions() {
|
||||
return {
|
||||
indentSize: 4,
|
||||
tabSize: 4,
|
||||
newLineCharacter: '\r\n',
|
||||
convertTabsToSpaces: true,
|
||||
indentStyle: ts.IndentStyle.Block,
|
||||
insertSpaceAfterCommaDelimiter: true,
|
||||
insertSpaceAfterSemicolonInForStatements: true,
|
||||
insertSpaceBeforeAndAfterBinaryOperators: true,
|
||||
insertSpaceAfterKeywordsInControlFlowStatements: true,
|
||||
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
|
||||
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: true,
|
||||
placeOpenBraceOnNewLineForFunctions: false,
|
||||
placeOpenBraceOnNewLineForControlBlocks: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
function createReplacer(data) {
|
||||
data = data || '';
|
||||
var rawDirectives = data.split(';');
|
||||
var directives = [];
|
||||
rawDirectives.forEach(function (rawDirective) {
|
||||
if (rawDirective.length === 0) {
|
||||
return;
|
||||
}
|
||||
var pieces = rawDirective.split('=>');
|
||||
var findStr = pieces[0];
|
||||
var replaceStr = pieces[1];
|
||||
findStr = findStr.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&');
|
||||
findStr = '\\b' + findStr + '\\b';
|
||||
directives.push([new RegExp(findStr, 'g'), replaceStr]);
|
||||
});
|
||||
return function (str) {
|
||||
for (var i = 0; i < directives.length; i++) {
|
||||
str = str.replace(directives[i][0], directives[i][1]);
|
||||
}
|
||||
return str;
|
||||
};
|
||||
}
|
||||
function generateDeclarationFile(out, inputFiles, recipe) {
|
||||
var lines = recipe.split(/\r\n|\n|\r/);
|
||||
var result = [];
|
||||
lines.forEach(function (line) {
|
||||
var m1 = line.match(/^\s*#include\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m1) {
|
||||
CURRENT_PROCESSING_RULE = line;
|
||||
var moduleId = m1[1];
|
||||
var sourceFile_1 = getSourceFile(out, inputFiles, moduleId);
|
||||
if (!sourceFile_1) {
|
||||
return;
|
||||
}
|
||||
var replacer_1 = createReplacer(m1[2]);
|
||||
var typeNames = m1[3].split(/,/);
|
||||
typeNames.forEach(function (typeName) {
|
||||
typeName = typeName.trim();
|
||||
if (typeName.length === 0) {
|
||||
return;
|
||||
}
|
||||
var declaration = getTopLevelDeclaration(sourceFile_1, typeName);
|
||||
if (!declaration) {
|
||||
logErr('Cannot find type ' + typeName);
|
||||
return;
|
||||
}
|
||||
result.push(replacer_1(getMassagedTopLevelDeclarationText(sourceFile_1, declaration)));
|
||||
});
|
||||
return;
|
||||
}
|
||||
var m2 = line.match(/^\s*#includeAll\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m2) {
|
||||
CURRENT_PROCESSING_RULE = line;
|
||||
var moduleId = m2[1];
|
||||
var sourceFile_2 = getSourceFile(out, inputFiles, moduleId);
|
||||
if (!sourceFile_2) {
|
||||
return;
|
||||
}
|
||||
var replacer_2 = createReplacer(m2[2]);
|
||||
var typeNames = m2[3].split(/,/);
|
||||
var typesToExcludeMap_1 = {};
|
||||
var typesToExcludeArr_1 = [];
|
||||
typeNames.forEach(function (typeName) {
|
||||
typeName = typeName.trim();
|
||||
if (typeName.length === 0) {
|
||||
return;
|
||||
}
|
||||
typesToExcludeMap_1[typeName] = true;
|
||||
typesToExcludeArr_1.push(typeName);
|
||||
});
|
||||
getAllTopLevelDeclarations(sourceFile_2).forEach(function (declaration) {
|
||||
if (isDeclaration(declaration)) {
|
||||
if (typesToExcludeMap_1[declaration.name.text]) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// node is ts.VariableStatement
|
||||
var nodeText = getNodeText(sourceFile_2, declaration);
|
||||
for (var i = 0; i < typesToExcludeArr_1.length; i++) {
|
||||
if (nodeText.indexOf(typesToExcludeArr_1[i]) >= 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
result.push(replacer_2(getMassagedTopLevelDeclarationText(sourceFile_2, declaration)));
|
||||
});
|
||||
return;
|
||||
}
|
||||
result.push(line);
|
||||
});
|
||||
var resultTxt = result.join('\n');
|
||||
resultTxt = resultTxt.replace(/\bURI\b/g, 'Uri');
|
||||
resultTxt = resultTxt.replace(/\bEvent</g, 'IEvent<');
|
||||
resultTxt = resultTxt.replace(/\bTPromise</g, 'Promise<');
|
||||
resultTxt = format(resultTxt);
|
||||
resultTxt = resultTxt.replace(/\r\n/g, '\n');
|
||||
return resultTxt;
|
||||
}
|
||||
function getFilesToWatch(out) {
|
||||
var recipe = fs.readFileSync(RECIPE_PATH).toString();
|
||||
var lines = recipe.split(/\r\n|\n|\r/);
|
||||
var result = [];
|
||||
lines.forEach(function (line) {
|
||||
var m1 = line.match(/^\s*#include\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m1) {
|
||||
var moduleId = m1[1];
|
||||
result.push(moduleIdToPath(out, moduleId));
|
||||
return;
|
||||
}
|
||||
var m2 = line.match(/^\s*#includeAll\(([^;)]*)(;[^)]*)?\)\:(.*)$/);
|
||||
if (m2) {
|
||||
var moduleId = m2[1];
|
||||
result.push(moduleIdToPath(out, moduleId));
|
||||
return;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
exports.getFilesToWatch = getFilesToWatch;
|
||||
function run(out, inputFiles) {
|
||||
log('Starting monaco.d.ts generation');
|
||||
SOURCE_FILE_MAP = {};
|
||||
var recipe = fs.readFileSync(RECIPE_PATH).toString();
|
||||
var result = generateDeclarationFile(out, inputFiles, recipe);
|
||||
var currentContent = fs.readFileSync(DECLARATION_PATH).toString();
|
||||
log('Finished monaco.d.ts generation');
|
||||
return {
|
||||
content: result,
|
||||
filePath: DECLARATION_PATH,
|
||||
isTheSame: currentContent === result
|
||||
};
|
||||
}
|
||||
exports.run = run;
|
||||
function complainErrors() {
|
||||
logErr('Not running monaco.d.ts generation due to compile errors');
|
||||
}
|
||||
exports.complainErrors = complainErrors;
|
||||
|
|
Loading…
Reference in New Issue