const difference = require('lodash.difference');
-const debug = require('debug')('purs-loader');
+const debug_ = require('debug');
+
+const debug = debug_('purs-loader');
+
+const debugVerbose = debug_('purs-loader:verbose');
const PsModuleMap = require('./PsModuleMap');
// Reference the bundle.
function makeBundleJS(psModule) {
- const bundleOutput = psMoudle.options.bundleOutput;
+ const bundleOutput = psModule.options.bundleOutput;
const name = psModule.name;
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);
})
;
- 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) {
+ debugVerbose('additional imports for %s: %o', name, additionalImports);
+ }
const additionalImportsResult = additionalImports.map(import_ => {
- const escapedPath = jsStringEscape(psModuleMap[import_].src);
+ const moduleValue = psModuleMap[import_];
+
+ 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}")`;
- }).join('\n');
+ return `var ${import_.replace(/\./g, '_')} = require("${escapedPath}")`;
+ }
+ }).filter(a => a !== null).join('\n');
const result_ = result + (additionalImports.length ? '\n' + additionalImportsResult : '');
const bundlePath = path.resolve(options.bundleOutput);
- const jsPath = cache.bundle ? bundlePath : psModule.jsPath;
+ const jsPath = options.bundle ? bundlePath : psModule.jsPath;
const js = fs.readFileAsync(jsPath, 'utf8').catch(() => '');
const psModuleMap = updatePsModuleMap(psModule);
- debug('loading JavaScript for %s', psModule.name);
+ debugVerbose('loading JavaScript for %s', psModule.name);
return Promise.props({js: js, psModuleMap: psModuleMap}).then(result =>
options.bundle ?