]> git.immae.eu Git - github/fretlink/purs-loader.git/blobdiff - src/index.js
Use 'spago path output' to choose default output path (#132)
[github/fretlink/purs-loader.git] / src / index.js
index 13bc5aedbac35b987f531d70523f71257c99fabd..ff1330232ec0b6d764f2828e85535357a55d75a4 100644 (file)
@@ -39,9 +39,71 @@ var CACHE_VAR = {
   compilationStarted: false,
   compilationFinished: false,
   installed: false,
-  srcOption: []
+  srcOption: [],
+  spagoOutputPath: null
 };
 
+// include src files provided by psc-package or Spago
+function requestDependencySources(packagerCommand, srcPath, loaderOptions) {
+  const packagerArgs = ['sources'];
+
+  const loaderSrc = loaderOptions.src || [
+    srcPath
+  ];
+
+  debug('%s %o', packagerCommand, packagerArgs);
+
+  const cmd = spawn(packagerCommand, packagerArgs);
+
+  if (cmd.error) {
+    throw new Error(cmd.error);
+  }
+  else 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('%s result: %o', packagerCommand, result);
+
+    CACHE_VAR.srcOption = result;
+
+    return result;
+  }
+}
+
+// 'spago output path' will return the output folder in a monorepo
+function getSpagoSources() {
+  const cachedVal = CACHE_VAR.spagoOutputPath;
+  if (cachedVal) {
+    return cachedVal
+  }
+  const command = "spago"
+  const args = ["path", "output"]
+
+  const cmd = spawn(command, args);
+
+  if (cmd.error) {
+    throw new Error(cmd.error);
+  }
+  else if (cmd.status !== 0) {
+    const error = cmd.stdout.toString();
+
+    throw new Error(error);
+  }
+  else {
+    const result = cmd.stdout.toString().split(eol)[0]
+
+    debug('"spago path output" result: %o', result);
+
+    CACHE_VAR.spagoOutputPath = result;
+
+    return result;
+  }
+}
+
 module.exports = function purescriptLoader(source, map) {
   this.cacheable && this.cacheable();
 
@@ -51,7 +113,7 @@ module.exports = function purescriptLoader(source, map) {
 
   const loaderOptions = loaderUtils.getOptions(this) || {};
 
-  const srcOption = (pscPackage => {
+  const srcOption = ((pscPackage, spago) => {
     const srcPath = path.join('src', '**', '*.purs');
 
     const bowerPath = path.join('bower_components', 'purescript-*', 'src', '**', '*.purs');
@@ -60,36 +122,11 @@ module.exports = function purescriptLoader(source, map) {
       return CACHE_VAR.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.error) {
-        throw new Error(cmd.error);
-      }
-      else 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_VAR.srcOption = result;
-
-        return result;
-      }
+      return requestDependencySources('psc-package', srcPath, loaderOptions)
     }
+    else if (spago) {
+      return requestDependencySources('spago', srcPath, loaderOptions)
+    } 
     else {
       const result = loaderOptions.src || [
         bowerPath,
@@ -100,7 +137,9 @@ module.exports = function purescriptLoader(source, map) {
 
       return result;
     }
-  })(loaderOptions.pscPackage);
+  })(loaderOptions.pscPackage, loaderOptions.spago);
+  
+  const outputPath = loaderOptions.spago ? getSpagoSources() : 'output'
 
   const options = Object.assign({
     context: webpackContext,
@@ -116,12 +155,13 @@ module.exports = function purescriptLoader(source, map) {
     pscIde: false,
     pscIdeColors: loaderOptions.psc === 'psa',
     pscPackage: false,
+    spago: false,
     bundleOutput: 'output/bundle.js',
     bundleNamespace: 'PS',
     bundle: false,
     warnings: true,
     watch: false,
-    output: 'output',
+    output: outputPath,
     src: []
   }, loaderOptions, {
     src: srcOption
@@ -132,8 +172,7 @@ module.exports = function purescriptLoader(source, map) {
 
     CACHE_VAR.installed = true;
 
-    // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
-    this._compiler.plugin('invalid', () => {
+    const invalidCb = () => {
       debugVerbose('invalidating loader CACHE_VAR');
 
       CACHE_VAR = {
@@ -149,10 +188,16 @@ module.exports = function purescriptLoader(source, map) {
         installed: CACHE_VAR.installed,
         srcOption: []
       };
-    });
+    }
 
-    // add psc warnings to webpack compilation warnings
-    this._compiler.plugin('after-compile', (compilation, callback) => {
+    // invalidate loader CACHE_VAR when bundle is marked as invalid (in watch mode)
+    if(this._compiler.hooks){
+      this._compiler.hooks.invalid.tap('purs-loader', invalidCb);
+    } else {
+      this._compiler.plugin('invalid', invalidCb);
+    }
+
+    const afterCompileCb = (compilation, callback) => {
       CACHE_VAR.warnings.forEach(warning => {
         compilation.warnings.push(warning);
       });
@@ -162,7 +207,14 @@ module.exports = function purescriptLoader(source, map) {
       });
 
       callback()
-    });
+    }
+
+    // add psc warnings to webpack compilation warnings
+    if(this._compiler.hooks) {
+      this._compiler.hooks.afterCompile.tapAsync('purs-loader', afterCompileCb);
+    } else {
+      this._compiler.plugin('after-compile', afterCompileCb);
+    }
   }
 
   const psModuleName = PsModuleMap.matchModule(source);