From f51314f534c7567a54a10ae487be5d434687399f Mon Sep 17 00:00:00 2001 From: Dan Bucholtz Date: Tue, 21 Mar 2017 00:14:02 -0500 Subject: [PATCH] fix(optimizations): only store ionic and src files in memory only store ionic and src files in memory --- src/webpack/optimization-loader-impl.spec.ts | 90 ++++++++++++++++++++ src/webpack/optimization-loader-impl.ts | 14 +-- 2 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 src/webpack/optimization-loader-impl.spec.ts diff --git a/src/webpack/optimization-loader-impl.spec.ts b/src/webpack/optimization-loader-impl.spec.ts new file mode 100644 index 00000000..c9e1565f --- /dev/null +++ b/src/webpack/optimization-loader-impl.spec.ts @@ -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); + }); + }); +}); diff --git a/src/webpack/optimization-loader-impl.ts b/src/webpack/optimization-loader-impl.ts index 2cc7d9f0..a4c82228 100644 --- a/src/webpack/optimization-loader-impl.ts +++ b/src/webpack/optimization-loader-impl.ts @@ -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); }