{"id":20513992,"url":"https://github.com/webreflection/common-js","last_synced_at":"2025-04-14T00:05:37.325Z","repository":{"id":57204321,"uuid":"79599773","full_name":"WebReflection/common-js","owner":"WebReflection","description":"CommonJS + module.import() for any Browser","archived":false,"fork":false,"pushed_at":"2017-02-12T17:17:59.000Z","size":45,"stargazers_count":35,"open_issues_count":0,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-14T00:05:25.618Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WebReflection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-20T21:23:15.000Z","updated_at":"2025-01-24T12:33:10.000Z","dependencies_parsed_at":"2022-09-06T11:11:53.814Z","dependency_job_id":null,"html_url":"https://github.com/WebReflection/common-js","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcommon-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcommon-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcommon-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WebReflection%2Fcommon-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WebReflection","download_url":"https://codeload.github.com/WebReflection/common-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248799954,"owners_count":21163404,"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-11-15T21:14:16.588Z","updated_at":"2025-04-14T00:05:37.278Z","avatar_url":"https://github.com/WebReflection.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CommonJS + module.import() [![build status](https://secure.travis-ci.org/WebReflection/common-js.svg)](http://travis-ci.org/WebReflection/common-js)\n\nThis module aim is to bring both CommonJS like module behavior on Web browsers,\nand a promise based `module.import(path)` to both browsers and NodeJS.\n\n\u003csub\u003eYes, it resolves paths relatively to the current one!\u003csub\u003e\n\n\u003csub\u003eYes, it is secure too, check the [CSP enabled page](https://webreflection.github.io/common-js/csp.html)!\u003csub\u003e\n\nDon't miss [the introductory blog post](https://medium.com/@WebReflection/asynchronous-module-import-path-b9f56675e109#.8nsdv9571) about this idea!\n\n\n### Browser Example\n```html\n\u003c!doctype html\u003e\n\u003chtml\u003e\n  \u003cscript\n    id=\"common-js\"\n    data-main=\"/js-browser/main.js\"\n    src=\"common.js\"\n  \u003e\u003c/script\u003e\n\u003c/html\u003e\n```\n\nHaving a single script with `id=\"common-js\"` is all it takes to be able to load asynchronously any other file or module.\n\nThe main entry point `/js-browser/main.js` will resolve relative paths from `/js-browser/` folder.\n\nIts loaded modules will resolve their own imported paths from where they've been loaded, and so on.\nThe same goes for NodeJS, it's indeed same logic behind `require`.\n\n```js\n// /js-browser/main.js loading /js-browser/test.js\nmodule.import('./test').then(function (test) {\n  test('Hello CommonJS!');\n  // will output:\n  // Hello CommonJS!\n  // from /js-browser/test.js\n});\n\n// the /js-browser/test.js content\nmodule.exports = function (message) {\n  alert(message + '\\nfrom ' + module.filename);\n};\n```\n\n\n\n### Load multiple modules at once\n```js\nPromise.all([\n  module.import('./a'),\n  module.import('//cdn.something.com/cool.js'),\n  module.import('../sw.js'),\n  module.import('/root/too.js')\n]).then(function (modules) {\n  const [a, cool, sw, too] = modules;\n});\n```\n\n\n### Exporting modules asynchronously\n```js\n// an async example of /js-browser/test.js content\n// for the /js-browser/main.js file nothing changes\nmodule.exports = new Promise(function (resolve) {\n  setTimeout(\n    resolve,\n    1000,\n    function (message) {\n      alert(message + '\\nfrom ' + module.filename);\n    }\n  );\n});\n```\n\n\n\n### Compatibility\nYou can test your target directly through the [live test page](https://webreflection.github.io/common-js/).\nWhat I could test was the following:\n\n**Mobile** Android 2+, iOS5+, WP7+, BBOS7+, FFOS1+, WebOS2+, Kindle Paper \u0026 Fire\n\n**Desktop** Chrome, FF, Safari, Opera, IE9+ (theoretically IE8 too but it needs few polyfills upfront)\n\n\n\n\n### What else?\nThe synchronous `require` and both `__filename` and `__dirname` are also exposed, but nothing else from NodeJS core is available.\nYou are responsible for loading all the modules you need, possibly only when you need them.\n\n\n\n### F.A.Q\n\n  * **Does it load every time?**\n    It uses a cache, like NodeJS does. If you load same module twice, even from different relative paths, it'll use the cached one.\n  * **Why on the module?**\n    There are scripts, script type module, `importScripts`, a [dynamic import proposal](https://github.com/tc39/proposal-dynamic-import#import), you name it ... this one actually works and it's backward compatible with modules that don't care about this solution existing.\n  * **Why not ES2015 modules?**\n    Because those, so far, never truly solved anything. Actually, ES6 modules created more problems due inability to require modules at runtime and/or on the browser.\n  * **Is there a CDN I can use to test?**\n    There is always one for npm modules. [https://unpkg.com/common-js@latest](https://unpkg.com/common-js@latest/common.min.js) should be already OK.\n  * **Is this using eval?**\n    No. It's using a technique that is even compatible with highest security standards such [Content Security Policy](https://w3c.github.io/webappsec-csp/)\n\n\n\n### License\nCopyright (C) 2017 by Andrea Giammarchi - @WebReflection","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fcommon-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebreflection%2Fcommon-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebreflection%2Fcommon-js/lists"}