Add support for wrapping the build in UMD syntax.

This commit is contained in:
Tim Schaub
2014-12-13 13:23:10 -08:00
parent 7fd017879f
commit b60b0ecdb0
5 changed files with 35 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
{
"exports": ["*"]
"exports": ["*"],
"umd": true
}

View File

@@ -1,5 +1,6 @@
{
"exports": ["*"],
"umd": true,
"compile": {
"externs": [
"externs/bingmaps.js",
@@ -59,7 +60,6 @@
],
"compilation_level": "ADVANCED",
"warning_level": "VERBOSE",
"output_wrapper": "(function(){%output%})();",
"use_types_for_optimization": true,
"manage_closure_dependencies": true
}

View File

@@ -16,9 +16,23 @@ var generateExports = require('./generate-exports');
var log = closure.log;
var root = path.join(__dirname, '..');
var umdWrapper = '(function (root, factory) {\n' +
' if (typeof define === "function" && define.amd) {\n' +
' define([], factory);\n' +
' } else if (typeof exports === "object") {\n' +
' module.exports = factory();\n' +
' } else {\n' +
' root.ol = factory();\n' +
' }\n' +
'}(this, function () {\n' +
' var OPENLAYERS = {};\n' +
' %output%\n' +
' return OPENLAYERS.ol;\n' +
'}));\n';
/**
* Assert that a provided config object is valid.
* Apply defaults and assert that a provided config object is valid.
* @param {Object} config Build configuration object.
* @param {function(Error)} callback Called with an error if config is invalid.
*/
@@ -44,6 +58,12 @@ function assertValidConfig(config, callback) {
callback(new Error('Config "src" must be an array'));
return;
}
if (config.umd) {
config.namespace = 'OPENLAYERS';
if (config.compile) {
config.compile.output_wrapper = umdWrapper;
}
}
callback(null);
});
}
@@ -156,7 +176,10 @@ function concatenate(paths, callback) {
callback(new Error(msg));
} else {
var preamble = 'var CLOSURE_NO_DEPS = true;\n';
callback(null, preamble + results.join('\n'));
var parts = umdWrapper.split('%output%');
var postamble = 'OPENLAYERS.ol = ol;\n';
callback(null,
preamble + parts[0] + results.join('\n') + postamble + parts[1]);
}
});
}

View File

@@ -172,6 +172,11 @@ function generateExports(symbols, namespace) {
Object.keys(requires).sort().reverse().forEach(function(name) {
blocks.unshift('goog.require(\'' + name + '\');');
});
blocks.unshift(
'/**\n' +
' * @fileoverview Custom exports file.\n' +
' * @suppress {checkVars}\n' +
' */\n');
return blocks.join('\n');
}

View File

@@ -26,6 +26,8 @@ Build configuration files are JSON files that are used to determine what should
If the **compile** object is not provided, the build task will generate a "debug" build of the library without any variable naming or other minification. This is suitable for development or debugging purposes, but should not be used in production.
* **umd** - `boolean` Optional flag to wrap the build in [UMD syntax](https://github.com/umdjs/umd). If set to `true`, the build output can be used with a CommonJS module loader (e.g. [Browserify](http://browserify.org/)), an AMD script loader (e.g. [RequireJS](http://requirejs.org/)), or just loaded with a `<script>` tag. If this option is specified, the **namespace** and any **compile.output_wrapper** options will be ignored.
* **src** - `Array.<string>` Optional array of [path patterns](https://github.com/isaacs/minimatch/blob/master/README.md) for source files, that is, those that provide the symbols/names included in `exports`. By default, all of the library source files will be included (`'src/**/*.js'`). If you want to provide additional source files to be configured together with the library, you need to provide path patterns to your source files *and* the library source files. Note that these patterns are `/` delimited even on Windows. There is a bit of special handling with the `src` config.
* **cwd** - `string` Optional path to be used as the current working directory. All paths in the `compile` object are assumed to be relative to `cwd`. Default is the root of the ol3 repository.