aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/index.js10
-rw-r--r--src/source-maps.js68
2 files changed, 77 insertions, 1 deletions
diff --git a/src/index.js b/src/index.js
index 4c5abd2..f90a218 100644
--- a/src/index.js
+++ b/src/index.js
@@ -22,6 +22,8 @@ const ide = require('./ide');
22 22
23const toJavaScript = require('./to-javascript'); 23const toJavaScript = require('./to-javascript');
24 24
25const sourceMaps = require('./source-maps');
26
25const dargs = require('./dargs'); 27const dargs = require('./dargs');
26 28
27const spawn = require('cross-spawn').sync 29const spawn = require('cross-spawn').sync
@@ -168,9 +170,11 @@ module.exports = function purescriptLoader(source, map) {
168 170
169 const psModule = { 171 const psModule = {
170 name: psModuleName, 172 name: psModuleName,
171 load: js => callback(null, js), 173 source: source,
174 load: ({js, map}) => callback(null, js, map),
172 reject: error => callback(error), 175 reject: error => callback(error),
173 srcPath: this.resourcePath, 176 srcPath: this.resourcePath,
177 remainingRequest: loaderUtils.getRemainingRequest(this),
174 srcDir: path.dirname(this.resourcePath), 178 srcDir: path.dirname(this.resourcePath),
175 jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')), 179 jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
176 options: options, 180 options: options,
@@ -226,6 +230,7 @@ module.exports = function purescriptLoader(source, map) {
226 ide.rebuild(psModule) 230 ide.rebuild(psModule)
227 .then(() => 231 .then(() =>
228 toJavaScript(psModule) 232 toJavaScript(psModule)
233 .then(js => sourceMaps(psModule, js))
229 .then(psModule.load) 234 .then(psModule.load)
230 .catch(psModule.reject) 235 .catch(psModule.reject)
231 ) 236 )
@@ -255,6 +260,7 @@ module.exports = function purescriptLoader(source, map) {
255 Promise.map(cache.deferred, psModule => 260 Promise.map(cache.deferred, psModule =>
256 ide.load(psModule) 261 ide.load(psModule)
257 .then(() => toJavaScript(psModule)) 262 .then(() => toJavaScript(psModule))
263 .then(js => sourceMaps(psModule, js))
258 .then(psModule.load) 264 .then(psModule.load)
259 ) 265 )
260 ) 266 )
@@ -287,6 +293,7 @@ module.exports = function purescriptLoader(source, map) {
287 debugVerbose('compilation is already finished, loading module %s', psModule.name); 293 debugVerbose('compilation is already finished, loading module %s', psModule.name);
288 294
289 toJavaScript(psModule) 295 toJavaScript(psModule)
296 .then(js => sourceMaps(psModule, js))
290 .then(psModule.load) 297 .then(psModule.load)
291 .catch(psModule.reject); 298 .catch(psModule.reject);
292 } 299 }
@@ -320,6 +327,7 @@ module.exports = function purescriptLoader(source, map) {
320 .then(() => 327 .then(() =>
321 Promise.map(cache.deferred, psModule => 328 Promise.map(cache.deferred, psModule =>
322 toJavaScript(psModule) 329 toJavaScript(psModule)
330 .then(js => sourceMaps(psModule, js))
323 .then(psModule.load) 331 .then(psModule.load)
324 ) 332 )
325 ) 333 )
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
3const Promise = require('bluebird');
4
5const fs = require('fs');
6
7const path = require('path');
8
9const debug_ = require('debug');
10
11const debugVerbose = debug_('purs-loader:verbose');
12
13module.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};