@ -34,17 +34,18 @@ export class Generator {
|
||||
configVersion: 1,
|
||||
timestamp: Date.now(),
|
||||
appData: config.appData,
|
||||
index: joinUrls(this.baseHref, config.index), assetGroups,
|
||||
index: joinUrls(this.baseHref, config.index),
|
||||
assetGroups,
|
||||
dataGroups: this.processDataGroups(config),
|
||||
hashTable: withOrderedKeys(unorderedHashTable),
|
||||
navigationUrls: processNavigationUrls(this.baseHref, config.navigationUrls),
|
||||
};
|
||||
}
|
||||
|
||||
private async processAssetGroups(config: Config, hashTable: {[file: string]: string | undefined}):
|
||||
private async processAssetGroups(config: Config, hashTable: {[file: string]: string|undefined}):
|
||||
Promise<Object[]> {
|
||||
const seenMap = new Set<string>();
|
||||
return Promise.all((config.assetGroups || []).map(async(group) => {
|
||||
return Promise.all((config.assetGroups || []).map(async (group) => {
|
||||
if ((group.resources as any).versionedFiles) {
|
||||
throw new Error(
|
||||
`Asset-group '${group.name}' in 'ngsw-config.json' uses the 'versionedFiles' option, ` +
|
||||
@ -58,7 +59,7 @@ export class Generator {
|
||||
matchedFiles.forEach(file => seenMap.add(file));
|
||||
|
||||
// Add the hashes.
|
||||
await matchedFiles.reduce(async(previous, file) => {
|
||||
await matchedFiles.reduce(async (previous, file) => {
|
||||
await previous;
|
||||
const hash = await this.fs.hash(file);
|
||||
hashTable[joinUrls(this.baseHref, file)] = hash;
|
||||
@ -143,8 +144,8 @@ function joinUrls(a: string, b: string): string {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
function withOrderedKeys<T extends{[key: string]: any}>(unorderedObj: T): T {
|
||||
const orderedObj = {} as{[key: string]: any};
|
||||
function withOrderedKeys<T extends {[key: string]: any}>(unorderedObj: T): T {
|
||||
const orderedObj = {} as {[key: string]: any};
|
||||
Object.keys(unorderedObj).sort().forEach(key => orderedObj[key] = unorderedObj[key]);
|
||||
return orderedObj as T;
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ export function globToRegex(glob: string, literalQuestionMark = false): string {
|
||||
const segments = glob.split('/').reverse();
|
||||
let regex: string = '';
|
||||
while (segments.length > 0) {
|
||||
const segment = segments.pop() !;
|
||||
const segment = segments.pop()!;
|
||||
if (segment === '**') {
|
||||
if (segments.length > 0) {
|
||||
regex += WILD_OPEN;
|
||||
|
@ -51,6 +51,8 @@ export interface DataGroup {
|
||||
urls: Glob[];
|
||||
version?: number;
|
||||
cacheConfig: {
|
||||
maxSize: number; maxAge: Duration; timeout?: Duration; strategy?: 'freshness' | 'performance';
|
||||
maxSize: number; maxAge: Duration;
|
||||
timeout?: Duration;
|
||||
strategy?: 'freshness' | 'performance';
|
||||
};
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import {MockFilesystem} from '../testing/mock';
|
||||
describe('Generator', () => {
|
||||
beforeEach(() => spyOn(Date, 'now').and.returnValue(1234567890123));
|
||||
|
||||
it('generates a correct config', async() => {
|
||||
it('generates a correct config', async () => {
|
||||
const fs = new MockFilesystem({
|
||||
'/index.html': 'This is a test',
|
||||
'/main.css': 'This is a CSS file',
|
||||
@ -125,7 +125,7 @@ describe('Generator', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('uses default `navigationUrls` if not provided', async() => {
|
||||
it('uses default `navigationUrls` if not provided', async () => {
|
||||
const fs = new MockFilesystem({
|
||||
'/index.html': 'This is a test',
|
||||
});
|
||||
@ -151,7 +151,7 @@ describe('Generator', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('throws if the obsolete `versionedFiles` is used', async() => {
|
||||
it('throws if the obsolete `versionedFiles` is used', async () => {
|
||||
const fs = new MockFilesystem({
|
||||
'/index.html': 'This is a test',
|
||||
'/main.js': 'This is a JS file',
|
||||
|
@ -12,17 +12,23 @@ import {Filesystem} from '../src/filesystem';
|
||||
export class MockFilesystem implements Filesystem {
|
||||
private files = new Map<string, string>();
|
||||
|
||||
constructor(files: {[name: string]: string | undefined}) {
|
||||
Object.keys(files).forEach(path => this.files.set(path, files[path] !));
|
||||
constructor(files: {[name: string]: string|undefined}) {
|
||||
Object.keys(files).forEach(path => this.files.set(path, files[path]!));
|
||||
}
|
||||
|
||||
async list(dir: string): Promise<string[]> {
|
||||
return Array.from(this.files.keys()).filter(path => path.startsWith(dir));
|
||||
}
|
||||
|
||||
async read(path: string): Promise<string> { return this.files.get(path) !; }
|
||||
async read(path: string): Promise<string> {
|
||||
return this.files.get(path)!;
|
||||
}
|
||||
|
||||
async hash(path: string): Promise<string> { return sha1(this.files.get(path) !); }
|
||||
async hash(path: string): Promise<string> {
|
||||
return sha1(this.files.get(path)!);
|
||||
}
|
||||
|
||||
async write(path: string, contents: string): Promise<void> { this.files.set(path, contents); }
|
||||
async write(path: string, contents: string): Promise<void> {
|
||||
this.files.set(path, contents);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user