aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/source-maps.js
blob: 9e65867fc8fac2d8db5e3ae3bd00aeee10e7deac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
          }
        }
      })
    }
  });
};