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