This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(optimizations): only store ionic and src files in memory
only store ionic and src files in memory
- Loading branch information
1 parent
f380fc0
commit f51314f
Showing
2 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |