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();
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');
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,
return result;
}
- })(loaderOptions.pscPackage);
+ })(loaderOptions.pscPackage, loaderOptions.spago);
+
+ const outputPath = loaderOptions.spago ? getSpagoSources() : 'output'
const options = Object.assign({
context: webpackContext,
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
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 = {
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);
});
});
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);