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

Commit 5fe21f3

Browse files
committed
fix(optimizations): make optimizations work on windows and mac
make optimizations work on windows and mac
1 parent 0c81ce6 commit 5fe21f3

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/optimization/decorators.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import * as decorators from './decorators';
2+
import * as Constants from '../util/constants';
23

4+
const entryPoint = '/Users/dan/Dev/myApp3/node_modules/ionic-angular/index.js';
5+
6+
let originalEnv = {};
37
describe('optimization', () => {
48
describe('purgeDecoratorStatements', () => {
9+
10+
beforeEach(() => {
11+
originalEnv = process.env;
12+
process.env[Constants.ENV_VAR_IONIC_ANGULAR_ENTRY_POINT] = entryPoint;
13+
});
14+
15+
afterEach(() => {
16+
process.env = originalEnv;
17+
});
18+
519
it('should purge the decorators', () => {
620
// arrange
721
const decoratorStatement = `
@@ -229,7 +243,7 @@ ${additionalGeneratedContent}
229243
some more content
230244
`;
231245
// act
232-
const result = decorators.purgeDecorators('/Users/dan/Dev/myApp3/node_modules/ionic-angular/index.js', knownContent);
246+
const result = decorators.purgeDecorators(entryPoint, knownContent);
233247

234248
// assert
235249
expect(result).not.toEqual(knownContent);

src/optimization/decorators.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import { Logger } from '../logger/logger';
2+
import * as Constants from '../util/constants';
23

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

7-
export function purgeDecoratorStatementsImpl(filePath: string, fileContent: string, inclusions: string[]) {
8-
const include = shouldInclude(filePath, inclusions);
9-
if (include) {
8+
export function purgeIndexDecorator(filePath: string, fileContent: string) {
9+
if (process.env[Constants.ENV_VAR_IONIC_ANGULAR_ENTRY_POINT] === filePath) {
1010
Logger.debug(`Purging decorators for ${filePath}`);
1111
return fileContent.replace(DECORATORS_REGEX, '');
1212
}
1313
return fileContent;
1414
}
1515

16-
function shouldInclude(filePath: string, inclusions: string[]) {
17-
// TODO - make this more robust
18-
for (const inclusion of inclusions) {
19-
20-
if (filePath.includes(inclusion)) {
21-
return true;
22-
}
23-
}
24-
return false;
25-
}
26-
2716
const DECORATORS_REGEX = /IonicModule.decorators.=[\s\S\n]*?([\s\S\n]*?)];/igm;

src/optimization/treeshake.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { dirname, join, relative } from 'path';
22
import { Logger } from '../logger/logger';
33
import * as Constants from '../util/constants';
4-
import { changeExtension, convertFilePathToNgFactoryPath, escapeStringForRegex } from '../util/helpers';
4+
import { changeExtension, convertFilePathToNgFactoryPath, escapeStringForRegex, toUnixPath } from '../util/helpers';
55
import { TreeShakeCalcResults } from '../util/interfaces';
66

77
export function calculateUnusedComponents(dependencyMap: Map<string, Set<string>>): TreeShakeCalcResults {
@@ -145,7 +145,8 @@ export function purgeUnusedImportsAndExportsFromIndex(indexFilePath: string, ind
145145
// I cannot get the './' prefix to show up when using path api
146146
Logger.debug(`[treeshake] purgeUnusedImportsFromIndex: Removing ${modulePath} from ${indexFilePath}`);
147147
const extensionless = changeExtension(modulePath, '');
148-
const importPath = './' + relative(dirname(indexFilePath), extensionless);
148+
const relativeImportPath = './' + relative(dirname(indexFilePath), extensionless);
149+
const importPath = toUnixPath(relativeImportPath);
149150
const importRegex = generateImportRegex(importPath);
150151
// replace the import if it's found
151152
let results: RegExpExecArray = null;
@@ -175,7 +176,8 @@ function generateExportRegex(relativeExportPath: string) {
175176
export function purgeComponentNgFactoryImportAndUsage(appModuleNgFactoryPath: string, appModuleNgFactoryContent: string, componentFactoryPath: string) {
176177
const extensionlessComponentFactoryPath = changeExtension(componentFactoryPath, '');
177178
const relativeImportPath = relative(dirname(appModuleNgFactoryPath), extensionlessComponentFactoryPath);
178-
const importRegex = generateWildCardImportRegex(relativeImportPath);
179+
const importPath = toUnixPath(relativeImportPath);
180+
const importRegex = generateWildCardImportRegex(importPath);
179181
const results = importRegex.exec(appModuleNgFactoryContent);
180182
if (results && results.length >= 2) {
181183
appModuleNgFactoryContent = appModuleNgFactoryContent.replace(importRegex, '');
@@ -189,7 +191,8 @@ export function purgeComponentNgFactoryImportAndUsage(appModuleNgFactoryPath: st
189191
export function purgeProviderControllerImportAndUsage(appModuleNgFactoryPath: string, appModuleNgFactoryContent: string, providerPath: string) {
190192
const extensionlessComponentFactoryPath = changeExtension(providerPath, '');
191193
const relativeImportPath = relative(dirname(process.env[Constants.ENV_VAR_IONIC_ANGULAR_DIR]), extensionlessComponentFactoryPath);
192-
const importRegex = generateWildCardImportRegex(relativeImportPath);
194+
const importPath = toUnixPath(relativeImportPath);
195+
const importRegex = generateWildCardImportRegex(importPath);
193196
const results = importRegex.exec(appModuleNgFactoryContent);
194197
if (results && results.length >= 2) {
195198
appModuleNgFactoryContent = appModuleNgFactoryContent.replace(importRegex, '');

0 commit comments

Comments
 (0)