]> git.immae.eu Git - github/fretlink/purs-loader.git/commitdiff
Adding source map support (#102)
authoreric <thul.eric@gmail.com>
Sat, 12 Aug 2017 14:47:55 +0000 (10:47 -0400)
committerGitHub <noreply@github.com>
Sat, 12 Aug 2017 14:47:55 +0000 (10:47 -0400)
* Adding source map support

Resolves #101

* Remove unused fs

src/index.js
src/source-maps.js [new file with mode: 0644]

index 4c5abd20a1e894ed827c1640b7c0a6c64bac9e12..f90a21886065b1be7f808cfec4bc3483cf0f6a3e 100644 (file)
@@ -22,6 +22,8 @@ const ide = require('./ide');
 
 const toJavaScript = require('./to-javascript');
 
+const sourceMaps = require('./source-maps');
+
 const dargs = require('./dargs');
 
 const spawn = require('cross-spawn').sync
@@ -168,9 +170,11 @@ module.exports = function purescriptLoader(source, map) {
 
   const psModule = {
     name: psModuleName,
-    load: js => callback(null, js),
+    source: source,
+    load: ({js, map}) => callback(null, js, map),
     reject: error => callback(error),
     srcPath: this.resourcePath,
+    remainingRequest: loaderUtils.getRemainingRequest(this),
     srcDir: path.dirname(this.resourcePath),
     jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
     options: options,
@@ -226,6 +230,7 @@ module.exports = function purescriptLoader(source, map) {
       ide.rebuild(psModule)
       .then(() =>
         toJavaScript(psModule)
+          .then(js => sourceMaps(psModule, js))
           .then(psModule.load)
           .catch(psModule.reject)
       )
@@ -255,6 +260,7 @@ module.exports = function purescriptLoader(source, map) {
                 Promise.map(cache.deferred, psModule =>
                   ide.load(psModule)
                     .then(() => toJavaScript(psModule))
+                    .then(js => sourceMaps(psModule, js))
                     .then(psModule.load)
                 )
               )
@@ -287,6 +293,7 @@ module.exports = function purescriptLoader(source, map) {
     debugVerbose('compilation is already finished, loading module %s', psModule.name);
 
     toJavaScript(psModule)
+      .then(js => sourceMaps(psModule, js))
       .then(psModule.load)
       .catch(psModule.reject);
   }
@@ -320,6 +327,7 @@ module.exports = function purescriptLoader(source, map) {
         .then(() =>
           Promise.map(cache.deferred, psModule =>
             toJavaScript(psModule)
+              .then(js => sourceMaps(psModule, js))
               .then(psModule.load)
           )
         )
diff --git a/src/source-maps.js b/src/source-maps.js
new file mode 100644 (file)
index 0000000..9e65867
--- /dev/null
@@ -0,0 +1,68 @@
+'use strict';
+
+const Promise = require('bluebird');
+
+const fs = require('fs');
+
+const path = require('path');
+
+const debug_ = require('debug');
+
+const debugVerbose = debug_('purs-loader:verbose');
+
+module.exports = function sourceMap(psModule, js) {
+  const options = psModule.options;
+
+  const jsPath = psModule.jsPath;
+
+  const srcPath = psModule.srcPath;
+
+  const source = psModule.source;
+
+  const remainingRequest = psModule.remainingRequest;
+
+  const sourceMapPath = path.join(path.dirname(jsPath), 'index.js.map');
+
+  const isSourceMapsEnabled = options.pscArgs && options.pscArgs.sourceMaps;
+
+  return new Promise((resolve, reject) => {
+    if (!isSourceMapsEnabled) {
+      resolve({
+        js: js,
+        map: undefined
+      });
+    }
+    else {
+      debugVerbose('loading source map %s', sourceMapPath);
+
+      fs.readFile(sourceMapPath, 'utf-8', (error, result) => {
+        if (error) {
+          reject(error);
+        }
+        else {
+          try {
+            const map = Object.assign(JSON.parse(result), {
+              sources: [
+                remainingRequest
+              ],
+              file: path.normalize(srcPath),
+              sourcesContent: [
+                source
+              ]
+            });
+
+            const jsRemovedMapUrl = js.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '')
+
+            resolve({
+              js: jsRemovedMapUrl,
+              map: map
+            });
+          }
+          catch (error) {
+            reject(error);
+          }
+        }
+      })
+    }
+  });
+};