@@ -3,11 +3,11 @@ import { Logger } from './logger/logger';
3
3
import { fillConfigDefaults , getUserConfigFile , replacePathVars } from './util/config' ;
4
4
import * as Constants from './util/constants' ;
5
5
import { BuildError } from './util/errors' ;
6
- import { getBooleanPropertyValue } from './util/helpers' ;
6
+ import { getBooleanPropertyValue , webpackStatsToDependencyMap , printDependencyMap } from './util/helpers' ;
7
7
import { BuildContext , TaskInfo } from './util/interfaces' ;
8
8
import { runWebpackFullBuild , WebpackConfig } from './webpack' ;
9
9
import { purgeDecorators } from './optimization/decorators' ;
10
- import { calculateUnusedComponents , purgeUnusedImportsAndExportsFromIndex } from './optimization/treeshake' ;
10
+ import { getAppModuleNgFactoryPath , calculateUnusedComponents , purgeUnusedImportsAndExportsFromIndex , purgeComponentNgFactoryImportAndUsage , purgeProviderControllerImportAndUsage , purgeProviderClassNameFromIonicModuleForRoot } from './optimization/treeshake' ;
11
11
12
12
export function optimization ( context : BuildContext , configFile : string ) {
13
13
const logger = new Logger ( `optimization` ) ;
@@ -24,7 +24,12 @@ export function optimization(context: BuildContext, configFile: string) {
24
24
function optimizationWorker ( context : BuildContext , configFile : string ) {
25
25
const webpackConfig = getConfig ( context , configFile ) ;
26
26
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
+ }
28
33
return doOptimizations ( context , dependencyMap ) ;
29
34
} ) ;
30
35
}
@@ -42,7 +47,11 @@ export function doOptimizations(context: BuildContext, dependencyMap: Map<string
42
47
purgeUnusedImports ( context , results . purgedModules ) ;
43
48
}
44
49
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
+ }
46
55
47
56
return modifiedMap ;
48
57
}
@@ -65,53 +74,39 @@ function purgeUnusedImports(context: BuildContext, purgeDependencyMap: Map<strin
65
74
66
75
const updatedFileContent = purgeUnusedImportsAndExportsFromIndex ( indexFilePath , file . content , modulesToPurge ) ;
67
76
context . fileCache . set ( indexFilePath , { path : indexFilePath , content : updatedFileContent } ) ;
68
- }
69
77
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 ] ) ;
80
85
}
81
86
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
95
90
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
+ }
99
97
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 } ) ;
102
101
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 ) ;
106
106
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
+ }
115
110
}
116
111
117
112
export function getConfig ( context : BuildContext , configFile : string ) : WebpackConfig {
@@ -132,15 +127,4 @@ const taskInfo: TaskInfo = {
132
127
defaultConfigFile : 'optimization.config'
133
128
} ;
134
129
135
- export interface WebpackStats {
136
- modules : WebpackModule [ ] ;
137
- } ;
138
130
139
- export interface WebpackModule {
140
- identifier : string ;
141
- reasons : WebpackDependency [ ] ;
142
- } ;
143
-
144
- export interface WebpackDependency {
145
- moduleIdentifier : string ;
146
- } ;
0 commit comments