]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/to-javascript.js
Handle missing module and adding debugging
[github/fretlink/purs-loader.git] / src / to-javascript.js
CommitLineData
8e21ab0a 1'use strict';
2
3const Promise = require('bluebird');
4
5const fs = Promise.promisifyAll(require('fs'));
6
7const path = require('path');
8
9const jsStringEscape = require('js-string-escape');
10
11const difference = require('lodash.difference');
12
13const debug = require('debug')('purs-loader');
14
15const PsModuleMap = require('./PsModuleMap');
16
17function updatePsModuleMap(psModule) {
18 const options = psModule.options;
19
20 const cache = psModule.cache;
21
22 const filePurs = psModule.srcPath;
23
24 if (!cache.psModuleMap) {
25 debug('module mapping does not exist');
26
27 return PsModuleMap.makeMap(options.src).then(map => {
28 cache.psModuleMap = map;
29 return cache.psModuleMap;
30 });
31 }
32 else {
33 return PsModuleMap.makeMapEntry(filePurs).then(result => {
34 const map = Object.assign(cache.psModuleMap, result);
35
36 cache.psModuleMap = map;
37
38 return cache.psModuleMap;
39 });
40 }
41}
42
43 // Reference the bundle.
44function makeBundleJS(psModule) {
75826060 45 const bundleOutput = psModule.options.bundleOutput;
8e21ab0a 46
47 const name = psModule.name;
48
49 const srcDir = psModule.srcDir;
50
51 const escaped = jsStringEscape(path.relative(srcDir, bundleOutput));
52
53 const result = `module.exports = require("${escaped}")["${name}"]`;
54
55 return result;
56}
57
58// Replace require paths to output files generated by psc with paths
59// to purescript sources, which are then also run through this loader.
60// Additionally, the imports replaced are tracked so that in the event
61// the compiler fails to compile the PureScript source, we can tack on
62// any new imports in order to allow webpack to watch the new files
63// before they have been successfully compiled.
64function makeJS(psModule, psModuleMap, js) {
65 const requireRE = /require\(['"]\.\.\/([\w\.]+)['"]\)/g;
66
67 const foreignRE = /require\(['"]\.\/foreign['"]\)/g;
68
69 const name = psModule.name;
70
71 const imports = psModuleMap[name].imports;
72
73 var replacedImports = [];
74
75 const result = js
76 .replace(requireRE, (m, p1) => {
4305f5b0 77 const moduleValue = psModuleMap[p1];
8e21ab0a 78
4305f5b0 79 if (!moduleValue) {
80 debug('module %s was not found in the map, replacing require with null', p1);
8e21ab0a 81
4305f5b0 82 return 'null';
83 }
84 else {
85 const escapedPath = jsStringEscape(moduleValue.src);
86
87 replacedImports.push(p1);
88
89 return `require("${escapedPath}")`;
90 }
8e21ab0a 91 })
92 .replace(foreignRE, () => {
93 const escapedPath = jsStringEscape(psModuleMap[name].ffi);
94
95 return `require("${escapedPath}")`;
96 })
97 ;
98
8e21ab0a 99 const additionalImports = difference(imports, replacedImports);
100
4305f5b0 101 if (additionalImports.length) {
102 debug('additional imports for %s: %o', name, additionalImports);
103 }
8e21ab0a 104
105 const additionalImportsResult = additionalImports.map(import_ => {
106 const escapedPath = jsStringEscape(psModuleMap[import_].src);
107
108 return `var ${import_.replace(/\./g, '_')} = require("${escapedPath}")`;
109 }).join('\n');
110
111 const result_ = result + (additionalImports.length ? '\n' + additionalImportsResult : '');
112
113 return result_;
114}
115
116module.exports = function toJavaScript(psModule) {
117 const options = psModule.options;
118
119 const cache = psModule.cache;
120
121 const bundlePath = path.resolve(options.bundleOutput);
122
123 const jsPath = cache.bundle ? bundlePath : psModule.jsPath;
124
125 const js = fs.readFileAsync(jsPath, 'utf8').catch(() => '');
126
127 const psModuleMap = updatePsModuleMap(psModule);
128
129 debug('loading JavaScript for %s', psModule.name);
130
131 return Promise.props({js: js, psModuleMap: psModuleMap}).then(result =>
132 options.bundle ?
133 makeBundleJS(psModule) :
134 makeJS(psModule, result.psModuleMap, result.js)
135 );
136};