Skip to content

Commit 466c006

Browse files
authored
Adding source map support (#102)
* Adding source map support Resolves #101 * Remove unused fs
1 parent 712dabc commit 466c006

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

src/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const ide = require('./ide');
2222

2323
const toJavaScript = require('./to-javascript');
2424

25+
const sourceMaps = require('./source-maps');
26+
2527
const dargs = require('./dargs');
2628

2729
const spawn = require('cross-spawn').sync
@@ -168,9 +170,11 @@ module.exports = function purescriptLoader(source, map) {
168170

169171
const psModule = {
170172
name: psModuleName,
171-
load: js => callback(null, js),
173+
source: source,
174+
load: ({js, map}) => callback(null, js, map),
172175
reject: error => callback(error),
173176
srcPath: this.resourcePath,
177+
remainingRequest: loaderUtils.getRemainingRequest(this),
174178
srcDir: path.dirname(this.resourcePath),
175179
jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
176180
options: options,
@@ -226,6 +230,7 @@ module.exports = function purescriptLoader(source, map) {
226230
ide.rebuild(psModule)
227231
.then(() =>
228232
toJavaScript(psModule)
233+
.then(js => sourceMaps(psModule, js))
229234
.then(psModule.load)
230235
.catch(psModule.reject)
231236
)
@@ -255,6 +260,7 @@ module.exports = function purescriptLoader(source, map) {
255260
Promise.map(cache.deferred, psModule =>
256261
ide.load(psModule)
257262
.then(() => toJavaScript(psModule))
263+
.then(js => sourceMaps(psModule, js))
258264
.then(psModule.load)
259265
)
260266
)
@@ -287,6 +293,7 @@ module.exports = function purescriptLoader(source, map) {
287293
debugVerbose('compilation is already finished, loading module %s', psModule.name);
288294

289295
toJavaScript(psModule)
296+
.then(js => sourceMaps(psModule, js))
290297
.then(psModule.load)
291298
.catch(psModule.reject);
292299
}
@@ -320,6 +327,7 @@ module.exports = function purescriptLoader(source, map) {
320327
.then(() =>
321328
Promise.map(cache.deferred, psModule =>
322329
toJavaScript(psModule)
330+
.then(js => sourceMaps(psModule, js))
323331
.then(psModule.load)
324332
)
325333
)

src/source-maps.js

+68
Original file line numberDiff line numberDiff line change
@@ -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+
};

0 commit comments

Comments
 (0)