]> git.immae.eu Git - github/fretlink/purs-loader.git/commitdiff
Merge pull request #77 from ethul/topic/psc-ide
authoreric <thul.eric@gmail.com>
Sat, 19 Nov 2016 19:09:12 +0000 (14:09 -0500)
committerGitHub <noreply@github.com>
Sat, 19 Nov 2016 19:09:12 +0000 (14:09 -0500)
Topic/psc ide

src/Psc.js
src/PscIde.js
src/index.js

index 0aa9fe2f90c5e2929d060fdce739c8c315259008..8fe843751442b42b2dae5de7ca4374d996b03661 100644 (file)
@@ -29,7 +29,7 @@ function compile(psModule) {
   debug('spawning compiler %s %o', options.psc, args)
 
   return (new Promise((resolve, reject) => {
-    debug('\nCompiling PureScript...')
+    debug('compiling PureScript...')
 
     const compilation = spawn(options.psc, args)
 
@@ -37,13 +37,19 @@ function compile(psModule) {
     compilation.stderr.on('data', data => stderr.push(data.toString()))
 
     compilation.on('close', code => {
-      debug('Finished compiling PureScript.')
+      debug('finished compiling PureScript.')
       cache.compilationFinished = true
       if (code !== 0) {
-        cache.errors = stderr.join('')
+        const errorMessage = stderr.join('');
+        if (errorMessage.length) {
+          psModule.emitError(errorMessage);
+        }
         reject(new Error('compilation failed'))
       } else {
-        cache.warnings = stderr.join('')
+        const warningMessage = stderr.join('');
+        if (options.warnings && warningMessage.length) {
+          psModule.emitWarning(warningMessage);
+        }
         resolve(psModule)
       }
     })
@@ -74,15 +80,19 @@ function bundle(options, cache) {
   debug('spawning bundler %s %o', options.pscBundle, args.join(' '))
 
   return (new Promise((resolve, reject) => {
-    debug('Bundling PureScript...')
+    debug('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 => {
+      debug('finished bundling PureScript.')
       if (code !== 0) {
-        cache.errors = (cache.errors || '') + stderr.join('')
+        const errorMessage = stderr.join('');
+        if (errorMessage.length) {
+          psModule.emitError(errorMessage);
+        }
         return reject(new Error('bundling failed'))
       }
       cache.bundle = stderr
index c9d0cfa5b5fc143bc3d8d195120f1ecb0c2ecfa5..27b49f6098ede154a3855d6800318e65eb868592 100644 (file)
@@ -77,10 +77,7 @@ function connect(psModule) {
       if (!cache.ideServer && number === 9) {
         debug(error)
 
-        console.log(
-          'failed to connect to or start psc-ide-server, ' +
-          'full compilation will occur on rebuild'
-        )
+        console.warn('Failed to connect to or start psc-ide-server. A full compilation will occur on rebuild');
 
         return Promise.resolve(psModule)
       }
@@ -133,9 +130,7 @@ function rebuild(psModule) {
       }
 
       if (res && !Array.isArray(res.result)) {
-        return res.resultType === 'success'
-               ? resolve(psModule)
-               : reject(new Error('psc-ide rebuild failed'))
+        return resolve(psModule);
       }
 
       Promise.map(res.result, (item, i) => {
@@ -144,7 +139,13 @@ function rebuild(psModule) {
       })
       .then(compileMessages => {
         if (res.resultType === 'error') {
-          if (res.result.some(item => item.errorCode === 'UnknownModule' || item.errorCode === 'UnknownName')) {
+          if (res.result.some(item => {
+            const isUnknownModule = item.errorCode === 'UnknownModule';
+
+            const isUnknownModuleImport = item.errorCode === 'UnknownName' && /Unknown module/.test(item.message);
+
+            return isUnknownModule || isUnknownModuleImport;
+          })) {
             debug('unknown module, attempting full recompile')
             return Psc.compile(psModule)
               .then(() => PsModuleMap.makeMap(options.src).then(map => {
@@ -153,13 +154,19 @@ function rebuild(psModule) {
               }))
               .then(() => request({ command: 'load' }))
               .then(resolve)
-              .catch(() => reject(new Error('psc-ide rebuild failed')))
+              .catch(() => resolve(psModule))
+          }
+          const errorMessage = compileMessages.join('\n');
+          if (errorMessage.length) {
+            psModule.emitError(errorMessage);
           }
-          cache.errors = compileMessages.join('\n')
-          reject(new Error('psc-ide rebuild failed'))
+          resolve(psModule);
         } else {
-          cache.warnings = compileMessages.join('\n')
-          resolve(psModule)
+          const warningMessage = compileMessages.join('\n');
+          if (options.warnings && warningMessage.length) {
+            psModule.emitWarning(warningMessage);
+          }
+          resolve(psModule);
         }
       })
     })
index f3b2fdec2205ed0ac88679bd8ee94e8e918d1fa9..7c296508a4e8b81cdb683d6351fd78b849f86984 100644 (file)
@@ -62,21 +62,6 @@ module.exports = function purescriptLoader(source, map) {
         psModuleMap: cache.psModuleMap
       }
     })
-
-    // add psc warnings to webpack compilation warnings
-    this._compiler.plugin('after-compile', (compilation, callback) => {
-      if (options.warnings && cache.warnings) {
-        compilation.warnings.unshift(`PureScript compilation:\n${cache.warnings}`)
-        cache.warnings = null;
-      }
-
-      if (cache.errors) {
-        compilation.errors.unshift(`PureScript compilation:\n${cache.errors}`)
-        cache.errors = null;
-      }
-
-      callback()
-    })
   }
 
   const psModuleName = PsModuleMap.match(source)
@@ -89,6 +74,8 @@ module.exports = function purescriptLoader(source, map) {
     jsPath: path.resolve(path.join(options.output, psModuleName, 'index.js')),
     options: options,
     cache: cache,
+    emitWarning: warning => this.emitWarning(warning),
+    emitError: error => this.emitError(error)
   }
 
   debug('loader called', psModule.name)