const toJavaScript = require('./to-javascript');
+const sourceMaps = require('./source-maps');
+
const dargs = require('./dargs');
const spawn = require('cross-spawn').sync
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,
ide.rebuild(psModule)
.then(() =>
toJavaScript(psModule)
+ .then(js => sourceMaps(psModule, js))
.then(psModule.load)
.catch(psModule.reject)
)
Promise.map(cache.deferred, psModule =>
ide.load(psModule)
.then(() => toJavaScript(psModule))
+ .then(js => sourceMaps(psModule, js))
.then(psModule.load)
)
)
debugVerbose('compilation is already finished, loading module %s', psModule.name);
toJavaScript(psModule)
+ .then(js => sourceMaps(psModule, js))
.then(psModule.load)
.catch(psModule.reject);
}
.then(() =>
Promise.map(cache.deferred, psModule =>
toJavaScript(psModule)
+ .then(js => sourceMaps(psModule, js))
.then(psModule.load)
)
)
--- /dev/null
+'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);
+ }
+ }
+ })
+ }
+ });
+};