Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 94f10e5

Browse files
committed
feature(optimization): purge decorators from ionic-angular/index.js file
1 parent fcbdf43 commit 94f10e5

File tree

4 files changed

+226
-26
lines changed

4 files changed

+226
-26
lines changed

src/aot/optimization.spec.ts

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import * as optimization from './optimization';
2+
3+
describe('optimization', () => {
4+
describe('purgeDecoratorStatements', () => {
5+
it('should purge the decorators', () => {
6+
// arrange
7+
const knownContent = `
8+
some various content
9+
IonicModule.decorators = [
10+
{ type: NgModule, args: [{
11+
imports: [BrowserModule, HttpModule, FormsModule, ReactiveFormsModule],
12+
exports: [
13+
BrowserModule,
14+
HttpModule,
15+
FormsModule,
16+
ReactiveFormsModule,
17+
Avatar,
18+
Backdrop,
19+
Badge,
20+
Button,
21+
Card,
22+
CardContent,
23+
CardHeader,
24+
CardTitle,
25+
Checkbox,
26+
Chip,
27+
ClickBlock,
28+
Col,
29+
Content,
30+
// DateTime,
31+
FabContainer,
32+
FabButton,
33+
FabList,
34+
Footer,
35+
Grid,
36+
Header,
37+
HideWhen,
38+
Icon,
39+
Img,
40+
InfiniteScroll,
41+
InfiniteScrollContent,
42+
IonicApp,
43+
Item,
44+
ItemContent,
45+
ItemDivider,
46+
ItemGroup,
47+
ItemOptions,
48+
ItemReorder,
49+
ItemSliding,
50+
Label,
51+
List,
52+
ListHeader,
53+
Menu,
54+
MenuClose,
55+
MenuToggle,
56+
NativeInput,
57+
Nav,
58+
Navbar,
59+
NavPop,
60+
NavPopAnchor,
61+
NavPush,
62+
NavPushAnchor,
63+
NextInput,
64+
Note,
65+
Option,
66+
OverlayPortal,
67+
PickerColumnCmp,
68+
RadioButton,
69+
RadioGroup,
70+
Range,
71+
RangeKnob,
72+
Refresher,
73+
RefresherContent,
74+
Reorder,
75+
Row,
76+
Scroll,
77+
Searchbar,
78+
Segment,
79+
SegmentButton,
80+
// Select,
81+
ShowWhen,
82+
Slide,
83+
Slides,
84+
Spinner,
85+
Tab,
86+
Tabs,
87+
TabButton,
88+
TabHighlight,
89+
TextInput,
90+
Thumbnail,
91+
Toggle,
92+
Toolbar,
93+
ToolbarItem,
94+
ToolbarTitle,
95+
Typography,
96+
VirtualFooter,
97+
VirtualHeader,
98+
VirtualItem,
99+
VirtualScroll,
100+
],
101+
declarations: [
102+
ActionSheetCmp,
103+
AlertCmp,
104+
ClickBlock,
105+
LoadingCmp,
106+
ModalCmp,
107+
PickerCmp,
108+
PopoverCmp,
109+
ToastCmp,
110+
Avatar,
111+
Backdrop,
112+
Badge,
113+
Button,
114+
Card,
115+
CardContent,
116+
CardHeader,
117+
CardTitle,
118+
Checkbox,
119+
Chip,
120+
ClickBlock,
121+
Col,
122+
Content,
123+
// DateTime,
124+
FabContainer,
125+
FabButton,
126+
FabList,
127+
Footer,
128+
Grid,
129+
Header,
130+
HideWhen,
131+
Icon,
132+
Img,
133+
InfiniteScroll,
134+
InfiniteScrollContent,
135+
IonicApp,
136+
Item,
137+
ItemContent,
138+
ItemDivider,
139+
ItemGroup,
140+
ItemOptions,
141+
ItemReorder,
142+
ItemSliding,
143+
Label,
144+
List,
145+
ListHeader,
146+
Menu,
147+
MenuClose,
148+
MenuToggle,
149+
NativeInput,
150+
Nav,
151+
Navbar,
152+
NavPop,
153+
NavPopAnchor,
154+
NavPush,
155+
NavPushAnchor,
156+
NextInput,
157+
Note,
158+
Option,
159+
OverlayPortal,
160+
PickerColumnCmp,
161+
RadioButton,
162+
RadioGroup,
163+
Range,
164+
RangeKnob,
165+
Refresher,
166+
RefresherContent,
167+
Reorder,
168+
Row,
169+
Scroll,
170+
Searchbar,
171+
Segment,
172+
SegmentButton,
173+
// Select,
174+
ShowWhen,
175+
Slide,
176+
Slides,
177+
Spinner,
178+
Tab,
179+
Tabs,
180+
TabButton,
181+
TabHighlight,
182+
TextInput,
183+
Thumbnail,
184+
Toggle,
185+
Toolbar,
186+
ToolbarItem,
187+
ToolbarTitle,
188+
Typography,
189+
VirtualFooter,
190+
VirtualHeader,
191+
VirtualItem,
192+
VirtualScroll,
193+
],
194+
entryComponents: []
195+
},] },
196+
];
197+
some more content
198+
`;
199+
// act
200+
const result = optimization.purgeDecoratorStatements('/Users/dan/Dev/myApp3/node_modules/ionic-angular/index.js', knownContent, ['ionic-angular/index.js']);
201+
202+
// assert
203+
expect(result).not.toEqual(knownContent);
204+
expect(result.indexOf('IonicModule.decorators')).toEqual(-1);
205+
});
206+
});
207+
});

src/aot/optimization.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
1-
import { removeDecorators } from '../util/typescript-utils';
1+
import { Logger } from '../logger/logger';
22

33
export function optimizeJavascript(filePath: string, fileContent: string) {
4-
fileContent = removeDecorators(filePath, fileContent);
5-
fileContent = purgeDecoratorStatements(filePath, fileContent, ['@angular']);
6-
// TODO - needs more testing to fully understand
7-
// fileContent = purgeCtorStatements(filePath, fileContent, ['@angular']);
8-
fileContent = purgeKnownContent(filePath, fileContent, ['@angular']);
9-
4+
fileContent = purgeDecoratorStatements(filePath, fileContent, ['ionic-angular/index.js']);
105
return fileContent;
116
}
127

13-
export function purgeDecoratorStatements(filePath: string, fileContent: string, exclusions: string[]) {
14-
const exclude = shouldExclude(filePath, exclusions);
15-
if (exclude) {
8+
export function purgeDecoratorStatements(filePath: string, fileContent: string, inclusions: string[]) {
9+
const include = shouldInclude(filePath, inclusions);
10+
if (include) {
11+
Logger.debug(`Purging decorators for ${filePath}`);
1612
return fileContent.replace(DECORATORS_REGEX, '');
1713
}
1814
return fileContent;
1915
}
2016

21-
export function purgeCtorStatements(filePath: string, fileContent: string, exclusions: string[]) {
22-
const exclude = shouldExclude(filePath, exclusions);
23-
if (exclude) {
24-
return fileContent.replace(CTOR_PARAM_REGEX, '');
25-
}
26-
return fileContent;
27-
}
28-
29-
export function purgeKnownContent(filePath: string, fileContent: string, exclusions: string[]) {
17+
/*export function purgeKnownContent(filePath: string, fileContent: string, exclusions: string[]) {
3018
const exclude = shouldExclude(filePath, exclusions);
3119
if (exclude) {
3220
return fileContent.replace(TREE_SHAKEABLE_IMPORTS, '');
3321
}
3422
return fileContent;
3523
}
24+
*/
25+
26+
function shouldInclude(filePath: string, inclusions: string[]) {
27+
// TODO - make this more robust
28+
for (const inclusion of inclusions) {
3629

37-
function shouldExclude(filePath: string, exclusions: string[]) {
38-
for (const exclusion in exclusions) {
39-
if (filePath.includes(exclusion)) {
30+
if (filePath.includes(inclusion)) {
4031
return true;
4132
}
4233
}
4334
return false;
4435
}
4536

4637
const DECORATORS_REGEX = /(.+)\.decorators[\s\S\n]*?([\s\S\n]*?)];/igm;
47-
const CTOR_PARAM_REGEX = /(.+).ctorParameters[\s\S\n]*?([\s\S\n]*?)];/igm;
48-
const TREE_SHAKEABLE_IMPORTS = /\/\* AoT Remove Start[\s\S\n]*?([\s\S\n]*?)AoT Remove End \*\//igm;

src/rollup/ionic-rollup-resolver-plugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Logger } from '../logger/logger';
55
import { dirname, join, resolve } from 'path';
66
import * as pluginutils from 'rollup-pluginutils';
77

8+
import { optimizeJavascript } from '../aot/optimization';
9+
810
export const PLUGIN_NAME = 'ion-rollup-resolver';
911

1012
export function ionicRollupResolverPlugin(context: BuildContext) {
@@ -65,7 +67,7 @@ export function ionicRollupResolverPlugin(context: BuildContext) {
6567
}
6668

6769
return {
68-
code: file.content,
70+
code: optimizeJavascript(jsSourcePath, file.content),
6971
map: mapContent
7072
};
7173
}

src/webpack/loader-impl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { normalize, resolve } from 'path';
22
import { changeExtension, getContext, readFileAsync} from '../util/helpers';
33
import { Logger } from '../logger/logger';
4-
import { File } from '../util/interfaces';
54
import { FileCache } from '../util/file-cache';
65

6+
import { optimizeJavascript } from '../aot/optimization';
7+
78
export function webpackLoader(source: string, map: any, webpackContex: any) {
89
webpackContex.cacheable();
910
var callback = webpackContex.async();
@@ -34,6 +35,7 @@ export function webpackLoader(source: string, map: any, webpackContex: any) {
3435
}
3536
}
3637
}
38+
javascriptFile.content = optimizeJavascript(javascriptPath, javascriptFile.content);
3739
callback(null, javascriptFile.content, sourceMapObject);
3840
}).catch(err => {
3941
Logger.debug(`[Webpack] loader: Encountered an unexpected error: ${err.message}`);

0 commit comments

Comments
 (0)