diff options
Diffstat (limited to 'lib/source-maps.js')
-rw-r--r-- | lib/source-maps.js | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/source-maps.js b/lib/source-maps.js new file mode 100644 index 0000000..3ad70dd --- /dev/null +++ b/lib/source-maps.js | |||
@@ -0,0 +1,61 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | var Promise = require('bluebird'); | ||
4 | |||
5 | var fs = require('fs'); | ||
6 | |||
7 | var path = require('path'); | ||
8 | |||
9 | var debug_ = require('debug'); | ||
10 | |||
11 | var debugVerbose = debug_('purs-loader:verbose'); | ||
12 | |||
13 | module.exports = function sourceMap(psModule, js) { | ||
14 | var options = psModule.options; | ||
15 | |||
16 | var jsPath = psModule.jsPath; | ||
17 | |||
18 | var srcPath = psModule.srcPath; | ||
19 | |||
20 | var source = psModule.source; | ||
21 | |||
22 | var remainingRequest = psModule.remainingRequest; | ||
23 | |||
24 | var sourceMapPath = path.join(path.dirname(jsPath), 'index.js.map'); | ||
25 | |||
26 | var isSourceMapsEnabled = options.pscArgs && options.pscArgs.sourceMaps; | ||
27 | |||
28 | return new Promise(function (resolve, reject) { | ||
29 | if (!isSourceMapsEnabled) { | ||
30 | resolve({ | ||
31 | js: js, | ||
32 | map: undefined | ||
33 | }); | ||
34 | } else { | ||
35 | debugVerbose('loading source map %s', sourceMapPath); | ||
36 | |||
37 | fs.readFile(sourceMapPath, 'utf-8', function (error, result) { | ||
38 | if (error) { | ||
39 | reject(error); | ||
40 | } else { | ||
41 | try { | ||
42 | var map = Object.assign(JSON.parse(result), { | ||
43 | sources: [remainingRequest], | ||
44 | file: path.normalize(srcPath), | ||
45 | sourcesContent: [source] | ||
46 | }); | ||
47 | |||
48 | var jsRemovedMapUrl = js.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''); | ||
49 | |||
50 | resolve({ | ||
51 | js: jsRemovedMapUrl, | ||
52 | map: map | ||
53 | }); | ||
54 | } catch (error) { | ||
55 | reject(error); | ||
56 | } | ||
57 | } | ||
58 | }); | ||
59 | } | ||
60 | }); | ||
61 | }; \ No newline at end of file | ||