Skip to content

Instantly share code, notes, and snippets.

@tupperkion tupperkion/derequire.js
Last active Dec 2, 2018

Embed
What would you like to do?
Poor man's derequire
#!/usr/bin/env node
let i = "";
process.stdin.on("readable", () => {
let chunk;
while (null !== (chunk = process.stdin.read())) {
i += chunk.toString();
}
});
process.stdin.on("end", () => {
process.stderr.write("Recieved input file\n");
// derequire stuff
i = i.split("require").join("_dereq_");
process.stderr.write("Building blacklist...\n");
let blacklist = new Set();
// parse browserify output
let r1 = /\},\{(?:(?:".+":(?:\d+|undefined),?)+)\}\],\d+:\[function\(_dereq_,module,exports/g;
let a;
while (a = r1.exec(i)) {
let r2 = /"([^"]+)":undefined/g;
let b;
while (b = r2.exec(a[0])) {
blacklist.add(b[1]);
}
}
process.stderr.write("Reverting " + blacklist.size + " dependencies...\n");
// revert unknown dependencies; hopefully webpack can find them
i = i.replace(new RegExp(`_dereq_(\\s*\\((['"])(?:${[...blacklist].join("|").split("/").join("\\/")})\\2\\s*\\))`, "g"), "require$1")
// allow bypassing browserify entirely
.split("global.nativeRequire").join("require");
process.stderr.write("Finished transform\n");
process.stdout.end(i);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.