-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
fix: update the .vue
file shim for Vue 3
#5903
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
5 changes: 5 additions & 0 deletions
5
packages/@vue/cli-plugin-typescript/codemods/__testfixtures__/shims-vue.input.ts
This file contains hidden or 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,5 @@ | ||
declare module '*.vue' { | ||
import { defineComponent } from 'vue'; | ||
const component: ReturnType<typeof defineComponent>; | ||
export default component; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/@vue/cli-plugin-typescript/codemods/__testfixtures__/shims-vue.output.ts
This file contains hidden or 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,5 @@ | ||
declare module '*.vue' { | ||
import { DefineComponent } from 'vue'; | ||
const component: DefineComponent; | ||
export default component; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/@vue/cli-plugin-typescript/codemods/__tests__/migrateComponentType.spec.js
This file contains hidden or 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,5 @@ | ||
jest.autoMockOff() | ||
|
||
const { defineTest } = require('jscodeshift/dist/testUtils') | ||
|
||
defineTest(__dirname, 'migrateComponentType', null, 'shims-vue', { parser: 'ts' }) |
93 changes: 93 additions & 0 deletions
93
packages/@vue/cli-plugin-typescript/codemods/migrateComponentType.js
This file contains hidden or 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,93 @@ | ||
// `shims-vue.d.ts` for Vue 3, generated by CLI 4.5.0-4.5.6, uses the following declaration: | ||
// `component: ReturnType<typeof defineComponent>` | ||
|
||
// So needs to update to: | ||
// `component: DefineComponent` | ||
|
||
module.exports = function migrateComponentType (file, api) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
const useDoubleQuote = root.find(j.StringLiteral).some(({ node }) => node.extra.raw.startsWith('"')) | ||
|
||
const tsmodule = root.find(j.TSModuleDeclaration, { | ||
id: { | ||
value: '*.vue' | ||
} | ||
}) | ||
|
||
const componentDecl = tsmodule.find(j.VariableDeclarator, { | ||
id: { | ||
name: 'component', | ||
typeAnnotation: { | ||
typeAnnotation: { | ||
typeName: { | ||
name: 'ReturnType' | ||
}, | ||
typeParameters: { | ||
params: { | ||
0: { | ||
exprName: { | ||
name: 'defineComponent' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
if (componentDecl.length !== 1) { | ||
return file.source | ||
} | ||
|
||
// update the component type | ||
componentDecl.forEach(({ node }) => { | ||
node.id.typeAnnotation = j.tsTypeAnnotation( | ||
j.tsTypeReference(j.identifier('DefineComponent')) | ||
) | ||
}) | ||
|
||
// insert DefineComponent type import | ||
const importDeclFromVue = tsmodule.find(j.ImportDeclaration, { | ||
source: { | ||
value: 'vue' | ||
} | ||
}) | ||
importDeclFromVue | ||
.get(0) | ||
.node.specifiers.push(j.importSpecifier(j.identifier('DefineComponent'))) | ||
|
||
// remove defineComponent import if unused | ||
const defineComponentUsages = tsmodule | ||
.find(j.Identifier, { name: 'defineComponent' }) | ||
.filter((identifierPath) => { | ||
const parent = identifierPath.parent.node | ||
|
||
// Ignore the import specifier | ||
if ( | ||
j.ImportDefaultSpecifier.check(parent) || | ||
j.ImportSpecifier.check(parent) || | ||
j.ImportNamespaceSpecifier.check(parent) | ||
) { | ||
return false | ||
} | ||
}) | ||
if (defineComponentUsages.length === 0) { | ||
tsmodule | ||
.find(j.ImportSpecifier, { | ||
local: { | ||
name: 'defineComponent' | ||
} | ||
}) | ||
.remove() | ||
} | ||
|
||
return root.toSource({ | ||
lineTerminator: '\n', | ||
quote: useDoubleQuote ? 'double' : 'single' | ||
}) | ||
} | ||
|
||
module.exports.parser = 'ts' |
4 changes: 2 additions & 2 deletions
4
packages/@vue/cli-plugin-typescript/generator/template-vue3/src/shims-vue.d.ts
This file contains hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
declare module '*.vue' { | ||
import { defineComponent } from 'vue' | ||
const component: ReturnType<typeof defineComponent> | ||
import type { DefineComponent } from 'vue' | ||
const component: DefineComponent | ||
export default component | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pikax @cexbrayat
Just to make sure I get it right.
Is this the recommended way to shim
.vue
files now in Vue 3 stable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you have something weird with
import type
, but otherwise yes this is what I've been using:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "something weird with
import type
" mean?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if I wasn't clear: I don't think
import type
is necessary here, asDefineComponent
is just a type, so there is no tree-shaking issue. I'm fine if you prefer to keep it, I was just pointing out that the syntax is a bit more advanced, and might lose some developers not familiar with the latest TS featuresThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Now that it's only a style issue then I'll keep it. Because now Vue CLI scaffolds with TS 3.9+, which supports this syntax. And the
import type
syntax is quite self-explaining so that people won't mistakenly takeDefineComponent
as a value, making the code a little clearer.