From b60b0ecdb01de0be7a1e12101cbc74459156dbf0 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 13 Dec 2014 13:23:10 -0800 Subject: [PATCH] Add support for wrapping the build in UMD syntax. --- config/ol-debug.json | 3 ++- config/ol.json | 2 +- tasks/build.js | 27 +++++++++++++++++++++++++-- tasks/generate-exports.js | 5 +++++ tasks/readme.md | 2 ++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/config/ol-debug.json b/config/ol-debug.json index b8f37e2ff1..cf20571932 100644 --- a/config/ol-debug.json +++ b/config/ol-debug.json @@ -1,3 +1,4 @@ { - "exports": ["*"] + "exports": ["*"], + "umd": true } diff --git a/config/ol.json b/config/ol.json index 1e86764780..5b09d266f6 100644 --- a/config/ol.json +++ b/config/ol.json @@ -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 } diff --git a/tasks/build.js b/tasks/build.js index 3f4fe2e2cf..8196acaabe 100644 --- a/tasks/build.js +++ b/tasks/build.js @@ -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]); } }); } diff --git a/tasks/generate-exports.js b/tasks/generate-exports.js index a9fb216f4e..630b1a7aa7 100644 --- a/tasks/generate-exports.js +++ b/tasks/generate-exports.js @@ -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'); } diff --git a/tasks/readme.md b/tasks/readme.md index d758a6d03f..a08a958770 100644 --- a/tasks/readme.md +++ b/tasks/readme.md @@ -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 `