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

Commit b626e00

Browse files
committed
feat(optimization): purge decorators enabled by default
purge decorators enabled by default
1 parent 4b7338e commit b626e00

File tree

10 files changed

+919
-49
lines changed

10 files changed

+919
-49
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"fs-extra": "2.0.0",
4242
"glob": "^7.1.1",
4343
"json-loader": "0.5.4",
44+
"magic-string": "^0.19.0",
4445
"node-sass": "4.5.0",
4546
"os-name": "2.0.1",
4647
"postcss": "5.2.11",

src/declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module 'autoprefixer';
22
declare module 'cross-spawn';
3+
declare module 'magic-string';
34
declare module 'mime-types';
45
declare module 'proxyquire';
56
declare module 'os-name';

src/optimization.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ describe('optimization task', () => {
1818
};
1919

2020
spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(false);
21-
spyOn(decorators, decorators.purgeDecorators.name);
21+
spyOn(decorators, decorators.purgeStaticFieldDecorators.name);
2222
spyOn(treeshake, treeshake.calculateUnusedComponents.name);
2323

2424
// act
2525
const result = optimization.doOptimizations(context, new Map());
2626

2727
// assert
2828
expect(result).toBeTruthy();
29-
expect(decorators.purgeDecorators).not.toHaveBeenCalled();
29+
expect(decorators.purgeStaticFieldDecorators).not.toHaveBeenCalled();
3030
expect(treeshake.calculateUnusedComponents).not.toHaveBeenCalled();
3131
});
3232
});

src/optimization.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { extname, join } from 'path';
1+
import { extname } from 'path';
2+
3+
import * as MagicString from 'magic-string';
4+
25
import { Logger } from './logger/logger';
36
import { fillConfigDefaults, getUserConfigFile, replacePathVars } from './util/config';
47
import * as Constants from './util/constants';
58
import { BuildError } from './util/errors';
6-
import { getBooleanPropertyValue, webpackStatsToDependencyMap, printDependencyMap } from './util/helpers';
9+
import { getBooleanPropertyValue, getStringPropertyValue, webpackStatsToDependencyMap, printDependencyMap } from './util/helpers';
710
import { BuildContext, TaskInfo } from './util/interfaces';
811
import { runWebpackFullBuild, WebpackConfig } from './webpack';
9-
import { purgeDecorators } from './optimization/decorators';
12+
import { purgeStaticFieldDecorators } from './optimization/decorators';
1013
import { getAppModuleNgFactoryPath, calculateUnusedComponents, purgeUnusedImportsAndExportsFromIndex, purgeComponentNgFactoryImportAndUsage, purgeProviderControllerImportAndUsage, purgeProviderClassNameFromIonicModuleForRoot } from './optimization/treeshake';
1114

1215
export function optimization(context: BuildContext, configFile: string) {
@@ -50,7 +53,7 @@ export function purgeGeneratedFiles(context: BuildContext, fileNameSuffix: strin
5053
export function doOptimizations(context: BuildContext, dependencyMap: Map<string, Set<string>>) {
5154
// remove decorators
5255
const modifiedMap = new Map(dependencyMap);
53-
if (getBooleanPropertyValue(Constants.ENV_EXPERIMENTAL_PURGE_DECORATORS)) {
56+
if (getBooleanPropertyValue(Constants.ENV_PURGE_DECORATORS)) {
5457
removeDecorators(context);
5558
}
5659

@@ -70,15 +73,18 @@ export function doOptimizations(context: BuildContext, dependencyMap: Map<string
7073
}
7174

7275
function optimizationEnabled() {
73-
const purgeDecorators = getBooleanPropertyValue(Constants.ENV_EXPERIMENTAL_PURGE_DECORATORS);
76+
const purgeDecorators = getBooleanPropertyValue(Constants.ENV_PURGE_DECORATORS);
7477
const manualTreeshaking = getBooleanPropertyValue(Constants.ENV_EXPERIMENTAL_MANUAL_TREESHAKING);
7578
return purgeDecorators || manualTreeshaking;
7679
}
7780

7881
function removeDecorators(context: BuildContext) {
7982
const jsFiles = context.fileCache.getAll().filter(file => extname(file.path) === '.js');
8083
jsFiles.forEach(jsFile => {
81-
jsFile.content = purgeDecorators(jsFile.path, jsFile.content);
84+
let magicString = new MagicString(jsFile.content);
85+
magicString = purgeStaticFieldDecorators(jsFile.path, jsFile.content, getStringPropertyValue(Constants.ENV_VAR_IONIC_ANGULAR_DIR), getStringPropertyValue(Constants.ENV_VAR_AT_ANGULAR_DIR), context.srcDir, magicString);
86+
jsFile.content = magicString.toString();
87+
// jsFile.content = removeTSickleClosureDeclarations(jsFile.path, jsFile.content, getStringPropertyValue(Constants.ENV_VAR_IONIC_ANGULAR_DIR), context.srcDir);
8288
});
8389
}
8490

0 commit comments

Comments
 (0)