diff options
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 64 |
1 files changed, 3 insertions, 61 deletions
diff --git a/src/index.js b/src/index.js index e49fd2c..047927c 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -3,18 +3,15 @@ | |||
3 | const debug = require('debug')('purs-loader') | 3 | const debug = require('debug')('purs-loader') |
4 | const loaderUtils = require('loader-utils') | 4 | const loaderUtils = require('loader-utils') |
5 | const Promise = require('bluebird') | 5 | const Promise = require('bluebird') |
6 | const fs = Promise.promisifyAll(require('fs')) | ||
7 | const path = require('path') | 6 | const path = require('path') |
8 | const jsStringEscape = require('js-string-escape') | ||
9 | const PsModuleMap = require('./PsModuleMap'); | 7 | const PsModuleMap = require('./PsModuleMap'); |
10 | const Psc = require('./Psc'); | 8 | const Psc = require('./Psc'); |
11 | const PscIde = require('./PscIde'); | 9 | const PscIde = require('./PscIde'); |
10 | const toJavaScript = require('./to-javascript'); | ||
12 | const dargs = require('./dargs'); | 11 | const dargs = require('./dargs'); |
13 | const spawn = require('cross-spawn').sync | 12 | const spawn = require('cross-spawn').sync |
14 | const eol = require('os').EOL | 13 | const eol = require('os').EOL |
15 | 14 | ||
16 | const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g | ||
17 | |||
18 | module.exports = function purescriptLoader(source, map) { | 15 | module.exports = function purescriptLoader(source, map) { |
19 | const callback = this.async() | 16 | const callback = this.async() |
20 | const config = this.options | 17 | const config = this.options |
@@ -49,6 +46,7 @@ module.exports = function purescriptLoader(source, map) { | |||
49 | bundleNamespace: 'PS', | 46 | bundleNamespace: 'PS', |
50 | bundle: false, | 47 | bundle: false, |
51 | warnings: true, | 48 | warnings: true, |
49 | watch: false, | ||
52 | output: 'output', | 50 | output: 'output', |
53 | src: [ | 51 | src: [ |
54 | path.join('src', '**', '*.purs'), | 52 | path.join('src', '**', '*.purs'), |
@@ -151,7 +149,7 @@ module.exports = function purescriptLoader(source, map) { | |||
151 | if (!cache.compilationStarted) { | 149 | if (!cache.compilationStarted) { |
152 | return Psc.compile(psModule) | 150 | return Psc.compile(psModule) |
153 | .then(() => PsModuleMap.makeMap(options.src).then(map => { | 151 | .then(() => PsModuleMap.makeMap(options.src).then(map => { |
154 | debug('rebuilt module map'); | 152 | debug('rebuilt module map after compile'); |
155 | cache.psModuleMap = map; | 153 | cache.psModuleMap = map; |
156 | })) | 154 | })) |
157 | .then(() => Promise.map(cache.deferred, psModule => { | 155 | .then(() => Promise.map(cache.deferred, psModule => { |
@@ -164,59 +162,3 @@ module.exports = function purescriptLoader(source, map) { | |||
164 | }) | 162 | }) |
165 | } | 163 | } |
166 | } | 164 | } |
167 | |||
168 | function updatePsModuleMap(psModule) { | ||
169 | const options = psModule.options | ||
170 | const cache = psModule.cache | ||
171 | const filePurs = psModule.srcPath | ||
172 | if (!cache.psModuleMap) { | ||
173 | debug('module mapping does not exist'); | ||
174 | return PsModuleMap.makeMap(options.src).then(map => { | ||
175 | cache.psModuleMap = map; | ||
176 | return cache.psModuleMap; | ||
177 | }); | ||
178 | } | ||
179 | else { | ||
180 | return PsModuleMap.makeMapEntry(filePurs).then(result => { | ||
181 | const map = Object.assign(cache.psModuleMap, result) | ||
182 | cache.psModuleMap = map; | ||
183 | return cache.psModuleMap; | ||
184 | }); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | // The actual loader is executed *after* purescript compilation. | ||
189 | function toJavaScript(psModule) { | ||
190 | const options = psModule.options | ||
191 | const cache = psModule.cache | ||
192 | const bundlePath = path.resolve(options.bundleOutput) | ||
193 | const jsPath = cache.bundle ? bundlePath : psModule.jsPath | ||
194 | |||
195 | debug('loading JavaScript for', psModule.name) | ||
196 | |||
197 | return Promise.props({ | ||
198 | js: fs.readFileAsync(jsPath, 'utf8'), | ||
199 | psModuleMap: updatePsModuleMap(psModule) | ||
200 | }).then(result => { | ||
201 | let js = '' | ||
202 | |||
203 | if (options.bundle) { | ||
204 | // if bundling, return a reference to the bundle | ||
205 | js = 'module.exports = require("' | ||
206 | + jsStringEscape(path.relative(psModule.srcDir, options.bundleOutput)) | ||
207 | + '")["' + psModule.name + '"]' | ||
208 | } else { | ||
209 | // replace require paths to output files generated by psc with paths | ||
210 | // to purescript sources, which are then also run through this loader. | ||
211 | js = result.js | ||
212 | .replace(requireRegex, (m, p1) => { | ||
213 | return 'require("' + jsStringEscape(result.psModuleMap[p1].src) + '")' | ||
214 | }) | ||
215 | .replace(/require\(['"]\.\/foreign['"]\)/g, (m, p1) => { | ||
216 | return 'require("' + jsStringEscape(result.psModuleMap[psModule.name].ffi) + '")' | ||
217 | }) | ||
218 | } | ||
219 | |||
220 | return js | ||
221 | }) | ||
222 | } | ||