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

Commit

Permalink
feat(optimization): purge decorators enabled by default
Browse files Browse the repository at this point in the history
purge decorators enabled by default
  • Loading branch information
danbucholtz committed Apr 3, 2017
1 parent 4b7338e commit b626e00
Show file tree
Hide file tree
Showing 10 changed files with 919 additions and 49 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"fs-extra": "2.0.0",
"glob": "^7.1.1",
"json-loader": "0.5.4",
"magic-string": "^0.19.0",
"node-sass": "4.5.0",
"os-name": "2.0.1",
"postcss": "5.2.11",
Expand Down
1 change: 1 addition & 0 deletions src/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module 'autoprefixer';
declare module 'cross-spawn';
declare module 'magic-string';
declare module 'mime-types';
declare module 'proxyquire';
declare module 'os-name';
Expand Down
4 changes: 2 additions & 2 deletions src/optimization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ describe('optimization task', () => {
};

spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(false);
spyOn(decorators, decorators.purgeDecorators.name);
spyOn(decorators, decorators.purgeStaticFieldDecorators.name);
spyOn(treeshake, treeshake.calculateUnusedComponents.name);

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

// assert
expect(result).toBeTruthy();
expect(decorators.purgeDecorators).not.toHaveBeenCalled();
expect(decorators.purgeStaticFieldDecorators).not.toHaveBeenCalled();
expect(treeshake.calculateUnusedComponents).not.toHaveBeenCalled();
});
});
Expand Down
18 changes: 12 additions & 6 deletions src/optimization.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { extname, join } from 'path';
import { extname } from 'path';

import * as MagicString from 'magic-string';

import { Logger } from './logger/logger';
import { fillConfigDefaults, getUserConfigFile, replacePathVars } from './util/config';
import * as Constants from './util/constants';
import { BuildError } from './util/errors';
import { getBooleanPropertyValue, webpackStatsToDependencyMap, printDependencyMap } from './util/helpers';
import { getBooleanPropertyValue, getStringPropertyValue, webpackStatsToDependencyMap, printDependencyMap } from './util/helpers';
import { BuildContext, TaskInfo } from './util/interfaces';
import { runWebpackFullBuild, WebpackConfig } from './webpack';
import { purgeDecorators } from './optimization/decorators';
import { purgeStaticFieldDecorators } from './optimization/decorators';
import { getAppModuleNgFactoryPath, calculateUnusedComponents, purgeUnusedImportsAndExportsFromIndex, purgeComponentNgFactoryImportAndUsage, purgeProviderControllerImportAndUsage, purgeProviderClassNameFromIonicModuleForRoot } from './optimization/treeshake';

export function optimization(context: BuildContext, configFile: string) {
Expand Down Expand Up @@ -50,7 +53,7 @@ export function purgeGeneratedFiles(context: BuildContext, fileNameSuffix: strin
export function doOptimizations(context: BuildContext, dependencyMap: Map<string, Set<string>>) {
// remove decorators
const modifiedMap = new Map(dependencyMap);
if (getBooleanPropertyValue(Constants.ENV_EXPERIMENTAL_PURGE_DECORATORS)) {
if (getBooleanPropertyValue(Constants.ENV_PURGE_DECORATORS)) {
removeDecorators(context);
}

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

function optimizationEnabled() {
const purgeDecorators = getBooleanPropertyValue(Constants.ENV_EXPERIMENTAL_PURGE_DECORATORS);
const purgeDecorators = getBooleanPropertyValue(Constants.ENV_PURGE_DECORATORS);
const manualTreeshaking = getBooleanPropertyValue(Constants.ENV_EXPERIMENTAL_MANUAL_TREESHAKING);
return purgeDecorators || manualTreeshaking;
}

function removeDecorators(context: BuildContext) {
const jsFiles = context.fileCache.getAll().filter(file => extname(file.path) === '.js');
jsFiles.forEach(jsFile => {
jsFile.content = purgeDecorators(jsFile.path, jsFile.content);
let magicString = new MagicString(jsFile.content);
magicString = purgeStaticFieldDecorators(jsFile.path, jsFile.content, getStringPropertyValue(Constants.ENV_VAR_IONIC_ANGULAR_DIR), getStringPropertyValue(Constants.ENV_VAR_AT_ANGULAR_DIR), context.srcDir, magicString);
jsFile.content = magicString.toString();
// jsFile.content = removeTSickleClosureDeclarations(jsFile.path, jsFile.content, getStringPropertyValue(Constants.ENV_VAR_IONIC_ANGULAR_DIR), context.srcDir);
});
}

Expand Down
Loading

0 comments on commit b626e00

Please sign in to comment.