aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Psc.js
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2016-06-12 15:17:44 -0400
committereric thul <thul.eric@gmail.com>2016-06-12 15:17:44 -0400
commit531c751fe5593750a377db38bcfaf9a5383ac661 (patch)
tree935b5f17cc29bee58e27b474fe604a3257b7fc63 /src/Psc.js
parent7243be70a2163be2230a5f2739768137305a24ef (diff)
downloadpurs-loader-531c751fe5593750a377db38bcfaf9a5383ac661.tar.gz
purs-loader-531c751fe5593750a377db38bcfaf9a5383ac661.tar.zst
purs-loader-531c751fe5593750a377db38bcfaf9a5383ac661.zip
Reduce building of PureScript module map
Resolves #59 and resolves #60
Diffstat (limited to 'src/Psc.js')
-rw-r--r--src/Psc.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/Psc.js b/src/Psc.js
new file mode 100644
index 0000000..9269e0f
--- /dev/null
+++ b/src/Psc.js
@@ -0,0 +1,92 @@
1'use strict';
2
3const path = require('path');
4
5const Promise = require('bluebird')
6
7const fs = Promise.promisifyAll(require('fs'))
8
9const spawn = require('cross-spawn')
10
11const debug = require('debug')('purs-loader');
12
13const dargs = require('./dargs');
14
15function compile(psModule) {
16 const options = psModule.options
17 const cache = psModule.cache
18 const stderr = []
19
20 if (cache.compilationStarted) return Promise.resolve(psModule)
21
22 cache.compilationStarted = true
23
24 const args = dargs(Object.assign({
25 _: options.src,
26 output: options.output,
27 }, options.pscArgs))
28
29 debug('spawning compiler %s %o', options.psc, args)
30
31 return (new Promise((resolve, reject) => {
32 console.log('\nCompiling PureScript...')
33
34 const compilation = spawn(options.psc, args)
35
36 compilation.stdout.on('data', data => stderr.push(data.toString()))
37 compilation.stderr.on('data', data => stderr.push(data.toString()))
38
39 compilation.on('close', code => {
40 console.log('Finished compiling PureScript.')
41 cache.compilationFinished = true
42 if (code !== 0) {
43 cache.errors = stderr.join('')
44 reject(true)
45 } else {
46 cache.warnings = stderr.join('')
47 resolve(psModule)
48 }
49 })
50 }))
51 .then(compilerOutput => {
52 if (options.bundle) {
53 return bundle(options, cache).then(() => psModule)
54 }
55 return psModule
56 })
57}
58module.exports.compile = compile;
59
60function bundle(options, cache) {
61 if (cache.bundle) return Promise.resolve(cache.bundle)
62
63 const stdout = []
64 const stderr = cache.bundle = []
65
66 const args = dargs(Object.assign({
67 _: [path.join(options.output, '*', '*.js')],
68 output: options.bundleOutput,
69 namespace: options.bundleNamespace,
70 }, options.pscBundleArgs))
71
72 cache.bundleModules.forEach(name => args.push('--module', name))
73
74 debug('spawning bundler %s %o', options.pscBundle, args.join(' '))
75
76 return (new Promise((resolve, reject) => {
77 console.log('Bundling PureScript...')
78
79 const compilation = spawn(options.pscBundle, args)
80
81 compilation.stdout.on('data', data => stdout.push(data.toString()))
82 compilation.stderr.on('data', data => stderr.push(data.toString()))
83 compilation.on('close', code => {
84 if (code !== 0) {
85 cache.errors = (cache.errors || '') + stderr.join('')
86 return reject(true)
87 }
88 cache.bundle = stderr
89 resolve(fs.appendFileAsync('output/bundle.js', `module.exports = ${options.bundleNamespace}`))
90 })
91 }))
92}