Permalink
Browse files

#91 added examples for "depends", "extends", "sandbox"; Added refere…

…nces in README.md
  • Loading branch information...
azproduction committed Jan 25, 2013
1 parent 91831e5 commit 3ed3add1099982f87f113352ef58ddd01b169911
View
@@ -189,7 +189,13 @@ See [grunt-lmd](https://github.com/azproduction/grunt-lmd) for details
* glob - you can specify glob pattern (eg `"${name}": "js/*.js"`) to match multiply files. See [demo](http://azproduction.ru/lmd/examples/features/glob/), [code](https://github.com/azproduction/lmd/tree/master/examples/features/glob/)
* interpolation - you can use templates in your config string values to make your life easier. See [demo](http://azproduction.ru/lmd/examples/features/interpolation/), [code](https://github.com/azproduction/lmd/tree/master/examples/features/interpolation/)
+ * sandbox - some of your modules can be in the "sandbox". They cant require, but can provide some resources. See [demo](http://azproduction.ru/lmd/examples/features/sandbox/), [code](https://github.com/azproduction/lmd/tree/master/examples/features/sandbox/)
+
+## Config extras
+
* mixins - you can mix your build configs (eg `lmd build index+ru+dev`) to create your special builds. See [demo](http://azproduction.ru/lmd/examples/features/mixins/), [code](https://github.com/azproduction/lmd/tree/master/examples/features/mixins/)
+ * depends - your modules can have own depends. You can notify LMD by adding global "depends" paramenter or specify "depends" for each module. See [demo](http://azproduction.ru/lmd/examples/features/depends/), [code](https://github.com/azproduction/lmd/tree/master/examples/features/depends/)
+ * extends - your configs can inherit other configs (eg development extends production). See [demo](http://azproduction.ru/lmd/examples/features/extends/), [code](https://github.com/azproduction/lmd/tree/master/examples/features/extends/)
## Bash/zsh completion
@@ -0,0 +1,17 @@
+{
+ "name": "Depends example",
+ "description": "`depends` field is mask for module depends. \nWith 'depends': '*.lmd.json' depends config for `main.js` will be `main.lmd.json`",
+
+ "root": "../js",
+ "output": "../index.js",
+ "modules": {
+ "main": "main.js",
+ "module_with_own_depends": {
+ "path": "module_with_own_depends.js",
+ "depends": "module_with_own_depends.json"
+ }
+ },
+ "main": "main",
+
+ "depends": "*.lmd.json"
+}
@@ -0,0 +1,3 @@
+This example covers
+
+ * depends - module depends
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>LMD depends example</title>
+ <script type="text/javascript" src="index.js"></script>
+</head>
+<body>
+See console for info.
+</body>
+</html>
@@ -0,0 +1,264 @@
+(function (global, main, modules, modules_options, options) {
+ var initialized_modules = {},
+ global_eval = function (code) {
+ return global.Function('return ' + code)();
+ },
+
+ global_document = global.document,
+ local_undefined,
+ /**
+ * @param {String} moduleName module name or path to file
+ * @param {*} module module content
+ *
+ * @returns {*}
+ */
+ register_module = function (moduleName, module) {
+ lmd_trigger('lmd-register:before-register', moduleName, module);
+ // Predefine in case of recursive require
+ var output = {exports: {}};
+ initialized_modules[moduleName] = 1;
+ modules[moduleName] = output.exports;
+
+ if (!module) {
+ // if undefined - try to pick up module from globals (like jQuery)
+ // or load modules from nodejs/worker environment
+ module = lmd_trigger('js:request-environment-module', moduleName, module)[1] || global[moduleName];
+ } else if (typeof module === "function") {
+ // Ex-Lazy LMD module or unpacked module ("pack": false)
+ var module_require = lmd_trigger('lmd-register:decorate-require', moduleName, lmd_require)[1];
+
+ // Make sure that sandboxed modules cant require
+ if (modules_options[moduleName] &&
+ modules_options[moduleName].sandbox &&
+ typeof module_require === "function") {
+
+ module_require = local_undefined;
+ }
+
+ module = module(module_require, output.exports, output) || output.exports;
+ }
+
+ module = lmd_trigger('lmd-register:after-register', moduleName, module)[1];
+ return modules[moduleName] = module;
+ },
+ /**
+ * List of All lmd Events
+ *
+ * @important Do not rename it!
+ */
+ lmd_events = {},
+ /**
+ * LMD event trigger function
+ *
+ * @important Do not rename it!
+ */
+ lmd_trigger = function (event, data, data2, data3) {
+ var list = lmd_events[event],
+ result;
+
+ if (list) {
+ for (var i = 0, c = list.length; i < c; i++) {
+ result = list[i](data, data2, data3) || result;
+ if (result) {
+ // enable decoration
+ data = result[0] || data;
+ data2 = result[1] || data2;
+ data3 = result[2] || data3;
+ }
+ }
+ }
+ return result || [data, data2, data3];
+ },
+ /**
+ * LMD event register function
+ *
+ * @important Do not rename it!
+ */
+ lmd_on = function (event, callback) {
+ if (!lmd_events[event]) {
+ lmd_events[event] = [];
+ }
+ lmd_events[event].push(callback);
+ },
+ /**
+ * @param {String} moduleName module name or path to file
+ *
+ * @returns {*}
+ */
+ lmd_require = function (moduleName) {
+ var module = modules[moduleName];
+
+ lmd_trigger('*:before-check', moduleName, module);
+ // Already inited - return as is
+ if (initialized_modules[moduleName] && module) {
+ return module;
+ }
+ var replacement = lmd_trigger('*:rewrite-shortcut', moduleName, module);
+ if (replacement) {
+ moduleName = replacement[0];
+ module = replacement[1];
+ }
+
+ lmd_trigger('*:before-init', moduleName, module);
+
+ // Lazy LMD module not a string
+ if (typeof module === "string" && module.indexOf('(function(') === 0) {
+ module = global_eval(module);
+ }
+
+ return register_module(moduleName, module);
+ },
+ output = {exports: {}},
+
+ /**
+ * Sandbox object for plugins
+ *
+ * @important Do not rename it!
+ */
+ sandbox = {
+ global: global,
+ modules: modules,
+ modules_options: modules_options,
+ options: options,
+
+ eval: global_eval,
+ register: register_module,
+ require: lmd_require,
+ initialized: initialized_modules,
+
+
+ document: global_document,
+
+
+
+ on: lmd_on,
+ trigger: lmd_trigger,
+ undefined: local_undefined
+ };
+
+ for (var moduleName in modules) {
+ // reset module init flag in case of overwriting
+ initialized_modules[moduleName] = 0;
+ }
+
+/**
+ * @name sandbox
+ */
+(function (sb) {
+
+// Simple JSON stringify
+function stringify(object) {
+ var properties = [];
+ for (var key in object) {
+ if (object.hasOwnProperty(key)) {
+ properties.push(quote(key) + ':' + getValue(object[key]));
+ }
+ }
+ return "{" + properties.join(",") + "}";
+}
+
+function getValue(value) {
+ if (typeof value === "string") {
+ return quote(value);
+ } else if (typeof value === "boolean") {
+ return "" + value;
+ } else if (value.join) {
+ if (value.length == 0) {
+ return "[]";
+ } else {
+ var flat = [];
+ for (var i = 0, len = value.length; i < len; i += 1) {
+ flat.push(getValue(value[i]));
+ }
+ return '[' + flat.join(",") + ']';
+ }
+ } else if (typeof value === "number") {
+ return value;
+ } else {
+ return stringify(value);
+ }
+}
+
+function pad(s) {
+ return '0000'.substr(s.length) + s;
+}
+
+function replacer(c) {
+ switch (c) {
+ case '\b': return '\\b';
+ case '\f': return '\\f';
+ case '\n': return '\\n';
+ case '\r': return '\\r';
+ case '\t': return '\\t';
+ case '"': return '\\"';
+ case '\\': return '\\\\';
+ default: return '\\u' + pad(c.charCodeAt(0).toString(16));
+ }
+}
+
+function quote(s) {
+ return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, replacer) + '"';
+}
+
+function indexOf(item) {
+ for (var i = this.length; i --> 0;) {
+ if (this[i] === item) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+ /**
+ * @event *:request-json requests JSON polifill with only stringify function!
+ *
+ * @param {Object|undefined} JSON default JSON value
+ *
+ * @retuns yes
+ */
+sb.on('*:request-json', function (JSON) {
+ if (typeof JSON === "object") {
+ return [JSON];
+ }
+
+ return [{stringify: stringify}];
+});
+
+ /**
+ * @event *:request-indexof requests indexOf polifill
+ *
+ * @param {Function|undefined} arrayIndexOf default indexOf value
+ *
+ * @retuns yes
+ */
+sb.on('*:request-indexof', function (arrayIndexOf) {
+ if (typeof arrayIndexOf === "function") {
+ return [arrayIndexOf];
+ }
+
+ return [indexOf];
+});
+
+}(sandbox));
+
+
+
+ main(lmd_trigger('lmd-register:decorate-require', "main", lmd_require)[1], output.exports, output);
+})/*DO NOT ADD ; !*/(this,(function (require, exports, module) { /* wrapped by builder */
+console.log('Main loaded');
+require('main_dependence');
+require('module_with_own_depends');
+
+}),{
+"data": {
+ "data": "module_with_own_depends depends loaded"
+},
+"main_dependence": (function (require, exports, module) { /* wrapped by builder */
+console.log('Dependence of main loaded');
+
+}),
+"module_with_own_depends": (function (require, exports, module) { /* wrapped by builder */
+console.log(require('data').data);
+
+})
+},{},{})
@@ -0,0 +1,3 @@
+console.log('Main loaded');
+require('main_dependence');
+require('module_with_own_depends');
@@ -0,0 +1,5 @@
+{
+ "modules": {
+ "main_dependence": "main_dependence.js"
+ }
+}
@@ -0,0 +1 @@
+console.log('Dependence of main loaded');
@@ -0,0 +1 @@
+console.log(require('data').data);
@@ -0,0 +1,6 @@
+{
+ "root": "../json",
+ "modules": {
+ "data": "data.json"
+ }
+}
@@ -0,0 +1,3 @@
+{
+ "data": "module_with_own_depends depends loaded"
+}
@@ -0,0 +1,10 @@
+{
+ "name": "dev build",
+ "root": "../js",
+ "output": "../index-dev.js",
+ "extends": "./prod.lmd.json",
+ "modules": {
+ "main": "main-dev.js",
+ "debug": "debug.js"
+ }
+}
@@ -0,0 +1,9 @@
+{
+ "name": "production build",
+ "root": "../js",
+ "output": "../index.js",
+ "modules": {
+ "main": "main.js"
+ },
+ "main": "main"
+}
@@ -0,0 +1,3 @@
+This example covers
+
+ * extends - config files inheritance
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>LMD extends example</title>
+ <script type="text/javascript" src="index-dev.js"></script>
+</head>
+<body>
+See console for info or open <a href="./index.html">Production</a> version.
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 3ed3add

Please sign in to comment.