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

feat(cli,cli-service,cli-test-utils): add ts declaration #5356

Merged
merged 14 commits into from Jul 15, 2020

Conversation

fangbinwei
Copy link
Collaborator

@fangbinwei fangbinwei commented Apr 5, 2020

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Docs
  • Underlying tools
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

Other information:
for issue #5324 , I have added some ts declarations

  • GeneratorAPI, PromptModuleAPI in @vue/cli
  • PluginAPI in @vue/cli-service
  • test utilities function declaration in @vue/cli-test-utils

I added test for these declarations. Also I linted the code by dtslint with some necessary rules according to the README of DefinitelyTyped .

test by npx tsc -p packages/@vue/cli/types/tsconfig.json

known issue

It seems there are some problem in webpack's declaration. so "skipLibCheck": true should be set in tsconfig.json when use PluginAPI in .ts.


By the way, I formated and linted the codes whithout adding config file (like tslint.json, .prettierrc) to vue-cli repo. Besides, vscode may report error for *.d.ts caused by eslint parser of cli repo. I believe I should avoid adding extra config files/codes or changing eslintignore. Core maintaner do this will be better, if necessary.

Add I used yarn link to verify the declarations of @vue/cli, @vue/cli-service, @vue/cli-test-utils in my own plugin repo.

  • vscode can finish code completion even in .js file according declaration of @vue/cli-test-utils
  • PluginAPI and GeneratorAPI can be used in .ts file normally

I hope this pr will be helpful.

appendix

for @vue/cli-test-utils,I added declaration to cli-test-utils folder directly, like createTestProject.d.ts

We can organize the file structure better, but It may cause break change. Or there is good way I overlooked.

@fangbinwei
Copy link
Collaborator Author

known issue
It seems there are some problem in webpack's declaration. so "skipLibCheck": true should be set in tsconfig.json when use PluginAPI in .ts

The problem come from here

import { WebpackOptions } from 'webpack/declarations/WebpackOptions'

if I change code to

import { Configuration as WebpackOptions } from 'webpack' // it will use @types/webpack

tsc -p packages/@vue/cli-service/types/tsconfig.json will be ok without "skipLibCheck": true

The key to the problem is which to use, @types/webpack or webpack/declarations/WebpackOptions

revert yarn.lock, use registry.yarnpkg.com instead of registry.npmjs.org
1. rename GeneratorRootOptions to Preset.
2. fix wrong use of type object, use Record<string,any>
instead.
3. support function type of plugin.
1. use @types/webpack instead of webpack/declarations.
2. add 'export {}' to the module to shut off
automatic exporting. see
https://stackoverflow.com/questions/52583603/intentional-that-export-shuts-off-automatic-export-of-all-symbols-from-a-ty.
3.
add reference node, so @types/node can be included even typeRoots/types is specified.
@fangbinwei
Copy link
Collaborator Author

Usage in *.js

maybe it's worth mentioning in the documentation, vscode can provide completions in this way.

service plugin

index.js

/** @type {import('@vue/cli-service').ServicePlugin} */
module.exports = (api, options) => {

}

generator plugin

generator/index.js

/** @type {import('@vue/cli').GeneratorPlugin} */
module.exports = (api) => {
}

project config

vue.config.js

/** @type {import('@vue/cli-service').ProjectOptions} */
module.exports = {
  // options...
}

https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html

Copy link
Member

@sodatea sodatea left a comment

Choose a reason for hiding this comment

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

Thanks for the great help!

Please also fix the merge conflicts with the dev branch and I think we'll be good to go.

packages/@vue/cli-service/types/index.d.ts Outdated Show resolved Hide resolved
packages/@vue/cli-service/types/index.d.ts Outdated Show resolved Hide resolved
packages/@vue/cli-test-utils/createServer.d.ts Outdated Show resolved Hide resolved
@sodatea sodatea merged commit 725ca41 into vuejs:dev Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants