From 466c00689bf6e411becec2c1b6d5d3246f4aa834 Mon Sep 17 00:00:00 2001 From: eric Date: Sat, 12 Aug 2017 10:47:55 -0400 Subject: Adding source map support (#102) * Adding source map support Resolves #101 * Remove unused fs --- src/index.js | 10 +++++++- src/source-maps.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/source-maps.js (limited to 'src') 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'); 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 index 0000000..9e65867 --- /dev/null +++ b/src/source-maps.js @@ -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); + } + } + }) + } + }); +}; -- cgit v1.2.3