-
-
Save Pauan/53b1af11a71d6ebac8defafad070b3fe to your computer and use it in GitHub Desktop.
purescript psc 0.9 output webpack loader that allows tree shaking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
/* | |
* Webpack 2 loader that can take CommonJS output by psc 0.9.1 and convert | |
* it into tree shakable ES6 modules. No transpiling required. | |
*/ | |
const fs = require('fs') | |
const commonJsRequire = /var ([_$a-zA-Z0-9]+) = require\([\'\"]([^\'\"]+)[\'\"]\)/g | |
const moduleExports = /module\.exports = \{(\n( ([\"\'_$a-zA-Z0-9]+): ([_$a-zA-Z0-9\.]+)(, )?\n)*)?\};/m | |
const actualExports = /( ((?:[\'\"][^\'\"]+[\'\"])|(?:[_$a-zA-Z0-9]+)): ([_$a-zA-Z0-9\.]+)(, )?\n)/g | |
module.exports = function (content) { | |
this.cacheable() | |
const funcs = [ | |
upconvertImports, | |
upconvertExports, | |
upconvertFfiExports | |
] | |
const out = funcs.reduce((prev, curr) => curr(prev), content) | |
console.log(out) | |
return out | |
} | |
function findMatches(regex, content, action) { | |
let current; | |
let updated = content.slice() | |
while ((current = regex.exec(content)) !== null) { | |
updated = action(updated, current) | |
} | |
return updated | |
} | |
function upconvertImports (content) { | |
function action (content, currentImport) { | |
return content.replace(currentImport[0], `import * as ${currentImport[1]} from "${currentImport[2]}"`) | |
} | |
return findMatches(commonJsRequire, content, action) | |
} | |
function upconvertExports (content) { | |
function action (noop, currentExport) { | |
const externalName = currentExport[2] | |
const internalName = currentExport[3] | |
if (externalName[0] === "'" || externalName[0] === "\"") { | |
content += `export { ${internalName} as ${externalName.slice(1, -1)} };` | |
} else if (externalName === internalName) { | |
content += `export { ${externalName} };` | |
} else { | |
content += `export var ${externalName} = ${internalName};` | |
} | |
} | |
const moduleExporting = moduleExports.exec(content) | |
if (moduleExporting) { | |
content = content.replace(moduleExports, '') | |
findMatches(actualExports, moduleExporting[0], action) | |
} | |
return content | |
} | |
function upconvertFfiExports (content) { | |
return content.replace(/^exports\./gm, 'export var ') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment