@@ -3,11 +3,11 @@ import { Logger } from './logger/logger';
33import { fillConfigDefaults , getUserConfigFile , replacePathVars } from './util/config' ;
44import * as Constants from './util/constants' ;
55import { BuildError } from './util/errors' ;
6- import { getBooleanPropertyValue } from './util/helpers' ;
6+ import { getBooleanPropertyValue , webpackStatsToDependencyMap , printDependencyMap } from './util/helpers' ;
77import { BuildContext , TaskInfo } from './util/interfaces' ;
88import { runWebpackFullBuild , WebpackConfig } from './webpack' ;
99import { purgeDecorators } from './optimization/decorators' ;
10- import { calculateUnusedComponents , purgeUnusedImportsAndExportsFromIndex } from './optimization/treeshake' ;
10+ import { getAppModuleNgFactoryPath , calculateUnusedComponents , purgeUnusedImportsAndExportsFromIndex , purgeComponentNgFactoryImportAndUsage , purgeProviderControllerImportAndUsage , purgeProviderClassNameFromIonicModuleForRoot } from './optimization/treeshake' ;
1111
1212export function optimization ( context : BuildContext , configFile : string ) {
1313 const logger = new Logger ( `optimization` ) ;
@@ -24,7 +24,12 @@ export function optimization(context: BuildContext, configFile: string) {
2424function optimizationWorker ( context : BuildContext , configFile : string ) {
2525 const webpackConfig = getConfig ( context , configFile ) ;
2626 return runWebpackFullBuild ( webpackConfig ) . then ( ( stats : any ) => {
27- const dependencyMap = processStats ( context , stats ) ;
27+ const dependencyMap = webpackStatsToDependencyMap ( context , stats ) ;
28+ if ( getBooleanPropertyValue ( Constants . ENV_PRINT_ORIGINAL_DEPENDENCY_TREE ) ) {
29+ Logger . debug ( 'Original Dependency Map Start' ) ;
30+ printDependencyMap ( dependencyMap ) ;
31+ Logger . debug ( 'Original Dependency Map End' ) ;
32+ }
2833 return doOptimizations ( context , dependencyMap ) ;
2934 } ) ;
3035}
@@ -42,7 +47,11 @@ export function doOptimizations(context: BuildContext, dependencyMap: Map<string
4247 purgeUnusedImports ( context , results . purgedModules ) ;
4348 }
4449
45- printDependencyMap ( modifiedMap ) ;
50+ if ( getBooleanPropertyValue ( Constants . ENV_PRINT_MODIFIED_DEPENDENCY_TREE ) ) {
51+ Logger . debug ( 'Modified Dependency Map Start' ) ;
52+ printDependencyMap ( modifiedMap ) ;
53+ Logger . debug ( 'Modified Dependency Map End' ) ;
54+ }
4655
4756 return modifiedMap ;
4857}
@@ -65,53 +74,39 @@ function purgeUnusedImports(context: BuildContext, purgeDependencyMap: Map<strin
6574
6675 const updatedFileContent = purgeUnusedImportsAndExportsFromIndex ( indexFilePath , file . content , modulesToPurge ) ;
6776 context . fileCache . set ( indexFilePath , { path : indexFilePath , content : updatedFileContent } ) ;
68- }
6977
70- function processStats ( context : BuildContext , stats : any ) {
71- const statsObj = stats . toJson ( {
72- source : false ,
73- timings : false ,
74- version : false ,
75- errorDetails : false ,
76- chunks : false ,
77- chunkModules : false
78- } ) ;
79- return processStatsImpl ( statsObj ) ;
78+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_ACTION_SHEET_CONTROLLER_PATH ] , process . env [ Constants . ENV_ACTION_SHEET_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_ACTION_SHEET_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_ACTION_SHEET_CONTROLLER_CLASSNAME ] ) ;
79+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_ALERT_CONTROLLER_PATH ] , process . env [ Constants . ENV_ALERT_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_ALERT_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_ALERT_CONTROLLER_CLASSNAME ] ) ;
80+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_LOADING_CONTROLLER_PATH ] , process . env [ Constants . ENV_LOADING_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_LOADING_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_LOADING_CONTROLLER_CLASSNAME ] ) ;
81+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_MODAL_CONTROLLER_PATH ] , process . env [ Constants . ENV_MODAL_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_MODAL_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_MODAL_CONTROLLER_CLASSNAME ] ) ;
82+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_PICKER_CONTROLLER_PATH ] , process . env [ Constants . ENV_PICKER_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_PICKER_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_PICKER_CONTROLLER_CLASSNAME ] ) ;
83+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_POPOVER_CONTROLLER_PATH ] , process . env [ Constants . ENV_POPOVER_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_POPOVER_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_POPOVER_CONTROLLER_CLASSNAME ] ) ;
84+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , process . env [ Constants . ENV_TOAST_CONTROLLER_PATH ] , process . env [ Constants . ENV_TOAST_VIEW_CONTROLLER_PATH ] , process . env [ Constants . ENV_TOAST_COMPONENT_FACTORY_PATH ] , process . env [ Constants . ENV_TOAST_CONTROLLER_CLASSNAME ] ) ;
8085}
8186
82- export function processStatsImpl ( webpackStats : WebpackStats ) {
83- const dependencyMap = new Map < string , Set < string > > ( ) ;
84- if ( webpackStats && webpackStats . modules ) {
85- webpackStats . modules . forEach ( webpackModule => {
86- const moduleId = purgeWebpackPrefixFromPath ( webpackModule . identifier ) ;
87- const dependencySet = new Set < string > ( ) ;
88- webpackModule . reasons . forEach ( webpackDependency => {
89- const depId = purgeWebpackPrefixFromPath ( webpackDependency . moduleIdentifier ) ;
90- dependencySet . add ( depId ) ;
91- } ) ;
92- dependencyMap . set ( moduleId , dependencySet ) ;
93- } ) ;
94- }
87+ function attemptToPurgeUnusedProvider ( context : BuildContext , dependencyMap : Map < string , Set < string > > , providerPath : string , providerComponentPath : string , providerComponentFactoryPath : string , providerClassName : string ) {
88+ if ( dependencyMap . has ( providerPath ) ) {
89+ // awwww yissssssss
9590
96- if ( getBooleanPropertyValue ( Constants . ENV_PRINT_DEPENDENCY_TREE ) ) {
97- printDependencyMap ( dependencyMap ) ;
98- }
91+ // first, get the content of the app module ngfactory file
92+ const appModuleNgFactoryPath = getAppModuleNgFactoryPath ( ) ;
93+ const file = context . fileCache . get ( appModuleNgFactoryPath ) ;
94+ if ( ! file ) {
95+ return ;
96+ }
9997
100- return dependencyMap ;
101- }
98+ let updatedContent = purgeComponentNgFactoryImportAndUsage ( file . path , file . content , providerComponentFactoryPath ) ;
99+ updatedContent = purgeProviderControllerImportAndUsage ( file . path , updatedContent , providerPath ) ;
100+ context . fileCache . set ( appModuleNgFactoryPath , { path : appModuleNgFactoryPath , content : updatedContent } ) ;
102101
103- export function purgeWebpackPrefixFromPath ( filePath : string ) {
104- return filePath . replace ( process . env [ Constants . ENV_OPTIMIZATION_LOADER ] , '' ) . replace ( process . env [ Constants . ENV_WEBPACK_LOADER ] , '' ) . replace ( '!' , '' ) ;
105- }
102+ // purge the provider name from the forRoot method providers list
103+ const indexFilePath = process . env [ Constants . ENV_VAR_IONIC_ANGULAR_ENTRY_POINT ] ;
104+ const ionicIndexFile = context . fileCache . get ( indexFilePath ) ;
105+ let newIndexFileContent = purgeProviderClassNameFromIonicModuleForRoot ( ionicIndexFile . content , providerClassName ) ;
106106
107- function printDependencyMap ( map : Map < string , Set < string > > ) {
108- map . forEach ( ( dependencySet : Set < string > , filePath : string ) => {
109- Logger . unformattedDebug ( '\n\n' ) ;
110- Logger . unformattedDebug ( `${ filePath } is imported by the following files:` ) ;
111- dependencySet . forEach ( ( importeePath : string ) => {
112- Logger . unformattedDebug ( ` ${ importeePath } ` ) ;
113- } ) ;
114- } ) ;
107+ // purge the component from the index file
108+ context . fileCache . set ( indexFilePath , { path : indexFilePath , content : newIndexFileContent } ) ;
109+ }
115110}
116111
117112export function getConfig ( context : BuildContext , configFile : string ) : WebpackConfig {
@@ -132,15 +127,4 @@ const taskInfo: TaskInfo = {
132127 defaultConfigFile : 'optimization.config'
133128} ;
134129
135- export interface WebpackStats {
136- modules : WebpackModule [ ] ;
137- } ;
138130
139- export interface WebpackModule {
140- identifier : string ;
141- reasons : WebpackDependency [ ] ;
142- } ;
143-
144- export interface WebpackDependency {
145- moduleIdentifier : string ;
146- } ;
0 commit comments