Skip to content

Commit

Permalink
Merge branch 'issue-168'
Browse files Browse the repository at this point in the history
  • Loading branch information
azproduction committed Jan 6, 2014
2 parents 0ac9d96 + da3b871 commit 32b798d
Show file tree
Hide file tree
Showing 20 changed files with 452 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ all: test

test: test_plugins test_builder build_examples

# Build time tests
test_builder:
@echo 'travis_fold:start:test_builder'
$(MOCHA) -u bdd -R spec --recursive test/build
@echo 'travis_fold:end:test_builder'

# Client-side tests of all plugins
test_plugins: build_test
@echo 'travis_fold:start:test_plugins'
@node ./test/qunit/run-test.js
Expand All @@ -31,7 +33,7 @@ build_stats:
@cd test/qunit; node $(LMD_BUILD) build worker_test
@cd test/qunit; node $(LMD_BUILD) build promise_test

# This is smoke test
# Smoke tests for all examples
build_examples:
@echo 'travis_fold:start:smoke'
# Features
Expand All @@ -48,6 +50,8 @@ build_examples:
@cd examples/features/optimize; node ../$(LMD_BUILD) build index
@cd examples/features/sandbox; node ../$(LMD_BUILD) build index
@cd examples/features/styles; node ../$(LMD_BUILD) build index
@cd examples/features/banner; node ../$(LMD_BUILD) build index
@cd examples/features/banner; node ../$(LMD_BUILD) build dynamic

# Plugins
@cd examples/plugins/amd; node ../$(LMD_BUILD) build index
Expand Down
12 changes: 9 additions & 3 deletions bin/lmd_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ LmdBuilder.prototype.renderLmdPackage = function (config, modulesBundle, isOptim
options = JSON.stringify(options);

result = this.templatePackage({
build_info: this._buildInfo(config),
build_info: this._getBundleBanner(config),
lmd_js: lmd_js,
global: config.global || 'this',
lmd_main: lmd_main || 'function(){}',
Expand Down Expand Up @@ -917,13 +917,19 @@ LmdBuilder.prototype.renderLmdBundle = function (config, modulesBundle) {
return this.templateBundle({
lmd_main: modulesBundle.main,
bundles_callback: config.bundles_callback,
build_info: this._buildInfo(config),
build_info: this._getBundleBanner(config),
lmd_modules: '{\n' + modulesBundle.modules.join(',\n') + '\n}',
modules_options: JSON.stringify(modulesBundle.options)
});
};

LmdBuilder.prototype._buildInfo = function (config) {
LmdBuilder.prototype._getBundleBanner = function (config) {
// If exists return
if (typeof config.banner === 'string') {
return config.banner;
}

// Else create default
var configFile = path.basename(this.configFile),
mixinFiles = (config.mixins || []).map(function (mixin) {
return path.basename(mixin);
Expand Down
15 changes: 15 additions & 0 deletions examples/features/banner/.lmd/dynamic.lmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var read = require('fs').readFileSync;

var banner = '/*!\n * ' + read(__dirname + '/../LICENCE', 'utf8').replace(/\n/g, '\n * ') + '\n */';

module.exports = {
name: 'dynamic banner example',
banner: banner,
root: '../js',
output: '../dynamic.js',
modules: {
main: 'main.js'
},
main: 'main',
ie: false
};
21 changes: 21 additions & 0 deletions examples/features/banner/.lmd/index.lmd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "static banner example",
"banner": "/*! Bundle from config \"<%= __filename.replace(__dirname, '').slice(1) %>\" */",
"root": "../js",
"output": "../index.js",
"modules": {
"main": "main.js"
},
"bundles": {
"test": {
"name": "bundles are inherit banner from parent module",
"modules": {
"main": "main.js"
},
"styles_output": false
}
},
"main": "main",
"ie": false,
"optimize": true
}
13 changes: 13 additions & 0 deletions examples/features/banner/LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Anonymous <i@anonymous.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
8 changes: 8 additions & 0 deletions examples/features/banner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This example covers

* banner - message before bundle code

```js
/*! Banner is this */
(function(global,main,modules,modules_options,options){})
```
170 changes: 170 additions & 0 deletions examples/features/banner/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*!
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Anonymous <i@anonymous.net>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
(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];

var replacement = lmd_trigger('*:rewrite-shortcut', moduleName, module);
if (replacement) {
moduleName = replacement[0];
module = replacement[1];
}

lmd_trigger('*:before-check', moduleName, module);
// Already inited - return as is
if (initialized_modules[moduleName] && module) {
return module;
}

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;
}



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('it works');

}),{

},{},{"bundle":"_ed84fa22"});
9 changes: 9 additions & 0 deletions examples/features/banner/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>LMD banner example</title>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
</body>
</html>
8 changes: 8 additions & 0 deletions examples/features/banner/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/features/banner/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*! Bundle from config "index.lmd.json" */
_b626bde7({
"main": (function (require, exports, module) { /* wrapped by builder */
console.log('it works');

})
},{});
1 change: 1 addition & 0 deletions examples/features/banner/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('it works');
30 changes: 27 additions & 3 deletions lib/lmd_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ var DEFAULT_DEPENDS_MASK = "*.lmd.json";
var SOURCE_TWEAK_FLAGS = ["warn", "log", "pack", "lazy", "optimize"];
exports.SOURCE_TWEAK_FLAGS = SOURCE_TWEAK_FLAGS;

var INHERITABLE_FIELDS = SOURCE_TWEAK_FLAGS.concat(['version', 'main', 'global', 'pack_options', 'mixins', 'bundles_callback']);
var INHERITABLE_FIELDS = SOURCE_TWEAK_FLAGS.concat(['version', 'main', 'global', 'pack_options', 'mixins', 'bundles_callback', 'banner']);
var MASTER_FIELDS = INHERITABLE_FIELDS.concat(['output', 'path', 'root', "sourcemap", "sourcemap_inline", "sourcemap_www", "sourcemap_url", "www_root", "name", "description", 'styles_output']);
exports.MASTER_FIELDS = MASTER_FIELDS;

var FILED_ALIASES = {"path": "root"};

// Fields that will come from package into bundle.
// There is some other fields like sourcemap_** or bundles_callback and output
var BUNDLE_INHERIT_FROM_PACKAGE = ['root', 'warn', 'log', 'lazy', 'pack', 'pack_options', 'optimize'],
var BUNDLE_INHERIT_FROM_PACKAGE = ['root', 'warn', 'log', 'lazy', 'pack', 'pack_options', 'optimize', 'banner'],
SUB_BUNDLE_SEPARATOR = ' → ',
ROOT_BUNDLE_ID = '';

Expand Down Expand Up @@ -297,10 +297,18 @@ var readConfig = function (file/*, filePart, filePart*/) {
var mod = new Module('.', null);
mod.load(file);
config = mod.exports;
}

// Some extra variables
var data = {
__dirname: path.dirname(file),
__filename: file
};
for (var key in config) {
data[key] = config[key];
}

return interpolateConfigStrings(config);
return interpolateConfigStrings(config, data);
};
exports.readConfig = readConfig;

Expand Down Expand Up @@ -1869,6 +1877,22 @@ var collectFlagsWarnings = function (config, deepModulesInfo) {
result.push('`promise`'.green + ' should be a string pointing to the deferred factory function. eg ' + '"promise"'.green + ': ' + '"$.Deferred"'.green);
}

if ('banner' in config) {
if (typeof config.banner !== 'string') {
result.push('`banner`'.green + ' should be a string');
} else {
// Banner should not contain any javascript. Checking it.
try {
var ast = parser.parse(config.banner);
if (ast[1].length !== 0) {
result.push('`banner`'.green + ' should be a JavaScript comment. Please remove any executable JavaScript from ' + '`banner`'.green + '.');
}
} catch (e) {
result.push('Something is wrong with your ' + '`banner`'.green + ', please check it');
}
}
}

var warnings = {
globals: 'Some of your modules are undeclared (register them as ' + '"name"'.green + ': ' + '"@shortcut"'.green + ' or use directly if they are globals):',
modules: 'Some of our modules are probably off-package (you can register them as ' + '"name"'.green + ': ' + '"@shortcut"'.green + '):',
Expand Down
Loading

0 comments on commit 32b798d

Please sign in to comment.