]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/Psc.js
3 const path
= require('path');
5 const Promise
= require('bluebird')
7 const fs
= Promise
.promisifyAll(require('fs'))
9 const spawn
= require('cross-spawn')
11 const debug
= require('debug')('purs-loader');
13 const dargs
= require('./dargs');
15 function compile(psModule
) {
16 const options
= psModule
.options
17 const cache
= psModule
.cache
20 if (cache
.compilationStarted
) return Promise
.resolve(psModule
)
22 cache
.compilationStarted
= true
24 const args
= dargs(Object
.assign({
26 output: options
.output
,
29 debug('spawning compiler %s %o', options
.psc
, args
)
31 return (new Promise((resolve
, reject
) => {
32 console
.log('\nCompiling PureScript...')
34 const compilation
= spawn(options
.psc
, args
)
36 compilation
.stdout
.on('data', data
=> stderr
.push(data
.toString()))
37 compilation
.stderr
.on('data', data
=> stderr
.push(data
.toString()))
39 compilation
.on('close', code
=> {
40 console
.log('Finished compiling PureScript.')
41 cache
.compilationFinished
= true
43 cache
.errors
= stderr
.join('')
46 cache
.warnings
= stderr
.join('')
51 .then(compilerOutput
=> {
53 return bundle(options
, cache
).then(() => psModule
)
58 module
.exports
.compile
= compile
;
60 function bundle(options
, cache
) {
61 if (cache
.bundle
) return Promise
.resolve(cache
.bundle
)
64 const stderr
= cache
.bundle
= []
66 const args
= dargs(Object
.assign({
67 _: [path
.join(options
.output
, '*', '*.js')],
68 output: options
.bundleOutput
,
69 namespace: options
.bundleNamespace
,
70 }, options
.pscBundleArgs
))
72 cache
.bundleModules
.forEach(name
=> args
.push('--module', name
))
74 debug('spawning bundler %s %o', options
.pscBundle
, args
.join(' '))
76 return (new Promise((resolve
, reject
) => {
77 console
.log('Bundling PureScript...')
79 const compilation
= spawn(options
.pscBundle
, args
)
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
=> {
85 cache
.errors
= (cache
.errors
|| '') + stderr
.join('')
89 resolve(fs
.appendFileAsync(options
.bundleOutput
, `module.exports = ${options.bundleNamespace}`))