- return connectIdeServer(psModule)
- .then(rebuild)
- .then(toJavaScript)
- .then(psModule.load)
- .catch(psModule.reject)
- }
-
- if (cache.compilationFinished) {
- return toJavaScript(psModule).then(psModule.load).catch(psModule.reject)
- }
-
- // We need to wait for compilation to finish before the loaders run so that
- // references to compiled output are valid.
- cache.deferred.push(psModule)
-
- if (!cache.compilationStarted) {
- return compile(psModule)
- .then(() => Promise.map(cache.deferred, psModule => {
- if (typeof cache.ideServer === 'object') cache.ideServer.kill()
- return toJavaScript(psModule).then(psModule.load)
- }))
- .catch(error => {
- cache.deferred[0].reject(error)
- cache.deferred.slice(1).forEach(psModule => psModule.reject(true))
- })
- }
-}
-
-// The actual loader is executed *after* purescript compilation.
-function toJavaScript(psModule) {
- const options = psModule.options
- const cache = psModule.cache
- const bundlePath = path.resolve(options.bundleOutput)
- const jsPath = cache.bundle ? bundlePath : psModule.jsPath
-
- debug('loading JavaScript for', psModule.name)
-
- return Promise.props({
- js: fs.readFileAsync(jsPath, 'utf8'),
- psModuleMap: psModuleMap(options, cache)
- }).then(result => {
- let js = ''
-
- if (options.bundle) {
- // if bundling, return a reference to the bundle
- js = 'module.exports = require("'
- + path.relative(psModule.srcDir, options.bundleOutput)
- + '")["' + psModule.name + '"]'
- } else {
- // replace require paths to output files generated by psc with paths
- // to purescript sources, which are then also run through this loader.
- js = result.js
- .replace(requireRegex, (m, p1) => {
- return 'require("' + result.psModuleMap[p1].src + '")'
- })
- .replace(/require\(['"]\.\/foreign['"]\)/g, (m, p1) => {
- return 'require("' + result.psModuleMap[psModule.name].ffi + '")'
- })
- }
-
- return js
- })
-}
-
-function compile(psModule) {
- const options = psModule.options
- const cache = psModule.cache
- const stderr = []
-
- if (cache.compilationStarted) return Promise.resolve(psModule)
-
- cache.compilationStarted = true
-
- const args = dargs(Object.assign({
- _: options.src,
- ffi: options.ffi,
- output: options.output,
- }, options.pscArgs))
-
- debug('spawning compiler %s %o', options.psc, args)
-
- return (new Promise((resolve, reject) => {
- console.log('\nCompiling PureScript...')
-
- const compilation = spawn(options.psc, args)
-
- compilation.stdout.on('data', data => stderr.push(data.toString()))
- compilation.stderr.on('data', data => stderr.push(data.toString()))
-
- compilation.on('close', code => {
- console.log('Finished compiling PureScript.')
- cache.compilationFinished = true
- if (code !== 0) {
- cache.errors = stderr.join('')
- reject(true)
- } else {
- cache.warnings = stderr.join('')
- resolve(psModule)
- }
- })
- }))
- .then(compilerOutput => {
- if (options.bundle) {
- return bundle(options, cache).then(() => psModule)
- }
- return psModule
- })
-}
-
-function rebuild(psModule) {
- const options = psModule.options
- const cache = psModule.cache
-
- debug('attempting rebuild with psc-ide-client %s', psModule.srcPath)
-
- const request = (body) => new Promise((resolve, reject) => {
- const args = dargs(options.pscIdeArgs)
- const ideClient = spawn('psc-ide-client', args)
-
- var stdout = ''
- var stderr = ''
-
- ideClient.stdout.on('data', data => {
- stdout = stdout + data.toString()
- })
-
- ideClient.stderr.on('data', data => {
- stderr = stderr + data.toString()
- })
-
- ideClient.on('close', code => {
- if (code !== 0) {
- const error = stderr === '' ? 'Failed to spawn psc-ide-client' : stderr
- return reject(new Error(error))