]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/bundle.js
Added hooks support (#122)
[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 reject(new Error(`bundling failed: ${errorMessage}`))
49 }
50 else {
51 reject(new Error('bundling failed'))
52 }
53 }
54 else {
55 resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`))
56 }
57 })
58 }))
59 };