{"_id":"commonjs-standalone","_rev":"4-177cbf2feacd78ed9e80e01f8bf9b72f","name":"commonjs-standalone","dist-tags":{"latest":"0.2.0"},"versions":{"0.1.0":{"name":"commonjs-standalone","version":"0.1.0","description":"Standalone CommonJS loader for any JS engine","main":"dist/index.js","repository":{"type":"git","url":"git+https://github.com/suchipi/commonjs-standalone.git"},"author":{"name":"Suchipi","email":"me@suchipi.com"},"license":"MIT","keywords":["loader","commonjs","cjs","module","require"],"bugs":{"url":"https://github.com/suchipi/commonjs-standalone/issues"},"homepage":"https://github.com/suchipi/commonjs-standalone#readme","_id":"commonjs-standalone@0.1.0","_npmVersion":"6.4.1","_nodeVersion":"8.12.0","_npmUser":{"name":"suchipi","email":"me@suchipi.com"},"dist":{"integrity":"sha512-k8x4r833yXfBhRGNXTAMvY/4vI+U/eqsn9LJAT6WrYki4yuF3H7T/bxtvximJWGuQb5X+umq3Rk9QhOLheg2pQ==","shasum":"4a1fd844ddc98e4763775c497130c4a3a0309aad","tarball":"https://registry.npmjs.org/commonjs-standalone/-/commonjs-standalone-0.1.0.tgz","fileCount":6,"unpackedSize":15666,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbz/A+CRA9TVsSAnZWagAAB4oP/j4fS+kYHSBnawRDFyI4\nRXg4P3W1jzpWa+9b9Z9+UfsOC2gjaKuKtpbEqwoJNNibz7gbtNzvBt/ZeAtG\neQ+cva9A7tieywjBZEcIHsmlEO9EHJw4C0D4T9bsS6FIqyU0qxizkgxkZxNP\nExF5atRJ/6AzoqnGsc8uRHlbyFHR35F+mBQscuCzvHnJj3c6XbFfhute9+PO\n6qsuZ4hXL2IT/Qm0Mx8wJxtQss7q+7NqxlCE9G/wgALVA46zdVL986WeqVZm\n84TE3PrQpHNMlnEP9Y5L2EXOavXJ5rzof3RSlIfNx5fc9x4HaqUl9pZYJ0Lv\n6z+KxbqGNF8uizBYGnJHUVZ/pf1A6VzFSaqyu4RQjP9LCXfTlrG4rkJX0yzI\n5v8Y6WBbPLIwIecb1Ns8fnG6MRlmJbO14vr7ZQKWoWdHZAEFV5U/LYlaWKNh\nc00teA6PV0eUOLhzpg1XTQJyN2HbZN9UJ0g2fpUEqrYeM4U33VxsEBd0XQMk\nCUS+AUrCHs64mMwZQ5/eXns/MwD88dUBksO7Kgm5/VY7fdEMXIU2HaMWXNCR\nVuzhWhq1B9D5aUtHIHPj7oaYu92MGBpvwKUrZ0aEp4C6LWpyImRpDME+P6y2\nMFEwWKzDHsmUf8XLCYKH/YGay3lUE87oiRx/F8Z7fo62X2eFXUL9jgUOpI7T\nS1qn\r\n=vP2p\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDnClFjXYkGA3rD8Lb9z6sWkcbN4n6qMC2qa9T0ae9KwgIhAIvJjj4I4ybgwHgKD1r074ARNulrL3zNnJIouyhp4olK"}]},"maintainers":[{"name":"suchipi","email":"me@suchipi.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/commonjs-standalone_0.1.0_1540354109813_0.38746943265448897"},"_hasShrinkwrap":false},"0.1.1":{"name":"commonjs-standalone","version":"0.1.1","description":"Standalone CommonJS loader for any JS engine","main":"dist/index.js","repository":{"type":"git","url":"git+https://github.com/suchipi/commonjs-standalone.git"},"author":{"name":"Suchipi","email":"me@suchipi.com"},"license":"MIT","keywords":["loader","commonjs","cjs","module","require"],"bugs":{"url":"https://github.com/suchipi/commonjs-standalone/issues"},"homepage":"https://github.com/suchipi/commonjs-standalone#readme","_id":"commonjs-standalone@0.1.1","_npmVersion":"6.4.1","_nodeVersion":"8.12.0","_npmUser":{"name":"suchipi","email":"me@suchipi.com"},"dist":{"integrity":"sha512-2kcG6TPETx/GfZtr5qMSyms269jk/athzRrWnoyVOuwUMbEk5/ajGYc0FQP0E0a7jABSzEedrHABYyu8DY2Wog==","shasum":"598cf58722eb59cd153e41d6c00866c68ed8c972","tarball":"https://registry.npmjs.org/commonjs-standalone/-/commonjs-standalone-0.1.1.tgz","fileCount":6,"unpackedSize":15826,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbz/e8CRA9TVsSAnZWagAAJ8gP/RIXiHuHfgnfyaf9vWC+\nmQVBg9eIzJLPXhbFylGfPrOELUh03ZDDQRGOSZjS+CwbsOj6zlc7Pktv7+4f\nlUG6Mxq4xZQ/6r8HGEnIsCDQdOUQovIG5QOZSfJALPAJRaQhfw1YlMIZdMv2\n/6kIoIK0VDVrvL0OljJ9eoS7JZ192E6fzVjYzcP6+zgkFMIqqXw4OLJU2S1L\naObD1uQlABnbs95oRphGmKHH6pJoZ8EXdKjYL7WN4j1afw/j53vYZDaCA8Tu\nnMYUfEvy1X7R43kUAsS2oE1YXzauP019x+YmtpAkAbgHVpqfJiXCsdfiTJ58\nr9ePnyyk2OdFmh6AO2SUNjiG1JtwMFfe46QKU0Ld8vi0v3kO7b9a+X8y0CdP\nKpYS/zi38GrSZgt4IyKetHGxFCQrip1ZDocCq6k1zyDq/XheT6/CK3tkKrf3\npuQ8udqvrqpsm1oc9ERwSPrgsTC7GkDYeHW2oi5lyKuJUG4nvPKjpuNHGRRZ\nuLlCpaElrMxAELu8mhCyq3CvYzA8d6yjkFXgaP5bwelkvT/5bFeDV+6RPN0S\n0Zf6b0Sa6j4D0akgrsx1dq918ibLLHFSj4Of/30CKys86ZuLzuV9uUoctSRh\ngkAYqrA9AGx6NOylpa9Hlhaj3lCu/cSZFv7bzljb7n6UWm7xNxBhHwbdD5tN\noEPF\r\n=QUae\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIH+H34YSuqsM83DYe5damm8+1jF5dJ+cqxoVYhe/GAFNAiEA+ylKoDbRdHq4tJX6D+4/gDHyqmH1kLaaDWStN2uYE+M="}]},"maintainers":[{"name":"suchipi","email":"me@suchipi.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/commonjs-standalone_0.1.1_1540356028384_0.9236118848408488"},"_hasShrinkwrap":false},"0.1.2":{"name":"commonjs-standalone","version":"0.1.2","description":"Standalone CommonJS loader for any JS engine","main":"dist/index.js","repository":{"type":"git","url":"git+https://github.com/suchipi/commonjs-standalone.git"},"author":{"name":"Suchipi","email":"me@suchipi.com"},"license":"MIT","keywords":["loader","commonjs","cjs","module","require"],"bugs":{"url":"https://github.com/suchipi/commonjs-standalone/issues"},"homepage":"https://github.com/suchipi/commonjs-standalone#readme","_id":"commonjs-standalone@0.1.2","_npmVersion":"6.4.1","_nodeVersion":"8.12.0","_npmUser":{"name":"suchipi","email":"me@suchipi.com"},"dist":{"integrity":"sha512-lvIKVYjLkKPfZPqvObAIYSRc4WcLKGsRJGJKxjWnEzH+MasZea7cMpnodaouZykwxZEJCtDLdRh4CjZFBqL4MA==","shasum":"8f070dd1451d261867b90bb8cfd4884b57c45a33","tarball":"https://registry.npmjs.org/commonjs-standalone/-/commonjs-standalone-0.1.2.tgz","fileCount":6,"unpackedSize":16123,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJb0S9PCRA9TVsSAnZWagAAFeYP/ju/rUK++Usu5wKK8Dk9\nTBbIEwiOPT7NUv3/YU2cj9hEpxBJvxiMujjTEk1Ea4lGVgN0eUUAbJ/dJqAJ\n0j4FdRZSgspYK0JCBopmPDmRzOvZDCxBq9iJ9PiNRE48w8Y9mpVoqtypBGBU\nacDpsgoIKri7z7YC3M5W9SyZ40I7EbQqE7HfLa48wCT6iRD+N+kt8U5Dnq8y\n1skVcEx/9mJCed2ORJWDnFE9XQxb+GI/YGWjvREpa9tmAkltTjX2uxVWBFaK\nHNccQz89pR+5jZHkObduqmBi6BezHxdFGpwVGiewyGepFyc0Ln89OleTWEfD\nsFMh+/JdTqr7N4WCDVUumEzvNGNLKuW3ZpoXwz5wBykPzoNRN+5pU5ybJNhl\n8H4K+J7txcVnAULVU2/y/TgDRfppYX55wWgv3/DR4XSmRNjirFQQKMSVbobK\nu5Qxo/Hu9ocpsNo12etzi8vy7opnuB2EJp1gI9KG7P7ZOLEEh1guw8KcetSA\nE1GKDBhNtatMS9dg70N7tEQxpRhJOCfYZxFGU0nigt+1CJSBHmUBs37lD1vk\nJ0/yI8a8onN/cB9Y2mvsHMEvaT6471/ZzMNnANOePqJS3vIDy5MH3jb38qj0\nrUVxjciyYu6Bakp4+QTKmEXp3T/kyRUxsO2Tu2dp3o3hznUgMs+5y4dqUy93\nR+aC\r\n=+mzD\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICE0UlPzj61t2R7oYczhr2frTV63lJiqG6BFcerN0tW8AiBg2G01tiMVgZbh1rw3NV/y5nto8Cq+EvhH+ymO4v9+7A=="}]},"maintainers":[{"name":"suchipi","email":"me@suchipi.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/commonjs-standalone_0.1.2_1540435790787_0.7050380085805046"},"_hasShrinkwrap":false},"0.2.0":{"name":"commonjs-standalone","version":"0.2.0","description":"Standalone CommonJS loader for any JS engine","main":"dist/index.js","types":"src/index.ts","browser":"dist/umd.js","repository":{"type":"git","url":"git+https://github.com/suchipi/commonjs-standalone.git"},"author":{"name":"Suchipi","email":"me@suchipi.com"},"license":"MIT","keywords":["loader","commonjs","cjs","module","require"],"gitHead":"754b1b773df2990d9fb590b41f926d5d648fc11f","bugs":{"url":"https://github.com/suchipi/commonjs-standalone/issues"},"homepage":"https://github.com/suchipi/commonjs-standalone#readme","_id":"commonjs-standalone@0.2.0","_nodeVersion":"16.14.0","_npmVersion":"8.3.1","dist":{"integrity":"sha512-5aOdDHpFCeIKDlG8P6zgBfCZ+TbCpoH+/mA48cIYwupCGnVAtZPkHaHOV+mqxqbJVAOORpUZmEU3VPyADhuoyg==","shasum":"1a48a06374edca66848f0f426db0df32290848cd","tarball":"https://registry.npmjs.org/commonjs-standalone/-/commonjs-standalone-0.2.0.tgz","fileCount":6,"unpackedSize":14194,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDBKY6m7IIgLoQIBwhtb4FrnpmD8nfGBTBlG//2Qp4DzgIhAMgjwtZDrzdx/ql/owIqZFJfL1NJ8ABemmN2gwzyUsyV"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiXjmbACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmp9sA//YFN4qXEswSTDG+a7/9Q4eQGofakwSuMuQQ8SfqWl4/talAXt\r\nuzAfckd35GJj6X4BVoOUfOc9yuSLJ4AXNG3FrtPFjc7Mnpyas4N6P6xVQ4nH\r\nnzBkluQEZkPz5aUD3FmtlDqbuExQdesHvL4RY5d96wiVcV/7QQElq/RoXKYp\r\ndwrnmkvSe3xG0M9J+HAsu8AwVkjcqb90suEZOQ/Fidv8jiecoPbGg/qfgndU\r\nj4azmsDI27f01/BBjZzliA8sDjx6QztB+rGPcIn9EGk6ZvXvVERvCa87uNN8\r\nTvwgyQYSLLWT2rV2WEPsUpxt6ADPyN8Bvlm/Ro4PyUTxrRrgeHZSiF9LFGBs\r\nxp+6vZZCpY1WGsW0ikl4N4vVxskStqczCLzsQkAo3WAdXvxX+Iwd9cn4PZ+K\r\nUc9Ytv0Ho7Af8lDar13cnWTQZnN+6/Bqpmmt9xVJjRA6R+eBTfDRAW6G7Xn8\r\n3lODaH3rngMPkE6VOjnXfxz9y9mRKXkZHNSuyM1L6RPdvbvbiNJ8DdPqAh0o\r\ni43E8RnqRhIIlE/x9fdvZWRjI32PnT4/ixN6QmwiQ4cJvl+WBeghmGgTWrCY\r\n+libPfvHZ7s3/f+NWA8yfj8EKz7DURSc5ayyWNsyV9P7zUOrTn4oAg/FqwCz\r\nX/JEEHgh/ZW7NwGT5WklW9zDbCu7EF84KXQ=\r\n=vMO7\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"suchipi","email":"me@suchipi.com"},"directories":{},"maintainers":[{"name":"suchipi","email":"me@suchipi.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/commonjs-standalone_0.2.0_1650342299250_0.360013776257762"},"_hasShrinkwrap":false}},"time":{"created":"2018-10-24T04:08:29.812Z","0.1.0":"2018-10-24T04:08:29.948Z","modified":"2022-04-27T14:36:43.331Z","0.1.1":"2018-10-24T04:40:28.485Z","0.1.2":"2018-10-25T02:49:50.903Z","0.2.0":"2022-04-19T04:24:59.436Z"},"maintainers":[{"name":"suchipi","email":"me@suchipi.com"}],"description":"Standalone CommonJS loader for any JS engine","homepage":"https://github.com/suchipi/commonjs-standalone#readme","keywords":["loader","commonjs","cjs","module","require"],"repository":{"type":"git","url":"git+https://github.com/suchipi/commonjs-standalone.git"},"author":{"name":"Suchipi","email":"me@suchipi.com"},"bugs":{"url":"https://github.com/suchipi/commonjs-standalone/issues"},"license":"MIT","readme":"# `commonjs-standalone`\n\n`commonjs-standalone` is a standalone CommonJS loader for use in any JavaScript engine. You give it a way to **resolve**, **read**, and **run** modules, and it will give you the `module`, `exports`, `require`, `__filename`, `__dirname` system you're familiar with from Node.js.\n\n## How it works\n\nThis module exports one function called `requireMain`. Given the absolute path to a module and a `Delegate` object, `requireMain` will load the module at that path. You must provide your own `Delegate` object that `commonjs-standalone` will use to **resolve**, **read**, and **run** modules. A `Delegate` object has this shape:\n\n```js\ntype Delegate = {\n  // A module at filepath `fromFilePath` is trying to require `id`.\n  // Resolve `id` into an absolute path, or throw an error if it can't be found.\n  resolve(id: string, fromFilePath: string): string,\n\n  // Read the contents of the file at `filepath` and return them as a string.\n  read(filepath: string): string,\n\n  // Run this code using the provided module environment object. The filepath\n  // is provided for your information; for configuring stack traces, or if you\n  // want to compile JSON to JS, etc.\n  run(\n    code: string,\n    moduleEnv: {\n      module: Object,\n      exports: Object,\n      require: Function,\n      __filename: string,\n      __dirname: string\n    },\n    filepath: string\n  ): void\n};\n```\n\nHow you **resolve**, **read**, and **run** modules will vary depending on your engine, which is why those things are left up to you.\n\n## Examples\n\nHere's an example of a very simple `Delegate` that keeps modules in a JavaScript object:\n\n```js\n// This delegate loads modules from a JavaScript Object.\nconst modules = {\n  \"module-one\": \"require('module-two');\",\n  \"module-two\": \"console.log('hi from module-two');\"\n};\n\nconst delegate = {\n  resolve(id, fromFilePath) {\n    // Normally you would use `fromFilePath` to resolve relative file paths,\n    // but in this example, only absolute paths are supported, so nothing\n    // needs to be resolved.\n    return id;\n  },\n\n  read(filepath) {\n    return modules[filepath];\n  },\n\n  run(code, moduleEnv, filepath) {\n    const wrapper = eval(\n      \"(function (exports, require, module, __filename, __dirname) { \" +\n        code +\n        \"\\n})\"\n    );\n    wrapper(\n      moduleEnv.exports,\n      moduleEnv.require,\n      moduleEnv.module,\n      moduleEnv.__filename,\n      moduleEnv.__dirname\n    );\n  }\n};\n\nrequireMain(\"module-one\", delegate); // logs 'hi from module-two'\n```\n\nHere's a more complex `Delegate` that uses [the `resolve` package from npm](https://npm.im/resolve) to resolve modules and node's `fs` and `vm` modules to read and run them:\n\n```js\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst vm = require(\"vm\");\nconst resolve = require(\"resolve\");\n\nconst delegate = {\n  resolve(id, fromFilePath) {\n    return resolve.sync(id, {\n      basedir: path.dirname(fromFilePath),\n      preserveSymlinks: false\n    });\n  },\n\n  read(filepath) {\n    return fs.readFileSync(filepath, \"utf-8\");\n  },\n\n  run(code, moduleEnv, filepath) {\n    const wrapper = vm.runInThisContext(\n      \"(function (exports, require, module, __filename, __dirname) { \" +\n        code +\n        \"\\n})\",\n      { filename: filepath }\n    );\n    wrapper(\n      moduleEnv.exports,\n      moduleEnv.require,\n      moduleEnv.module,\n      moduleEnv.__filename,\n      moduleEnv.__dirname\n    );\n  }\n};\n```\n\n## API Documentation\n\n### `requireMain(filepath: string, delegate: Delegate): void`\n\n`requireMain` is exported from `commonjs-standalone` as a named export. It loads the first module (also called the main module), which will load other modules using its `require` function.\n\nIt should be called with an **absolute** path to a file to load, and a `Delegate` that it will use to **resolve**, **read**, and **run** modules.\n\n```js\nfunction requireMain(filepath: string, delegate: Delegate);\n```\n\n### `Delegate`\n\nA `Delegate` is an object with three functions on it: `resolve`, `read`, and `run`. Each are documented here.\n\n### `Delegate.resolve(id: string, fromFilePath: string): string`\n\nThis function is called when a module at the absolute filepath `fromFilePath`\nis trying to require `id`. **Resolve** `id` into an absolute path, or throw an error if it can't be found.\n\nFor example, if `/Users/suchipi/my-package/index.js` contained:\n\n```js\nrequire(\"./foo\");\n```\n\nThen `Delegate.resolve` would be called with an `id` of `\"./foo\"` and a `fromFilePath` of `\"/Users/suchipi/my-package/index.js\"`.\n\nIn that example, you would probably want to return `\"/Users/suchipi/my-package/foo.js\"` (assuming it exists).\n\n### `Delegate.read(filepath: string): string`\n\nThis function is called when the module system wants to **read** the contents of the file at the absolute path `filepath`. You should read them and return the code as a string.\n\n### `Delegate.run(code: string, moduleEnv: Object, filepath: string): void`\n\nThis function is called when the module system wants to **run** some code. It's called with:\n\n- The code to run,\n- a `ModuleEnvironment` object, and\n- the absolute path to where the module came from.\n\nThe `ModuleEnvironment` object has 5 properties on it that you should expose to the running code: `module`, `exports`, `require`, `__filename`, and `__dirname`.\n\nOne way to expose these to your code is to wrap your code in a function:\n\n```js\nconst wrappedCode =\n  \"(function (exports, require, module, __filename, __dirname) { \" +\n  code +\n  \"\\n})\";\n```\n\nThen you can pass everything from the `ModuleEnvironment` object into the function wrapper after you compile it:\n\n```js\n// You probably shouldn't use `eval`, since it leaks local variables into the scope. There is probably a way to\n// run this cleanly from your JavaScript Engine's API.\nconst wrapperFunction = eval(wrappedCode);\n\nwrapperFunction(\n  moduleEnv.exports,\n  moduleEnv.require,\n  moduleEnv.module,\n  moduleEnv.__filename,\n  moduleEnv.__dirname\n);\n```\n\n## Supported features\n\n- `module`\n  - `module.id`\n  - `module.exports`\n- `require`\n  - `require.resolve`\n  - `require.cache` and deleting from `require.cache`\n- `exports`\n- `__filename`\n- `__dirname`\n- Circular dependencies\n\n## License\n\nMIT\n","readmeFilename":"README.md"}