]>
Commit | Line | Data |
---|---|---|
531c751f | 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 | function compile(psModule) { | |
16 | const options = psModule.options | |
17 | const cache = psModule.cache | |
18 | const stderr = [] | |
19 | ||
20 | if (cache.compilationStarted) return Promise.resolve(psModule) | |
21 | ||
22 | cache.compilationStarted = true | |
23 | ||
24 | const args = dargs(Object.assign({ | |
25 | _: options.src, | |
26 | output: options.output, | |
27 | }, options.pscArgs)) | |
28 | ||
29 | debug('spawning compiler %s %o', options.psc, args) | |
30 | ||
31 | return (new Promise((resolve, reject) => { | |
514ab146 | 32 | debug('compiling PureScript...') |
531c751f | 33 | |
34 | const compilation = spawn(options.psc, args) | |
35 | ||
b683b0b1 | 36 | compilation.stderr.on('data', data => { |
37 | stderr.push(data.toString()); | |
38 | }); | |
531c751f | 39 | |
40 | compilation.on('close', code => { | |
514ab146 | 41 | debug('finished compiling PureScript.') |
531c751f | 42 | cache.compilationFinished = true |
43 | if (code !== 0) { | |
45c62a2c | 44 | const errorMessage = stderr.join(''); |
45 | if (errorMessage.length) { | |
46 | psModule.emitError(errorMessage); | |
47 | } | |
df8798fa | 48 | if (options.watch) { |
49 | resolve(psModule); | |
50 | } | |
51 | else { | |
52 | reject(new Error('compilation failed')) | |
53 | } | |
531c751f | 54 | } else { |
45c62a2c | 55 | const warningMessage = stderr.join(''); |
56 | if (options.warnings && warningMessage.length) { | |
57 | psModule.emitWarning(warningMessage); | |
58 | } | |
531c751f | 59 | resolve(psModule) |
60 | } | |
61 | }) | |
62 | })) | |
63 | .then(compilerOutput => { | |
64 | if (options.bundle) { | |
65 | return bundle(options, cache).then(() => psModule) | |
66 | } | |
67 | return psModule | |
68 | }) | |
69 | } | |
70 | module.exports.compile = compile; | |
71 | ||
72 | function bundle(options, cache) { | |
73 | if (cache.bundle) return Promise.resolve(cache.bundle) | |
74 | ||
75 | const stdout = [] | |
76 | const stderr = cache.bundle = [] | |
77 | ||
78 | const args = dargs(Object.assign({ | |
79 | _: [path.join(options.output, '*', '*.js')], | |
80 | output: options.bundleOutput, | |
81 | namespace: options.bundleNamespace, | |
82 | }, options.pscBundleArgs)) | |
83 | ||
84 | cache.bundleModules.forEach(name => args.push('--module', name)) | |
85 | ||
86 | debug('spawning bundler %s %o', options.pscBundle, args.join(' ')) | |
87 | ||
88 | return (new Promise((resolve, reject) => { | |
514ab146 | 89 | debug('bundling PureScript...') |
531c751f | 90 | |
91 | const compilation = spawn(options.pscBundle, args) | |
92 | ||
93 | compilation.stdout.on('data', data => stdout.push(data.toString())) | |
94 | compilation.stderr.on('data', data => stderr.push(data.toString())) | |
95 | compilation.on('close', code => { | |
514ab146 | 96 | debug('finished bundling PureScript.') |
531c751f | 97 | if (code !== 0) { |
45c62a2c | 98 | const errorMessage = stderr.join(''); |
99 | if (errorMessage.length) { | |
100 | psModule.emitError(errorMessage); | |
101 | } | |
4b99e432 | 102 | return reject(new Error('bundling failed')) |
531c751f | 103 | } |
104 | cache.bundle = stderr | |
896c11ec | 105 | resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`)) |
531c751f | 106 | }) |
107 | })) | |
108 | } |