-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from kulshekhar/revert-50-remove-handlers-for-sms
Revert "Removing handlers for source-map-support."
- Loading branch information
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |