]>
Commit | Line | Data |
---|---|---|
1c12889c | 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 | ||
7f0547d4 | 15 | module.exports = function bundle(options, bundleModules) { |
1c12889c | 16 | const stdout = [] |
17 | ||
7f0547d4 | 18 | const stderr = [] |
1c12889c | 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 | ||
7f0547d4 | 28 | bundleModules.forEach(name => bundleArgs.push('--module', name)) |
1c12889c | 29 | |
78e2b0d9 | 30 | debug('bundle: %s %O', bundleCommand, bundleArgs); |
1c12889c | 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(''); | |
7f0547d4 | 46 | |
1c12889c | 47 | if (errorMessage.length) { |
9020b66f | 48 | reject(new Error(`bundling failed: ${errorMessage}`)) |
49 | } | |
50 | else { | |
51 | reject(new Error('bundling failed')) | |
1c12889c | 52 | } |
7f0547d4 | 53 | } |
54 | else { | |
55 | resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`)) | |
56 | } | |
1c12889c | 57 | }) |
58 | })) | |
59 | }; |