diff options
Diffstat (limited to 'src/bundle.js')
-rw-r--r-- | src/bundle.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/bundle.js b/src/bundle.js new file mode 100644 index 0000000..6627ffe --- /dev/null +++ b/src/bundle.js | |||
@@ -0,0 +1,59 @@ | |||
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, 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 | }; | ||