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
|
'use strict';
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');
const jsStringEscape = require('js-string-escape');
const difference = require('lodash.difference');
const debug = require('debug')('purs-loader');
const PsModuleMap = require('./PsModuleMap');
function updatePsModuleMap(psModule) {
const options = psModule.options;
const cache = psModule.cache;
const filePurs = psModule.srcPath;
if (!cache.psModuleMap) {
debug('module mapping does not exist');
return PsModuleMap.makeMap(options.src).then(map => {
cache.psModuleMap = map;
return cache.psModuleMap;
});
}
else {
return PsModuleMap.makeMapEntry(filePurs).then(result => {
const map = Object.assign(cache.psModuleMap, result);
cache.psModuleMap = map;
return cache.psModuleMap;
});
}
}
// Reference the bundle.
function makeBundleJS(psModule) {
const bundleOutput = psModule.options.bundleOutput;
const name = psModule.name;
const srcDir = psModule.srcDir;
const escaped = jsStringEscape(path.relative(srcDir, bundleOutput));
const result = `module.exports = require("${escaped}")["${name}"]`;
return 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) {
const requireRE = /require\(['"]\.\.\/([\w\.]+)['"]\)/g;
const foreignRE = /require\(['"]\.\/foreign['"]\)/g;
const name = psModule.name;
const imports = psModuleMap[name].imports;
var replacedImports = [];
const result = js
.replace(requireRE, (m, p1) => {
const escapedPath = jsStringEscape(psModuleMap[p1].src);
replacedImports.push(p1);
return `require("${escapedPath}")`;
})
.replace(foreignRE, () => {
const escapedPath = jsStringEscape(psModuleMap[name].ffi);
return `require("${escapedPath}")`;
})
;
debug('imports %o', imports);
debug('replaced imports %o', replacedImports);
const additionalImports = difference(imports, replacedImports);
debug('additional imports for %s: %o', name, additionalImports);
const additionalImportsResult = additionalImports.map(import_ => {
const escapedPath = jsStringEscape(psModuleMap[import_].src);
return `var ${import_.replace(/\./g, '_')} = require("${escapedPath}")`;
}).join('\n');
const result_ = result + (additionalImports.length ? '\n' + additionalImportsResult : '');
return result_;
}
module.exports = function toJavaScript(psModule) {
const options = psModule.options;
const cache = psModule.cache;
const bundlePath = path.resolve(options.bundleOutput);
const jsPath = cache.bundle ? bundlePath : psModule.jsPath;
const js = fs.readFileAsync(jsPath, 'utf8').catch(() => '');
const psModuleMap = updatePsModuleMap(psModule);
debug('loading JavaScript for %s', psModule.name);
return Promise.props({js: js, psModuleMap: psModuleMap}).then(result =>
options.bundle ?
makeBundleJS(psModule) :
makeJS(psModule, result.psModuleMap, result.js)
);
};
|