@@ -35,9 +35,10 @@ function generateResults(dependencyMap: Map<string, Set<string>>) {
3535
3636function requiredModule ( modulePath : string ) {
3737 const mainJsFile = changeExtension ( process . env [ Constants . ENV_APP_ENTRY_POINT ] , '.js' ) ;
38+ const mainTsFile = changeExtension ( process . env [ Constants . ENV_APP_ENTRY_POINT ] , '.ts' ) ;
3839 const appModule = changeExtension ( process . env [ Constants . ENV_APP_NG_MODULE_PATH ] , '.js' ) ;
3940 const appModuleNgFactory = getAppModuleNgFactoryPath ( ) ;
40- return modulePath === mainJsFile || modulePath === appModule || modulePath === appModuleNgFactory ;
41+ return modulePath === mainJsFile || modulePath === mainTsFile || modulePath === appModule || modulePath === appModuleNgFactory ;
4142}
4243
4344function filterMap ( dependencyMap : Map < string , Set < string > > ) {
@@ -73,23 +74,37 @@ function processImportTree(dependencyMap: Map<string, Set<string>>, importee: st
7374}
7475
7576function calculateUnusedIonicProviders ( dependencyMap : Map < string , Set < string > > ) {
77+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: beginning to purge providers` ) ;
7678
77-
79+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to purge action sheet controller` ) ;
7880 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_ACTION_SHEET_CONTROLLER_PATH ] ) ;
81+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to purge alert controller` ) ;
7982 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_ALERT_CONTROLLER_PATH ] ) ;
83+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to loading controller` ) ;
8084 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_LOADING_CONTROLLER_PATH ] ) ;
85+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to modal controller` ) ;
8186 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_MODAL_CONTROLLER_PATH ] ) ;
87+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to picker controller` ) ;
8288 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_PICKER_CONTROLLER_PATH ] ) ;
89+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to popover controller` ) ;
8390 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_POPOVER_CONTROLLER_PATH ] ) ;
91+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to toast controller` ) ;
8492 processIonicProviders ( dependencyMap , process . env [ Constants . ENV_TOAST_CONTROLLER_PATH ] ) ;
8593
8694 // check if the controllers were deleted, if so, purge the component too
95+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to action sheet component` ) ;
8796 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_ACTION_SHEET_CONTROLLER_PATH ] , process . env [ Constants . ENV_ACTION_SHEET_COMPONENT_FACTORY_PATH ] ) ;
97+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to alert component` ) ;
8898 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_ALERT_CONTROLLER_PATH ] , process . env [ Constants . ENV_ALERT_COMPONENT_FACTORY_PATH ] ) ;
99+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to loading component` ) ;
89100 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_LOADING_CONTROLLER_PATH ] , process . env [ Constants . ENV_LOADING_COMPONENT_FACTORY_PATH ] ) ;
101+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to modal component` ) ;
90102 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_MODAL_CONTROLLER_PATH ] , process . env [ Constants . ENV_MODAL_COMPONENT_FACTORY_PATH ] ) ;
103+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to picker component` ) ;
91104 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_PICKER_CONTROLLER_PATH ] , process . env [ Constants . ENV_PICKER_COMPONENT_FACTORY_PATH ] ) ;
105+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to popover component` ) ;
92106 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_POPOVER_CONTROLLER_PATH ] , process . env [ Constants . ENV_POPOVER_COMPONENT_FACTORY_PATH ] ) ;
107+ Logger . debug ( `[treeshake] calculateUnusedIonicProviders: attempting to toast component` ) ;
93108 processIonicProviderComponents ( dependencyMap , process . env [ Constants . ENV_TOAST_CONTROLLER_PATH ] , process . env [ Constants . ENV_TOAST_COMPONENT_FACTORY_PATH ] ) ;
94109
95110}
@@ -110,8 +125,10 @@ export function getAppModuleNgFactoryPath() {
110125function processIonicProviders ( dependencyMap : Map < string , Set < string > > , providerPath : string ) {
111126 const importeeSet = dependencyMap . get ( providerPath ) ;
112127 const appModuleNgFactoryPath = getAppModuleNgFactoryPath ( ) ;
128+
113129 // we can only purge an ionic provider if it is imported from one module, which is the AppModuleNgFactory
114130 if ( importeeSet && importeeSet . size === 1 && importeeSet . has ( appModuleNgFactoryPath ) ) {
131+ Logger . debug ( `[treeshake] processIonicProviders: Purging ${ providerPath } ` ) ;
115132 importeeSet . delete ( appModuleNgFactoryPath ) ;
116133 // loop over the dependency map and remove this provider from importee sets
117134 processImportTreeForProviders ( dependencyMap , providerPath ) ;
@@ -141,12 +158,15 @@ export function isNgFactory(modulePath: string) {
141158}
142159
143160export function purgeUnusedImportsAndExportsFromIndex ( indexFilePath : string , indexFileContent : string , modulePathsToPurge : string [ ] ) {
161+ Logger . debug ( `[treeshake] purgeUnusedImportsFromIndex: Starting to purge import/exports ... ` ) ;
144162 for ( const modulePath of modulePathsToPurge ) {
145163 // I cannot get the './' prefix to show up when using path api
146164 Logger . debug ( `[treeshake] purgeUnusedImportsFromIndex: Removing ${ modulePath } from ${ indexFilePath } ` ) ;
165+
147166 const extensionless = changeExtension ( modulePath , '' ) ;
148167 const relativeImportPath = './' + relative ( dirname ( indexFilePath ) , extensionless ) ;
149168 const importPath = toUnixPath ( relativeImportPath ) ;
169+ Logger . debug ( `[treeshake] purgeUnusedImportsFromIndex: Removing imports with path ${ importPath } ` ) ;
150170 const importRegex = generateImportRegex ( importPath ) ;
151171 // replace the import if it's found
152172 let results : RegExpExecArray = null ;
@@ -156,10 +176,13 @@ export function purgeUnusedImportsAndExportsFromIndex(indexFilePath: string, ind
156176
157177 results = null ;
158178 const exportRegex = generateExportRegex ( importPath ) ;
179+ Logger . debug ( `[treeshake] purgeUnusedImportsFromIndex: Removing exports with path ${ importPath } ` ) ;
159180 while ( ( results = exportRegex . exec ( indexFileContent ) ) && results . length ) {
160181 indexFileContent = indexFileContent . replace ( exportRegex , '' ) ;
161182 }
162183 }
184+
185+ Logger . debug ( `[treeshake] purgeUnusedImportsFromIndex: Starting to purge import/exports ... DONE` ) ;
163186 return indexFileContent ;
164187}
165188
@@ -174,29 +197,35 @@ function generateExportRegex(relativeExportPath: string) {
174197}
175198
176199export function purgeComponentNgFactoryImportAndUsage ( appModuleNgFactoryPath : string , appModuleNgFactoryContent : string , componentFactoryPath : string ) {
200+ Logger . debug ( `[treeshake] purgeComponentNgFactoryImportAndUsage: Starting to purge component ngFactory import/export ...` ) ;
177201 const extensionlessComponentFactoryPath = changeExtension ( componentFactoryPath , '' ) ;
178202 const relativeImportPath = relative ( dirname ( appModuleNgFactoryPath ) , extensionlessComponentFactoryPath ) ;
179203 const importPath = toUnixPath ( relativeImportPath ) ;
204+ Logger . debug ( `[treeshake] purgeComponentNgFactoryImportAndUsage: Purging imports from ${ importPath } ` ) ;
180205 const importRegex = generateWildCardImportRegex ( importPath ) ;
181206 const results = importRegex . exec ( appModuleNgFactoryContent ) ;
182207 if ( results && results . length >= 2 ) {
183208 appModuleNgFactoryContent = appModuleNgFactoryContent . replace ( importRegex , '' ) ;
184209 const namedImport = results [ 1 ] . trim ( ) ;
210+ Logger . debug ( `[treeshake] purgeComponentNgFactoryImportAndUsage: Purging code using named import ${ namedImport } ` ) ;
185211 const purgeFromConstructor = generateRemoveComponentFromConstructorRegex ( namedImport ) ;
186212 appModuleNgFactoryContent = appModuleNgFactoryContent . replace ( purgeFromConstructor , '' ) ;
187213 }
214+ Logger . debug ( `[treeshake] purgeComponentNgFactoryImportAndUsage: Starting to purge component ngFactory import/export ... DONE` ) ;
188215 return appModuleNgFactoryContent ;
189216}
190217
191218export function purgeProviderControllerImportAndUsage ( appModuleNgFactoryPath : string , appModuleNgFactoryContent : string , providerPath : string ) {
219+ Logger . debug ( `[treeshake] purgeProviderControllerImportAndUsage: Starting to purge provider controller and usage ...` ) ;
192220 const extensionlessComponentFactoryPath = changeExtension ( providerPath , '' ) ;
193221 const relativeImportPath = relative ( dirname ( process . env [ Constants . ENV_VAR_IONIC_ANGULAR_DIR ] ) , extensionlessComponentFactoryPath ) ;
194222 const importPath = toUnixPath ( relativeImportPath ) ;
223+ Logger . debug ( `[treeshake] purgeProviderControllerImportAndUsage: Looking for imports from ${ importPath } ` ) ;
195224 const importRegex = generateWildCardImportRegex ( importPath ) ;
196225 const results = importRegex . exec ( appModuleNgFactoryContent ) ;
197226 if ( results && results . length >= 2 ) {
198- appModuleNgFactoryContent = appModuleNgFactoryContent . replace ( importRegex , '' ) ;
199227 const namedImport = results [ 1 ] . trim ( ) ;
228+
200229 // purge the getter
201230 const purgeGetterRegEx = generateRemoveGetterFromImportRegex ( namedImport ) ;
202231 const purgeGetterResults = purgeGetterRegEx . exec ( appModuleNgFactoryContent ) ;
@@ -205,21 +234,31 @@ export function purgeProviderControllerImportAndUsage(appModuleNgFactoryPath: st
205234 const purgeIfResults = purgeIfRegEx . exec ( appModuleNgFactoryContent ) ;
206235
207236 if ( purgeGetterResults && purgeIfResults ) {
237+
238+ Logger . debug ( `[treeshake] purgeProviderControllerImportAndUsage: Purging imports ${ namedImport } ` ) ;
239+ appModuleNgFactoryContent = appModuleNgFactoryContent . replace ( importRegex , '' ) ;
240+
241+ Logger . debug ( `[treeshake] purgeProviderControllerImportAndUsage: Purging getter logic using ${ namedImport } ` ) ;
208242 const getterContentToReplace = purgeGetterResults [ 0 ] ;
209243 const newGetterContent = `/*${ getterContentToReplace } */` ;
210244 appModuleNgFactoryContent = appModuleNgFactoryContent . replace ( getterContentToReplace , newGetterContent ) ;
211245
246+ Logger . debug ( `[treeshake] purgeProviderControllerImportAndUsage: Purging additional logic using ${ namedImport } ` ) ;
212247 const purgeIfContentToReplace = purgeIfResults [ 0 ] ;
213248 const newPurgeIfContent = `/*${ purgeIfContentToReplace } */` ;
214249 appModuleNgFactoryContent = appModuleNgFactoryContent . replace ( purgeIfContentToReplace , newPurgeIfContent ) ;
215250 }
216251 }
252+
253+ Logger . debug ( `[treeshake] purgeProviderControllerImportAndUsage: Starting to purge provider controller and usage ... DONE` ) ;
217254 return appModuleNgFactoryContent ;
218255}
219256
220257export function purgeProviderClassNameFromIonicModuleForRoot ( indexFileContent : string , providerClassName : string ) {
258+ Logger . debug ( `[treeshake] purgeProviderClassNameFromIonicModuleForRoot: Purging reference in the ionicModule forRoot method ...` ) ;
221259 const regex = generateIonicModulePurgeProviderRegex ( providerClassName ) ;
222260 indexFileContent = indexFileContent . replace ( regex , '' ) ;
261+ Logger . debug ( `[treeshake] purgeProviderClassNameFromIonicModuleForRoot: Purging reference in the ionicModule forRoot method ... DONE` ) ;
223262 return indexFileContent ;
224263}
225264
0 commit comments