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

Commit

Permalink
fix(optimizations): only store ionic and src files in memory
Browse files Browse the repository at this point in the history
only store ionic and src files in memory
  • Loading branch information
danbucholtz committed Mar 21, 2017
1 parent f380fc0 commit f51314f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
90 changes: 90 additions & 0 deletions src/webpack/optimization-loader-impl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { join } from 'path';

import { optimizationLoader } from './optimization-loader-impl';
import * as helpers from '../util/helpers';
import { FileCache } from '../util/file-cache';

describe('optimization loader impl', () => {
describe('optimizationLoader', () => {
it('should not cache files not in srcDir or ionicAngularDir', () => {
const appDir = join('some', 'fake', 'path', 'myApp');
const srcDir = join(appDir, 'src');
const ionicAngularDir = join(appDir, 'node_modules', 'ionic-angular');
const fileCache = new FileCache();
const context = {
srcDir: srcDir,
ionicAngularDir: ionicAngularDir,
fileCache: fileCache
};

const spy = jasmine.createSpy('callback');
const webpackContext = {
cacheable: () => {},
async: () => spy,
resourcePath: join(appDir, 'node_modules', '@angular', 'core', 'index.js')
};

spyOn(helpers, helpers.getContext.name).and.returnValue(context);

optimizationLoader('someSource', {}, webpackContext);

expect(fileCache.getAll().length).toEqual(0);
});

it('should cache files in the ionicAngularDir', () => {
const appDir = join('some', 'fake', 'path', 'myApp');
const srcDir = join(appDir, 'src');
const ionicAngularDir = join(appDir, 'node_modules', 'ionic-angular');
const fileCache = new FileCache();
const context = {
srcDir: srcDir,
ionicAngularDir: ionicAngularDir,
fileCache: fileCache
};

const spy = jasmine.createSpy('callback');
const webpackContext = {
cacheable: () => {},
async: () => spy,
resourcePath: join(ionicAngularDir, 'index.js')
};

spyOn(helpers, helpers.getContext.name).and.returnValue(context);

const knownSource = 'someSource';
optimizationLoader(knownSource, {}, webpackContext);

expect(fileCache.getAll().length).toEqual(1);
expect(fileCache.get(webpackContext.resourcePath).path).toEqual(webpackContext.resourcePath);
expect(fileCache.get(webpackContext.resourcePath).content).toEqual(knownSource);
});

it('should cache files in the srcDir', () => {
const appDir = join('some', 'fake', 'path', 'myApp');
const srcDir = join(appDir, 'src');
const ionicAngularDir = join(appDir, 'node_modules', 'ionic-angular');
const fileCache = new FileCache();
const context = {
srcDir: srcDir,
ionicAngularDir: ionicAngularDir,
fileCache: fileCache
};

const spy = jasmine.createSpy('callback');
const webpackContext = {
cacheable: () => {},
async: () => spy,
resourcePath: join(srcDir, 'pages', 'page-one.ts')
};

spyOn(helpers, helpers.getContext.name).and.returnValue(context);

const knownSource = 'someSource';
optimizationLoader(knownSource, {}, webpackContext);

expect(fileCache.getAll().length).toEqual(1);
expect(fileCache.get(webpackContext.resourcePath).path).toEqual(webpackContext.resourcePath);
expect(fileCache.get(webpackContext.resourcePath).content).toEqual(knownSource);
});
});
});
14 changes: 7 additions & 7 deletions src/webpack/optimization-loader-impl.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { normalize, resolve } from 'path';
import { readAndCacheFile} from '../util/helpers';
import { getContext } from '../util/helpers';
import { Logger } from '../logger/logger';

/* This loader is purely for caching stuff */

export function optimizationLoader(source: string, map: any, webpackContex: any) {
webpackContex.cacheable();
const context = getContext();
var callback = webpackContex.async();

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

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

0 comments on commit f51314f

Please sign in to comment.