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

Commit f51314f

Browse files
committed
fix(optimizations): only store ionic and src files in memory
only store ionic and src files in memory
1 parent f380fc0 commit f51314f

File tree

2 files changed

+97
-7
lines changed

2 files changed

+97
-7
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { join } from 'path';
2+
3+
import { optimizationLoader } from './optimization-loader-impl';
4+
import * as helpers from '../util/helpers';
5+
import { FileCache } from '../util/file-cache';
6+
7+
describe('optimization loader impl', () => {
8+
describe('optimizationLoader', () => {
9+
it('should not cache files not in srcDir or ionicAngularDir', () => {
10+
const appDir = join('some', 'fake', 'path', 'myApp');
11+
const srcDir = join(appDir, 'src');
12+
const ionicAngularDir = join(appDir, 'node_modules', 'ionic-angular');
13+
const fileCache = new FileCache();
14+
const context = {
15+
srcDir: srcDir,
16+
ionicAngularDir: ionicAngularDir,
17+
fileCache: fileCache
18+
};
19+
20+
const spy = jasmine.createSpy('callback');
21+
const webpackContext = {
22+
cacheable: () => {},
23+
async: () => spy,
24+
resourcePath: join(appDir, 'node_modules', '@angular', 'core', 'index.js')
25+
};
26+
27+
spyOn(helpers, helpers.getContext.name).and.returnValue(context);
28+
29+
optimizationLoader('someSource', {}, webpackContext);
30+
31+
expect(fileCache.getAll().length).toEqual(0);
32+
});
33+
34+
it('should cache files in the ionicAngularDir', () => {
35+
const appDir = join('some', 'fake', 'path', 'myApp');
36+
const srcDir = join(appDir, 'src');
37+
const ionicAngularDir = join(appDir, 'node_modules', 'ionic-angular');
38+
const fileCache = new FileCache();
39+
const context = {
40+
srcDir: srcDir,
41+
ionicAngularDir: ionicAngularDir,
42+
fileCache: fileCache
43+
};
44+
45+
const spy = jasmine.createSpy('callback');
46+
const webpackContext = {
47+
cacheable: () => {},
48+
async: () => spy,
49+
resourcePath: join(ionicAngularDir, 'index.js')
50+
};
51+
52+
spyOn(helpers, helpers.getContext.name).and.returnValue(context);
53+
54+
const knownSource = 'someSource';
55+
optimizationLoader(knownSource, {}, webpackContext);
56+
57+
expect(fileCache.getAll().length).toEqual(1);
58+
expect(fileCache.get(webpackContext.resourcePath).path).toEqual(webpackContext.resourcePath);
59+
expect(fileCache.get(webpackContext.resourcePath).content).toEqual(knownSource);
60+
});
61+
62+
it('should cache files in the srcDir', () => {
63+
const appDir = join('some', 'fake', 'path', 'myApp');
64+
const srcDir = join(appDir, 'src');
65+
const ionicAngularDir = join(appDir, 'node_modules', 'ionic-angular');
66+
const fileCache = new FileCache();
67+
const context = {
68+
srcDir: srcDir,
69+
ionicAngularDir: ionicAngularDir,
70+
fileCache: fileCache
71+
};
72+
73+
const spy = jasmine.createSpy('callback');
74+
const webpackContext = {
75+
cacheable: () => {},
76+
async: () => spy,
77+
resourcePath: join(srcDir, 'pages', 'page-one.ts')
78+
};
79+
80+
spyOn(helpers, helpers.getContext.name).and.returnValue(context);
81+
82+
const knownSource = 'someSource';
83+
optimizationLoader(knownSource, {}, webpackContext);
84+
85+
expect(fileCache.getAll().length).toEqual(1);
86+
expect(fileCache.get(webpackContext.resourcePath).path).toEqual(webpackContext.resourcePath);
87+
expect(fileCache.get(webpackContext.resourcePath).content).toEqual(knownSource);
88+
});
89+
});
90+
});
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { normalize, resolve } from 'path';
2-
import { readAndCacheFile} from '../util/helpers';
2+
import { getContext } from '../util/helpers';
33
import { Logger } from '../logger/logger';
44

55
/* This loader is purely for caching stuff */
66

77
export function optimizationLoader(source: string, map: any, webpackContex: any) {
88
webpackContex.cacheable();
9+
const context = getContext();
910
var callback = webpackContex.async();
1011

1112
const absolutePath = resolve(normalize(webpackContex.resourcePath));
1213
Logger.debug(`[Webpack] optimization: processing the following file: ${absolutePath}`);
1314

14-
return readAndCacheFile(absolutePath).then((fileContent: string) => {
15-
callback(null, source, map);
16-
}).catch(err => {
17-
Logger.debug(`[Webpack] optimization: Encountered an unexpected error: ${err.message}`);
18-
callback(err);
19-
});
15+
if (absolutePath.indexOf(context.srcDir) >= 0 || absolutePath.indexOf(context.ionicAngularDir) >= 0) {
16+
Logger.debug(`[Webpack] optimization: Caching the following file: ${absolutePath}`);
17+
context.fileCache.set(webpackContex.resourcePath, { path: webpackContex.resourcePath, content: source});
18+
}
19+
return callback(null, source, map);
2020
}

0 commit comments

Comments
 (0)