This commit is contained in:
hatuhn
2019-09-13 09:45:04 +07:00
parent f14a34ba19
commit 558fb07261
16790 changed files with 0 additions and 1642370 deletions

View File

@@ -1 +0,0 @@
module.exports = require('debug-fabulous').spawnable(require('../package.json').name);

View File

@@ -1,128 +0,0 @@
'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
};
};

View File

@@ -1,141 +0,0 @@
'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;

View File

@@ -1,76 +0,0 @@
'use strict';
var path = require('path'),
detectNewline = require('detect-newline');
function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
}
var PLUGIN_NAME = require('../package.json').name;
var urlRegex = /^(https?|webpack(-[^:]+)?):\/\//;
var debug = require('./debug').spawn('utils');
/*
So reusing the same ref for a regex (with global (g)) is from a poor decision in js.
See http://stackoverflow.com/questions/10229144/bug-with-regexp-in-javascript-when-do-global-search
So we either need to use a new instance of a regex everywhere.
*/
var sourceMapUrlRegEx = function(){ return /\/\/\# sourceMappingURL\=.*/g;};
var commentFormatters = {
css: function cssCommentFormatter(preLine, newline, url) {
return preLine + "/*# sourceMappingURL=" + url + " */" + newline;
},
js: function jsCommentFormatter(preLine, newline, url) {
return preLine + "//# sourceMappingURL=" + url + newline;
},
'default': function defaultFormatter() {
return '';
}
}
var getCommentFormatter = function (file) {
var extension = file.relative.split('.').pop(),
fileContents = file.contents.toString(),
newline = detectNewline.graceful(fileContents || '');
var commentFormatter = commentFormatters.default;
if (file.sourceMap.preExistingComment) {
commentFormatter = (commentFormatters[extension] || commentFormatter).bind(undefined, '', newline)
debug(function () {
return 'preExistingComment commentFormatter ' + commentFormatter.name;
});
} else {
commentFormatter = (commentFormatters[extension] || commentFormatter).bind(undefined, newline, newline);
}
debug(function () {
return 'commentFormatter ' + commentFormatter.name;
});
return commentFormatter;
};
var getInlinePreExisting = function(fileContent){
if(sourceMapUrlRegEx().test(fileContent)){
debug(function() { return 'has preExisting'; });
return fileContent.match(sourceMapUrlRegEx())[0];
}
};
var exceptionToString = function (exception) {
return exception.message || '';
};
module.exports = {
unixStylePath: unixStylePath,
PLUGIN_NAME: PLUGIN_NAME,
urlRegex: urlRegex,
sourceMapUrlRegEx: sourceMapUrlRegEx,
getCommentFormatter: getCommentFormatter,
getInlinePreExisting: getInlinePreExisting,
exceptionToString: exceptionToString
};

View File

@@ -1,179 +0,0 @@
'use strict';
module.exports = function(destPath, options) {
var utils = require('../utils');
var unixStylePath = utils.unixStylePath;
var fs = require('graceful-fs');
var path = require('path');
var stripBom = require('strip-bom-string');
var rootDebug = require('../debug').spawn('write:internals');
rootDebug(function() { return "options"; });
rootDebug(function() { return options; });
function setSourceRoot(file) {
var debug = rootDebug.spawn('setSourceRoot');
var sourceMap = file.sourceMap;
if (typeof options.sourceRoot === 'function') {
debug(function() { return 'is function'; });
sourceMap.sourceRoot = options.sourceRoot(file);
} else {
debug(function() { return 'from options'; });
sourceMap.sourceRoot = options.sourceRoot;
}
if (sourceMap.sourceRoot === null) {
debug(function() { return 'undefined'; });
sourceMap.sourceRoot = undefined;
}
}
function mapSources(file) {
var debug = rootDebug.spawn('mapSources');
//NOTE: make sure source mapping happens after content has been loaded
if (options.mapSources && typeof options.mapSources === 'function') {
debug(function() { return '**Option is deprecated, update to use sourcemap.mapSources stream**'; });
debug(function() { return 'function'; });
file.sourceMap.sources = file.sourceMap.sources.map(function (filePath) {
return options.mapSources(filePath, file);
});
return;
}
debug(function() { return "file.path: " + file.path; });
debug(function() { return "file.cwd: " + file.cwd; });
debug(function() { return "file.base: " + file.base; });
file.sourceMap.sources = file.sourceMap.sources.map(function(filePath) {
// keep the references files like ../node_modules within the sourceRoot
debug(function() { return "filePath: " + filePath; });
if (options.mapSourcesAbsolute === true){
debug(function() { return 'mapSourcesAbsolute'; });
if (!file.dirname){
debug(function() { return '!file.dirname'; });
filePath = path.join(file.base, filePath).replace(file.cwd, '');
} else {
debug(function() { return 'file.dirname: ' + file.dirname; });
filePath = path.resolve(file.dirname, filePath).replace(file.cwd, '');
}
}
return unixStylePath(filePath);
});
}
function loadContent(file) {
var debug = rootDebug.spawn('loadContent');
var sourceMap = file.sourceMap;
if (options.includeContent) {
sourceMap.sourcesContent = sourceMap.sourcesContent || [];
// load missing source content
for (var i = 0; i < sourceMap.sources.length; i++) {
if (!sourceMap.sourcesContent[i]) {
var sourcePath = path.resolve(file.base, sourceMap.sources[i]);
try {
debug('No source content for "' + sourceMap.sources[i] + '". Loading from file.');
sourceMap.sourcesContent[i] = stripBom(fs.readFileSync(sourcePath, 'utf8'));
}
catch (e) {
debug('source file not found: ' + sourcePath);
}
}
}
} else {
delete sourceMap.sourcesContent;
}
}
function mapDestPath(file, stream) {
var debug = rootDebug.spawn('mapDestPath');
var sourceMap = file.sourceMap;
var comment,
commentFormatter = utils.getCommentFormatter(file);
if (destPath === undefined || destPath === null) {
// encode source map into comment
var base64Map = new Buffer(JSON.stringify(sourceMap)).toString('base64');
comment = commentFormatter('data:application/json;charset=' + options.charset + ';base64,' + base64Map);
} else {
var mapFile = path.join(destPath, file.relative) + '.map';
// custom map file name
if (options.mapFile && typeof options.mapFile === 'function') {
mapFile = options.mapFile(mapFile);
}
var sourceMapPath = path.join(file.base, mapFile);
// if explicit destination path is set
if (options.destPath) {
var destSourceMapPath = path.join(file.cwd, options.destPath, mapFile);
var destFilePath = path.join(file.cwd, options.destPath, file.relative);
sourceMap.file = unixStylePath(path.relative(path.dirname(destSourceMapPath), destFilePath));
if (sourceMap.sourceRoot === undefined) {
sourceMap.sourceRoot = unixStylePath(path.relative(path.dirname(destSourceMapPath), file.base));
} else if (sourceMap.sourceRoot === '' || (sourceMap.sourceRoot && sourceMap.sourceRoot[0] === '.')) {
sourceMap.sourceRoot = unixStylePath(path.join(path.relative(path.dirname(destSourceMapPath), file.base), sourceMap.sourceRoot));
}
} else {
// best effort, can be incorrect if options.destPath not set
sourceMap.file = unixStylePath(path.relative(path.dirname(sourceMapPath), file.path));
if (sourceMap.sourceRoot === '' || (sourceMap.sourceRoot && sourceMap.sourceRoot[0] === '.')) {
sourceMap.sourceRoot = unixStylePath(path.join(path.relative(path.dirname(sourceMapPath), file.base), sourceMap.sourceRoot));
}
}
var sourceMapFile;
sourceMapFile = file.clone(options.clone || {deep:false, contents:false});
sourceMapFile.path = sourceMapPath;
sourceMapFile.contents = new Buffer(JSON.stringify(sourceMap));
sourceMapFile.stat = {
isFile: function () { return true; },
isDirectory: function () { return false; },
isBlockDevice: function () { return false; },
isCharacterDevice: function () { return false; },
isSymbolicLink: function () { return false; },
isFIFO: function () { return false; },
isSocket: function () { return false; }
};
stream.push(sourceMapFile);
var sourceMapPathRelative = path.relative(path.dirname(file.path), sourceMapPath);
if (options.sourceMappingURLPrefix) {
var prefix = '';
if (typeof options.sourceMappingURLPrefix === 'function') {
prefix = options.sourceMappingURLPrefix(file);
} else {
prefix = options.sourceMappingURLPrefix;
}
sourceMapPathRelative = prefix + path.join('/', sourceMapPathRelative);
}
debug(function() { return "destPath comment"; });
comment = commentFormatter(unixStylePath(sourceMapPathRelative));
if (options.sourceMappingURL && typeof options.sourceMappingURL === 'function') {
debug(function() { return "options.sourceMappingURL comment"; });
comment = commentFormatter(options.sourceMappingURL(file));
}
}
// append source map comment
if (options.addComment){
file.contents = Buffer.concat([file.contents, new Buffer(comment)]);
}
}
return {
setSourceRoot: setSourceRoot,
loadContent: loadContent,
mapSources: mapSources,
mapDestPath: mapDestPath
};
};

View File

@@ -1,70 +0,0 @@
'use strict';
var utils = require('../utils');
var through = require('through2');
var unixStylePath = utils.unixStylePath;
var internalsInit = require('./index.internals');
/**
* Write the source map
*
* @param options options to change the way the source map is written
*
*/
function write(destPath, options) {
var debug = require('../debug').spawn('write');
debug(function() { return "destPath"; });
debug(function() { return destPath; });
debug(function() { return "original options";});
debug(function() { return options; });
if (options === undefined && typeof destPath !== 'string') {
options = destPath;
destPath = undefined;
}
options = options || {};
// set defaults for options if unset
if (options.includeContent === undefined)
options.includeContent = true;
if (options.addComment === undefined)
options.addComment = true;
if (options.charset === undefined)
options.charset = "utf8";
debug(function() { return "derrived options"; });
debug(function() { return options; });
var internals = internalsInit(destPath, options);
function sourceMapWrite(file, encoding, callback) {
/*jshint validthis:true */
if (file.isNull() || !file.sourceMap) {
this.push(file);
return callback();
}
if (file.isStream()) {
return callback(new Error(utils.PLUGIN_NAME + '-write: Streaming not supported'));
}
// fix paths if Windows style paths
file.sourceMap.file = unixStylePath(file.relative);
internals.setSourceRoot(file);
internals.loadContent(file);
internals.mapSources(file);
internals.mapDestPath(file, this);
this.push(file);
callback();
}
return through.obj(sourceMapWrite);
}
module.exports = write;