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

Commit

Permalink
fix(optimizations): make optimizations work on windows and mac
Browse files Browse the repository at this point in the history
make optimizations work on windows and mac
  • Loading branch information
danbucholtz committed Feb 10, 2017
1 parent 0c81ce6 commit 5fe21f3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
16 changes: 15 additions & 1 deletion src/optimization/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import * as decorators from './decorators';
import * as Constants from '../util/constants';

const entryPoint = '/Users/dan/Dev/myApp3/node_modules/ionic-angular/index.js';

let originalEnv = {};
describe('optimization', () => {
describe('purgeDecoratorStatements', () => {

beforeEach(() => {
originalEnv = process.env;
process.env[Constants.ENV_VAR_IONIC_ANGULAR_ENTRY_POINT] = entryPoint;
});

afterEach(() => {
process.env = originalEnv;
});

it('should purge the decorators', () => {
// arrange
const decoratorStatement = `
Expand Down Expand Up @@ -229,7 +243,7 @@ ${additionalGeneratedContent}
some more content
`;
// act
const result = decorators.purgeDecorators('/Users/dan/Dev/myApp3/node_modules/ionic-angular/index.js', knownContent);
const result = decorators.purgeDecorators(entryPoint, knownContent);

// assert
expect(result).not.toEqual(knownContent);
Expand Down
19 changes: 4 additions & 15 deletions src/optimization/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import { Logger } from '../logger/logger';
import * as Constants from '../util/constants';

export function purgeDecorators(filePath: string, fileContent: string) {
return purgeDecoratorStatementsImpl(filePath, fileContent, ['ionic-angular/index.js']);
return purgeIndexDecorator(filePath, fileContent);
}

export function purgeDecoratorStatementsImpl(filePath: string, fileContent: string, inclusions: string[]) {
const include = shouldInclude(filePath, inclusions);
if (include) {
export function purgeIndexDecorator(filePath: string, fileContent: string) {
if (process.env[Constants.ENV_VAR_IONIC_ANGULAR_ENTRY_POINT] === filePath) {
Logger.debug(`Purging decorators for ${filePath}`);
return fileContent.replace(DECORATORS_REGEX, '');
}
return fileContent;
}

function shouldInclude(filePath: string, inclusions: string[]) {
// TODO - make this more robust
for (const inclusion of inclusions) {

if (filePath.includes(inclusion)) {
return true;
}
}
return false;
}

const DECORATORS_REGEX = /IonicModule.decorators.=[\s\S\n]*?([\s\S\n]*?)];/igm;
11 changes: 7 additions & 4 deletions src/optimization/treeshake.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname, join, relative } from 'path';
import { Logger } from '../logger/logger';
import * as Constants from '../util/constants';
import { changeExtension, convertFilePathToNgFactoryPath, escapeStringForRegex } from '../util/helpers';
import { changeExtension, convertFilePathToNgFactoryPath, escapeStringForRegex, toUnixPath } from '../util/helpers';
import { TreeShakeCalcResults } from '../util/interfaces';

export function calculateUnusedComponents(dependencyMap: Map<string, Set<string>>): TreeShakeCalcResults {
Expand Down Expand Up @@ -145,7 +145,8 @@ export function purgeUnusedImportsAndExportsFromIndex(indexFilePath: string, ind
// I cannot get the './' prefix to show up when using path api
Logger.debug(`[treeshake] purgeUnusedImportsFromIndex: Removing ${modulePath} from ${indexFilePath}`);
const extensionless = changeExtension(modulePath, '');
const importPath = './' + relative(dirname(indexFilePath), extensionless);
const relativeImportPath = './' + relative(dirname(indexFilePath), extensionless);
const importPath = toUnixPath(relativeImportPath);
const importRegex = generateImportRegex(importPath);
// replace the import if it's found
let results: RegExpExecArray = null;
Expand Down Expand Up @@ -175,7 +176,8 @@ function generateExportRegex(relativeExportPath: string) {
export function purgeComponentNgFactoryImportAndUsage(appModuleNgFactoryPath: string, appModuleNgFactoryContent: string, componentFactoryPath: string) {
const extensionlessComponentFactoryPath = changeExtension(componentFactoryPath, '');
const relativeImportPath = relative(dirname(appModuleNgFactoryPath), extensionlessComponentFactoryPath);
const importRegex = generateWildCardImportRegex(relativeImportPath);
const importPath = toUnixPath(relativeImportPath);
const importRegex = generateWildCardImportRegex(importPath);
const results = importRegex.exec(appModuleNgFactoryContent);
if (results && results.length >= 2) {
appModuleNgFactoryContent = appModuleNgFactoryContent.replace(importRegex, '');
Expand All @@ -189,7 +191,8 @@ export function purgeComponentNgFactoryImportAndUsage(appModuleNgFactoryPath: st
export function purgeProviderControllerImportAndUsage(appModuleNgFactoryPath: string, appModuleNgFactoryContent: string, providerPath: string) {
const extensionlessComponentFactoryPath = changeExtension(providerPath, '');
const relativeImportPath = relative(dirname(process.env[Constants.ENV_VAR_IONIC_ANGULAR_DIR]), extensionlessComponentFactoryPath);
const importRegex = generateWildCardImportRegex(relativeImportPath);
const importPath = toUnixPath(relativeImportPath);
const importRegex = generateWildCardImportRegex(importPath);
const results = importRegex.exec(appModuleNgFactoryContent);
if (results && results.length >= 2) {
appModuleNgFactoryContent = appModuleNgFactoryContent.replace(importRegex, '');
Expand Down

0 comments on commit 5fe21f3

Please sign in to comment.