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

fix(custom-element): correctly handle number type props in prod #8989

Merged
merged 23 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7306078
fix(custom-element): Correctly handle number type props in prod
baiwusanyu-c Aug 16, 2023
981852f
fix(custom-element): added unit test
baiwusanyu-c Aug 16, 2023
df02ef9
chore: remove console
baiwusanyu-c Aug 17, 2023
bd3b0f5
chore: remove expect
baiwusanyu-c Aug 17, 2023
ad7dd23
chore: updated complier
baiwusanyu-c Aug 18, 2023
057886a
chore: format code
baiwusanyu-c Aug 21, 2023
b0d7417
chore: added isCE option to compiler sfc ctx
baiwusanyu-c Aug 21, 2023
8f1da9a
chore: updated unit test
baiwusanyu-c Aug 21, 2023
cc0ff63
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Aug 21, 2023
4a651d1
chore: added customElement options in SFCScriptCompileOptions
baiwusanyu-c Aug 22, 2023
f070820
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Aug 22, 2023
26fd1e3
Merge remote-tracking branch 'origin/bwsy/fix/CENumProps' into bwsy/f…
baiwusanyu-c Aug 22, 2023
dbfaf97
chore: updated code
baiwusanyu-c Aug 22, 2023
916388e
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Sep 5, 2023
64f9c5d
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Oct 20, 2023
6d0c7b4
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Oct 26, 2023
d2d98e6
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Oct 31, 2023
af82b74
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Nov 1, 2023
a7d48fb
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Nov 10, 2023
d73f608
[autofix.ci] apply automated fixes
autofix-ci[bot] Nov 10, 2023
eb89b9e
Merge remote-tracking branch 'origin/main' into bwsy/fix/CENumProps
baiwusanyu-c Nov 13, 2023
3564fce
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Nov 30, 2023
3640d9f
Merge branch 'main' into bwsy/fix/CENumProps
baiwusanyu-c Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,66 @@ return { props, bar }
}"
`;

exports[`defineProps > custom element retains the props type & default value & production mode 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
interface Props {
foo?: number;
}

export default /*#__PURE__*/_defineComponent({
__name: 'app.ce',
props: {
foo: { default: 5.5, type: Number }
},
setup(__props: any, { expose: __expose }) {
__expose();

const props = __props;

return { props }
}

})"
`;

exports[`defineProps > custom element retains the props type & production mode 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

export default /*#__PURE__*/_defineComponent({
__name: 'app.ce',
props: {
foo: {type: Number}
},
setup(__props: any, { expose: __expose }) {
__expose();

const props = __props

return { props }
}

})"
`;

exports[`defineProps > custom element retains the props type w/ production mode 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

export default /*#__PURE__*/_defineComponent({
__name: 'app.ce',
props: {
foo: {type: Number}
},
setup(__props: any, { expose: __expose }) {
__expose();

const props = __props

return { props }
}

})"
`;

exports[`defineProps > defineProps w/ runtime options 1`] = `
"import { defineComponent as _defineComponent } from 'vue'

Expand Down
31 changes: 31 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,4 +608,35 @@ const props = defineProps({ foo: String })
}).toThrow(`cannot accept both type and non-type arguments`)
})
})

// #8989
test('custom element retains the props type & production mode', () => {
const { content } = compile(
`<script setup lang="ts">
const props = defineProps<{ foo: number}>()
</script>`,
{ isProd: true },
{ filename: 'app.ce.vue' }
)

expect(content).toMatch(`foo: {type: Number}`)
assertCode(content)
})

test('custom element retains the props type & default value & production mode', () => {
const { content } = compile(
`<script setup lang="ts">
interface Props {
foo?: number;
}
const props = withDefaults(defineProps<Props>(), {
foo: 5.5,
});
</script>`,
{ isProd: true },
{ filename: 'app.ce.vue' }
)
expect(content).toMatch(`foo: { default: 5.5, type: Number }`)
assertCode(content)
})
})
8 changes: 8 additions & 0 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ export interface SFCScriptCompileOptions {
* using it, disable this and switch to the [Vue Macros implementation](https://vue-macros.sxzz.moe/features/reactivity-transform.html).
*/
reactivityTransform?: boolean
/**
* Transform Vue SFCs into custom elements.
* - `true`: all `*.vue` imports are converted into custom elements
* - `RegExp`: matched files are converted into custom elements
*
* @default /\.ce\.vue$/
*/
customElement?: boolean | RegExp | RegExp[]
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
}

export interface ImportBinding {
Expand Down
21 changes: 20 additions & 1 deletion packages/compiler-sfc/src/script/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CallExpression, Node, ObjectPattern, Program } from '@babel/types'
import { SFCDescriptor } from '../parse'
import { generateCodeFrame } from '@vue/shared'
import { generateCodeFrame, isArray, isRegExp } from '@vue/shared'
import { parse as babelParse, ParserPlugin } from '@babel/parser'
import { ImportBinding, SFCScriptCompileOptions } from '../compileScript'
import { PropsDestructureBindings } from './defineProps'
Expand All @@ -12,6 +12,7 @@ import { TypeScope } from './resolveType'
export class ScriptCompileContext {
isJS: boolean
isTS: boolean
isCE = false

scriptAst: Program | null
scriptSetupAst: Program | null
Expand Down Expand Up @@ -95,6 +96,24 @@ export class ScriptCompileContext {
scriptSetupLang === 'ts' ||
scriptSetupLang === 'tsx'

const customElement = options.customElement
const filename = this.descriptor.filename

let isCE = false
if (typeof customElement === 'boolean') {
isCE = customElement
} else if (isRegExp(customElement)) {
isCE = customElement.test(filename)
} else if (isArray(customElement)) {
isCE = customElement.some(
pattern => isRegExp(pattern) && pattern.test(filename)
)
} else {
isCE = /\.ce\.vue$/.test(filename)
}

this.isCE = isCE

// resolve parser plugins
const plugins: ParserPlugin[] = resolveParserPlugins(
(scriptLang || scriptSetupLang)!,
Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ function genRuntimePropFromType(
defaultString
])} }`
} else {
// #8989 for custom element, should keep the type
if (ctx.isCE) {
if (defaultString) {
return `${finalKey}: ${`{ ${defaultString}, type: ${toRuntimeTypeString(
type
)} }`}`
} else {
return `${finalKey}: {type: ${toRuntimeTypeString(type)}}`
}
}

// production: checks are useless
return `${finalKey}: ${defaultString ? `{ ${defaultString} }` : `{}`}`
}
Expand Down
Loading