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

Commit cf107ed

Browse files
authored
Merge pull request #723 from driftyco/shake-dat-tree
2 parents 3744b3e + b5a6060 commit cf107ed

28 files changed

+1163
-153
lines changed

config/optimization.config.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var path = require('path');
2+
var webpack = require('webpack');
3+
var ionicWebpackFactory = require(process.env.IONIC_WEBPACK_FACTORY);
4+
5+
module.exports = {
6+
entry: process.env.IONIC_APP_ENTRY_POINT,
7+
output: {
8+
path: '{{BUILD}}',
9+
publicPath: 'build/',
10+
filename: 'deptree.js',
11+
},
12+
13+
resolve: {
14+
extensions: ['.ts', '.js']
15+
},
16+
17+
module: {
18+
loaders: [
19+
{
20+
test: /\.ts$/,
21+
loader: process.env.IONIC_WEBPACK_LOADER
22+
},
23+
{
24+
test: /\.js$/,
25+
loader: process.env.IONIC_OPTIMIZATION_LOADER
26+
}
27+
]
28+
},
29+
30+
plugins: [
31+
ionicWebpackFactory.getIonicEnvironmentPlugin(),
32+
],
33+
34+
// Some libraries import Node modules but don't use them in the browser.
35+
// Tell Webpack to provide empty mocks for them so importing them works.
36+
node: {
37+
fs: 'empty',
38+
net: 'empty',
39+
tls: 'empty'
40+
}
41+
};

config/webpack.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module.exports = {
2424
loader: 'json-loader'
2525
},
2626
{
27-
//test: /\.(ts|ngfactory.js)$/,
2827
test: /\.ts$/,
2928
loader: process.env.IONIC_WEBPACK_LOADER
3029
}
@@ -33,8 +32,6 @@ module.exports = {
3332

3433
plugins: [
3534
ionicWebpackFactory.getIonicEnvironmentPlugin(),
36-
// ionicWebpackFactory.getNonIonicCommonChunksPlugin(),
37-
// ionicWebpackFactory.getIonicCommonChunksPlugin()
3835
],
3936

4037
// Some libraries import Node modules but don't use them in the browser.

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@
6161
"xml2js": "^0.4.17"
6262
},
6363
"devDependencies": {
64-
"@angular/common": "2.4.5",
65-
"@angular/compiler": "2.4.5",
66-
"@angular/compiler-cli": "2.4.5",
67-
"@angular/core": "2.4.5",
68-
"@angular/forms": "2.4.5",
69-
"@angular/http": "2.4.5",
70-
"@angular/platform-browser": "2.4.5",
71-
"@angular/platform-browser-dynamic": "2.4.5",
72-
"@angular/platform-server": "2.4.5",
64+
"@angular/common": "4.0.0-beta.6",
65+
"@angular/compiler": "4.0.0-beta.6",
66+
"@angular/compiler-cli": "4.0.0-beta.6",
67+
"@angular/core": "4.0.0-beta.6",
68+
"@angular/forms": "4.0.0-beta.6",
69+
"@angular/http": "4.0.0-beta.6",
70+
"@angular/platform-browser": "4.0.0-beta.6",
71+
"@angular/platform-browser-dynamic": "4.0.0-beta.6",
72+
"@angular/platform-server": "4.0.0-beta.6",
73+
"@angular/tsc-wrapped": "4.0.0-beta.6",
7374
"@types/chalk": "^0.4.30",
7475
"@types/chokidar": "1.4.29",
7576
"@types/clean-css": "^3.4.29",
@@ -89,13 +90,12 @@
8990
"ionic-cz-conventional-changelog": "1.0.0",
9091
"jest": "^18.0.0",
9192
"mock-fs": "3.11.0",
92-
"node-sass": "3.10.1",
9393
"rewire": "^2.5.2",
9494
"rimraf": "2.5.4",
9595
"rxjs": "5.0.0-beta.12",
9696
"tslint-ionic-rules": "0.0.8",
9797
"typescript": "~2.0.10",
98-
"zone.js": "^0.6.26"
98+
"zone.js": "^0.7.2"
9999
},
100100
"peerDependencies": {
101101
"@angular/core": "*",

src/aot/aot-compiler.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { mkdirpSync, readFileSync, writeFileSync } from 'fs-extra';
2-
import { basename, dirname, extname, join, normalize, relative, resolve } from 'path';
1+
import { readFileSync } from 'fs-extra';
2+
import { extname, normalize, resolve } from 'path';
33

44
import 'reflect-metadata';
55
import { CompilerOptions, createProgram, ParsedCommandLine, Program, transpileModule, TranspileOptions, TranspileOutput } from 'typescript';
@@ -15,7 +15,6 @@ import { getFallbackMainContent, replaceBootstrap } from './utils';
1515
import { Logger } from '../logger/logger';
1616
import { printDiagnostics, clearDiagnostics, DiagnosticsType } from '../logger/logger-diagnostics';
1717
import { runTypeScriptDiagnostics } from '../logger/logger-typescript';
18-
import { isDebugMode } from '../util/config';
1918
import { BuildError } from '../util/errors';
2019
import { changeExtension } from '../util/helpers';
2120
import { BuildContext } from '../util/interfaces';
@@ -151,37 +150,19 @@ function transpileFiles(context: BuildContext, tsConfig: ParsedTsConfig, fileSys
151150
fileSystem.addVirtualFile(jsFilePath, transpileOutput.outputText);
152151
fileSystem.addVirtualFile(jsFilePath + '.map', transpileOutput.sourceMapText);
153152

154-
// write files to disk here if debug is enabled
155-
if (isDebugMode()) {
156-
writeNgcFilesToDisk(context, tsFile.path, tsFile.content, transpileOutput.outputText, transpileOutput.sourceMapText);
157-
}
158153
Logger.debug(`[AotCompiler] transpileFiles: Transpiling file ${tsFile.path} ... DONE`);
159154
}
160155
}
161156

162157
function transpileFileContent(fileName: string, sourceText: string, options: CompilerOptions): TranspileOutput {
163-
const transpileOptions: TranspileOptions = {
164-
compilerOptions: options,
165-
fileName: fileName,
166-
reportDiagnostics: true
167-
};
168-
169-
return transpileModule(sourceText, transpileOptions);
170-
}
158+
const transpileOptions: TranspileOptions = {
159+
compilerOptions: options,
160+
fileName: fileName,
161+
reportDiagnostics: true
162+
};
171163

172-
function writeNgcFilesToDisk(context: BuildContext, typescriptFilePath: string, typescriptFileContent: string, transpiledFileContent: string, sourcemapContent: string) {
173-
const dirName = dirname(typescriptFilePath);
174-
const relativePath = relative(process.cwd(), dirName);
175-
const tmpPath = join(context.tmpDir, relativePath);
176-
const fileName = basename(typescriptFilePath);
177-
const fileToWrite = join(tmpPath, fileName);
178-
const jsFileToWrite = changeExtension(fileToWrite, '.js');
179-
180-
mkdirpSync(tmpPath);
181-
writeFileSync(fileToWrite, typescriptFileContent);
182-
writeFileSync(jsFileToWrite, transpiledFileContent);
183-
writeFileSync(jsFileToWrite + '.map', sourcemapContent);
184-
}
164+
return transpileModule(sourceText, transpileOptions);
165+
}
185166

186167
export interface AotOptions {
187168
tsConfigPath: string;

src/aot/optimization.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/build.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ function buildWorker(context: BuildContext) {
4040
return validateTsConfigSettings(tsConfigContents);
4141
})
4242
.then(() => {
43-
return preprocess(context);
44-
}).then(() => {
4543
return buildProject(context);
46-
}).then(() => {
47-
return postprocess(context);
4844
});
4945
}
5046

@@ -107,6 +103,9 @@ function buildProject(context: BuildContext) {
107103
const compilePromise = (context.runAot) ? ngc(context) : transpile(context);
108104

109105
return compilePromise
106+
.then(() => {
107+
return preprocess(context);
108+
})
110109
.then(() => {
111110
return bundle(context);
112111
})
@@ -123,6 +122,9 @@ function buildProject(context: BuildContext) {
123122
copyPromise
124123
]);
125124
})
125+
.then(() => {
126+
return postprocess(context);
127+
})
126128
.then(() => {
127129
// kick off the tslint after everything else
128130
// nothing needs to wait on its completion unless bailing on lint error is enabled

src/cleancss.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('clean css task', () => {
6666
spyOn(config, config.fillConfigDefaults.name).and.returnValue(cleanCssConfig);
6767
spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.resolve(fileContent));
6868
spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
69-
spyOn(cleanCssFactory, 'getCleanCssInstance').and.returnValue({
69+
spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue({
7070
minify: (content: string, cb: Function) => {
7171
cb(null, { styles: minifiedContent });
7272
}

src/copy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as config from './util/config';
44

55
describe('copy task', () => {
66
describe('copyConfigToWatchConfig', () => {
7-
it('should do something', () => {
7+
it('should convert to watch config format', () => {
88
// arrange
99
const context = { };
1010
const configFile = 'configFile';

src/deep-linking.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Logger } from './logger/logger';
2+
import * as Constants from './util/constants';
3+
import { BuildError } from './util/errors';
4+
import { FileCache } from './util/file-cache';
5+
import { readFileAsync, setParsedDeepLinkConfig } from './util/helpers';
6+
import { BuildContext, ChangedFile, HydratedDeepLinkConfigEntry } from './util/interfaces';
7+
8+
import { getDeepLinkData } from './deep-linking/util';
9+
10+
11+
export function deepLinking(context: BuildContext) {
12+
const logger = new Logger(`deep links`);
13+
return deepLinkingWorker(context).then((hydratedDeepLinkEntryList: HydratedDeepLinkConfigEntry[]) => {
14+
setParsedDeepLinkConfig(hydratedDeepLinkEntryList);
15+
logger.finish();
16+
})
17+
.catch((err: Error) => {
18+
const error = new BuildError(err.message);
19+
error.isFatal = true;
20+
throw logger.fail(error);
21+
});
22+
}
23+
24+
25+
function deepLinkingWorker(context: BuildContext) {
26+
const appModulePath = process.env[Constants.ENV_APP_NG_MODULE_PATH];
27+
return getAppNgModuleContent(appModulePath, context.fileCache).then((fileContent: string) => {
28+
return extractDeepLinkData(appModulePath, fileContent, context.runAot);
29+
});
30+
}
31+
32+
function getAppNgModuleContent(filePath: string, fileCache: FileCache): Promise<string> {
33+
const file = fileCache.get(filePath);
34+
if (file) {
35+
return Promise.resolve(file.content);
36+
}
37+
return readFileAsync(filePath).then((fileContent: string) => {
38+
// cache it!
39+
fileCache.set(filePath, { path: filePath, content: fileContent});
40+
return fileContent;
41+
});
42+
}
43+
44+
export function deepLinkingUpdate(changedFiles: ChangedFile[], context: BuildContext) {
45+
const appNgModuleChangedFiles = changedFiles.filter(changedFile => changedFile.filePath === process.env[Constants.ENV_APP_NG_MODULE_PATH]);
46+
if (appNgModuleChangedFiles.length) {
47+
const fileContent = context.fileCache.get(appNgModuleChangedFiles[0].filePath).content;
48+
const hydratedDeepLinkEntries = extractDeepLinkData(appNgModuleChangedFiles[0].filePath, fileContent, context.runAot);
49+
setParsedDeepLinkConfig(hydratedDeepLinkEntries);
50+
}
51+
return Promise.resolve();
52+
}
53+
54+
function extractDeepLinkData(appNgModulePath: string, fileContent: string, isAot: boolean) {
55+
return getDeepLinkData(appNgModulePath, fileContent, isAot);
56+
}

src/optimization.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as optimization from './optimization';
2+
import * as decorators from './optimization/decorators';
3+
import * as treeshake from './optimization/treeshake';
4+
import * as helpers from './util/helpers';
5+
6+
import { FileCache } from './util/file-cache';
7+
8+
describe('optimization task', () => {
9+
describe('processStatsImpl', () => {
10+
it('should convert object graph to known module map', () => {
11+
// arrange
12+
const moduleOne = '/Users/dan/myModuleOne.js';
13+
const moduleTwo = '/Users/dan/myModuleTwo.js';
14+
const moduleThree = '/Users/dan/myModuleThree.js';
15+
const moduleFour = '/Users/dan/myModuleFour.js';
16+
const objectGraph: any = {
17+
modules: [
18+
{
19+
identifier: moduleOne,
20+
reasons: [
21+
{
22+
moduleIdentifier: moduleTwo
23+
},
24+
{
25+
moduleIdentifier: moduleThree
26+
}
27+
]
28+
},
29+
{
30+
identifier: moduleTwo,
31+
reasons: [
32+
{
33+
moduleIdentifier: moduleThree
34+
}
35+
]
36+
},
37+
{
38+
identifier: moduleThree,
39+
reasons: [
40+
{
41+
moduleIdentifier: moduleOne
42+
}
43+
]
44+
},
45+
{
46+
identifier: moduleFour,
47+
reasons: []
48+
}
49+
]
50+
};
51+
// act
52+
const result = optimization.processStatsImpl(objectGraph);
53+
54+
// assert
55+
const setOne = result.get(moduleOne);
56+
expect(setOne.has(moduleTwo)).toBeTruthy();
57+
expect(setOne.has(moduleThree)).toBeTruthy();
58+
59+
const setTwo = result.get(moduleTwo);
60+
expect(setTwo.has(moduleThree)).toBeTruthy();
61+
62+
const setThree = result.get(moduleThree);
63+
expect(setThree.has(moduleOne)).toBeTruthy();
64+
65+
const setFour = result.get(moduleFour);
66+
expect(setFour.size).toEqual(0);
67+
});
68+
});
69+
70+
describe('doOptimizations', () => {
71+
it('should not run optimizations unless flags are set', () => {
72+
// arrange
73+
const fileCache = new FileCache();
74+
fileCache.set('somePath', { path: 'somePath', content: 'someContent'});
75+
const context = {
76+
fileCache: fileCache
77+
};
78+
79+
spyOn(helpers, helpers.getBooleanPropertyValue.name).and.returnValue(false);
80+
spyOn(decorators, decorators.purgeDecorators.name);
81+
spyOn(treeshake, treeshake.calculateUnusedComponents.name);
82+
83+
// act
84+
const result = optimization.doOptimizations(context, null);
85+
86+
// assert
87+
expect(result).toEqual(null);
88+
expect(decorators.purgeDecorators).not.toHaveBeenCalled();
89+
expect(treeshake.calculateUnusedComponents).not.toHaveBeenCalled();
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)