aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/to-javascript.js
blob: ce4970440e574bc286b2bb8e96fdd2988edd703e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';

var Promise = require('bluebird');

var fs = Promise.promisifyAll(require('fs'));

var path = require('path');

var jsStringEscape = require('js-string-escape');

var difference = require('lodash.difference');

var debug_ = require('debug');

var debug = debug_('purs-loader');

var debugVerbose = debug_('purs-loader:verbose');

var PsModuleMap = require('./purs-module-map');

function updatePsModuleMap(psModule) {
  var options = psModule.options;

  var cache = psModule.cache;

  var filePurs = psModule.srcPath;

  if (!cache.psModuleMap) {
    debugVerbose('module mapping does not exist - making a new module map');

    cache.psModuleMap = PsModuleMap.makeMap(options.src);

    return cache.psModuleMap;
  } else {
    debugVerbose('module mapping exists - updating module map for %s', filePurs);

    cache.psModuleMap = cache.psModuleMap.then(function (psModuleMap) {
      return PsModuleMap.makeMapEntry(filePurs).then(function (result) {
        var map = Object.assign(psModuleMap, result);

        return map;
      });
    });

    return cache.psModuleMap;
  }
}

// Reference the bundle.
function makeBundleJS(psModule) {
  var bundleOutput = psModule.options.bundleOutput;

  var name = psModule.name;

  var srcDir = psModule.srcDir;

  var escaped = jsStringEscape(path.relative(srcDir, bundleOutput));

  var result = 'module.exports = require("' + escaped + '")["' + name + '"]';

  return Promise.resolve(result);
}

// Replace require paths to output files generated by psc with paths
// to purescript sources, which are then also run through this loader.
// Additionally, the imports replaced are tracked so that in the event
// the compiler fails to compile the PureScript source, we can tack on
// any new imports in order to allow webpack to watch the new files
// before they have been successfully compiled.
function makeJS(psModule, psModuleMap, js) {
  var requireRE = /require\(['"]\.\.\/([\w\.]+)(?:\/index\.js)?['"]\)/g;

  var foreignRE = /require\(['"]\.\/foreign(?:\.js)?['"]\)/g;

  var name = psModule.name;

  var imports = psModuleMap[name].imports;

  var replacedImports = [];

  var result = js.replace(requireRE, function (m, p1) {
    var moduleValue = psModuleMap[p1];

    if (!moduleValue) {
      debug('module %s was not found in the map, replacing require with null', p1);

      return 'null';
    } else {
      var escapedPath = jsStringEscape(moduleValue.src);

      replacedImports.push(p1);

      return 'require("' + escapedPath + '")';
    }
  }).replace(foreignRE, function () {
    var escapedPath = jsStringEscape(psModuleMap[name].ffi);

    return 'require("' + escapedPath + '")';
  });

  var additionalImports = difference(imports, replacedImports);

  if (!additionalImports.length) {
    return Promise.resolve(result);
  } else {
    debug('rebuilding module map due to additional imports for %s: %o', name, additionalImports);

    psModule.cache.psModuleMap = null;

    return updatePsModuleMap(psModule).then(function (updatedPsModuleMap) {
      var additionalImportsResult = additionalImports.map(function (import_) {
        var moduleValue = updatedPsModuleMap[import_];

        if (!moduleValue) {
          debug('module %s was not found in the map, skipping require', import_);

          return null;
        } else {
          var escapedPath = jsStringEscape(moduleValue.src);

          return 'var ' + import_.replace(/\./g, '_') + ' = require("' + escapedPath + '")';
        }
      }).filter(function (a) {
        return a !== null;
      }).join('\n');

      return result + '\n' + additionalImportsResult;
    });
  }
}

module.exports = function toJavaScript(psModule) {
  var options = psModule.options;

  var cache = psModule.cache;

  var bundlePath = path.resolve(options.bundleOutput);

  var jsPath = options.bundle ? bundlePath : psModule.jsPath;

  var js = fs.readFileAsync(jsPath, 'utf8').catch(function () {
    return '';
  });

  var psModuleMap = updatePsModuleMap(psModule);

  debugVerbose('loading JavaScript for %s', psModule.name);

  return Promise.props({ js: js, psModuleMap: psModuleMap }).then(function (result) {
    return options.bundle ? makeBundleJS(psModule) : makeJS(psModule, result.psModuleMap, result.js);
  });
};