X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Findex.js;h=249f4723a249e540c13656fbb8245413081bee03;hb=86e2b3d4854157bbb65987f2939f50275edaea71;hp=fe1455bfc32a686d322c48c35d6ff5d581043c6a;hpb=531c751fe5593750a377db38bcfaf9a5383ac661;p=github%2Ffretlink%2Fpurs-loader.git diff --git a/src/index.js b/src/index.js index fe1455b..249f472 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,8 @@ const PsModuleMap = require('./PsModuleMap'); const Psc = require('./Psc'); const PscIde = require('./PscIde'); const dargs = require('./dargs'); +const spawn = require('cross-spawn').sync +const eol = require('os').EOL const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g @@ -19,15 +21,30 @@ module.exports = function purescriptLoader(source, map) { const query = loaderUtils.parseQuery(this.query) const webpackOptions = this.options.purescriptLoader || {} - const options = Object.assign({ + const depsPaths = (pscPackage => { + if (pscPackage) { + debug('calling psc-package...') + + return spawn('psc-package', ['sources']).stdout.toString().split(eol).filter(v => v != '') + } + else { + return [ path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') ] + } + }) + + let options = Object.assign(webpackOptions, query) + + const defaultDeps = depsPaths(options.pscPackage) + const defaultOptions = { context: config.context, psc: 'psc', pscArgs: {}, pscBundle: 'psc-bundle', pscBundleArgs: {}, pscIde: false, - pscIdeColors: webpackOptions.psc === 'psa' || query.psc === 'psa', + pscIdeColors: options.psc === 'psa', pscIdeArgs: {}, + pscPackage: false, bundleOutput: 'output/bundle.js', bundleNamespace: 'PS', bundle: false, @@ -35,18 +52,26 @@ module.exports = function purescriptLoader(source, map) { output: 'output', src: [ path.join('src', '**', '*.purs'), - path.join('bower_components', 'purescript-*', 'src', '**', '*.purs') + ...defaultDeps ] - }, webpackOptions, query) + } this.cacheable && this.cacheable() let cache = config.purescriptLoaderCache = config.purescriptLoaderCache || { rebuild: false, deferred: [], - bundleModules: [] + bundleModules: [], + warnings: [], + errors: [] + } + + if (options.pscPackage && options.src) { + options.src = options.src.concat(defaultDeps) // append psc-package-provided source paths with users' } + options = Object.assign(defaultOptions, options) + if (!config.purescriptLoaderInstalled) { config.purescriptLoaderInstalled = true @@ -59,22 +84,24 @@ module.exports = function purescriptLoader(source, map) { deferred: [], bundleModules: [], ideServer: cache.ideServer, - psModuleMap: cache.psModuleMap + psModuleMap: cache.psModuleMap, + warnings: [], + errors: [] } - }) + }); // 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.forEach(warning => { + compilation.warnings.push(warning); + }); - if (cache.errors) { - compilation.errors.unshift(`PureScript compilation:\n${cache.errors}`) - } + cache.errors.forEach(error => { + compilation.errors.push(error); + }); callback() - }) + }); } const psModuleName = PsModuleMap.match(source) @@ -87,6 +114,16 @@ 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('loader called', psModule.name) @@ -123,7 +160,7 @@ module.exports = function purescriptLoader(source, map) { })) .catch(error => { cache.deferred[0].reject(error) - cache.deferred.slice(1).forEach(psModule => psModule.reject(true)) + cache.deferred.slice(1).forEach(psModule => psModule.reject(new Error('purs-loader failed'))) }) } }