Skip to content

Commit

Permalink
Merge d03da07 into b9e5060
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckdumont committed Nov 15, 2017
2 parents b9e5060 + d03da07 commit d57379d
Show file tree
Hide file tree
Showing 28 changed files with 585 additions and 373 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ plugins: [
]
```

The loader config may be specified as an object, a function that returns the config object, or as a string which represents the name of a CommonJS module that exports the config (object or function). If specified as an object or a function, then the config expressions are evaluated at build time and the config is exported to the client as a JSON object that is mixed with the `window.dojoConfig` property at application load time.
The loader config may be specified as an object, a function that returns the config object, or as a string which represents the name of a CommonJS module that exports the config (object or function). If specified as an object or a function, then the config expressions are evaluated at build time and the config object is serialized to the client and mixed with the `window.dojoConfig` property at application load time.

If the config is specified as a module name, then the config module will be evaluated both at build time (for the purpose of resolving modules for webpack), and then again at application run time when the config module is loaded on the client. Note that if you want webpack to process the config module (i.e. perform build time variable substitution, etc.) then you must specify the config as a module name.

If you want the config to specify different properties at build time vs. run time, then specify the config as a function that returns the config object and use the [environment](#environment) and [buildEnvironment](#buildenvironment) options so set the properties who's values change depending on the target environment. This works both when the config is evaluated at build time (specified as a function) and when the config is evaluated at build time and runtime (specified as the name of a CommonJS module that exports a function).

There is a small (~0.5KB) size benefit to specifying the loader config as an object or function vs. as a module name, because the embedded Dojo loader can be built without the code needed for parsing the Dojo loader config on the client. When using an embedded Dojo loader that does not include the config api, post-processed config objects generated at build time are serialized to the application and used to initialize the embedded Dojo loader at application run-time. This plugin will automatically build the embedded Dojo loader with, or without, the config api depending on the value type of the [loaderConfig](#loaderconfig) option, unless the loader config specifies a value for the `dojo-config-api` feature in the `has` property (in which case the embedded Dojo loader is built using the specified value for the feature), or unless you specify a previously built loader using the [loader](#loader) option. See [Overriding profile features](#overriding-profile-features) for details on how to build the loader without the config api. Note that the plugin is able to detect at application build time whether or not the embedded Dojo loader has the config API and can work with either one, with the caveat that the config api is required if the loader config is specified as a module name.

See [js/loaderConfig.js](https://github.com/OpenNTF/dojo-webpack-plugin-sample/blob/master/js/loaderConfig.js) in the sample project for an example of a Dojo loader config that uses the [environment](#environment) and [buildEnvironment](#buildenvironment) options to specify different config paths for build time vs run time. The config also supports running the sample app as a non-packed application with Dojo loaded from a CDN.

# Dojo loader extensions
Expand Down Expand Up @@ -222,9 +224,9 @@ This property is optional. If the value is truthy, then console output from bui

# Building the Dojo loader

This plugin uses a custom build of the Dojo loader. The built loader is packaged as a CommonJS module so that it may be more easily consumed by Webpack. The loader build config specifies has.js features which exclude unneeded code (e.g. for loading modules) so that the loader embedded into the client is as small as possible (~4KB after uglify and gzip). The Dojo loader builder requires that the Dojo util directory is a sibling of the `dojo` directory and is named either `util` or `dojo-util`.
This plugin embeds a custom build of the Dojo loader (dojo.js) in your packed application. The built loader is packaged as a CommonJS module so that it may be more easily consumed by Webpack. The loader build profile specifies has.js features which exclude unneeded code (e.g. for loading modules) so that the loader embedded into the client is as small as possible (less than 4KB after uglify and gzip). The Dojo loader builder requires that the Dojo util directory is a sibling of the `dojo` directory and is named either `util` or `dojo-util`.

If you do not want to build the Dojo loader every time Webpack is run, then you can build it manually and specify the location of the built loader using the `loader` option. You can produce a manual build of the loader by running the build script in the buildDojo directory.
If you do not want to build the Dojo loader every time Webpack is run, then you can build it manually and specify the location of the built loader using the [loader](#loader) option. You can produce a manual build of the loader by running the build script in the buildDojo directory.

node node_modules/dojo-webpack-plugin/buildDojo/build.js node_modules/dojo/dojo.js ./release

Expand All @@ -243,6 +245,12 @@ plugins: [
]
```

#### Overriding profile features

By default, the embedded loader is built using the static has features defined [here](https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/buildDojo/loaderDefaultFeatures.js). You may override these features by providing an optional, third argument to the build script which specifies the features you want to override as a JSON string. For example, if you specify the [loaderConfig](#loaderconfig) option as an object, or a function that returns an object (as opposed to specifying it as a module name), then you can make the embedded loader smaller by omitting the config api. This would be done as follows:

node node_modules/dojo-webpack-plugin/buildDojo/build.js node_modules/dojo/dojo.js ./release {\"dojo-config-api\":false}

# ES6 Promise dependency in Webpack 2.x

Webpack 2.x includes code in your packed application that uses ES6 Promise. If you need to support browsers that lack ES6 Promise support (e.g. IE 11), then you will need to provide this capability in your application. This plugin provides a tiny wrapper module named [dojoES6Promise](https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/amd/dojoES6Promise.js) that implements ES6 Promise using dojo/Deferred. All you need to do is include this module as an AMD dependency in your application. See [bootstrap.js](https://github.com/OpenNTF/dojo-webpack-plugin-sample/blob/master/js/bootstrap.js) in the sample application for an example.
Expand Down
5 changes: 4 additions & 1 deletion buildDojo/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const fork = require("child_process").fork;

const dojoPath = global.process.argv.length > 2 && global.process.argv[2];
const releaseDir = global.process.argv.length > 3 && global.process.argv[3];
const featureOverrides = global.process.argv.length > 4 && global.process.argv[4] || "{}";
if (!dojoPath) {
throw Error("Path to dojo not specified");
}
Expand All @@ -34,7 +35,9 @@ const ls = fork(
path.join(__dirname, "loader.profile.js"),
"--release",
"--releaseDir",
path.resolve(releaseDir)
path.resolve(releaseDir),
"--has",
featureOverrides
]
);

Expand Down
59 changes: 22 additions & 37 deletions buildDojo/loader.profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const nodeRequire = require.rawConfig && require.rawConfig.loaderPatch.nodeRequi
const path = nodeRequire("path");
const fs = nodeRequire("fs");

var profilePath, dojoPath;
var profilePath, dojoPath, featureOverrides = {};
process.argv.forEach((arg, i) => {
if (arg === '--profile') {
profilePath = process.argv[i+1];
} else if (arg === '--dojoPath') {
dojoPath = process.argv[i+1];
} else if (arg === '--has') {
featureOverrides = JSON.parse(process.argv[i+1]);
}
});
if (!profilePath) {
Expand All @@ -35,17 +37,24 @@ if (!dojoPath) {
throw new Error("--dojoPath command line option not specified");
}

const version = nodeRequire(path.resolve(dojoPath, "../", "./package.json")).version;
const versionParts = version.split(".");
const majorVersion = parseInt(versionParts[0]), minorVersion = parseInt(versionParts[1]), patchVersion = parseInt(versionParts[2]);
if (majorVersion !== 1) {
throw new Error("Unsupported Dojo Version");
function getLoaderDefaultFeatures() {
const version = nodeRequire(path.resolve(dojoPath, "../", "./package.json")).version;
const versionParts = version.split(".");
const majorVersion = parseInt(versionParts[0]), minorVersion = parseInt(versionParts[1]), patchVersion = parseInt(versionParts[2]);
if (majorVersion !== 1) {
throw new Error("Unsupported Dojo Version");
}
const hasInjectApiFix = /* True if the version of Dojo has https://github.com/dojo/dojo/pull/266 */
minorVersion > 12 ||
minorVersion === 12 && patchVersion >= 3 ||
minorVersion === 11 && patchVersion >= 5 ||
minorVersion === 10 && patchVersion >= 9;

var features = nodeRequire(path.join(process.argv[1], "../loaderDefaultFeatures"));
features = Object.assign({}, features, featureOverrides);
features['dojo-inject-api'] = !hasInjectApiFix;
return features;
}
const hasInjectApiFix = /* True if the version of Dojo has https://github.com/dojo/dojo/pull/266 */
minorVersion > 12 ||
minorVersion === 12 && patchVersion >= 3 ||
minorVersion === 11 && patchVersion >= 5 ||
minorVersion === 10 && patchVersion >= 9;

var profile = (() => {
const profileDir = path.resolve(profilePath);
Expand All @@ -54,6 +63,7 @@ var profile = (() => {
if (!fs.existsSync(path.resolve(dojoDir, util))) {
util = "../util";
}

return {
layerOptimize: false,
releaseDir: "./release",
Expand All @@ -72,32 +82,7 @@ var profile = (() => {
trees: [[".", ".", /\.*/]]
}],

staticHasFeatures:{
'dojo-config-api': 1,
'dojo-inject-api': hasInjectApiFix ? 0 : 1,
'csp-restrictions': 1,
'dojo-built': 1,
'config-dojo-loader-catches': 0,
'config-tlmSiblingOfDojo': 0,
'dojo-log-api': 0,
'dojo-publish-privates': 0,
'dojo-sync-loader': 0,
'dojo-timeout-api': 0,
'dojo-trace-api': 0,
'dojo-sniff': 0,
'dojo-test-sniff': 0,
'dojo-cdn': 0,
'dojo-loader-eval-hint-url': 0,
'config-stripStrict': 0,
'ie-event-behavior': 0,
'dom': 0,
'host-node': 0,
'host-rhino': 0,
'host-webworker': 0,
'dojo-force-activex-xhr': 0,
'dojo-enforceDefine': 0,
'dojo-combo-api': 0
},
staticHasFeatures: getLoaderDefaultFeatures(),

layers: {
"dojo/dojo": {
Expand Down
42 changes: 42 additions & 0 deletions buildDojo/loaderDefaultFeatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module.exports = {
'dojo-config-api': 1,
'dojo-inject-api': 1,
'csp-restrictions': 1,
'dojo-built': 1,
'config-dojo-loader-catches': 0,
'config-tlmSiblingOfDojo': 0,
'dojo-log-api': 0,
'dojo-publish-privates': 0,
'dojo-sync-loader': 0,
'dojo-timeout-api': 0,
'dojo-trace-api': 0,
'dojo-sniff': 0,
'dojo-test-sniff': 0,
'dojo-cdn': 0,
'dojo-loader-eval-hint-url': 0,
'config-stripStrict': 0,
'ie-event-behavior': 0,
'dom': 0,
'host-node': 0,
'host-rhino': 0,
'host-webworker': 0,
'dojo-force-activex-xhr': 0,
'dojo-enforceDefine': 0,
'dojo-combo-api': 0
};
Loading

0 comments on commit d57379d

Please sign in to comment.