Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Add options to create bundles that can be lazy loaded #35

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ The build tools also include a CLI, which can be used by installing the tools gl
| ----------------------- | ----------- | -------------------------------------------------------------------------- |
| `destination` (*) | `string` | Target location for the Material build. |
| `modules` | `string[]` | Modules that should be part of the build.<br/> All modules will be built if nothing is specified. |
| `version` | `string` | Version of AngularJS Material.<br/> If set to `local`, it will take the local installed AngularJS Material version from the node modules. <br/> If set to `latest`, the latest version will be downloaded. |
| `version` | `string` | Version of AngularJS Material.<br/> If set to `local`, it will take the local installed Angular Material version from the node modules. <br/> If set to `latest`, the latest version will be downloaded. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AngularJS is the correct form to use here. Please revert.

| `theme` | `MdTheme` | Material Theme to be used to generate a static theme stylesheet. |
| `themes` | `MdTheme[]` | Multiple Material Themes, which are used to generated a static stylesheet. |
| `cache` | `string` | Directory for caching the downloads |
| `mainFilename` | `string` | Name of the entry file that will be loaded to figure out the dependencies. |
| `destinationFilename` | `string` | Name to be used as a base for the output files. |
| `excludeModules` | `string[]` | Modules that should be excluded from the build.<br/> Use to exclude e.g. core modules when building a bundle that should get lazy-loaded. |
| `excludeMainModule` | `boolean` | Set to `true` to exclude the code for the Angular Material main module. Use when building a bundle that will get lazy-loaded and extend an already existing main module. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a lot of extra space here after the name and before the type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change Angular to AngularJS.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version has been also changed from AngularJS Material to Angular Material 😄

Please fix that issue and also mention that the excludeModules option "partially" matches modules. This would be ready then!


> **Note:** The options can be set in a JSON file whose path can be passed to the CLI or API.

Expand Down
2 changes: 2 additions & 0 deletions lib/MaterialTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,13 @@ export interface MaterialToolsData {
export interface MaterialToolsOptions {
destination?: string;
modules?: string[];
excludeModules?: string[];
version?: string;
palettes?: MdPaletteDefinition;
mainFilename?: string;
cache?: string;
destinationFilename?: string;
excludeMainModule?: boolean;
/* Theming Options */
theme?: MdTheme;
themes?: MdTheme[];
Expand Down
13 changes: 9 additions & 4 deletions lib/builders/JSBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MaterialToolsData} from '../MaterialTools';
import {MaterialToolsData, MaterialToolsOptions} from '../MaterialTools';
import {MaterialToolsOutput} from './MaterialBuilder';

const fse = require('fs-extra');
Expand All @@ -8,9 +8,14 @@ export class JSBuilder {
/**
* Generates the minified and non-minified JS, as well as a source map, based on the options.
*/
static build(data: MaterialToolsData, filename: string): MaterialToolsOutput {

let mainModule = this._buildMainModule(data.dependencies._mainModule);
static build(
data: MaterialToolsData,
filename: string,
options: MaterialToolsOptions
): MaterialToolsOutput {
let mainModule = !options.excludeMainModule ?
this._buildMainModule(data.dependencies._mainModule) :
'';
let raw = data.files.js.map(path => fse.readFileSync(path).toString()).join('\n');
let source = [mainModule, '', raw].join('\n');
let compressed = uglify.minify(source, {
Expand Down
23 changes: 18 additions & 5 deletions lib/builders/MaterialBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,25 @@ export class MaterialBuilder {
// Update the resolved version, in case it was `node`.
this._options.version = versionData.version;

let deps = DependencyResolver.resolve(
this._getModuleEntry(versionData),
this._options.modules
);

if (this._options.excludeModules) {
// Remove modules that were explicitly flagged to be excluded from the build
deps._flat = deps._flat.filter(dep => this._options.excludeModules.indexOf(dep) === -1);
deps._mainModule.dependencies = deps._mainModule.dependencies.filter(
dep => !this._options.excludeModules.some(
// E.g. match 'animate' in 'material.core.animate'
_module => dep.indexOf(_module) !== -1
)
);
}

return {
versionData: versionData,
dependencies: DependencyResolver.resolve(
this._getModuleEntry(versionData),
this._options.modules
)
dependencies: deps
};
})
.then(data => {
Expand All @@ -54,7 +67,7 @@ export class MaterialBuilder {
* Outputs the necessary JS, based on the options.
*/
_buildJS(buildData: MaterialToolsData): MaterialToolsOutput {
return JSBuilder.build(buildData, `${this._outputBase}.min.js`);
return JSBuilder.build(buildData, `${this._outputBase}.min.js`, this._options);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion lib/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ export function registerOptions(yargs: any): any {
describe: 'Directory to be used as a cache for downloaded versions.',
default: DEFAULT_OPTIONS.cache,
group: OPTIONAL_GROUP
}));
}))
.option('exclude-modules', addDefaults({
alias: 'em',
describe: 'List of modules to be excluded from the build.',
type: 'array',
group: OPTIONAL_GROUP
}))
.option('exclude-main-module', {
describe: 'Exclude code for main AngularJS Material module with list of dependencies.',
boolean: true,
group: OPTIONAL_GROUP
});

// Logging arguments
yargs.option('verbose', {
Expand Down