diff options
author | eric <thul.eric@gmail.com> | 2017-08-12 10:47:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-12 10:47:55 -0400 |
commit | 466c00689bf6e411becec2c1b6d5d3246f4aa834 (patch) | |
tree | cdf352238fb96cf09602edc58d8e5da4eadef18f /src/source-maps.js | |
parent | 712dabc76c3f4495098e8f6bae47610c2857c59f (diff) | |
download | purs-loader-466c00689bf6e411becec2c1b6d5d3246f4aa834.tar.gz purs-loader-466c00689bf6e411becec2c1b6d5d3246f4aa834.tar.zst purs-loader-466c00689bf6e411becec2c1b6d5d3246f4aa834.zip |
Adding source map support (#102)
* Adding source map support
Resolves #101
* Remove unused fs
Diffstat (limited to 'src/source-maps.js')
-rw-r--r-- | src/source-maps.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/source-maps.js b/src/source-maps.js new file mode 100644 index 0000000..9e65867 --- /dev/null +++ b/src/source-maps.js | |||
@@ -0,0 +1,68 @@ | |||
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 | }; | ||