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-core to Typescript #2058

Merged
merged 5 commits into from
Jun 13, 2019
Merged

Convert workbox-core to Typescript #2058

merged 5 commits into from
Jun 13, 2019

Conversation

philipwalton
Copy link
Member

R: @jeffposnick

This PR is an initial attempt at converting our packages to TypeScript (and doing so in a backwards compatible way). Here's a summary of what I've done:

  • All .mjs source files have been moved inside /src and renamed to .ts
  • Type definitions have been added to the .ts files, and a global type-overrides.d.ts was added for some missing IDB and SW types.
  • A new transpile-typescript gulp task has been added that converts .ts files in /src to .mjs files in the root directory (mimicking the current structure).
  • All other build tasks have been updated to run transpile-typescript prior to them doing their thing.

// Delete generate files from the the TypeScript transpile.
if (await fs.pathExists(path.join(packagePath, 'src', 'index.ts'))) {
const deletedPaths = await del([
path.join(packagePath, '**/*.+(mjs|d.ts)'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want path.posix.join() here, since I believe these patterns should always use /, event when run on Windows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might be right. I know that's true for node-glob, but I believe del uses globby, so it may be different?

'!**/node_modules', '!**/node_modules/**/*',
]);

const directoriesToDelete = new Set();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this bit of code is deleting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When TypeScript generates .mjs and .d.ts files, it does so keeping the same directory structure as was in /src, so any sub-directories will get copied to the root-level package directory, and this code deletes those.

I've added a comment to help better explain what this does.

return {
generateBundle: (options, bundle) => {
const importPattern =
new RegExp(`((?:import|export)[^']+')([^']+?)\\${oldExt}';`, 'g');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include " along with ' as the possible quote characters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we stick to our style guide, we shouldn't ever have double quotes. And since we're only matching our files, I think this is fine for now. Though I suppose if we add any third-party libraries (like maybe idb) then yeah, we'd have to change this.


/**
* Transpiles a package's source TypeScript files to `.mjs` JavaScript files
* in the root package directory, along with the correspdonging definition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correspdonging => corresponding

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

constants.PACKAGE_BUILD_DIRNAME);
return fse.remove(outputDirectory);
const cleanPackage = async (packagePath) => {
// Delete generate files from the the TypeScript transpile.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generate => generated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


import '../_version';

export interface Plugin {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add in some TypeScript definitions for the signatures of these various callbacks, and then say that each of those properties maps to the corresponding callback signature, instead of a generic Function?

(I'm happy to figure out the syntax for doing that if you're not sure, as I don't want to create more work for you.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely think we can, and consider this initial conversion the first step in what will likely be more changes as I learn more about Typescript definition capabilities.

My expectation is as we convert more packages over, it'll start to become clear how we can update these plugin definitions to be more useful for consumers of them.

@coveralls
Copy link

coveralls commented Jun 13, 2019

Coverage Status

Coverage increased (+0.02%) to 80.794% when pulling 25a699b on typescript into b10f13f on master.

@philipwalton
Copy link
Member Author

@jeffposnick I updated this PR with the changes we talked about today.

Now the transpile-typescript task will create a .js files for each source file, and it will also create a corresponding .mjs file that just re-exports any exports from the respective .js file:

// moduleName.mjs
export * from './moduleName.js';

This seemed like a good way to keep backwards compatibility with existing .mjs files while we wait for Typescript to officially support .mjs.

@philipwalton
Copy link
Member Author

Hmmm, looks like appveyor is failing with some weird npm install error. Any idea why that might be?

@jeffposnick
Copy link
Contributor

It looks like it's due to

    "jsdoc-baseline": "https://github.com/hegemonic/baseline/tarball/master",

in our package.json. I don't remember the history behind that, but do you want to try swapping that out for the latest release of https://www.npmjs.com/package/jsdoc-baseline? It was pushed out 21 days ago, so presumably it has whatever we previously needed to get from the GitHub source.

@philipwalton
Copy link
Member Author

🤦 why didn't I think of that!

@philipwalton philipwalton force-pushed the typescript branch 6 times, most recently from 01bfb85 to a0a8a92 Compare June 13, 2019 17:27
@workbox-pr-bot
Copy link
Collaborator

PR-Bot Size Plugin

Changed File Sizes

File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.81 KB 3.85 KB +1% 1.59 KB
packages/workbox-core/build/workbox-core.prod.js 5.88 KB 5.90 KB +0% 2.44 KB
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.89 KB 1.94 KB +3% 904 B
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.51 KB 1.54 KB +2% 763 B
packages/workbox-window/build/workbox-window.dev.umd.js 30.42 KB 30.38 KB -0% 8.09 KB

New Files

No new files have been added.

All File Sizes

View Table
File Before After Change GZipped
packages/workbox-background-sync/build/workbox-background-sync.prod.js 3.81 KB 3.85 KB +1% 1.59 KB
packages/workbox-broadcast-update/build/workbox-broadcast-update.prod.js 1.89 KB 1.89 KB 0% 944 B
packages/workbox-build/build/_types.js 41 B 41 B 0% 61 B
packages/workbox-build/build/index.js 3.64 KB 3.64 KB 0% 1.36 KB
packages/workbox-cacheable-response/build/workbox-cacheable-response.prod.js 579 B 579 B 0% 345 B
packages/workbox-cli/build/app.js 5.58 KB 5.58 KB 0% 1.98 KB
packages/workbox-cli/build/bin.js 1.16 KB 1.16 KB 0% 580 B
packages/workbox-core/build/workbox-core.prod.js 5.88 KB 5.90 KB +0% 2.44 KB
packages/workbox-expiration/build/workbox-expiration.prod.js 2.89 KB 2.89 KB 0% 1.25 KB
packages/workbox-google-analytics/build/workbox-offline-ga.prod.js 1.89 KB 1.94 KB +3% 904 B
packages/workbox-navigation-preload/build/workbox-navigation-preload.prod.js 652 B 652 B 0% 317 B
packages/workbox-precaching/build/workbox-precaching.prod.js 4.24 KB 4.24 KB 0% 1.70 KB
packages/workbox-range-requests/build/workbox-range-requests.prod.js 1.51 KB 1.54 KB +2% 763 B
packages/workbox-routing/build/workbox-routing.prod.js 3.40 KB 3.40 KB 0% 1.47 KB
packages/workbox-strategies/build/workbox-strategies.prod.js 4.86 KB 4.86 KB 0% 1.19 KB
packages/workbox-streams/build/workbox-streams.prod.js 1.38 KB 1.38 KB 0% 678 B
packages/workbox-sw/build/workbox-sw.js 1.33 KB 1.33 KB 0% 741 B
packages/workbox-webpack-plugin/build/generate-sw.js 5.29 KB 5.29 KB 0% 1.84 KB
packages/workbox-webpack-plugin/build/index.js 349 B 349 B 0% 255 B
packages/workbox-webpack-plugin/build/inject-manifest.js 7.22 KB 7.22 KB 0% 2.48 KB
packages/workbox-window/build/workbox-window.dev.umd.js 30.42 KB 30.38 KB -0% 8.09 KB
packages/workbox-window/build/workbox-window.prod.umd.js 4.54 KB 4.54 KB 0% 1.82 KB

Workbox Aggregate Size Plugin

6.85KB gzip'ed (46% of limit)
17.46KB uncompressed

Copy link
Contributor

@jeffposnick jeffposnick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cool with moving ahead with merging this PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants