{"id":30107068,"url":"https://github.com/benignware/requirejs-initscript","last_synced_at":"2026-02-10T21:32:29.620Z","repository":{"id":17152350,"uuid":"19919137","full_name":"benignware/requirejs-initscript","owner":"benignware","description":"A requirejs-plugin to find the main executing script element","archived":false,"fork":false,"pushed_at":"2015-10-01T22:21:03.000Z","size":225,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-09-05T11:12:55.221Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benignware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-18T19:33:13.000Z","updated_at":"2015-10-01T15:53:43.000Z","dependencies_parsed_at":"2022-09-24T13:44:45.964Z","dependency_job_id":null,"html_url":"https://github.com/benignware/requirejs-initscript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/benignware/requirejs-initscript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benignware%2Frequirejs-initscript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benignware%2Frequirejs-initscript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benignware%2Frequirejs-initscript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benignware%2Frequirejs-initscript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benignware","download_url":"https://codeload.github.com/benignware/requirejs-initscript/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benignware%2Frequirejs-initscript/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29317956,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-10T20:44:44.282Z","status":"ssl_error","status_checked_at":"2026-02-10T20:44:43.393Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":"2025-08-10T01:32:13.113Z","updated_at":"2026-02-10T21:32:29.584Z","avatar_url":"https://github.com/benignware.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"requirejs-initscript\n====================\n\nA requirejs-plugin to find the main executing script element.\n\nCase\n----\nWhen building a reuseable widget-application with requirejs, you may need to access the embedding script element to gather certain parameters, such as data-attributes, CDATA-/JSON-content or the url's querystring.\n\n\nUsage\n-----\n\nRequire the initscript-plugin from your main-file and only once in an application. Catch information as needed and delegate to your app instance.\nSince the plugin works by finding the script-element via its src-attribute, the filename of the executing script needs to be predictable. \n\n### Regular and single builds\nTo non-builds and loose single or project builds applies that our requirejs-application is only executed once in a document. \nThat means, that we're done by finding our script-element by its source. \nIn this case, the plugin reads out its first dependency which holds the name of the executing file. \n```\n// main.js\nrequire.config(\n  {\n    paths: {\n      'initscript': 'lib/requirejs-initscript/build/initscript'\n    }\n  }\n);\nrequire(['initscript!'], function(initscript) {\n  // init app\n}\n```\n\n```\n\u003cscript src=\"http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.11/require.min.js\" data-main=\"main.js\" data-value=\"static_1\"\u003e\u003c/script\u003e\n```\nAs data-main-source either use your main.js or your single build output file. \n\n\n### Bundle build\nIn order to make our widget-application run in other requirejs-environments we need requirejs itself namespaced and bundled with the application script as described here: \nhttp://requirejs.org/docs/faq-advanced.html#rename\n\n```\n// example optimizer build config:\n{\n  optimize: \"none\",\n  appDir: 'samples/bundle/src',  \n  baseUrl: \".\",\n  mainConfigFile: \"samples/bundle/src/main.js\",\n  findNestedDependencies: true, \n  dir: \"samples/bundle/build\", \n  namespace: 'initscriptExample', \n  paths: {\n      requireLib: 'lib/requirejs/require'\n  },\n  modules: [\n      {\n          name: \"bundle\",\n          include: [\"requireLib\", \"main\"],\n          //True tells the optimizer it is OK to create\n          //a new file foo.js. Normally the optimizer\n          //wants foo.js to exist in the source directory.\n          create: true\n      }\n  ]\n}\n```\n\nWhen using a bundle build, your requirejs-application can also be executed multiple times.  \nIn this case, we need a dynamic require with a cache-busting id on the initscript-plugin in order to make the plugin run any time the script gets executed.\n\nSince we cannot ressolve any dependency filenames in a bundled build, the name of the output-file needs to be configured.\nAlso optimizer throws an error on dynamic require-calls at the first level, so we workaround this by wrapping it in a static call referencing local-require.\n\n```\n// src/main.js\nrequire.config(\n  {\n    paths: {\n      'initscript': 'lib/requirejs-initscript/build/initscript'\n    }, \n    config: {\n      initscript: {\n        // filename of executing requirejs-bundle\n        name: 'bundle' \n      }\n    } \n  }\n);\n// need to wrap it into a static require call to get it work with optimizer namespace option.\n// also add a static reference to the initscript-plugin in order to be included in optimized build.\nrequire(['require', 'initscript'], function(require) {\n  // cache-bust the src to make it call every time the script executes\n  require(['initscript!' + new Date().getTime()], function(initscript) {\n    // init app\n  });\n});\n```\n```\n\u003cscript src=\"bundle.js\" data-value=\"static_1\"\u003e\u003c/script\u003e\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenignware%2Frequirejs-initscript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenignware%2Frequirejs-initscript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenignware%2Frequirejs-initscript/lists"}