From 8e21ab0ab3f8ba9d129f1cf3b59f87d7a2b5bfc2 Mon Sep 17 00:00:00 2001 From: eric thul Date: Sun, 12 Feb 2017 18:05:07 -0500 Subject: Ensure that all imported files are watched In order to handle the case where a new PureScript file is imported, but fails to compile, the purs-loader now tracks imports for each PureScript file in order to append any additional imports to the resulting JS. This ensures that webpack will watch the new file even before it successfully compiles. --- src/index.js | 61 +----------------------------------------------------------- 1 file changed, 1 insertion(+), 60 deletions(-) (limited to 'src/index.js') diff --git a/src/index.js b/src/index.js index 249f472..3f5e6a8 100644 --- a/src/index.js +++ b/src/index.js @@ -3,18 +3,15 @@ const debug = require('debug')('purs-loader') const loaderUtils = require('loader-utils') const Promise = require('bluebird') -const fs = Promise.promisifyAll(require('fs')) const path = require('path') -const jsStringEscape = require('js-string-escape') const PsModuleMap = require('./PsModuleMap'); const Psc = require('./Psc'); const PscIde = require('./PscIde'); +const toJavaScript = require('./to-javascript'); const dargs = require('./dargs'); const spawn = require('cross-spawn').sync const eol = require('os').EOL -const requireRegex = /require\(['"]\.\.\/([\w\.]+)['"]\)/g - module.exports = function purescriptLoader(source, map) { const callback = this.async() const config = this.options @@ -164,59 +161,3 @@ module.exports = function purescriptLoader(source, map) { }) } } - -function updatePsModuleMap(psModule) { - const options = psModule.options - const cache = psModule.cache - const filePurs = psModule.srcPath - if (!cache.psModuleMap) { - debug('module mapping does not exist'); - return PsModuleMap.makeMap(options.src).then(map => { - cache.psModuleMap = map; - return cache.psModuleMap; - }); - } - else { - return PsModuleMap.makeMapEntry(filePurs).then(result => { - const map = Object.assign(cache.psModuleMap, result) - cache.psModuleMap = map; - return cache.psModuleMap; - }); - } -} - -// The actual loader is executed *after* purescript compilation. -function toJavaScript(psModule) { - const options = psModule.options - const cache = psModule.cache - const bundlePath = path.resolve(options.bundleOutput) - const jsPath = cache.bundle ? bundlePath : psModule.jsPath - - debug('loading JavaScript for', psModule.name) - - return Promise.props({ - js: fs.readFileAsync(jsPath, 'utf8'), - psModuleMap: updatePsModuleMap(psModule) - }).then(result => { - let js = '' - - if (options.bundle) { - // if bundling, return a reference to the bundle - js = 'module.exports = require("' - + jsStringEscape(path.relative(psModule.srcDir, options.bundleOutput)) - + '")["' + psModule.name + '"]' - } else { - // replace require paths to output files generated by psc with paths - // to purescript sources, which are then also run through this loader. - js = result.js - .replace(requireRegex, (m, p1) => { - return 'require("' + jsStringEscape(result.psModuleMap[p1].src) + '")' - }) - .replace(/require\(['"]\.\/foreign['"]\)/g, (m, p1) => { - return 'require("' + jsStringEscape(result.psModuleMap[psModule.name].ffi) + '")' - }) - } - - return js - }) -} -- cgit v1.2.3 From df8798fa0eeeb3dfcce14dc10100b03f708b57f6 Mon Sep 17 00:00:00 2001 From: eric thul Date: Sun, 19 Feb 2017 11:29:57 -0500 Subject: Add a watch option Similar to the way psc-ide support works, the purs-loader now tolerates compiler errors when the `watch` option is true. When webpack is being run in watch mode the user can set `watch` to true in order to avoid failing the webpack bundle creation when the PureScript compiler fails. Resolves issue #66 Resolves issue #73 Resolves issue #74 --- src/index.js | 1 + 1 file changed, 1 insertion(+) (limited to 'src/index.js') diff --git a/src/index.js b/src/index.js index 3f5e6a8..f4a6dff 100644 --- a/src/index.js +++ b/src/index.js @@ -46,6 +46,7 @@ module.exports = function purescriptLoader(source, map) { bundleNamespace: 'PS', bundle: false, warnings: true, + watch: false, output: 'output', src: [ path.join('src', '**', '*.purs'), -- cgit v1.2.3 From 4305f5b0053d6fd2887b364c5da0a1ca6c06fc54 Mon Sep 17 00:00:00 2001 From: eric thul Date: Sat, 25 Feb 2017 09:55:18 -0500 Subject: Handle missing module and adding debugging --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/index.js') diff --git a/src/index.js b/src/index.js index f4a6dff..6fb2fce 100644 --- a/src/index.js +++ b/src/index.js @@ -149,7 +149,7 @@ module.exports = function purescriptLoader(source, map) { if (!cache.compilationStarted) { return Psc.compile(psModule) .then(() => PsModuleMap.makeMap(options.src).then(map => { - debug('rebuilt module map'); + debug('rebuilt module map after compile'); cache.psModuleMap = map; })) .then(() => Promise.map(cache.deferred, psModule => { -- cgit v1.2.3