]>
Commit | Line | Data |
---|---|---|
466c0068 | 1 | 'use strict'; |
2 | ||
3 | const Promise = require('bluebird'); | |
4 | ||
5 | const fs = require('fs'); | |
6 | ||
7 | const path = require('path'); | |
8 | ||
9 | const debug_ = require('debug'); | |
10 | ||
11 | const debugVerbose = debug_('purs-loader:verbose'); | |
12 | ||
13 | module.exports = function sourceMap(psModule, js) { | |
14 | const options = psModule.options; | |
15 | ||
16 | const jsPath = psModule.jsPath; | |
17 | ||
18 | const srcPath = psModule.srcPath; | |
19 | ||
20 | const source = psModule.source; | |
21 | ||
22 | const remainingRequest = psModule.remainingRequest; | |
23 | ||
24 | const sourceMapPath = path.join(path.dirname(jsPath), 'index.js.map'); | |
25 | ||
26 | const isSourceMapsEnabled = options.pscArgs && options.pscArgs.sourceMaps; | |
27 | ||
28 | return new Promise((resolve, reject) => { | |
29 | if (!isSourceMapsEnabled) { | |
30 | resolve({ | |
31 | js: js, | |
32 | map: undefined | |
33 | }); | |
34 | } | |
35 | else { | |
36 | debugVerbose('loading source map %s', sourceMapPath); | |
37 | ||
38 | fs.readFile(sourceMapPath, 'utf-8', (error, result) => { | |
39 | if (error) { | |
40 | reject(error); | |
41 | } | |
42 | else { | |
43 | try { | |
44 | const map = Object.assign(JSON.parse(result), { | |
45 | sources: [ | |
46 | remainingRequest | |
47 | ], | |
48 | file: path.normalize(srcPath), | |
49 | sourcesContent: [ | |
50 | source | |
51 | ] | |
52 | }); | |
53 | ||
54 | const jsRemovedMapUrl = js.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '') | |
55 | ||
56 | resolve({ | |
57 | js: jsRemovedMapUrl, | |
58 | map: map | |
59 | }); | |
60 | } | |
61 | catch (error) { | |
62 | reject(error); | |
63 | } | |
64 | } | |
65 | }) | |
66 | } | |
67 | }); | |
68 | }; |