diff options
author | Alex Mingoia <talk@alexmingoia.com> | 2016-05-21 17:04:26 -0700 |
---|---|---|
committer | Alex Mingoia <talk@alexmingoia.com> | 2016-05-21 17:04:26 -0700 |
commit | 17acb575860cf1bed9e1f6d992a9b7cd66057464 (patch) | |
tree | 10a7677d88c750b360f141b2e9d5ca095e01ab4c /src/index.js | |
parent | c0f935ab2372125cf00cdd722f83afa31e07eb9a (diff) | |
download | purs-loader-17acb575860cf1bed9e1f6d992a9b7cd66057464.tar.gz purs-loader-17acb575860cf1bed9e1f6d992a9b7cd66057464.tar.zst purs-loader-17acb575860cf1bed9e1f6d992a9b7cd66057464.zip |
Reference source ffi module instead of output ffi module.
Diffstat (limited to 'src/index.js')
-rw-r--r-- | src/index.js | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/src/index.js b/src/index.js index e09d247..72da4d0 100644 --- a/src/index.js +++ b/src/index.js | |||
@@ -10,7 +10,8 @@ const spawn = require('child_process').spawn | |||
10 | const path = require('path') | 10 | const path = require('path') |
11 | const retryPromise = require('promise-retry') | 11 | const retryPromise = require('promise-retry') |
12 | 12 | ||
13 | const psModuleRegex = /(?:^|\n)module\s+([\w\.]+)/i | 13 | const ffiModuleRegex = /\/\/\s+module\s+([\w\.]+)/i |
14 | const srcModuleRegex = /(?:^|\n)module\s+([\w\.]+)/i | ||
14 | const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g | 15 | const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g |
15 | 16 | ||
16 | module.exports = function purescriptLoader(source, map) { | 17 | module.exports = function purescriptLoader(source, map) { |
@@ -77,7 +78,7 @@ module.exports = function purescriptLoader(source, map) { | |||
77 | }) | 78 | }) |
78 | } | 79 | } |
79 | 80 | ||
80 | const psModuleName = match(psModuleRegex, source) | 81 | const psModuleName = match(srcModuleRegex, source) |
81 | const psModule = { | 82 | const psModule = { |
82 | name: psModuleName, | 83 | name: psModuleName, |
83 | load: js => callback(null, js), | 84 | load: js => callback(null, js), |
@@ -135,7 +136,7 @@ function toJavaScript(psModule) { | |||
135 | 136 | ||
136 | return Promise.props({ | 137 | return Promise.props({ |
137 | js: fs.readFileAsync(jsPath, 'utf8'), | 138 | js: fs.readFileAsync(jsPath, 'utf8'), |
138 | psModuleMap: psModuleMap(options.src, cache) | 139 | psModuleMap: psModuleMap(options, cache) |
139 | }).then(result => { | 140 | }).then(result => { |
140 | let js = '' | 141 | let js = '' |
141 | 142 | ||
@@ -147,15 +148,13 @@ function toJavaScript(psModule) { | |||
147 | } else { | 148 | } else { |
148 | // replace require paths to output files generated by psc with paths | 149 | // replace require paths to output files generated by psc with paths |
149 | // to purescript sources, which are then also run through this loader. | 150 | // to purescript sources, which are then also run through this loader. |
150 | const foreignRequire = 'require("' + path.resolve( | ||
151 | path.join(psModule.options.output, psModule.name, 'foreign.js') | ||
152 | ) + '")' | ||
153 | |||
154 | js = result.js | 151 | js = result.js |
155 | .replace(requireRegex, (m, p1) => { | 152 | .replace(requireRegex, (m, p1) => { |
156 | return 'require("' + result.psModuleMap[p1] + '")' | 153 | return 'require("' + result.psModuleMap[p1].src + '")' |
154 | }) | ||
155 | .replace(/require\(['"]\.\/foreign['"]\)/g, (m, p1) => { | ||
156 | return 'require("' + result.psModuleMap[psModule.name].ffi + '")' | ||
157 | }) | 157 | }) |
158 | .replace(/require\(['"]\.\/foreign['"]\)/g, foreignRequire) | ||
159 | } | 158 | } |
160 | 159 | ||
161 | return js | 160 | return js |
@@ -357,20 +356,30 @@ function bundle(options, cache) { | |||
357 | } | 356 | } |
358 | 357 | ||
359 | // map of PS module names to their source path | 358 | // map of PS module names to their source path |
360 | function psModuleMap(globs, cache) { | 359 | function psModuleMap(options, cache) { |
361 | if (cache.psModuleMap) return Promise.resolve(cache.psModuleMap) | 360 | if (cache.psModuleMap) return Promise.resolve(cache.psModuleMap) |
362 | 361 | ||
362 | const globs = [].concat(options.src).concat(options.ffi) | ||
363 | |||
363 | return globby(globs).then(paths => { | 364 | return globby(globs).then(paths => { |
364 | return Promise | 365 | return Promise |
365 | .props(paths.reduce((map, file) => { | 366 | .props(paths.reduce((map, file) => { |
366 | map[file] = fs.readFileAsync(file, 'utf8') | 367 | map[file] = fs.readFileAsync(file, 'utf8') |
367 | return map | 368 | return map |
368 | }, {})) | 369 | }, {})) |
369 | .then(srcMap => { | 370 | .then(fileMap => { |
370 | cache.psModuleMap = Object.keys(srcMap).reduce((map, file) => { | 371 | cache.psModuleMap = Object.keys(fileMap).reduce((map, file) => { |
371 | const source = srcMap[file] | 372 | const source = fileMap[file] |
372 | const psModuleName = match(psModuleRegex, source) | 373 | const ext = path.extname(file) |
373 | map[psModuleName] = path.resolve(file) | 374 | const isPurs = ext.match(/purs$/i) |
375 | const moduleRegex = isPurs ? srcModuleRegex : ffiModuleRegex | ||
376 | const moduleName = match(moduleRegex, source) | ||
377 | map[moduleName] = map[moduleName] || {} | ||
378 | if (isPurs) { | ||
379 | map[moduleName].src = path.resolve(file) | ||
380 | } else { | ||
381 | map[moduleName].ffi = path.resolve(file) | ||
382 | } | ||
374 | return map | 383 | return map |
375 | }, {}) | 384 | }, {}) |
376 | return cache.psModuleMap | 385 | return cache.psModuleMap |