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/PsModuleMap.js | 22 +++++++-- src/index.js | 61 +----------------------- src/to-javascript.js | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 65 deletions(-) create mode 100644 src/to-javascript.js (limited to 'src') diff --git a/src/PsModuleMap.js b/src/PsModuleMap.js index 2193f02..0ae687c 100644 --- a/src/PsModuleMap.js +++ b/src/PsModuleMap.js @@ -8,15 +8,23 @@ const fs = Promise.promisifyAll(require('fs')); const globby = require('globby'); -const debug = require('debug')('purs-loader') +const debug = require('debug')('purs-loader'); -const srcModuleRegex = /(?:^|\n)module\s+([\w\.]+)/i +const srcModuleRegex = /(?:^|\n)module\s+([\w\.]+)/i; -function match(str) { +const importModuleRegex = /(?:^|\n)\s*import\s+([\w\.]+)/ig; + +function matchModule(str) { const matches = str.match(srcModuleRegex); return matches && matches[1]; } -module.exports.match = match; +module.exports.match = matchModule; + +function matchImports(str) { + const matches = str.match(importModuleRegex); + return (matches || []).map(a => a.replace(/\n?\s*import\s+/i, '')); +} +module.exports.matchImports = matchImports; function makeMapEntry(filePurs) { const dirname = path.dirname(filePurs); @@ -33,7 +41,9 @@ function makeMapEntry(filePurs) { const sourceJs = fileMap.fileJs; - const moduleName = match(sourcePurs); + const moduleName = matchModule(sourcePurs); + + const imports = matchImports(sourcePurs); const map = {}; @@ -41,6 +51,8 @@ function makeMapEntry(filePurs) { map[moduleName].src = path.resolve(filePurs); + map[moduleName].imports = imports; + if (sourceJs) { map[moduleName].ffi = path.resolve(fileJs); } 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 - }) -} 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