]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/bundle.js
Immediately reject modules if the compilation already failed
[github/fretlink/purs-loader.git] / src / bundle.js
1 'use strict';
2
3 const path = require('path');
4
5 const Promise = require('bluebird')
6
7 const fs = Promise.promisifyAll(require('fs'))
8
9 const spawn = require('cross-spawn')
10
11 const debug = require('debug')('purs-loader');
12
13 const dargs = require('./dargs');
14
15 module.exports = function bundle(options, bundleModules) {
16 const stdout = []
17
18 const stderr = []
19
20 const bundleCommand = options.pscBundle || 'purs';
21
22 const bundleArgs = (options.pscBundle ? [] : [ 'bundle' ]).concat(dargs(Object.assign({
23 _: [path.join(options.output, '*', '*.js')],
24 output: options.bundleOutput,
25 namespace: options.bundleNamespace,
26 }, options.pscBundleArgs)));
27
28 bundleModules.forEach(name => bundleArgs.push('--module', name))
29
30 debug('bundle: %s %O', bundleCommand, bundleArgs);
31
32 return (new Promise((resolve, reject) => {
33 debug('bundling PureScript...')
34
35 const compilation = spawn(bundleCommand, bundleArgs)
36
37 compilation.stdout.on('data', data => stdout.push(data.toString()))
38
39 compilation.stderr.on('data', data => stderr.push(data.toString()))
40
41 compilation.on('close', code => {
42 debug('finished bundling PureScript.')
43
44 if (code !== 0) {
45 const errorMessage = stderr.join('');
46
47 if (errorMessage.length) {
48 psModule.emitError(errorMessage);
49 }
50
51 reject(new Error('bundling failed'))
52 }
53 else {
54 resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`))
55 }
56 })
57 }))
58 };