From bef7f4e2a86551735cdf24d661b20b1ed83cc276 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sun, 2 Jun 2019 13:31:30 +0100 Subject: Add option to include Spago sources (#126) --- README.md | 14 +++++++++--- src/index.js | 69 +++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index cc4373e..28c2783 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,8 @@ const loaderConfig = { pscIdeServerArgs: {}, // for example, to change the port {port: 4088} pscIdeRebuildArgs: {} // for example, for sourcemaps {codegen: ['js', 'sourcemaps']} pscIdeColors: false, // defaults to true if psc === 'psa' - pscPackage: false, + pscPackage: false, // include dependencies from psc-package + spago: false, // include dependencies from spago bundleOutput: 'output/bundle.js', bundleNamespace: 'PS', bundle: false, @@ -71,10 +72,12 @@ const loaderConfig = { output: 'output', src: [ path.join('src', '**', '*.purs'), - // if pscPackage = false - path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') // if pscPackage = true // source paths reported by `psc-package sources` + // if spago = true + // source paths reported by `spago sources` + // if pscPackage = false and spago = false + path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') ] } ``` @@ -91,6 +94,11 @@ if there is no server running this loader will start one for you. Set `pscPackage` query parameter to `true` to enable `psc-package` support. The `psc-package`-supplied source paths will be appended to `src` parameter. +### `spago` support (experimental) + +Set `spago` query parameter to `true` to enable `spago` support. The `spago`-supplied source paths +will be appended to `src` parameter. + ### Troubleshooting #### Slower webpack startup after enabling psc-ide support? diff --git a/src/index.js b/src/index.js index 0c620bf..a6ddc35 100644 --- a/src/index.js +++ b/src/index.js @@ -42,6 +42,37 @@ var CACHE_VAR = { srcOption: [] }; +// 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; + } +} + module.exports = function purescriptLoader(source, map) { this.cacheable && this.cacheable(); @@ -51,7 +82,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 +91,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 +106,7 @@ module.exports = function purescriptLoader(source, map) { return result; } - })(loaderOptions.pscPackage); + })(loaderOptions.pscPackage, loaderOptions.spago); const options = Object.assign({ context: webpackContext, @@ -116,6 +122,7 @@ module.exports = function purescriptLoader(source, map) { pscIde: false, pscIdeColors: loaderOptions.psc === 'psa', pscPackage: false, + spago: false, bundleOutput: 'output/bundle.js', bundleNamespace: 'PS', bundle: false, -- cgit v1.2.3