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/to-javascript.js | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/to-javascript.js (limited to 'src/to-javascript.js') diff --git a/src/to-javascript.js b/src/to-javascript.js new file mode 100644 index 0000000..b0b9dda --- /dev/null +++ b/src/to-javascript.js @@ -0,0 +1,129 @@ +'use strict'; + +const Promise = require('bluebird'); + +const fs = Promise.promisifyAll(require('fs')); + +const path = require('path'); + +const jsStringEscape = require('js-string-escape'); + +const difference = require('lodash.difference'); + +const debug = require('debug')('purs-loader'); + +const PsModuleMap = require('./PsModuleMap'); + +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; + }); + } +} + + // Reference the bundle. +function makeBundleJS(psModule) { + const bundleOutput = psMoudle.options.bundleOutput; + + const name = psModule.name; + + const srcDir = psModule.srcDir; + + const escaped = jsStringEscape(path.relative(srcDir, bundleOutput)); + + const result = `module.exports = require("${escaped}")["${name}"]`; + + return result; +} + +// Replace require paths to output files generated by psc with paths +// to purescript sources, which are then also run through this loader. +// Additionally, the imports replaced are tracked so that in the event +// the compiler fails to compile the PureScript source, we can tack on +// any new imports in order to allow webpack to watch the new files +// before they have been successfully compiled. +function makeJS(psModule, psModuleMap, js) { + const requireRE = /require\(['"]\.\.\/([\w\.]+)['"]\)/g; + + const foreignRE = /require\(['"]\.\/foreign['"]\)/g; + + const name = psModule.name; + + const imports = psModuleMap[name].imports; + + var replacedImports = []; + + const result = js + .replace(requireRE, (m, p1) => { + const escapedPath = jsStringEscape(psModuleMap[p1].src); + + replacedImports.push(p1); + + return `require("${escapedPath}")`; + }) + .replace(foreignRE, () => { + const escapedPath = jsStringEscape(psModuleMap[name].ffi); + + return `require("${escapedPath}")`; + }) + ; + + debug('imports %o', imports); + + debug('replaced imports %o', replacedImports); + + const additionalImports = difference(imports, replacedImports); + + debug('additional imports for %s: %o', name, additionalImports); + + const additionalImportsResult = additionalImports.map(import_ => { + const escapedPath = jsStringEscape(psModuleMap[import_].src); + + return `var ${import_.replace(/\./g, '_')} = require("${escapedPath}")`; + }).join('\n'); + + const result_ = result + (additionalImports.length ? '\n' + additionalImportsResult : ''); + + return result_; +} + +module.exports = function toJavaScript(psModule) { + const options = psModule.options; + + const cache = psModule.cache; + + const bundlePath = path.resolve(options.bundleOutput); + + const jsPath = cache.bundle ? bundlePath : psModule.jsPath; + + const js = fs.readFileAsync(jsPath, 'utf8').catch(() => ''); + + const psModuleMap = updatePsModuleMap(psModule); + + debug('loading JavaScript for %s', psModule.name); + + return Promise.props({js: js, psModuleMap: psModuleMap}).then(result => + options.bundle ? + makeBundleJS(psModule) : + makeJS(psModule, result.psModuleMap, result.js) + ); +}; -- cgit v1.2.3 From 75826060798271a0ef6691ab1be5f4fc4372f538 Mon Sep 17 00:00:00 2001 From: eric thul Date: Mon, 20 Feb 2017 13:03:26 -0500 Subject: Fix typo in makeBundleJS --- src/to-javascript.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/to-javascript.js') diff --git a/src/to-javascript.js b/src/to-javascript.js index b0b9dda..237ef1e 100644 --- a/src/to-javascript.js +++ b/src/to-javascript.js @@ -42,7 +42,7 @@ function updatePsModuleMap(psModule) { // Reference the bundle. function makeBundleJS(psModule) { - const bundleOutput = psMoudle.options.bundleOutput; + const bundleOutput = psModule.options.bundleOutput; const name = psModule.name; -- 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/to-javascript.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/to-javascript.js') diff --git a/src/to-javascript.js b/src/to-javascript.js index 237ef1e..0acf180 100644 --- a/src/to-javascript.js +++ b/src/to-javascript.js @@ -74,11 +74,20 @@ function makeJS(psModule, psModuleMap, js) { const result = js .replace(requireRE, (m, p1) => { - const escapedPath = jsStringEscape(psModuleMap[p1].src); + const moduleValue = psModuleMap[p1]; - replacedImports.push(p1); + if (!moduleValue) { + debug('module %s was not found in the map, replacing require with null', p1); - return `require("${escapedPath}")`; + return 'null'; + } + else { + const escapedPath = jsStringEscape(moduleValue.src); + + replacedImports.push(p1); + + return `require("${escapedPath}")`; + } }) .replace(foreignRE, () => { const escapedPath = jsStringEscape(psModuleMap[name].ffi); @@ -87,13 +96,11 @@ function makeJS(psModule, psModuleMap, js) { }) ; - debug('imports %o', imports); - - debug('replaced imports %o', replacedImports); - const additionalImports = difference(imports, replacedImports); - debug('additional imports for %s: %o', name, additionalImports); + if (additionalImports.length) { + debug('additional imports for %s: %o', name, additionalImports); + } const additionalImportsResult = additionalImports.map(import_ => { const escapedPath = jsStringEscape(psModuleMap[import_].src); -- cgit v1.2.3 From 09e09fb310cf98cfd9581425b6a75b013f7be74e Mon Sep 17 00:00:00 2001 From: eric thul Date: Sat, 25 Feb 2017 10:09:45 -0500 Subject: Adding an additional module check --- src/to-javascript.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/to-javascript.js') diff --git a/src/to-javascript.js b/src/to-javascript.js index 0acf180..b402ad4 100644 --- a/src/to-javascript.js +++ b/src/to-javascript.js @@ -103,10 +103,19 @@ function makeJS(psModule, psModuleMap, js) { } const additionalImportsResult = additionalImports.map(import_ => { - const escapedPath = jsStringEscape(psModuleMap[import_].src); + const moduleValue = psModuleMap[import_]; - return `var ${import_.replace(/\./g, '_')} = require("${escapedPath}")`; - }).join('\n'); + if (!moduleValue) { + debug('module %s was not found in the map, skipping require', import_); + + return null; + } + else { + const escapedPath = jsStringEscape(moduleValue.src); + + return `var ${import_.replace(/\./g, '_')} = require("${escapedPath}")`; + } + }).filter(a => a !== null).join('\n'); const result_ = result + (additionalImports.length ? '\n' + additionalImportsResult : ''); -- cgit v1.2.3