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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/plugin-vue/src/index.ts
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
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,
customElement: 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,
customElement,
)
} else {
if (template.src) {
Expand Down Expand Up @@ -309,14 +313,15 @@ async function genScriptCode(
options: ResolvedOptions,
pluginContext: PluginContext,
ssr: boolean,
customElement: 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, customElement)
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
Expand Up @@ -48,6 +48,7 @@ export function resolveScript(
descriptor: SFCDescriptor,
options: ResolvedOptions,
ssr: boolean,
customElement: boolean,
): 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,
})

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

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