]>
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
.stderr
.on('data', data
=> {
37 stderr
.push(data
.toString());
40 compilation
.on('close', code
=> {
41 debug('finished compiling PureScript.')
42 cache
.compilationFinished
= true
44 const errorMessage
= stderr
.join('');
45 if (errorMessage
.length
) {
46 psModule
.emitError(errorMessage
);
52 reject(new Error('compilation failed'))
55 const warningMessage
= stderr
.join('');
56 if (options
.warnings
&& warningMessage
.length
) {
57 psModule
.emitWarning(warningMessage
);
63 .then(compilerOutput
=> {
65 return bundle(options
, cache
).then(() => psModule
)
70 module
.exports
.compile
= compile
;
72 function bundle(options
, cache
) {
73 if (cache
.bundle
) return Promise
.resolve(cache
.bundle
)
76 const stderr
= cache
.bundle
= []
78 const args
= dargs(Object
.assign({
79 _: [path
.join(options
.output
, '*', '*.js')],
80 output: options
.bundleOutput
,
81 namespace: options
.bundleNamespace
,
82 }, options
.pscBundleArgs
))
84 cache
.bundleModules
.forEach(name
=> args
.push('--module', name
))
86 debug('spawning bundler %s %o', options
.pscBundle
, args
.join(' '))
88 return (new Promise((resolve
, reject
) => {
89 debug('bundling PureScript...')
91 const compilation
= spawn(options
.pscBundle
, args
)
93 compilation
.stdout
.on('data', data
=> stdout
.push(data
.toString()))
94 compilation
.stderr
.on('data', data
=> stderr
.push(data
.toString()))
95 compilation
.on('close', code
=> {
96 debug('finished bundling PureScript.')
98 const errorMessage
= stderr
.join('');
99 if (errorMessage
.length
) {
100 psModule
.emitError(errorMessage
);
102 return reject(new Error('bundling failed'))
104 cache
.bundle
= stderr
105 resolve(fs
.appendFileAsync(options
.bundleOutput
, `module.exports = ${options.bundleNamespace}`))