├── .gitignore ├── .jshintrc ├── .npmignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── bower.json ├── package.json ├── require1k.js ├── require1k.min.js └── test ├── comment ├── index.js └── should-not-load.js ├── index.html ├── index.js ├── multi-require ├── a.js ├── b.js └── index.js ├── node_modules └── dep │ ├── index.js │ ├── index2.js │ └── node_modules │ └── dep2 │ └── dep2.js ├── second.js └── three.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "unused": true, 3 | "evil": true, 4 | "undef": true, 5 | "globals": { 6 | "R": true, 7 | "XMLHttpRequest": false, 8 | "location": false, 9 | "btoa": false, 10 | "window": false, 11 | "document": false 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | spec 2 | .jshintrc 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v2.0.0 4 | 5 | * Ignore `require` calls in comments (#16) 6 | * Ignore `$require` and `object.require` calls (#16) 7 | 8 | ## v1.0.1 9 | 10 | * Initial release 11 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013, Stuart Knightley 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | * Neither the name of require1k nor the 15 | names of its contributors may be used to endorse or promote products 16 | derived from this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL STUART KNIGHTLEY BE LIABLE FOR ANY 22 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CommonJS `require` in 1k, with no build needed 2 | 3 | This project implements a minimal, and yet practically useful, CommonJS/Node.js `require` module loader for the browser in under 1000 bytes. 4 | 5 | ## Features 6 | 7 | * Synchronous `require` in modules 8 | * `exports` and `module.exports` object 9 | * Loaded modules are debuggable as normal in Chrome, Opera 15+, Firefox 36+ and Safari with `debugger` statement. (IE to be tested.) 10 | * Works with npm, by searching `node_modules` directories (see Limitations) 11 | * Handles circular dependencies 12 | * Just runs, no need to build your JavaScript 13 | * A pretty rich API, considering: 14 | * it's 1k! 15 | 16 | ## Examples 17 | 18 | * [Simple](http://stuk.github.io/require1k/examples/simple/) 19 | * [Marked](http://stuk.github.io/require1k/examples/marked) markdown parser 20 | * [React](http://stuk.github.io/require1k/examples/react) framework (!!) 21 | 22 | ## Usage 23 | 24 | In your modules you can use the `require` function, `exports` object and `module.exports` object as you would in Node. There are two ways to kick off the require system: 25 | 26 | ### script `data-main` attribute 27 | 28 | ```html 29 | 30 | ``` 31 | 32 | On start require1k will search for the first ` 50 | ``` 51 | 52 | Require1k also adds a global function, `R`, that accepts either a function or a module ID and an optional `callback`. When given a function it will load all the dependencies of the function and then execute it. If there was an error the callback gets passed the XMLHttpRequest object that failed as the first argument. It also gets passed the `exports` of the function, which you may find useful. 53 | 54 | ### global `R(moduleId, callback)` 55 | 56 | ```html 57 | 66 | ``` 67 | 68 | Alternatively you can provide a module ID and a optional `callback`. The named module is resolved against the location of the HTML file. If there was an error the callback gets passed the XMLHttpRequest object that failed as the first argument, otherwise the `exports` of the module are passed as the second argument. The callback may be called synchronously if the module is already in the internal module cache. 69 | 70 | ## Limitations 71 | 72 | * `package.json` files are not loaded, and so the `"main"` property is ignored. Modules inside packages must be requested by their full path, e.g. `var _ = require("underscore/underscore");` 73 | * Cross-package dependencies are found by walking up the URL path until a successful request is returned. This means that 74 | * loading cross-package dependencies is slow because requests are made sequentially until the module is found 75 | * 404 errors appear in the console 76 | * Missing cross-package dependencies will exceed the stack size and halt script execution. 77 | * Callback functions are called synchronously if the data is already available. This releases [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony), but hey, it's 1k. 78 | * Does not load `.json` files 79 | 80 | 81 | ## The point 82 | 83 | The short answer: because it's a fun hack. 84 | 85 | The the long answer? 86 | 87 | Using ` 7 | 14 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | console.log("loaded index.js"); 2 | 3 | var second = require("./second"); 4 | assert(second.thing === "asd", "required export should be correct"); 5 | 6 | R("./three", function (err, exports) { 7 | console.log("R callback"); 8 | assert(err === undefined, "err in callback should be undefined"); 9 | assert(exports === "three", "exports in callback should be correct"); 10 | }); 11 | 12 | require("dep/index"); 13 | 14 | require("./multi-require/index"); 15 | 16 | require("./comment/index"); 17 | -------------------------------------------------------------------------------- /test/multi-require/a.js: -------------------------------------------------------------------------------- 1 | module.exports = "a"; 2 | -------------------------------------------------------------------------------- /test/multi-require/b.js: -------------------------------------------------------------------------------- 1 | module.exports = "b"; 2 | -------------------------------------------------------------------------------- /test/multi-require/index.js: -------------------------------------------------------------------------------- 1 | var a = require("./a"); var b = require("./b"); 2 | assert(a === "a", "first require on a line should run"); 3 | assert(b === "b", "second require on a line should run"); 4 | -------------------------------------------------------------------------------- /test/node_modules/dep/index.js: -------------------------------------------------------------------------------- 1 | console.log("dep/index run"); 2 | 3 | require("dep2/dep2"); 4 | -------------------------------------------------------------------------------- /test/node_modules/dep/index2.js: -------------------------------------------------------------------------------- 1 | console.log("dep index2"); 2 | -------------------------------------------------------------------------------- /test/node_modules/dep/node_modules/dep2/dep2.js: -------------------------------------------------------------------------------- 1 | console.log("dep2/dep2 run!"); 2 | 3 | require("dep/index2"); 4 | -------------------------------------------------------------------------------- /test/second.js: -------------------------------------------------------------------------------- 1 | console.log("loaded second"); 2 | 3 | exports.thing = "asd"; 4 | 5 | require("./index"); 6 | assert(require("./three") === "three", "require should return exports") 7 | -------------------------------------------------------------------------------- /test/three.js: -------------------------------------------------------------------------------- 1 | module.exports = "three"; 2 | --------------------------------------------------------------------------------