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(plugin-vue): add customElement option to compiler #238

Merged
merged 16 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
9 changes: 8 additions & 1 deletion packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,14 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
: getDescriptor(filename, options)!

if (query.type === 'template') {
return transformTemplateAsModule(code, descriptor, options, this, ssr)
return transformTemplateAsModule(
code,
descriptor,
options,
this,
ssr,
customElementFilter(filename),
)
} else if (query.type === 'style') {
return transformStyle(
code,
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export async function transformMain(
options,
pluginContext,
ssr,
asCustomElement,
sxzz marked this conversation as resolved.
Show resolved Hide resolved
)

// template
Expand All @@ -75,6 +76,7 @@ export async function transformMain(
options,
pluginContext,
ssr,
asCustomElement,
))
}

Expand Down Expand Up @@ -262,6 +264,7 @@ async function genTemplateCode(
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean,
asCustomElement: boolean,
) {
const template = descriptor.template!
const hasScoped = descriptor.styles.some((style) => style.scoped)
Expand All @@ -276,6 +279,7 @@ async function genTemplateCode(
options,
pluginContext,
ssr,
asCustomElement,
)
} else {
if (template.src) {
Expand Down Expand Up @@ -309,14 +313,15 @@ async function genScriptCode(
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean,
asCustomElement: boolean,
): Promise<{
code: string
map: RawSourceMap | undefined
}> {
let scriptCode = `const ${scriptIdentifier} = {}`
let map: RawSourceMap | undefined

const script = resolveScript(descriptor, options, ssr)
const script = resolveScript(descriptor, options, ssr, asCustomElement)
if (script) {
// If the script is js/ts and has no external src, it can be directly placed
// in the main module.
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin-vue/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function resolveScript(
descriptor: SFCDescriptor,
options: ResolvedOptions,
ssr: boolean,
asCustomElement: boolean,
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
): SFCScriptBlock | null {
if (!descriptor.script && !descriptor.scriptSetup) {
return null
Expand All @@ -72,6 +73,7 @@ export function resolveScript(
genDefaultAs: canInlineMain(descriptor, options)
? scriptIdentifier
: undefined,
customElement: asCustomElement,
baiwusanyu-c marked this conversation as resolved.
Show resolved Hide resolved
})

if (!options.isProduction && resolved?.deps) {
Expand Down
23 changes: 20 additions & 3 deletions packages/plugin-vue/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ export async function transformTemplateAsModule(
options: ResolvedOptions,
pluginContext: TransformPluginContext,
ssr: boolean,
asCustomElement: boolean,
): Promise<{
code: string
map: any
}> {
const result = compile(code, descriptor, options, pluginContext, ssr)
const result = compile(
code,
descriptor,
options,
pluginContext,
ssr,
asCustomElement,
)

let returnCode = result.code
if (
Expand Down Expand Up @@ -50,8 +58,16 @@ export function transformTemplateInMain(
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean,
asCustomElement: boolean,
): SFCTemplateCompileResults {
const result = compile(code, descriptor, options, pluginContext, ssr)
const result = compile(
code,
descriptor,
options,
pluginContext,
ssr,
asCustomElement,
)
return {
...result,
code: result.code.replace(
Expand All @@ -68,9 +84,10 @@ export function compile(
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean,
asCustomElement: boolean,
) {
const filename = descriptor.filename
resolveScript(descriptor, options, ssr)
resolveScript(descriptor, options, ssr, asCustomElement)
const result = options.compiler.compileTemplate({
...resolveTemplateCompilerOptions(descriptor, options, ssr)!,
source: code,
Expand Down