-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2058 from GoogleChrome/typescript
Convert workbox-core to Typescript
- Loading branch information
Showing
51 changed files
with
1,357 additions
and
736 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
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
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
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,179 @@ | ||
/* | ||
Copyright 2019 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
const fs = require('fs-extra'); | ||
const gulp = require('gulp'); | ||
const ol = require('common-tags').oneLine; | ||
const path = require('path'); | ||
const {rollup} = require('rollup'); | ||
const resolve = require('rollup-plugin-node-resolve'); | ||
const typescript2 = require('rollup-plugin-typescript2'); | ||
const packageRunnner = require('./utils/package-runner'); | ||
const logHelper = require('../infra/utils/log-helper'); | ||
const {AsyncDebounce} = require('../infra/utils/AsyncDebounce'); | ||
|
||
|
||
/** | ||
* @type {string} | ||
*/ | ||
const packagesDir = path.join(__dirname, '..', 'packages'); | ||
|
||
|
||
/** | ||
* Returns a Rollup plugin that generates both `.js` and `.mjs` files for each | ||
* `.ts` source file. This is a bit of a hack, but it's needed until to solve: | ||
* https://github.com/rollup/rollup/issues/2847 | ||
*/ | ||
const generateMjsOutputFiles = () => { | ||
return { | ||
generateBundle: (options, bundle) => { | ||
const importPattern = | ||
new RegExp(`((?:import|export)[^']+')([^']+?)\\.ts';`, 'g'); | ||
|
||
for (const chunkInfo of Object.values(bundle)) { | ||
if (chunkInfo.fileName.endsWith('.ts')) { | ||
// Rename the `.ts` imports and filenames to `.js`; | ||
const assetBasename = chunkInfo.fileName.replace(/.ts$/, ''); | ||
|
||
chunkInfo.fileName = assetBasename + '.js'; | ||
chunkInfo.code = chunkInfo.code.replace(importPattern, `$1$2.js';`); | ||
|
||
// Create mirror `.mjs` files for each `.js` file. | ||
const mjsSource = | ||
`export * from './${path.basename(assetBasename)}.js';`; | ||
|
||
fs.outputFileSync( | ||
path.join(options.dir, assetBasename + '.mjs'), mjsSource); | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* @type {RollupCache} | ||
*/ | ||
let moduleBundleCache; | ||
|
||
/** | ||
* Transpiles a package's source TypeScript files to `.mjs` JavaScript files | ||
* in the root package directory, along with the corresponding definition | ||
* files. | ||
* | ||
* @param {string} packageName | ||
*/ | ||
const transpilePackage = async (packageName) => { | ||
const packagePath = path.join(packagesDir, packageName); | ||
const input = path.join(packagePath, 'src', 'index.ts'); | ||
|
||
const plugins = [ | ||
resolve(), | ||
typescript2(), | ||
generateMjsOutputFiles(), | ||
]; | ||
|
||
const bundle = await rollup({ | ||
input, | ||
plugins, | ||
cache: moduleBundleCache, | ||
preserveModules: true, | ||
// We don't need to tree shake the TS to JS conversion, as that'll happen | ||
// later, and we don't need to add to the transpilation time. | ||
treeshake: false, | ||
}); | ||
|
||
// Ensure rebuilds are as fast as possible in watch mode. | ||
moduleBundleCache = bundle.cache; | ||
|
||
await bundle.write({ | ||
dir: packagePath, | ||
format: 'esm', | ||
}); | ||
}; | ||
|
||
/** | ||
* Transpiles a package iff it has TypeScript source files. | ||
* | ||
* @param {string} packagePath | ||
*/ | ||
const transpilePackagesOrSkip = async (packagePath) => { | ||
// `packagePath` will be posix style because it comes from `glog()`. | ||
const packageName = packagePath.split('/').slice(-1)[0]; | ||
|
||
if (await fs.pathExists(path.join(packagePath, 'src'))) { | ||
await queueTranspile(packageName); | ||
} else { | ||
logHelper.log(ol`Skipping package '${packageName}' | ||
not yet converted to typescript.`); | ||
} | ||
}; | ||
|
||
/** | ||
* A mapping between each package name and its corresponding `AsyncDebounce` | ||
* instance of the `transpilePackage()` function. | ||
* | ||
* @type {{[key: string]: AsyncDebounce}} | ||
*/ | ||
const debouncedTranspilerMap = {}; | ||
|
||
/** | ||
* Takes a package name and schedules that package's source TypeScript code | ||
* to be converted to JavaScript. If a transpilation is already scheduled, it | ||
* won't be queued twice, so this function is safe to call as frequenty as | ||
* needed. | ||
* | ||
* @param {string} packageName | ||
*/ | ||
const queueTranspile = async (packageName) => { | ||
if (!debouncedTranspilerMap[packageName]) { | ||
debouncedTranspilerMap[packageName] = new AsyncDebounce(async () => { | ||
await transpilePackage(packageName); | ||
}); | ||
} | ||
await debouncedTranspilerMap[packageName].call(); | ||
}; | ||
|
||
/** | ||
* A mapping between package names and whether or not they have pending | ||
* file changes | ||
* | ||
* @type {{[key: string]: boolean}} | ||
*/ | ||
const pendingChangesMap = {}; | ||
|
||
/** | ||
* Returns true if a TypeScript file has been modified in the package since | ||
* the last time it was transpiled. | ||
* | ||
* @param {string} packageName | ||
*/ | ||
const needsTranspile = (packageName) => { | ||
return pendingChangesMap[packageName] === true; | ||
}; | ||
|
||
gulp.task('transpile-typescript', gulp.series(packageRunnner( | ||
'transpile-typescript', 'browser', transpilePackagesOrSkip))); | ||
|
||
gulp.task('transpile-typescript:watch', gulp.series(packageRunnner( | ||
'transpile-typescript', 'browser', transpilePackagesOrSkip))); | ||
|
||
gulp.task('transpile-typescript:watch', () => { | ||
const watcher = gulp.watch(`./${global.packageOrStar}/workbox-*/src/**/*.ts`); | ||
watcher.on('all', async (event, file) => { | ||
const changedPkg = path.relative(packagesDir, file).split(path.sep)[0]; | ||
|
||
pendingChangesMap[changedPkg] = true; | ||
await queueTranspile(changedPkg); | ||
pendingChangesMap[changedPkg] = false; | ||
}); | ||
}); | ||
|
||
module.exports = { | ||
queueTranspile, | ||
needsTranspile, | ||
}; |
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
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
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
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
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,27 @@ | ||
// IndexedDB | ||
// ------------------------------------------------------------------------- // | ||
|
||
interface IDBIndex { | ||
openCursor(range?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): IDBRequest<IDBCursorWithValue | null>; | ||
openKeyCursor(range?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): IDBRequest<IDBCursor | null>; | ||
} | ||
|
||
interface IDBObjectStore { | ||
openCursor(range?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): IDBRequest<IDBCursorWithValue | null>; | ||
openKeyCursor(query?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): IDBRequest<IDBCursor | null>; | ||
} | ||
|
||
// Service Worker | ||
// ------------------------------------------------------------------------- // | ||
|
||
interface Clients { | ||
claim(): 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; | ||
} |
Oops, something went wrong.