Skip to content

Commit

Permalink
Merge pull request #51 from kulshekhar/revert-50-remove-handlers-for-sms
Browse files Browse the repository at this point in the history
Revert "Removing handlers for source-map-support."
  • Loading branch information
Igmat authored Nov 3, 2016
2 parents 4c7b257 + a26cba6 commit 37c7a2e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/default-retrieve-file-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as fs from 'fs';
import { transpileIfTypescript } from './transpile-if-ts';

export function defaultRetrieveFileHandler(path) {
// Trim the path to make sure there is no extra whitespace.
path = path.trim();

// This was removed because it seems that we can't use cache while expecting correct results
// TODO: check correctness and performance with file caching
// if (path in fileContentsCache) {
// return fileContentsCache[path];
// }

var contents: string;
try {
contents = fs.readFileSync(path, 'utf8');
contents = transpileIfTypescript(path, contents);
} catch (e) {
contents = null;
}

return contents;
}
20 changes: 20 additions & 0 deletions src/default-retrieve-map-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { retrieveSourceMapURL } from './retrieve-sourceMap-url';

export function defaultRetrieveMapHandler(source) {
var sourceMappingURL = retrieveSourceMapURL(source);
if (!sourceMappingURL) return null;
var startOfSourceMap = sourceMappingURL.indexOf(',') + 1;
/// Check that there is source map
if (startOfSourceMap === 0) return null;
// Reading source map URL as a data url, because it is always inlined
var rawData = sourceMappingURL.slice(startOfSourceMap);
var sourceMapData = new Buffer(rawData, 'base64').toString();
sourceMappingURL = null; //TODO: why `null` instead of `source` as in original sourceMaphandler?

if (!sourceMapData) return null;

return {
url: sourceMappingURL,
map: sourceMapData
};
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { defaultRetrieveMapHandler } from './default-retrieve-map-handler';
import { defaultRetrieveFileHandler } from './default-retrieve-file-handler';
import * as sourceMapSupport from 'source-map-support';

export function install() {
var options: sourceMapSupport.Options = {};
options.retrieveFile = defaultRetrieveFileHandler;
options.retrieveSourceMap = defaultRetrieveMapHandler;
options.emptyCacheBetweenOperations = true; // left here only for sourceMapCache TODO: check this for correctness and performance with false velue
options['environment'] = 'node';

Expand Down
14 changes: 14 additions & 0 deletions src/retrieve-sourceMap-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defaultRetrieveFileHandler } from './default-retrieve-file-handler';

export function retrieveSourceMapURL(source) {
// Get the URL of the source map
var fileData = defaultRetrieveFileHandler(source);
// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg;
// Keep executing the search to find the *last* sourceMappingURL to avoid
// picking up sourceMappingURLs from comments, strings, etc.
var lastMatch, match;
while (match = re.exec(fileData)) lastMatch = match;
if (!lastMatch) return null;
return lastMatch[1];
};
21 changes: 21 additions & 0 deletions src/transpile-if-ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as tsc from 'typescript';
import { getTSConfig } from './utils';

export function transpileIfTypescript(path, contents) {
if (path && (path.endsWith('.tsx') || path.endsWith('.ts'))) {

let transpiled = tsc.transpileModule(contents, {
compilerOptions: addSourceMapToTSConfig(),
fileName: path
});

return transpiled.outputText;
}
return contents;
}

function addSourceMapToTSConfig() {
// if a global __TS_CONFIG__ is set, update the compiler setting to include inline SourceMap
var config = getTSConfig({ __TS_CONFIG__: global['__TS_CONFIG__'] }, true);
return config;
}

0 comments on commit 37c7a2e

Please sign in to comment.