initial commit

This commit is contained in:
air66
2019-07-24 18:16:32 +01:00
commit 5efebf4ded
8591 changed files with 899103 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
'use strict';
var utils = require('../utils');
var rootDebug = require('../debug');
var convert = require('convert-source-map');
var stripBom = require('strip-bom-string');
var urlRegex = utils.urlRegex;
var fs = require('graceful-fs');
var path = require('path');
var unixStylePath = utils.unixStylePath;
var exceptionToString = utils.exceptionToString;
module.exports = function(options, file, fileContent) {
function loadMaps() {
var sources = {
path: '',
map: null,
content: fileContent,
preExistingComment: null
};
_getInlineSources(sources);
if (!sources.map) // ahh not inline, so try file
_getFileSources(sources);
_fixSources(sources);
return sources;
}
function _fixSources(sources) {
var debug = rootDebug.spawn('init:internals:loadMaps:_fixSources');
// fix source paths and sourceContent for imported source map
if (sources.map) {
sources.map.sourcesContent = sources.map.sourcesContent || [];
sources.map.sources.forEach(function(source, i) {
if (source.match(urlRegex)) {
sources.map.sourcesContent[i] = sources.map.sourcesContent[i] || null;
return;
}
var absPath = path.resolve(sources.path, source);
sources.map.sources[i] = unixStylePath(path.relative(file.base, absPath));
if (!sources.map.sourcesContent[i]) {
var sourceContent = null;
if (sources.map.sourceRoot) {
if (sources.map.sourceRoot.match(urlRegex)) {
sources.map.sourcesContent[i] = null;
return;
}
absPath = path.resolve(sources.path, sources.map.sourceRoot, source);
}
// if current file: use content
if (absPath === file.path) {
sourceContent = sources.content;
} else { //attempt load content from file
try {
debug(function() { return 'No source content for "' + source + '". Loading from file.'; });
sourceContent = stripBom(fs.readFileSync(absPath, 'utf8'));
} catch (e) {
debug(function() { return 'warn: source file not found: ' + absPath; });
}
}
sources.map.sourcesContent[i] = sourceContent;
}
});
// remove source map comment from source
file.contents = new Buffer(sources.content, 'utf8');
}
}
function _getInlineSources(sources) {
var debug = rootDebug.spawn('init:internals:loadMaps:_getInlineSources');
sources.preExistingComment = utils.getInlinePreExisting(sources.content);
// Try to read inline source map
sources.map = convert.fromSource(sources.content, options.largeFile);
if (!sources.map)
return sources;
sources.map = sources.map.toObject();
// sources in map are relative to the source file
sources.path = path.dirname(file.path);
if (!options.largeFile) {
debug('comment REMOVED');
sources.content = convert.removeComments(sources.content);
}
}
function _getFileSources(sources) {
var debug = rootDebug.spawn('init:internals:loadMaps:_getFileSources');
// look for source map comment referencing a source map file
var mapComment = convert.mapFileCommentRegex.exec(sources.content);
var mapFile;
if (mapComment) {
sources.preExistingComment = mapComment[1] || mapComment[2];
mapFile = path.resolve(path.dirname(file.path), sources.preExistingComment);
sources.content = convert.removeMapFileComments(sources.content);
// if no comment try map file with same name as source file
} else {
mapFile = file.path + '.map';
}
// sources in external map are relative to map file
sources.path = path.dirname(mapFile);
try {
sources.map = JSON.parse(stripBom(fs.readFileSync(mapFile, 'utf8')));
} catch (e) {
debug(function() {
return 'warn: external source map not found or invalid: ' + mapFile + ' ' + exceptionToString(e);
});
}
}
return {
loadMaps: loadMaps
};
};

141
node_modules/gulp-sourcemaps/src/init/index.js generated vendored Normal file
View File

@@ -0,0 +1,141 @@
'use strict';
var utils = require('../utils');
var unixStylePath = utils.unixStylePath;
var through = require('through2');
var path = require('path');
var acorn = require('acorn');
var SourceMapGenerator = require('source-map').SourceMapGenerator;
var css = require('css');
var initInternals = require('./index.internals');
/**
* Initialize source mapping chain
*/
function init(options) {
var debug = require('../debug').spawn('init');
function sourceMapInit(file, encoding, callback) {
/*jshint validthis:true */
// pass through if file is null or already has a source map
if (file.isNull() || file.sourceMap) {
this.push(file);
return callback();
}
if (file.isStream()) {
return callback(new Error(utils.PLUGIN_NAME + '-init: Streaming not supported'));
}
if (options === undefined) {
options = {};
}
debug(function() {
return options;
});
var fileContent = file.contents.toString();
var sourceMap, preExistingComment;
var internals = initInternals(options, file, fileContent);
if (options.loadMaps) {
var result = internals.loadMaps();
sourceMap = result.map;
fileContent = result.content;
preExistingComment = result.preExistingComment;
}
if (!sourceMap && options.identityMap) {
debug(function() { return '**identityMap option is deprecated, update to use sourcemap.identityMap stream**'; });
debug(function() {
return 'identityMap';
});
var fileType = path.extname(file.path);
var source = unixStylePath(file.relative);
var generator = new SourceMapGenerator({file: source});
if (fileType === '.js') {
var tokenizer = acorn.tokenizer(fileContent, {locations: true});
while (true) {
var token = tokenizer.getToken();
if (token.type.label === "eof")
break;
var mapping = {
original: token.loc.start,
generated: token.loc.start,
source: source
};
if (token.type.label === 'name') {
mapping.name = token.value;
}
generator.addMapping(mapping);
}
generator.setSourceContent(source, fileContent);
sourceMap = generator.toJSON();
} else if (fileType === '.css') {
debug('css');
var ast = css.parse(fileContent, {silent: true});
debug(function() {
return ast;
});
var registerTokens = function(ast) {
if (ast.position) {
generator.addMapping({original: ast.position.start, generated: ast.position.start, source: source});
}
function logAst(key, ast) {
debug(function() {
return 'key: ' + key;
});
debug(function() {
return ast[key];
});
}
for (var key in ast) {
logAst(key, ast);
if (key !== "position") {
if (Object.prototype.toString.call(ast[key]) === '[object Object]') {
registerTokens(ast[key]);
} else if (Array.isArray(ast[key])) {
debug(function() {
return "@@@@ ast[key] isArray @@@@";
});
for (var i = 0; i < ast[key].length; i++) {
registerTokens(ast[key][i]);
}
}
}
}
};
registerTokens(ast);
generator.setSourceContent(source, fileContent);
sourceMap = generator.toJSON();
}
}
if (!sourceMap) {
// Make an empty source map
sourceMap = {
version: 3,
names: [],
mappings: '',
sources: [unixStylePath(file.relative)],
sourcesContent: [fileContent]
};
}
else if(preExistingComment !== null && typeof preExistingComment !== 'undefined')
sourceMap.preExistingComment = preExistingComment;
sourceMap.file = unixStylePath(file.relative);
file.sourceMap = sourceMap;
this.push(file);
callback();
}
return through.obj(sourceMapInit);
}
module.exports = init;