@@ -6,14 +6,15 @@ import { Logger } from './logger/logger';
6
6
import { fillConfigDefaults , getUserConfigFile , replacePathVars } from './util/config' ;
7
7
import * as Constants from './util/constants' ;
8
8
import { BuildError } from './util/errors' ;
9
- import { getBooleanPropertyValue , getStringPropertyValue , webpackStatsToDependencyMap , printDependencyMap } from './util/helpers' ;
9
+ import { changeExtension , getBooleanPropertyValue , getStringPropertyValue , webpackStatsToDependencyMap , printDependencyMap } from './util/helpers' ;
10
10
import { BuildContext , TaskInfo } from './util/interfaces' ;
11
11
import { runWebpackFullBuild , WebpackConfig } from './webpack' ;
12
12
import { addPureAnnotation , purgeStaticCtorFields , purgeStaticFieldDecorators , purgeTranspiledDecorators } from './optimization/decorators' ;
13
- import { getAppModuleNgFactoryPath ,
14
- calculateUnusedComponents ,
13
+ import { calculateUnusedComponents ,
14
+ checkIfProviderIsUsedInSrc ,
15
15
getIonicModuleFilePath ,
16
- purgeUnusedImportsAndExportsFromIndex ,
16
+ purgeUnusedImportsAndExportsFromModuleFile ,
17
+ purgeUnusedExportsFromIndexFile ,
17
18
purgeComponentNgFactoryImportAndUsage ,
18
19
purgeProviderControllerImportAndUsage ,
19
20
purgeProviderClassNameFromIonicModuleForRoot
@@ -59,7 +60,7 @@ export function purgeGeneratedFiles(context: BuildContext, fileNameSuffix: strin
59
60
60
61
export function doOptimizations ( context : BuildContext , dependencyMap : Map < string , Set < string > > ) {
61
62
// remove decorators
62
- const modifiedMap = new Map ( dependencyMap ) ;
63
+ let modifiedMap = new Map ( dependencyMap ) ;
63
64
if ( getBooleanPropertyValue ( Constants . ENV_PURGE_DECORATORS ) ) {
64
65
removeDecorators ( context ) ;
65
66
}
@@ -71,6 +72,8 @@ export function doOptimizations(context: BuildContext, dependencyMap: Map<string
71
72
// since there is a breaking change here
72
73
const ionicModulePath = getIonicModuleFilePath ( ) ;
73
74
if ( context . fileCache . get ( ionicModulePath ) ) {
75
+ // due to how the angular compiler works in angular 4, we need to check if
76
+ modifiedMap = checkIfProviderIsUsedInSrc ( context , modifiedMap ) ;
74
77
const results = calculateUnusedComponents ( modifiedMap ) ;
75
78
purgeUnusedImports ( context , results . purgedModules ) ;
76
79
}
@@ -127,43 +130,57 @@ function purgeUnusedImports(context: BuildContext, purgeDependencyMap: Map<strin
127
130
modulesToPurge . push ( moduleToPurge ) ;
128
131
} ) ;
129
132
130
- const updatedFileContent = purgeUnusedImportsAndExportsFromIndex ( moduleFilePath , moduleFile . content , modulesToPurge ) ;
133
+ const updatedFileContent = purgeUnusedImportsAndExportsFromModuleFile ( moduleFilePath , moduleFile . content , modulesToPurge ) ;
131
134
context . fileCache . set ( moduleFilePath , { path : moduleFilePath , content : updatedFileContent } ) ;
132
135
133
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_CONTROLLER_CLASSNAME ) ) ;
134
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_ALERT_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_ALERT_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_ALERT_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_ALERT_CONTROLLER_CLASSNAME ) ) ;
135
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_LOADING_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_LOADING_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_LOADING_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_LOADING_CONTROLLER_CLASSNAME ) ) ;
136
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_MODAL_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_MODAL_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_MODAL_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_MODAL_CONTROLLER_CLASSNAME ) ) ;
137
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_PICKER_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_PICKER_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_PICKER_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_PICKER_CONTROLLER_CLASSNAME ) ) ;
138
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_POPOVER_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_POPOVER_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_POPOVER_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_POPOVER_CONTROLLER_CLASSNAME ) ) ;
139
- attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_TOAST_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_TOAST_VIEW_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_TOAST_COMPONENT_FACTORY_PATH ) , getStringPropertyValue ( Constants . ENV_TOAST_CONTROLLER_CLASSNAME ) ) ;
136
+ const updatedIndexContent = purgeUnusedExportsFromIndexFile ( file . path , file . content , modulesToPurge ) ;
137
+ context . fileCache . set ( file . path , { path : file . path , content : updatedIndexContent } ) ;
138
+
139
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_CONTROLLER_CLASSNAME ) ) ;
140
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_ALERT_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_ALERT_CONTROLLER_CLASSNAME ) ) ;
141
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_LOADING_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_LOADING_CONTROLLER_CLASSNAME ) ) ;
142
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_MODAL_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_MODAL_CONTROLLER_CLASSNAME ) ) ;
143
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_PICKER_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_PICKER_CONTROLLER_CLASSNAME ) ) ;
144
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_POPOVER_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_POPOVER_CONTROLLER_CLASSNAME ) ) ;
145
+ attemptToPurgeUnusedProvider ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_TOAST_CONTROLLER_PATH ) , getStringPropertyValue ( Constants . ENV_TOAST_CONTROLLER_CLASSNAME ) ) ;
146
+
147
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_ACTION_SHEET_COMPONENT_FACTORY_PATH ) ) ;
148
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_ALERT_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_ALERT_COMPONENT_FACTORY_PATH ) ) ;
149
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_LOADING_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_LOADING_COMPONENT_FACTORY_PATH ) ) ;
150
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_MODAL_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_MODAL_COMPONENT_FACTORY_PATH ) ) ;
151
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_PICKER_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_PICKER_COMPONENT_FACTORY_PATH ) ) ;
152
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_POPOVER_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_POPOVER_COMPONENT_FACTORY_PATH ) ) ;
153
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_TOAST_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_TOAST_COMPONENT_FACTORY_PATH ) ) ;
154
+ attemptToPurgeUnusedEntryComponents ( context , purgeDependencyMap , getStringPropertyValue ( Constants . ENV_SELECT_POPOVER_COMPONENT_PATH ) , getStringPropertyValue ( Constants . ENV_SELECT_POPOVER_COMPONENT_FACTORY_PATH ) ) ;
140
155
}
141
156
142
- function attemptToPurgeUnusedProvider ( context : BuildContext , dependencyMap : Map < string , Set < string > > , providerPath : string , providerComponentPath : string , providerComponentFactoryPath : string , providerClassName : string ) {
157
+ function attemptToPurgeUnusedProvider ( context : BuildContext , dependencyMap : Map < string , Set < string > > , providerPath : string , providerClassName : string ) {
143
158
if ( dependencyMap . has ( providerPath ) ) {
144
- // awwww yissssssss
145
-
146
- // first, get the content of the app module ngfactory file
147
- const appModuleNgFactoryPath = getAppModuleNgFactoryPath ( ) ;
148
- const file = context . fileCache . get ( appModuleNgFactoryPath ) ;
149
- if ( ! file ) {
150
- return ;
151
- }
152
-
153
- let updatedContent = purgeComponentNgFactoryImportAndUsage ( file . path , file . content , providerComponentFactoryPath ) ;
154
- updatedContent = purgeProviderControllerImportAndUsage ( file . path , updatedContent , providerPath ) ;
155
- context . fileCache . set ( appModuleNgFactoryPath , { path : appModuleNgFactoryPath , content : updatedContent } ) ;
159
+ const ngModuleFactoryFiles = context . fileCache . getAll ( ) . filter ( file => file . path . endsWith ( changeExtension ( getStringPropertyValue ( Constants . ENV_NG_MODULE_FILE_NAME_SUFFIX ) , '.ngfactory.js' ) ) ) ;
160
+ ngModuleFactoryFiles . forEach ( ngModuleFactoryFile => {
161
+ const newContent = purgeProviderControllerImportAndUsage ( ngModuleFactoryFile . path , ngModuleFactoryFile . content , providerPath ) ;
162
+ context . fileCache . set ( ngModuleFactoryFile . path , { path : ngModuleFactoryFile . path , content : newContent } ) ;
163
+ } ) ;
156
164
157
- // purge the provider name from the forRoot method providers list
158
165
const moduleFilePath = getIonicModuleFilePath ( ) ;
159
166
const ionicModuleFile = context . fileCache . get ( moduleFilePath ) ;
160
- let newModuleFileContent = purgeProviderClassNameFromIonicModuleForRoot ( ionicModuleFile . content , providerClassName ) ;
167
+ const newModuleFileContent = purgeProviderClassNameFromIonicModuleForRoot ( ionicModuleFile . content , providerClassName ) ;
161
168
162
- // purge the component from the index file
169
+ // purge the component from the module file
163
170
context . fileCache . set ( moduleFilePath , { path : moduleFilePath , content : newModuleFileContent } ) ;
164
171
}
165
172
}
166
173
174
+ function attemptToPurgeUnusedEntryComponents ( context : BuildContext , dependencyMap : Map < string , Set < string > > , entryComponentPath : string , entryComponentFactoryPath : string ) {
175
+ if ( dependencyMap . has ( entryComponentPath ) ) {
176
+ const ngModuleFactoryFiles = context . fileCache . getAll ( ) . filter ( file => file . path . endsWith ( changeExtension ( getStringPropertyValue ( Constants . ENV_NG_MODULE_FILE_NAME_SUFFIX ) , '.ngfactory.js' ) ) ) ;
177
+ ngModuleFactoryFiles . forEach ( ngModuleFactoryFile => {
178
+ const updatedContent = purgeComponentNgFactoryImportAndUsage ( ngModuleFactoryFile . path , ngModuleFactoryFile . content , entryComponentFactoryPath ) ;
179
+ context . fileCache . set ( ngModuleFactoryFile . path , { path : ngModuleFactoryFile . path , content : updatedContent } ) ;
180
+ } ) ;
181
+ }
182
+ }
183
+
167
184
export function getConfig ( context : BuildContext , configFile : string ) : WebpackConfig {
168
185
configFile = getUserConfigFile ( context , taskInfo , configFile ) ;
169
186
0 commit comments