{"id":16528722,"url":"https://github.com/zetlen/require-shim","last_synced_at":"2025-03-03T06:24:44.300Z","repository":{"id":5860121,"uuid":"7077051","full_name":"zetlen/require-shim","owner":"zetlen","description":"Dynamic, optimizer-aware non-AMD shim plugin for RequireJS.","archived":false,"fork":false,"pushed_at":"2014-03-07T16:48:00.000Z","size":504,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-16T10:10:24.545Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zetlen.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":"2012-12-09T08:48:25.000Z","updated_at":"2016-01-20T12:58:54.000Z","dependencies_parsed_at":"2022-08-31T15:42:17.067Z","dependency_job_id":null,"html_url":"https://github.com/zetlen/require-shim","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/zetlen%2Frequire-shim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetlen%2Frequire-shim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetlen%2Frequire-shim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zetlen%2Frequire-shim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zetlen","download_url":"https://codeload.github.com/zetlen/require-shim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241618162,"owners_count":19991809,"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-11T17:41:21.018Z","updated_at":"2025-03-03T06:24:44.256Z","avatar_url":"https://github.com/zetlen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RequireJS Dynamic Shimming Plugin\n\nPlugin for [RequireJS](http://requirejs.org) that allows the open-minded module author to \"shim\" non-AMD scripts **at define time**, rather than in the `shim` configuration option. Works with the optimizer, and better than the shim config does, at that. No disrespect intended to @jrburke of course.\n\n## Overview\n\nYou have chosen to use AMDs and RequireJS in your project. Congratulations. You have chosen the path of righteousness. Still, we live in a fallen world, and if you're building a project of sufficient size, you may encounter [useful](http://handlebarsjs.com/) [tools](http://underscorejs.org/) [that](http://twitter.github.com/bootstrap/) [remain](http://davisjs.com/) [unenlightened](http://backbonejs.org). You want to use them in your project, maybe even inline them in your build, but you'd like to avoid modifying these libraries' source or depending on AMD forks which may not be up to date. The shim plugin can help you specify a complex dependency in a single, concise string that will correctly inject your dependency, and write a modularized version of the noncompliant script into your r.js-optimized build.\n\nThe [shim config](http://requirejs.org/docs/api.html#config-shim) is the official RequireJS feature for non-AMD scripts. To use a noncompliant script like Backbone, you would have to configure your Require instance like this:\n\n```javascript\nrequirejs.config({\n    shim: {\n        'backbone': {\n            //These script dependencies should be loaded before loading\n            //backbone.js\n            deps: ['underscore', 'jquery'],\n            //Once loaded, use the global 'Backbone' as the\n            //module value.\n            exports: 'Backbone'\n        },\n    }\n})\n```\n\nThe above config would enable you to require Backbone in your scripts like so:\n\n```javascript\nrequire(['backbone'], function(Backbone){\n    // Backbone available within callback.\n})\n```\n\nThere are three drawbacks to this approach. In ascending order of seriousness, they are:\n\n-   It's wordy, which adds file weight\n\n-   It requires maintenance of a separate file, the config, with a dependency dictionary in it\n\n-   It does not play nice with the optimizer. As the Require docs put it: \n    \u003e*\"if you use an AMD module as a dependency for a shim config module, after a build, that AMD module may not be evaluated until after the shimmed code in the build executes, and an error will occur. The ultimate fix is to upgrade all the shimmed code to have optional AMD define() calls.\"*\n\n\n\n\nThe shim plugin addresses these concerns:\n \n-   It has a concise syntax\n\n-   It allows a description of the dependency *in the place where it is required*, making code more modular\n\n-   When invoked via r.js, it writes an \"upgraded\" version of the non-AMD module into an optimized build.\n\nThe shim plugin allows you to declare dependencies, specify exports, and name the arguments of nested dependencies.\n\n \n## Usage\n\nThe shim plugin equivalent of the above Backbone shim is:\n\n```javascript\nrequire(['shim!backbone[underscore,jquery]\u003eBackbone'], function(Backbone) {\n    // Backbone available within callback.\n});\n```\n\nThe angle bracket is the equivalent of the `exports` property of shim config. It tells the plugin that the `Backbone` variable of the shimmed script must be returned to the `define()` call, so it can be received as the first argument to the callback function. \n\nSquare Brackets enclose dependency descriptions. Optionally, you can name the argument that your shimmed script will receive in its AMD wrapper. It's not usually necessary for scripts that set globals, as Backbone and Underscore do, but it allows you to uglify top-level symbols and still maintain your references:\n\n```javascript\nrequire(['shim!backbone[shim!underscore\u003e_=_, jquery=jQuery]\u003eBackbone'], function(Backbone){\n    // Backbone available even with all top-level symbols obfuscated.\n})\n```\n\nNote that the shimmed Backbone is declaring a *shimmed Underscore* as a dependency. You can nest calls to the shim plugin arbitrarily deep. Shimming is not necessary with jQuery, since jQuery already includes an AMD `define()` call. We merely need to name it jQuery, since that's what Backbone expects it to be called. This will result in the dynamic creation (or, in the case of the optimizer, the permanent creation) of the following:\n\n```javascript\n// underscore first\ndefine('underscore', [], function() {\n    // vendored Underscore code here\n    return _;\n})\n\n// then backbone\ndefine('backbone', ['underscore','jquery'], function(_, jQuery){\n    // vendored Backbone code here\n    return Backbone;\n})\n```\n\nAnd all from a single string that only looks a little wacky.\n\n## Installation\n\n### **Note**: The shim plugin has a hard dependency on the RequireJS text plugin. That plugin must be already installed in the manner described below.\n\nDownload src/shim.js and put it in your RequireJS project's baseURL or appDir (alternately, write a paths config that points to it, like `paths: { shim: 'lib/shim.js'}`.)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzetlen%2Frequire-shim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzetlen%2Frequire-shim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzetlen%2Frequire-shim/lists"}