]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/bundle.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 module
.exports
= function bundle(options
, cache
) {
16 if (cache
.bundle
) return Promise
.resolve(cache
.bundle
)
20 const stderr
= cache
.bundle
= []
22 const bundleCommand
= options
.pscBundle
|| 'purs';
24 const bundleArgs
= (options
.pscBundle
? [] : [ 'bundle' ]).concat(dargs(Object
.assign({
25 _: [path
.join(options
.output
, '*', '*.js')],
26 output: options
.bundleOutput
,
27 namespace: options
.bundleNamespace
,
28 }, options
.pscBundleArgs
)));
30 cache
.bundleModules
.forEach(name
=> bundleArgs
.push('--module', name
))
32 debug('spawning bundler %s %o', bundleCommand
, bundleArgs
);
34 return (new Promise((resolve
, reject
) => {
35 debug('bundling PureScript...')
37 const compilation
= spawn(bundleCommand
, bundleArgs
)
39 compilation
.stdout
.on('data', data
=> stdout
.push(data
.toString()))
41 compilation
.stderr
.on('data', data
=> stderr
.push(data
.toString()))
43 compilation
.on('close', code
=> {
44 debug('finished bundling PureScript.')
47 const errorMessage
= stderr
.join('');
48 if (errorMessage
.length
) {
49 psModule
.emitError(errorMessage
);
51 return reject(new Error('bundling failed'))
56 resolve(fs
.appendFileAsync(options
.bundleOutput
, `module.exports = ${options.bundleNamespace}`))