]> git.immae.eu Git - github/fretlink/purs-loader.git/blobdiff - src/index.js
Caching the result of psc-package
[github/fretlink/purs-loader.git] / src / index.js
index 9db8a6337a0afe31d0f02e32b6de48bda168e4e3..19a549e3aa06b8bbf15b1f87991c6260ebabdfdc 100644 (file)
 'use strict'
 
-const colors = require('chalk')
-const debug = require('debug')('purs-loader')
+const debug_ = require('debug');
+
+const debug = debug_('purs-loader');
+
+const debugVerbose = debug_('purs-loader:verbose');
+
 const loaderUtils = require('loader-utils')
-const globby = require('globby')
+
 const Promise = require('bluebird')
-const fs = Promise.promisifyAll(require('fs'))
-const spawn = require('child_process').spawn
+
 const path = require('path')
-const retryPromise = require('promise-retry')
 
-const psModuleRegex = /(?:^|\n)module\s+([\w\.]+)/i
-const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g
+const PsModuleMap = require('./purs-module-map');
+
+const compile = require('./compile');
+
+const bundle = require('./bundle');
+
+const ide = require('./ide');
+
+const toJavaScript = require('./to-javascript');
+
+const dargs = require('./dargs');
+
+const spawn = require('cross-spawn').sync
+
+const eol = require('os').EOL
 
 module.exports = function purescriptLoader(source, map) {
-  const callback = this.async()
-  const config = this.options
-  const query = loaderUtils.parseQuery(this.query)
-  const webpackOptions = this.options.purescriptLoader || {}
+  this.cacheable && this.cacheable();
+
+  const webpackConfig = this.options;
+
+  var cache = webpackConfig.purescriptLoaderCache = webpackConfig.purescriptLoaderCache || {
+    rebuild: false,
+    deferred: [],
+    bundleModules: [],
+    ideServer: null,
+    psModuleMap: null,
+    warnings: [],
+    errors: [],
+    compilationStarted: false,
+    compilationFinished: false,
+    installed: false,
+    srcOption: []
+  };
+
+  const callback = this.async();
+
+  const loaderOptions = loaderUtils.getOptions(this) || {};
+
+  const srcOption = (pscPackage => {
+    const srcPath = path.join('src', '**', '*.purs');
+
+    const bowerPath = path.join('bower_components', 'purescript-*', 'src', '**', '*.purs');
+
+    if (cache.srcOption.length > 0) {
+      return cache.srcOption;
+    }
+    else if (pscPackage) {
+      const pscPackageCommand = 'psc-package';
+
+      const pscPackageArgs = ['sources'];
+
+      const loaderSrc = loaderOptions.src || [
+        srcPath
+      ];
+
+      debug('psc-package %s %o', pscPackageCommand, pscPackageArgs);
+
+      const cmd = spawn(pscPackageCommand, pscPackageArgs);
+
+      if (cmd.status !== 0) {
+        const error = cmd.stdout.toString();
+
+        throw new Error(error);
+      }
+      else {
+        const result = cmd.stdout.toString().split(eol).filter(v => v != '').concat(loaderSrc);
+
+        debug('psc-package result: %o', result);
+
+        cache.srcOption = result;
+
+        return result;
+      }
+    }
+    else {
+      const result = loaderOptions.src || [
+        bowerPath,
+        srcPath
+      ];
+
+      cache.srcOption = result;
+
+      return result;
+    }
+  })(loaderOptions.pscPackage);
 
   const options = Object.assign({
-    context: config.context,
-    psc: 'psc',
+    context: webpackConfig.context,
+    psc: null,
     pscArgs: {},
-    pscBundle: 'psc-bundle',
+    pscBundle: null,
     pscBundleArgs: {},
     pscIde: false,
-    pscIdeColors: webpackOptions.psc === 'psa' || query.psc === 'psa',
+    pscIdeColors: loaderOptions.psc === 'psa',
     pscIdeArgs: {},
+    pscPackage: false,
     bundleOutput: 'output/bundle.js',
     bundleNamespace: 'PS',
     bundle: false,
     warnings: true,
+    watch: false,
     output: 'output',
-    src: [
-      path.join('src', '**', '*.purs'),
-      path.join('bower_components', 'purescript-*', 'src', '**', '*.purs')
-    ],
-    ffi: [
-      path.join('src', '**', '*.js'),
-      path.join('bower_components', 'purescript-*', 'src', '**', '*.js')
-    ],
-  }, webpackOptions, query)
-
-  this.cacheable && this.cacheable()
-
-  let cache = config.purescriptLoaderCache = config.purescriptLoaderCache || {
-    rebuild: false,
-    deferred: [],
-    bundleModules: [],
-  }
+    src: []
+  }, loaderOptions, {
+    src: srcOption
+  });
 
-  if (!config.purescriptLoaderInstalled) {
-    config.purescriptLoaderInstalled = true
+  if (!cache.installed) {
+    debugVerbose('installing purs-loader with options: %O', options);
+
+    cache.installed = true;
 
     // invalidate loader cache when bundle is marked as invalid (in watch mode)
     this._compiler.plugin('invalid', () => {
-      cache = config.purescriptLoaderCache = {
+      debugVerbose('invalidating loader cache');
+
+      cache = webpackConfig.purescriptLoaderCache = {
         rebuild: options.pscIde,
         deferred: [],
-        ideServer: cache.ideServer
-      }
-    })
+        bundleModules: [],
+        ideServer: cache.ideServer,
+        psModuleMap: cache.psModuleMap,
+        warnings: [],
+        errors: [],
+        compilationStarted: cache.compilationStarted,
+        compilationFinished: cache.compilationFinished,
+        installed: cache.installed,
+        srcOption: cache.srcOption
+      };
+    });
 
     // add psc warnings to webpack compilation warnings
     this._compiler.plugin('after-compile', (compilation, callback) => {
-      if (options.warnings && cache.warnings && cache.warnings.length) {
-        compilation.warnings.unshift(`PureScript compilation:\n${cache.warnings.join('')}`)
-      }
+      cache.warnings.forEach(warning => {
+        compilation.warnings.push(warning);
+      });
 
-      if (cache.errors && cache.errors.length) {
-        compilation.errors.unshift(`PureScript compilation:\n${cache.errors.join('\n')}`)
-      }
+      cache.errors.forEach(error => {
+        compilation.errors.push(error);
+      });
 
       callback()
-    })
+    });
   }
 
-  const psModuleName = match(psModuleRegex, source)
+  const psModuleName = PsModuleMap.matchModule(source);
+
   const psModule = {
     name: psModuleName,
     load: js => callback(null, js),
@@ -87,385 +169,144 @@ module.exports = function purescriptLoader(source, map) {
     jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
     options: options,
     cache: cache,
+    emitWarning: warning => {
+      if (options.warnings && warning.length) {
+        cache.warnings.push(warning);
+      }
+    },
+    emitError: error => {
+      if (error.length) {
+        cache.errors.push(error);
+      }
+    }
   }
 
+  debug('loading %s', psModule.name);
+
   if (options.bundle) {
-    cache.bundleModules.push(psModule.name)
+    cache.bundleModules.push(psModule.name);
   }
 
   if (cache.rebuild) {
-    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);
+          })
+        ;
       }
-    })
-  }))
-  .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)
-
-    ideClient.stdout.once('data', data => {
-      let res = null
-
-      try {
-        res = JSON.parse(data.toString())
-        debug(res)
-      } catch (err) {
-        return reject(err)
+      else {
+        return Promise.resolve(psModule);
       }
+    };
 
-      if (res && !Array.isArray(res.result)) {
-        return res.resultType === 'success'
-               ? resolve(psModule)
-               : reject('psc-ide rebuild failed')
-      }
+    const rebuild = () =>
+      ide.rebuild(psModule).catch(error => {
+        if (error instanceof ide.UnknownModuleError) {
+          if (!cache.compilationStarted) {
+            cache.compilationStarted = true;
 
-      Promise.map(res.result, (item, i) => {
-        debug(item)
-        return formatIdeResult(item, options, i, res.result.length)
-      })
-      .then(compileMessages => {
-        if (res.resultType === 'error') {
-          if (res.result.some(item => item.errorCode === 'UnknownModule')) {
-            console.log('Unknown module, attempting full recompile')
             return compile(psModule)
-              .then(() => request({ command: 'load' }))
-              .then(resolve)
-              .catch(() => reject('psc-ide rebuild failed'))
+              .then(() => {
+                cache.compilationFinished = true;
+              })
+              .then(() =>
+                PsModuleMap.makeMap(options.src).then(map => {
+                  debug('rebuilt module map after unknown module forced a recompilation');
+
+                  cache.psModuleMap = map;
+                })
+              )
+              .then(() => ide.load(psModule))
+              .then(() => psModule)
+            ;
+          }
+          else {
+            return Promise.resolve(psModule);
           }
-          cache.errors = compileMessages
-          reject('psc-ide rebuild failed')
-        } else {
-          cache.warnings = compileMessages
-          resolve(psModule)
         }
-      })
-    })
-
-    ideClient.stderr.once('data', data => reject(data.toString()))
-
-    ideClient.stdin.write(JSON.stringify(body))
-    ideClient.stdin.write('\n')
-  })
+        else {
+          debug('ide rebuild failed due to an unhandled error: %o', error);
 
-  return request({
-    command: 'rebuild',
-    params: {
-      file: psModule.srcPath,
-    }
-  })
-}
-
-function formatIdeResult(result, options, index, length) {
-  const srcPath = path.relative(options.context, result.filename)
-  const pos = result.position
-  const fileAndPos = `${srcPath}:${pos.startLine}:${pos.startColumn}`
-  let numAndErr = `[${index+1}/${length} ${result.errorCode}]`
-  numAndErr = options.pscIdeColors ? colors.yellow(numAndErr) : numAndErr
-
-  return fs.readFileAsync(result.filename, 'utf8').then(source => {
-    const lines = source.split('\n').slice(pos.startLine - 1, pos.endLine)
-    const endsOnNewline = pos.endColumn === 1 && pos.startLine !== pos.endLine
-    const up = options.pscIdeColors ? colors.red('^') : '^'
-    const down = options.pscIdeColors ? colors.red('v') : 'v'
-    let trimmed = lines.slice(0)
-
-    if (endsOnNewline) {
-      lines.splice(lines.length - 1, 1)
-      pos.endLine = pos.endLine - 1
-      pos.endColumn = lines[lines.length - 1].length || 1
-    }
-
-    // strip newlines at the end
-    if (endsOnNewline) {
-      trimmed = lines.reverse().reduce((trimmed, line, i) => {
-        if (i === 0 && line === '') trimmed.trimming = true
-        if (!trimmed.trimming) trimmed.push(line)
-        if (trimmed.trimming && line !== '') {
-          trimmed.trimming = false
-          trimmed.push(line)
+          return Promise.reject(error);
         }
-        return trimmed
-      }, []).reverse()
-      pos.endLine = pos.endLine - (lines.length - trimmed.length)
-      pos.endColumn = trimmed[trimmed.length - 1].length || 1
-    }
-
-    const spaces = ' '.repeat(String(pos.endLine).length)
-    let snippet = trimmed.map((line, i) => {
-      return `  ${pos.startLine + i}  ${line}`
-    }).join('\n')
-
-    if (trimmed.length === 1) {
-      snippet += `\n  ${spaces}  ${' '.repeat(pos.startColumn - 1)}${up.repeat(pos.endColumn - pos.startColumn + 1)}`
-    } else {
-      snippet = `  ${spaces}  ${' '.repeat(pos.startColumn - 1)}${down}\n${snippet}`
-      snippet += `\n  ${spaces}  ${' '.repeat(pos.endColumn - 1)}${up}`
-    }
-
-    return Promise.resolve(
-      `\n${numAndErr} ${fileAndPos}\n\n${snippet}\n\n${result.message}`
-    )
-  })
-}
-
-function bundle(options, cache) {
-  if (cache.bundle) return Promise.resolve(cache.bundle)
-
-  const stdout = []
-  const stderr = cache.bundle = []
-
-  const args = dargs(Object.assign({
-    _: [path.join(options.output, '*', '*.js')],
-    output: options.bundleOutput,
-    namespace: options.bundleNamespace,
-  }, options.pscBundleArgs))
-
-  cache.bundleModules.forEach(name => args.push('--module', name))
-
-  debug('spawning bundler %s %o', options.pscBundle, args.join(' '))
-
-  return (new Promise((resolve, reject) => {
-    console.log('Bundling PureScript...')
-
-    const compilation = spawn(options.pscBundle, args)
-
-    compilation.stdout.on('data', data => stdout.push(data.toString()))
-    compilation.stderr.on('data', data => stderr.push(data.toString()))
-    compilation.on('close', code => {
-      if (code !== 0) {
-        cache.errors.concat(stderr)
-        return reject(true)
-      }
-      cache.bundle = stderr
-      resolve(fs.appendFileAsync('output/bundle.js', `module.exports = ${options.bundleNamespace}`))
-    })
-  }))
-}
-
-// map of PS module names to their source path
-function psModuleMap(globs, cache) {
-  if (cache.psModuleMap) return Promise.resolve(cache.psModuleMap)
-
-  return globby(globs).then(paths => {
-    return Promise
-      .props(paths.reduce((map, file) => {
-        map[file] = fs.readFileAsync(file, 'utf8')
-        return map
-      }, {}))
-      .then(srcMap => {
-        cache.psModuleMap = Object.keys(srcMap).reduce((map, file) => {
-          const source = srcMap[file]
-          const psModuleName = match(psModuleRegex, source)
-          map[psModuleName] = path.resolve(file)
-          return map
-        }, {})
-        return cache.psModuleMap
       })
-  })
-}
+    ;
 
-function connectIdeServer(psModule) {
-  const options = psModule.options
-  const cache = psModule.cache
-
-  if (cache.ideServer) return Promise.resolve(psModule)
+    connect()
+      .then(rebuild)
+      .then(toJavaScript)
+      .then(psModule.load)
+      .catch(psModule.reject)
+    ;
+  }
+  else if (cache.compilationFinished) {
+    debugVerbose('compilation is already finished, loading module %s', psModule.name);
 
-  cache.ideServer = true
+    toJavaScript(psModule)
+      .then(psModule.load)
+      .catch(psModule.reject);
+  }
+  else {
+    // The compilation has not finished yet. We need to wait for
+    // compilation to finish before the loaders run so that references
+    // to compiled output are valid. Push the modules into the cache to
+    // be loaded once the complation is complete.
 
-  const connect = () => new Promise((resolve, reject) => {
-    const args = dargs(options.pscIdeArgs)
+    cache.deferred.push(psModule);
 
-    debug('attempting to connect to psc-ide-server', args)
+    if (!cache.compilationStarted) {
+      cache.compilationStarted = true;
 
-    const ideClient = spawn('psc-ide-client', args)
+      compile(psModule)
+        .then(() => {
+          cache.compilationFinished = true;
+        })
+        .then(() => {
+          if (options.bundle) {
+            return bundle(options, cache.bundleModules);
+          }
+        })
+        .then(() =>
+          PsModuleMap.makeMap(options.src).then(map => {
+            debug('rebuilt module map after compilation');
 
-    ideClient.stderr.on('data', data => {
-      debug(data.toString())
-      cache.ideServer = false
-      reject(true)
-    })
-    ideClient.stdout.once('data', data => {
-      debug(data.toString())
-      if (data.toString()[0] === '{') {
-        const res = JSON.parse(data.toString())
-        if (res.resultType === 'success') {
-          cache.ideServer = ideServer
-          resolve(psModule)
-        } else {
-          cache.ideServer = ideServer
-          reject(true)
-        }
-      } else {
-        cache.ideServer = false
-        reject(true)
-      }
-    })
-    ideClient.stdin.resume()
-    ideClient.stdin.write(JSON.stringify({ command: 'load' }))
-    ideClient.stdin.write('\n')
-  })
-
-  const args = dargs(Object.assign({
-    outputDirectory: options.output,
-  }, options.pscIdeArgs))
-
-  debug('attempting to start psc-ide-server', args)
-
-  const ideServer = cache.ideServer = spawn('psc-ide-server', [])
-  ideServer.stderr.on('data', data => {
-    debug(data.toString())
-  })
-
-  return retryPromise((retry, number) => {
-    return connect().catch(error => {
-      if (!cache.ideServer && number === 9) {
-        debug(error)
-
-        console.log(
-          'failed to connect to or start psc-ide-server, ' +
-          'full compilation will occur on rebuild'
+            cache.psModuleMap = map;
+          })
         )
+        .then(() =>
+          Promise.map(cache.deferred, psModule =>
+            toJavaScript(psModule).then(psModule.load)
+          )
+        )
+        .catch(error => {
+          cache.deferred[0].reject(error);
 
-        return Promise.resolve(psModule)
-      }
-
-      return retry(error)
-    })
-  }, {
-    retries: 9,
-    factor: 1,
-    minTimeout: 333,
-    maxTimeout: 333,
-  })
-}
-
-function match(regex, str) {
-  const matches = str.match(regex)
-  return matches && matches[1]
-}
-
-function dargs(obj) {
-  return Object.keys(obj).reduce((args, key) => {
-    const arg = '--' + key.replace(/[A-Z]/g, '-$&').toLowerCase();
-    const val = obj[key]
-
-    if (key === '_') val.forEach(v => args.push(v))
-    else if (Array.isArray(val)) val.forEach(v => args.push(arg, v))
-    else args.push(arg, obj[key])
-
-    return args
-  }, [])
+          cache.deferred.slice(1).forEach(psModule => {
+            psModule.reject(new Error('purs-loader failed'));
+          })
+        })
+      ;
+    }
+    else {
+      // The complation has started. Nothing to do but wait until it is
+      // done before loading all of the modules.
+    }
+  }
 }