src/to-javascript.js
function makeJS(psModule, psModuleMap, js) {
  const requireRE = /require\(['"]\.\.\/([\w\.]+)(?:\/index\.js)?['"]\)/g;

  const foreignRE = /require\(['"]\.\/foreign(?:\.js)?['"]\)/g;

  const name = psModule.name;

  const imports = psModuleMap[name].imports;

  var replacedImports = [];

  const result = js
    .replace(requireRE, (m, p1) => {
      const moduleValue = psModuleMap[p1];

      if (!moduleValue) {
        debug('module %s was not found in the map, replacing require with null', p1);

        return 'null';
      }
      else {
        const escapedPath = jsStringEscape(moduleValue.src);

        replacedImports.push(p1);

        return `require("${escapedPath}")`;
      }
    })
    .replace(foreignRE, () => {
      const escapedPath = jsStringEscape(psModuleMap[name].ffi);

      return `require("${escapedPath}")`;
    })
  ;

  const additionalImports = difference(imports, replacedImports);

  if (!additionalImports.length) {
    return Promise.resolve(result);
  }
  else {
    debug('rebuilding module map due to additional imports for %s: %o', name, additionalImports);

    psModule.cache.psModuleMap = null;

    return updatePsModuleMap(psModule).then(updatedPsModuleMap => {
      const additionalImportsResult = additionalImports.map(import_ => {
        const moduleValue = updatedPsModuleMap[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}")`;
        }
      }).filter(a => a !== null).join('\n');

      return result + '\n' + additionalImportsResult;
    });
  }
}
function makeJS(psModule, psModuleMap, js) {
  const requireRE = /require\(['"]\.\.\/([\w\.]+)(?:\/index\.js)?['"]\)/g;

  const foreignRE = /require\(['"]\.\/foreign(?:\.js)?['"]\)/g;

  const name = psModule.name;

  const imports = psModuleMap[name].imports;

  var replacedImports = [];

  const result = js
    .replace(requireRE, (m, p1) => {
      const moduleValue = psModuleMap[p1];

      if (!moduleValue) {
        debug('module %s was not found in the map, replacing require with null', p1);

        return 'null';
      }
      else {
        const escapedPath = jsStringEscape(moduleValue.src);

        replacedImports.push(p1);

        return `require("${escapedPath}")`;
      }
    })
    .replace(foreignRE, () => {
      const escapedPath = jsStringEscape(psModuleMap[name].ffi);

      return `require("${escapedPath}")`;
    })
  ;

  const additionalImports = difference(imports, replacedImports);

  if (!additionalImports.length) {
    return Promise.resolve(result);
  }
  else {
    debug('rebuilding module map due to additional imports for %s: %o', name, additionalImports);

    psModule.cache.psModuleMap = null;

    return updatePsModuleMap(psModule).then(updatedPsModuleMap => {
      const additionalImportsResult = additionalImports.map(import_ => {
        const moduleValue = updatedPsModuleMap[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}")`;
        }
      }).filter(a => a !== null).join('\n');

      return result + '\n' + additionalImportsResult;
    });
  }
}
function makeJS ( psModule , psModuleMap , js ) { const requireRE = / require\(['"]\.\.\/([\w\.]+)(?:\/index\.js)?['"]\) / g ; const foreignRE = / require\(['"]\.\/foreign(?:\.js)?['"]\) / g ; const name = psModule . name ; const imports = psModuleMap [ name ] . imports ; var replacedImports = [ ] ; const result = js . replace ( requireRE , ( m , p1 ) => { const moduleValue = psModuleMap [ p1 ] ; if ( ! moduleValue ) { debug ( 'module %s was not found in the map, replacing require with null' , p1 ) ; return 'null' ; } else { const escapedPath = jsStringEscape ( moduleValue . src ) ; replacedImports . push ( p1 ) ; return ` ${ escapedPath } ` ; } } ) . replace ( foreignRE , ( ) => { const escapedPath = jsStringEscape ( psModuleMap [ name ] . ffi ) ; return ` ${ escapedPath } ` ; } ) ; const additionalImports = difference ( imports , replacedImports ) ; if ( ! additionalImports . length ) { return Promise . resolve ( result ) ; } else { debug ( 'rebuilding module map due to additional imports for %s: %o' , name , additionalImports ) ; psModule . cache . psModuleMap = null ; return updatePsModuleMap ( psModule ) . then ( updatedPsModuleMap => { const additionalImportsResult = additionalImports . map ( import_ => { const moduleValue = updatedPsModuleMap [ import_ ] ; if ( ! moduleValue ) { debug ( 'module %s was not found in the map, skipping require' , import_ ) ; return null ; } else { const escapedPath = jsStringEscape ( moduleValue . src ) ; return ` ${ import_ . replace ( / \. / g , '_' ) } ${ escapedPath } ` ; } } ) . filter ( a => a !== null ) . join ( '\n' ) ; return result + '\n' + additionalImportsResult ; } ) ; } }
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.
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 .