Skip to content

Commit

Permalink
Migrate workbox-build to TypeScript (#2867)
Browse files Browse the repository at this point in the history
* WIP

* Finished getManifest

* GenerateSW migrated

* Getting there

* WIP

* Runtime caching TS

* More WIP

* More WIP

* Validation, type fixes

* Builds!

* No default exports

* null support in optional strings

* Accept string or buffer again

* Fix JSDocs

* CanIUse update

* test/additional-manifest-entries-transform

* lib/bundle

* cdnUtils test

* Switched to esModuleInterop

* Still WIP

* get-file-size migration

* get-string-details test migration

* A few more

* populateSWTemplate changes

* A few more

* validate options

* Update ts-auto-guard

* Fix dependency-check

* Swapped the exports

* JSON Schema + ajv

* TS 4.1.5

* Getting closer to validation

* Some default logic

* Switched default quotes

* Cleanup optional types

* Figuring out errors

* Refactor

* Update validate-options test

* Started integration test migration

* Remove joi schema

* Fixed some swDest logic

* Closer to webpack tests working

* Fix transformManifest usage

* Use a copy of the exclude array

* More include/exclude fun

* swDest default fix

* Function support?

* modifyURLPrefix workaround

* Fix an import

* Improve error messages

* Fix test string

* New validation unit test

* getManifest working

* Working through generateSW

* Function massaging

* Cleanup of exclude/include

* Fix a few more gSW tests

* RouteMatchCallback function

* Expiration w/cacheName check

* Optional globDirectory

* InjectManifest tests

* Test cleanup

* Fix a test

* Fix a config modification

* v5 injectmanifest test

* esModuleInterop support

* Fixes for proxied build

* Final tweaks

* Linting

* Automated linting fixes

* Tweak linting rules

* Update packages/workbox-build/src/lib/get-file-details.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update test/workbox-build/node/generate-sw.js

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/additional-manifest-entries-transform.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/additional-manifest-entries-transform.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/cdn-utils.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/get-file-manifest-entries.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/stringify-without-comments.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/validate-options.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

* Update packages/workbox-build/src/lib/additional-manifest-entries-transform.ts

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>

Co-authored-by: Adriana Jara <32825533+tropicadri@users.noreply.github.com>
  • Loading branch information
jeffposnick and tropicadri committed Jun 17, 2021
1 parent f6fb94a commit 77b1c2f
Show file tree
Hide file tree
Showing 119 changed files with 2,040 additions and 1,444 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ firebase-debug.log
# Generated TypeScript files and build data
tsconfig.tsbuildinfo
packages/workbox-*/**/*.d.ts
!packages/workbox-cli/src/index.d.ts
!packages/workbox-*/src/**/*.d.ts
packages/workbox-*/**/*.js
!packages/workbox-build/**/*.js
!packages/workbox-webpack-plugin/**/*.js
Expand Down
54 changes: 54 additions & 0 deletions gulp-tasks/build-node-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

const {parallel} = require('gulp');
const execa = require('execa');
const fse = require('fs-extra');
const TJS = require('typescript-json-schema');
const upath = require('upath');

const constants = require('./utils/constants');
Expand All @@ -28,7 +30,59 @@ async function buildNodePackage(packagePath) {
], {preferLocal: true});
}

async function generateWorkboxBuildJSONSchema(packagePath) {
const program = TJS.programFromConfig(upath.join(packagePath,
'tsconfig.json'));
const generator = TJS.buildGenerator(program, {
noExtraProps: true,
required: true,
});
const optionTypes = [
'GenerateSWOptions',
'GetManifestOptions',
'InjectManifestOptions',
'WebpackGenerateSWOptions',
'WebpackInjectManifestOptions',
];
for (const optionType of optionTypes) {
const schema = generator.getSchemaForSymbol(optionType);
// Ideally, we'd set typeOfKeyword so that functions could be represented.
// Instead, we need to hardcode a few overrides to deal with functions.
// See https://github.com/YousefED/typescript-json-schema/issues/424
if (schema.properties.manifestTransforms) {
schema.properties.manifestTransforms.items = {typeof: 'function'};
}
if (schema.properties.exclude) {
schema.properties.exclude.items.anyOf = [
{'$ref': '#/definitions/RegExp'},
{type: 'string'},
{typeof: 'function'},
];
}
if (schema.properties.include) {
schema.properties.include.items.anyOf = [
{'$ref': '#/definitions/RegExp'},
{type: 'string'},
{typeof: 'function'},
];
}
if (schema.definitions.RouteMatchCallback) {
delete schema.definitions.RouteMatchCallback.type;
delete schema.definitions.RouteMatchCallback.additionalProperties;
schema.definitions.RouteMatchCallback.typeof = 'function';
}
await fse.writeJSON(upath.join(packagePath, 'src', 'schema',
`${optionType}.json`), schema);
}
}

async function buildNodeTSPackage(packagePath) {
// Hardcode special logic for workbox-build, as it's the only package
// that requires JSON schema generation.
if (packagePath.endsWith('workbox-build')) {
await generateWorkboxBuildJSONSchema(packagePath);
}

await execa('tsc', ['-b', packagePath], {preferLocal: true});
}

Expand Down
12 changes: 7 additions & 5 deletions gulp-tasks/build-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ async function cleanPackage(packagePath) {
if (await fse.pathExists(upath.join(packagePath, 'src', 'index.ts'))) {
// Store the list of deleted files, so we can delete directories after.
const deletedPaths = await del([
upath.join(packagePath, '**/*.+(js|mjs|d.ts)'),
`${packagePath}/**/*.+(js|mjs|d.ts)`,
// Don't delete files in node_modules.
'!**/node_modules', '!**/node_modules/**/*',
'!**/node_modules/**/*',
// Don't delete anything under src.
`!${packagePath}/src/**/*`,
]);

// Any directories in `deletedPaths` that are top-level directories to the
Expand Down Expand Up @@ -57,12 +59,12 @@ module.exports = {
build_packages_clean: cleanSequence(),
build_packages: series(
cleanSequence(),
// This needs to be a series, not in parallel, so that there isn't a
// race condition with the terser nameCache.
series(build_sw_packages, build_window_packages),
parallel(
build_node_packages,
build_node_ts_packages,
// This needs to be a series, not in parallel, so that there isn't a
// race condition with the terser nameCache.
series(build_sw_packages, build_window_packages),
),
),
};
6 changes: 3 additions & 3 deletions gulp-tasks/utils/versioned-cdn-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
https://opensource.org/licenses/MIT.
*/

const {getCDNOrigin} = require(
'../../packages/workbox-build/src/lib/cdn-utils');
const cdn = require('../../packages/workbox-build/src/cdn-details.json');
const lernaPkg = require('../../lerna.json');

module.exports = () => `${getCDNOrigin()}/${lernaPkg.version}`;
module.exports = () => `${cdn.origin}/${cdn.bucketName}/${cdn.releasesDir}` +
`/${lernaPkg.version}`;
2 changes: 1 addition & 1 deletion infra/testing/webpack-build-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

function joinMessages(errorsOrWarnings) {
if ('message' in errorsOrWarnings[0]) {
if (errorsOrWarnings[0].message) {
return errorsOrWarnings.map((item) => item.message).join('\n');
} else {
return errorsOrWarnings.join('\n');
Expand Down
Loading

0 comments on commit 77b1c2f

Please sign in to comment.