X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbundle.js;fp=src%2Fbundle.js;h=6627ffeb2b1a9b76620b62b8f953b4dc5dfdef97;hb=1c12889c0adf91cf3116a9d5ff44b7466b1dfcc9;hp=0000000000000000000000000000000000000000;hpb=e1bf1f98de548fb2cbaca1a2b6c1d4cb3e173848;p=github%2Ffretlink%2Fpurs-loader.git 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 @@ +'use strict'; + +const path = require('path'); + +const Promise = require('bluebird') + +const fs = Promise.promisifyAll(require('fs')) + +const spawn = require('cross-spawn') + +const debug = require('debug')('purs-loader'); + +const dargs = require('./dargs'); + +module.exports = function bundle(options, cache) { + if (cache.bundle) return Promise.resolve(cache.bundle) + + const stdout = [] + + const stderr = cache.bundle = [] + + const bundleCommand = options.pscBundle || 'purs'; + + const bundleArgs = (options.pscBundle ? [] : [ 'bundle' ]).concat(dargs(Object.assign({ + _: [path.join(options.output, '*', '*.js')], + output: options.bundleOutput, + namespace: options.bundleNamespace, + }, options.pscBundleArgs))); + + cache.bundleModules.forEach(name => bundleArgs.push('--module', name)) + + debug('spawning bundler %s %o', bundleCommand, bundleArgs); + + return (new Promise((resolve, reject) => { + debug('bundling PureScript...') + + const compilation = spawn(bundleCommand, bundleArgs) + + compilation.stdout.on('data', data => stdout.push(data.toString())) + + compilation.stderr.on('data', data => stderr.push(data.toString())) + + compilation.on('close', code => { + debug('finished bundling PureScript.') + + if (code !== 0) { + const errorMessage = stderr.join(''); + if (errorMessage.length) { + psModule.emitError(errorMessage); + } + return reject(new Error('bundling failed')) + } + + cache.bundle = stderr + + resolve(fs.appendFileAsync(options.bundleOutput, `module.exports = ${options.bundleNamespace}`)) + }) + })) +};