{"id":16641797,"url":"https://github.com/samthor/cjs-loader","last_synced_at":"2025-03-21T15:32:22.406Z","repository":{"id":57199127,"uuid":"102742865","full_name":"samthor/cjs-loader","owner":"samthor","description":"🔥👨‍💻🔥 This is a successful but terrible idea and should not be used by anyone.","archived":false,"fork":false,"pushed_at":"2017-09-12T23:32:11.000Z","size":39,"stargazers_count":17,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T02:44:45.122Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samthor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-07T13:51:50.000Z","updated_at":"2022-09-14T16:39:36.000Z","dependencies_parsed_at":"2022-09-16T14:51:15.069Z","dependency_job_id":null,"html_url":"https://github.com/samthor/cjs-loader","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fcjs-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fcjs-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fcjs-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fcjs-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samthor","download_url":"https://codeload.github.com/samthor/cjs-loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244822732,"owners_count":20516156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T07:47:54.592Z","updated_at":"2025-03-21T15:32:22.115Z","avatar_url":"https://github.com/samthor.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"cjs-loader transitively includes commonJS modules for use on the web, and is useful for developers wanting to migrate to ES6 modules who still have legacy dependencies.\n[Go here for a demo](https://samthor.github.io/cjs-loader/demo/index.html) (needs a browser with native modules support—Safari 10.1+, or Chrome 61+).\n\nWe do this using just your browser, without a compile step, by implementing `require()` and other methods just while loading the module code.\nFor users of cjs-loader, we provide `load(moduleName)` that returns a `Promise` of the exports.\n\n🔥👨‍💻🔥 This is an interesting, successful but terrible idea and should not be used by anyone.\nIt reqiures support for ES6 Modules 🛠️ and has only really been tested on Chrome 61+.\n\n# Usage\n\nFirst, install any public modules you want to use with NPM or Yarn.\nThen, use the loader:\n\n```html\n\u003cscript type=\"module\"\u003e\nimport {load, setup} from './path/to/cjs-loader.js';\nsetup('./path/to/cjs-loader.js');\n\nload('npm-package-name').then((out) =\u003e {\n  // do whatever as if you require()'d the package\n});\n\u003c/script\u003e\n```\n\n(This assumes `npm-package-name` is in the path `./node_modules/npm-package-name`. You can also `require()` a relative path.)\n\nFor example, to use Handlebars ([as per the demo](https://samthor.github.io/cjs-loader/demo/index.html)):\n\n```js\nload('handlebars').then((handlebars) =\u003e {\n  const Handlebars = handlebars.create();\n\n  const source = '\u003cp\u003eHello {{name}}, you have {{name.length}} letters\u003c/p\u003e';\n  const template = Handlebars.compile(source);\n  console.info(template({name: 'Sam'}));\n});\n```\n\nHandlebars internally fetches about ~35 modules (via `require()`), which all get wrapped.\n\n# Implementation\n\n1. Stub out `module.exports`, `require()` etc.\n2. Load the target module as an ES6 module^1\n  * If a further dependency is unavailable in `require()`, throw a known exception^2\n  * Catch in a global handler, and load the dependency via step 2\n  * Retry running the target module when done\n\n^1 more or less\n\n^2 `require()` is synchronous, so we can't block to load more code\n\n## Specific Hacks\n\n[We abuse](https://gist.github.com/samthor/8c5ebf3239bfeaca6c92299bb12b2a79) the fact that Chrome reruns but does _not_ need to reload script files with the same path, but a different hash.\nThis allows for \"efficient\" retries.\ne.g.:\n\n```js\nimport * as foo1 from './foo.js#1';\nimport * as foo2 from './foo.js#2';\nfoo1 !== foo2  // not the same, but only caused one network request\n```\n\n*TODO: document more hacks*\n\n# Notes\n\nThings that we can't fix:\n\n* Don't use this in production.\n  It's horrible.\n\n* Modules can't _really_ determine their path, so if one of your dependenies is from a 302 etc, all bets are off\n\n* Runtime `require()` (i.e., not run on initial import) calls will fail if the code isn't available\n\n## TODOs\n\nThings that we can fix:\n\n* Built-in Node packages don't work (`fs`, `path` etc)\n\n* We should coalesce multiple failures to `require()` (just return `null` until an actual error occurs) and request further code in parallel\n\n* This code is forked from [cjs-faker](https://github.com/samthor/cjs-faker), and still supports AMD, but calling an unplanned `require()` from within `define()` doesn't work yet\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamthor%2Fcjs-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamthor%2Fcjs-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamthor%2Fcjs-loader/lists"}