diff --git a/README.md b/README.md index 1c3b950..751b4c4 100644 --- a/README.md +++ b/README.md @@ -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.
All modules will be built if nothing is specified. | -| `version` | `string` | Version of AngularJS Material.
If set to `local`, it will take the local installed AngularJS Material version from the node modules.
If set to `latest`, the latest version will be downloaded. | +| `version` | `string` | Version of AngularJS Material.
If set to `local`, it will take the local installed Angular Material version from the node modules.
If set to `latest`, the latest version will be downloaded. | | `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.
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. | > **Note:** The options can be set in a JSON file whose path can be passed to the CLI or API. diff --git a/lib/MaterialTools.ts b/lib/MaterialTools.ts index bd3d608..92815b5 100644 --- a/lib/MaterialTools.ts +++ b/lib/MaterialTools.ts @@ -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[]; diff --git a/lib/builders/JSBuilder.ts b/lib/builders/JSBuilder.ts index 7ff3078..e9d36f8 100644 --- a/lib/builders/JSBuilder.ts +++ b/lib/builders/JSBuilder.ts @@ -1,4 +1,4 @@ -import {MaterialToolsData} from '../MaterialTools'; +import {MaterialToolsData, MaterialToolsOptions} from '../MaterialTools'; import {MaterialToolsOutput} from './MaterialBuilder'; const fse = require('fs-extra'); @@ -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, { diff --git a/lib/builders/MaterialBuilder.ts b/lib/builders/MaterialBuilder.ts index 22dbebe..148dd4e 100644 --- a/lib/builders/MaterialBuilder.ts +++ b/lib/builders/MaterialBuilder.ts @@ -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 => { @@ -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); } /** diff --git a/lib/cli/options.ts b/lib/cli/options.ts index d42f4fd..dcf1578 100644 --- a/lib/cli/options.ts +++ b/lib/cli/options.ts @@ -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', {