Guest User

Untitled

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. const fs = require("fs")
  2. const path = require("path")
  3. const jsfiles = [...new Set(process.argv.slice(2))]
  4.  
  5. if(jsfiles.length === 0){
  6. throw new Error("no files")
  7. } else {
  8. console.log(jsfiles)
  9. }
  10.  
  11. const modules = []
  12. for(const file of jsfiles){
  13. const abspath = path.resolve(file)
  14. const content = fs.readFileSync(abspath).toString()
  15. const key = new URL(abspath, "file:///")
  16. modules.push(`"${key}": ({require, module, exports}) => {${content}},`)
  17. }
  18.  
  19. const entrypoint = new URL(path.resolve(jsfiles[0]), "file:///")
  20.  
  21. const output = `
  22. const sources = {
  23. ${modules.join("\n")}
  24. }
  25. const cache = {}
  26. const sym = Symbol()
  27. const createRequire = file => reqpath => {
  28. const key = new URL(reqpath, file)
  29. if(key in cache){
  30. if(cache[key] === sym) throw new Error("Cyclic require is detected.")
  31. return cache[key]
  32. }
  33. cache[key] = sym
  34. const exports = {}
  35. const module = {exports}
  36. sources[key]({
  37. require: createRequire(key),
  38. module,
  39. exports,
  40. })
  41. cache[key] = module.exports
  42. return module.exports
  43. }
  44. const entrypoint = "${entrypoint}"
  45. createRequire("file:///")(entrypoint)
  46. `
  47.  
  48. fs.writeFileSync("./output.js", output)
Add Comment
Please, Sign In to add comment