]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/bundle.js
Support for PureScript 0.11
[github/fretlink/purs-loader.git] / src / bundle.js
CommitLineData
1c12889c 1'use strict';
2
3const path = require('path');
4
5const Promise = require('bluebird')
6
7const fs = Promise.promisifyAll(require('fs'))
8
9const spawn = require('cross-spawn')
10
11const debug = require('debug')('purs-loader');
12
13const dargs = require('./dargs');
14
15module.exports = function bundle(options, cache) {
16 if (cache.bundle) return Promise.resolve(cache.bundle)
17
18 const stdout = []
19
20 const stderr = cache.bundle = []
21
22 const bundleCommand = options.pscBundle || 'purs';
23
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)));
29
30 cache.bundleModules.forEach(name => bundleArgs.push('--module', name))
31
32 debug('spawning bundler %s %o', bundleCommand, bundleArgs);
33
34 return (new Promise((resolve, reject) => {
35 debug('bundling PureScript...')
36
37 const compilation = spawn(bundleCommand, bundleArgs)
38
39 compilation.stdout.on('data', data => stdout.push(data.toString()))
40
41 compilation.stderr.on('data', data => stderr.push(data.toString()))
42
43 compilation.on('close', code => {
44 debug('finished bundling PureScript.')
45
46 if (code !== 0) {
47 const errorMessage = stderr.join('');
48 if (errorMessage.length) {
49 psModule.emitError(errorMessage);
50 }
51 return reject(new Error('bundling failed'))
52 }
53
54 cache.bundle = stderr
55
56 resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`))
57 })
58 }))
59};