]>
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 debug('compiling 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 debug('finished compiling PureScript.')
41 cache
.compilationFinished
= true
43 const errorMessage
= stderr
.join('');
44 if (errorMessage
.length
) {
45 psModule
.emitError(errorMessage
);
47 reject(new Error('compilation failed'))
49 const warningMessage
= stderr
.join('');
50 if (options
.warnings
&& warningMessage
.length
) {
51 psModule
.emitWarning(warningMessage
);
57 .then(compilerOutput
=> {
59 return bundle(options
, cache
).then(() => psModule
)
64 module
.exports
.compile
= compile
;
66 function bundle(options
, cache
) {
67 if (cache
.bundle
) return Promise
.resolve(cache
.bundle
)
70 const stderr
= cache
.bundle
= []
72 const args
= dargs(Object
.assign({
73 _: [path
.join(options
.output
, '*', '*.js')],
74 output: options
.bundleOutput
,
75 namespace: options
.bundleNamespace
,
76 }, options
.pscBundleArgs
))
78 cache
.bundleModules
.forEach(name
=> args
.push('--module', name
))
80 debug('spawning bundler %s %o', options
.pscBundle
, args
.join(' '))
82 return (new Promise((resolve
, reject
) => {
83 debug('bundling PureScript...')
85 const compilation
= spawn(options
.pscBundle
, args
)
87 compilation
.stdout
.on('data', data
=> stdout
.push(data
.toString()))
88 compilation
.stderr
.on('data', data
=> stderr
.push(data
.toString()))
89 compilation
.on('close', code
=> {
90 debug('finished bundling PureScript.')
92 const errorMessage
= stderr
.join('');
93 if (errorMessage
.length
) {
94 psModule
.emitError(errorMessage
);
96 return reject(new Error('bundling failed'))
99 resolve(fs
.appendFileAsync(options
.bundleOutput
, `module.exports = ${options.bundleNamespace}`))