Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert workbox-navigation-preload to Typescript #2097

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ packages/workbox-broadcast-update/**/*.mjs
packages/workbox-cacheable-response/**/*.mjs
packages/workbox-core/**/*.mjs
packages/workbox-expiration/**/*.mjs
packages/workbox-navigation-preload/**/*.mjs
packages/workbox-window/**/*.mjs
18 changes: 1 addition & 17 deletions infra/type-overrides.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,9 @@ interface IDBObjectStore {
openKeyCursor(query?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): IDBRequest<IDBCursor | null>;
}

// Service Worker
// DOM
// ------------------------------------------------------------------------- //

interface Clients {
claim(): Promise<any>;
matchAll(): Promise<any>;
}

// NOTE(philipwalton): we need to use `WorkerGlobalScope` here because
// TypeScript does not allow us to re-declare self to a different type, and
// currently TypeScript only has a webworker types files (no SW types).
interface WorkerGlobalScope {
clients: Clients;
skipWaiting(): void;
registration: {
sync: SyncManager;
};
}

// Copied from lib.dom.iterable.d.ts for codes that don't include DOM typings:
// https://github.com/microsoft/TypeScript/blob/00bf32ca3967b07e8663d0cd2b3e2bbf572da88b/lib/lib.dom.iterable.d.ts#L103-L117
interface Headers {
Expand Down
3 changes: 3 additions & 0 deletions packages/workbox-background-sync/src/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {StorableRequest} from './lib/StorableRequest.js';
import './_version.js';


// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

const TAG_PREFIX = 'workbox-background-sync';
const MAX_RETENTION_TIME = 60 * 24 * 7; // 7 days in minutes

Expand Down
4 changes: 4 additions & 0 deletions packages/workbox-broadcast-update/src/broadcastUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {assert} from 'workbox-core/_private/assert.js';
import {CACHE_UPDATED_MESSAGE_TYPE, CACHE_UPDATED_MESSAGE_META} from './utils/constants.js';
import './_version.js';


// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

export interface BroadcastUpdateOptions {
cacheName: string;
url: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/workbox-core/src/clientsClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import './_version.js';


// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

/**
* Claim any currently available clients once the service worker
* becomes active. This is normally used in conjunction with `skipWaiting()`.
Expand Down
3 changes: 3 additions & 0 deletions packages/workbox-core/src/skipWaiting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import './_version.js';


// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

/**
* Force a service worker to become active, instead of waiting. This is
* normally used in conjunction with `clientsClaim()`.
Expand Down
1 change: 0 additions & 1 deletion packages/workbox-navigation-preload/_version.mjs

This file was deleted.

3 changes: 2 additions & 1 deletion packages/workbox-navigation-preload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
"browserNamespace": "workbox.navigationPreload",
"packageType": "browser"
},
"main": "build/workbox-navigation-preload.prod.js",
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"dependencies": {
"workbox-core": "^4.3.1"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/workbox-navigation-preload/src/_version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-ignore
try{self['workbox:navigation-preload:4.3.1']&&_()}catch(e){}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
https://opensource.org/licenses/MIT.
*/

import {logger} from 'workbox-core/_private/logger.mjs';
import {logger} from 'workbox-core/_private/logger.js';
import {isSupported} from './isSupported.js';
import './_version.js';

import {isSupported} from './isSupported.mjs';

import './_version.mjs';
// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

/**
* If the browser supports Navigation Preload, then this will disable it.
Expand All @@ -19,7 +21,7 @@ import './_version.mjs';
*/
function disable() {
if (isSupported()) {
self.addEventListener('activate', (event) => {
self.addEventListener('activate', (event: ExtendableEvent) => {
event.waitUntil(
self.registration.navigationPreload.disable().then(() => {
if (process.env.NODE_ENV !== 'production') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
https://opensource.org/licenses/MIT.
*/

import {logger} from 'workbox-core/_private/logger.mjs';
import {logger} from 'workbox-core/_private/logger.js';
import {isSupported} from './isSupported.js';
import './_version.js';

import {isSupported} from './isSupported.mjs';

import './_version.mjs';
// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

/**
* If the browser supports Navigation Preload, then this will enable it.
Expand All @@ -22,9 +24,9 @@ import './_version.mjs';
*
* @memberof workbox.navigationPreload
*/
function enable(headerValue) {
function enable(headerValue: string) {
if (isSupported()) {
self.addEventListener('activate', (event) => {
self.addEventListener('activate', (event: ExtendableEvent) => {
event.waitUntil(
self.registration.navigationPreload.enable().then(() => {
// Defaults to Service-Worker-Navigation-Preload: true if not set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
https://opensource.org/licenses/MIT.
*/

import {disable} from './disable.mjs';
import {enable} from './enable.mjs';
import {isSupported} from './isSupported.mjs';
import './_version.mjs';
import {disable} from './disable.js';
import {enable} from './enable.js';
import {isSupported} from './isSupported.js';
import './_version.js';


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
https://opensource.org/licenses/MIT.
*/

import './_version.mjs';
import './_version.js';


// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;

/**
* @return {boolean} Whether or not the current browser supports enabling
* navigation preload.
*
* @memberof workbox.navigationPreload
*/
function isSupported() {
function isSupported(): boolean {
return Boolean(self.registration && self.registration.navigationPreload);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/workbox-navigation-preload/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "./",
"rootDir": "./src"
},
"include": [
"src/**/*.ts"
]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{ "path": "./packages/workbox-cacheable-response/" },
{ "path": "./packages/workbox-core/" },
{ "path": "./packages/workbox-expiration/" },
{ "path": "./packages/workbox-navigation-preload/" },
{ "path": "./packages/workbox-window/" }
]
}