Skip to content

Commit

Permalink
Merge pull request #784 from stealjs/minor
Browse files Browse the repository at this point in the history
Merge @loader and non-normalized module name changes into master
  • Loading branch information
matthewp committed Jul 11, 2017
2 parents adaef1c + f157903 commit 930158d
Show file tree
Hide file tree
Showing 23 changed files with 283 additions and 219 deletions.
26 changes: 6 additions & 20 deletions doc/guides/optimized_builds.md
Expand Up @@ -8,7 +8,7 @@ as the default way of creating a build of a module and all of its dependencies;

Unlike regular [builds](steal-tools.build), optimized builds don't need to load or bundle StealJS at all; a thin wrapper is added instead to the main bundle so the browser can load and execute the modules correctly.

> The **optimize** API is still a work in progress, some StealJS features are still not supported.
> The **optimize** API is a work in progress, some StealJS features are not supported yet.
In this guide, we'll go through the steps required to create and use an optimized build. We'll be using the
`myhub` application created in the [Progressive Loading](./StealJS.guides.progressive_loading) guide.
Expand Down Expand Up @@ -38,36 +38,20 @@ Run the following command:
### Install dependencies

As mentioned before, the **optimize** API is still in its early days, for that reason
we need to use the pre-release packages of `steal-tools` and `steal-css`.
we need to use some pre-release packages.

Edit your `package.json` like:

```json
"devDependencies": {
...
"steal-css": "^1.3.0-pre.0",
"steal-tools": "^1.4.0-pre.1"
"steal-tools": "^1.4.0"
}
```

Run `npm install` to install all the application dependencies.

### Update dynamic module identifiers

One limitation of the optimized loader is that unlike StealJS's loader it does not normalize
module identifiers on runtime. For static imports that's not a problem, but it's an issue for
dynamic imports (through `steal.import`), a workaround for that is to use the full module
name.

Edit the dynamic import in `myhub.js` to:

```js
steal.import(`myhub@1.0.0#${hash}/${hash}`).then(function(moduleOrPlugin) {
```
where "myhub" is the package name, the number after the "@" symbol is the package version and
the rest of the string after the "#" is the actual module identifier.
### Make an optimize build script

Currently, there is no CLI option to use the `stealTools.optimize` function, so a NodeJS script is required.
Expand All @@ -90,6 +74,8 @@ Run the build script with:
Now, start an http server by running `npm start` and open `http://127.0.0.1:8080/`
in a web browser. You should see myhub's home page.

> One limitation of the optimized loader is that unlike StealJS' loader it does not normalize module identifiers on runtime. For static imports that's not a problem, but it's an issue for dynamic imports (through steal.import), the module identifier needs to match the name set in [config.bundle].
### Performance comparison

For most application builds where StealJS is not included in each of the main bundles, optimized builds will
Expand Down Expand Up @@ -119,7 +105,7 @@ Edit `index.html` to asynchronously load the bundles of the other two pages like
```html
<body>
<div class="container">Hello World.</div>
<script src="./dist/bundles/myhub/myhub.js"></script>
<script async src="./dist/bundles/myhub/myhub.js"></script>
<script async src="./dist/bundles/myhub/weather/weather.js"></script>
<script async src="./dist/bundles/myhub/puppies/puppies.js"></script>
</body>
Expand Down
2 changes: 2 additions & 0 deletions lib/build/slim.js
Expand Up @@ -13,6 +13,7 @@ var streams = {
addBundleIds: require("../stream/add_bundle_ids"),
filterGraph: require("../stream/filter_slim_graph"),
addPluginNames: require("../stream/add_plugin_names"),
addAtLoaderShim: require("../stream/add_loader_shim"),
checkSlimSupport: require("../stream/check_slim_support"),
write: require("../bundle/write_bundles").createWriteStream,
writeBundlesManifest: require("../stream/write_bundle_manifest"),
Expand All @@ -39,6 +40,7 @@ module.exports = function(config, options) {
streams.graph(config, buildOptions),
streams.filterGraph(),
streams.checkSlimSupport(),
streams.addAtLoaderShim(),
streams.addModuleIds(),
streams.loadOptimizedPlugins(),
streams.transpile({ outputFormat: "slim" }),
Expand Down
16 changes: 9 additions & 7 deletions lib/graph/make_graph.js
@@ -1,10 +1,12 @@
var winston = require('winston');
var trace = require("../trace");
var steal = require("steal");
var _ = require("lodash");
var clone = _.cloneDeep;
var omit = require("lodash/omit");
var assign = require("lodash/assign");
var clone = require("lodash/cloneDeep");
var addParseAMD = require("steal-parse-amd");
var makeDeferred = require("../make-deferred");
var isUndefined = require("lodash/isUndefined");
var makePredefinedPluginDependencies = require('./make_predefined_plugin_dependencies');

var formatMap = {
Expand All @@ -27,7 +29,7 @@ module.exports = function(config, options){
addParseAMD( localSteal.System );

localSteal.System.config({ env: "build-development" });
localSteal.System.config(_.omit(config, ["meta"]));
localSteal.System.config(omit(config, ["meta"]));
if(options.localStealConfig) {
localSteal.System.config(options.localStealConfig);
}
Expand All @@ -42,7 +44,7 @@ module.exports = function(config, options){
// Special types don't work that write to AMD if we add parseAMD to this.
//addParseAMD( buildSteal.System );
// It should be confured exactly like `localSteal`
buildSteal.System.config(_.omit(config, ["meta"]));
buildSteal.System.config(omit(config, ["meta"]));
buildSteal.System.systemName = (buildSteal.System.systemName ||"")+ "-build";

if(options.system) {
Expand Down Expand Up @@ -110,16 +112,16 @@ module.exports = function(config, options){

// set config one more time on startup. This is to make sure the final values
// are these values.
var localConfig = _.omit(config, ["config","systemName"]);
_.assign(localConfig, ignoredMetas(config.meta, buildSteal.System.meta));
var localConfig = omit(config, ["config","systemName"]);
assign(localConfig, ignoredMetas(config.meta, buildSteal.System.meta));

var appPromise = localSteal.startup(localConfig);
startupCalledDfd.resolve();

return appPromise.then(function(){
var main = localSteal.System.main;

if (_.isUndefined(main)) {
if (isUndefined(main)) {
return Promise.reject(
new Error(
"Attribute 'main' is required\n" +
Expand Down
10 changes: 6 additions & 4 deletions lib/graph/make_graph_with_bundles.js
@@ -1,5 +1,7 @@
var clone = require("lodash/clone");
var assign = require("lodash/assign");
var includes = require("lodash/includes");
var dependencyGraph = require("./make_graph");
var _ = require("lodash");
var normalizeBundle = require("../loader/normalize_bundle");
var through = require("through2");
var fs = require("fs-extra");
Expand Down Expand Up @@ -52,7 +54,7 @@ function mergeIntoMasterAndAddBundle(opts) {

// do not track bundles of entry point modules (a.k.a mains) unless
// it is its own bundle.
if (name === bundleName || !_.includes(mains, name)) {
if (name === bundleName || !includes(mains, name)) {
addBundle(masterGraph[name], bundleName);
}
}
Expand Down Expand Up @@ -90,7 +92,7 @@ var makeBundleGraph = module.exports = function(config, options){
// the names of everything we are going to load
var bundleNames = [];

var cfg = _.clone(config, true);
var cfg = clone(config, true);
if( Array.isArray(cfg.main) ) {
bundleNames = slice.call(cfg.main);
cfg.main = bundleNames.shift();
Expand Down Expand Up @@ -144,7 +146,7 @@ var makeBundleGraph = module.exports = function(config, options){
}
else {
var nmains;
var copy = _.assign(_.clone(cfg, true), { main: nextBundle });
var copy = assign(clone(cfg), { main: nextBundle });

return mainsPromise
.then(function(normalised) {
Expand Down
10 changes: 10 additions & 0 deletions lib/node/is_plugin_excluded.js
@@ -0,0 +1,10 @@
/**
* Whether the node is a plugin that won't be included in the build
* @param {Object} node - A node from the dependency graph
* @return {boolean}
*/
module.exports = function isPluginExcludedFromBuild(node) {
return Boolean(
node.isPlugin && (!node.value.includeInBuild || node.value.pluginBuilder)
);
};
2 changes: 1 addition & 1 deletion lib/node/make_node.js
Expand Up @@ -3,7 +3,7 @@ var nodeSource = require("./source");
module.exports = function(name, source, type){
return {
load: {
metadata: {format: type || global},
metadata: {format: type || "global"},
source: source || "",
name: name
},
Expand Down
6 changes: 3 additions & 3 deletions lib/node/make_slim_config_node.js
Expand Up @@ -55,9 +55,9 @@ module.exports = function(options) {
});
});

options.progressiveBundles.forEach(function(bundleName) {
var node = options.graph[bundleName];
slimMapConfig[bundleName] = node.load.uniqueId;
// [{ id : number, name : string }]
options.progressiveBundles.forEach(function(bundle) {
slimMapConfig[bundle.name] = bundle.id;
});

var configSource = `
Expand Down
17 changes: 0 additions & 17 deletions lib/slim/checks/production_env_config.js

This file was deleted.

36 changes: 36 additions & 0 deletions lib/slim/checks/steal.js
@@ -0,0 +1,36 @@
var keys = require("lodash/keys");
var isPluginExcludedFromBuild = require("../../node/is_plugin_excluded");

/**
* Checks whether the @steal node is in the graph
* @param {string} configMain - The configMain module name
* @throws if @steal is found in the graph
*/
module.exports = function(configMain, graph) {
keys(graph).forEach(function(name) {
var node = graph[name];

// the configMain node does depend on @steal/@loader but it won't be
// included in the slim build, we can skip it. Same for plugins that
// will be only be used during the build process.
if (name === configMain || isPluginExcludedFromBuild(node)) {
return;
}

isAtSteal(name);
node.dependencies.forEach(isAtSteal);
});
};

/**
* Checks whether the module name is @steal
* @param {string} name - The module name to be checked
* @throws if the name is @steal
*/
function isAtSteal(name) {
if (name === "@steal") {
throw new Error(
`Cannot create slim build. "@steal" module is not supported`
);
}
}
47 changes: 0 additions & 47 deletions lib/slim/checks/steal_and_loader.js

This file was deleted.

0 comments on commit 930158d

Please sign in to comment.