- return connectIdeServer(psModule)
- .then(rebuild)
- .then(toJavaScript)
- .then(psModule.load)
- .catch(psModule.reject)
- }
-
- if (cache.compilation && cache.compilation.length) {
- 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.compilation) {
- 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.srcPath)
-
- return Promise.props({
- js: fs.readFileAsync(jsPath, 'utf8'),
- psModuleMap: psModuleMap(options.src, 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.
- const foreignRequire = 'require("' + path.resolve(
- path.join(psModule.options.output, psModule.name, 'foreign.js')
- ) + '")'
-
- js = result.js
- .replace(requireRegex, (m, p1) => {
- return 'require("' + result.psModuleMap[p1] + '")'
- })
- .replace(/require\(['"]\.\/foreign['"]\)/g, foreignRequire)
- }
-
- return js
- })
-}
-
-function compile(psModule) {
- const options = psModule.options
- const cache = psModule.cache
- const stderr = []
-
- if (cache.compilation) return Promise.resolve(cache.compilation)
-
- cache.compilation = []
- cache.warnings = []
- cache.errors = []
-
-
- 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.stderr.on('data', data => stderr.push(data.toString()))
-
- compilation.on('close', code => {
- console.log('Finished compiling PureScript.')
- if (code !== 0) {
- cache.compilation = cache.errors = stderr
- reject(true)
- } else {
- cache.compilation = cache.warnings = stderr
- resolve(psModule)
+ const connect = () => {
+ if (!cache.ideServer) {
+ cache.ideServer = true;
+
+ return ide.connect(psModule)
+ .then(ideServer => {
+ cache.ideServer = ideServer;
+ return psModule;
+ })
+ .then(ide.loadWithRetry)
+ .catch(error => {
+ if (cache.ideServer.kill) {
+ debug('ide failed to initially load modules, stopping the ide server process');
+
+ cache.ideServer.kill();
+ }
+
+ cache.ideServer = null;
+
+ return Promise.reject(error);
+ })
+ ;