Skip to content

Commit

Permalink
fix: vue2 complier
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyinws committed Apr 22, 2024
1 parent bf40351 commit 58cf4ae
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 84 deletions.
6 changes: 4 additions & 2 deletions src/core/transform/compilers/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { CompileResult, Compiler, Context } from '../../../types'
import { vueCompiler } from './vue'
import { vue3Compiler } from './vue3'
import { vue2Compiler } from './vue2'
import { vanillaCompiler } from './vanilla'
import { svelteCompiler } from './svelte'

export const compilers: Record<Compiler, (context: Context) => Promise<CompileResult>> = {
vue: vueCompiler,
vue3: vue3Compiler,
vue2: vue2Compiler,
vanilla: vanillaCompiler,
svelte: svelteCompiler,
}
73 changes: 0 additions & 73 deletions src/core/transform/compilers/vue.ts

This file was deleted.

48 changes: 48 additions & 0 deletions src/core/transform/compilers/vue2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PLUGIN_NAME } from '../../constants'
import type { CompileResult, Context } from '../../../types'

export async function vue2Compiler(context: Context): Promise<CompileResult> {
try {
const { code, id } = context
const { parse } = await import('vue/compiler-sfc')

const compileResults = {
script: '',
line: 0,
offset: 0,
}

// @ts-expect-error vue2 compiler-sfc
const descriptor: any = parse({
source: code,
filename: id,
})

if (descriptor.errors.length === 0) {
if (descriptor.script) {
compileResults.script = descriptor.script.content
const offset = descriptor.script.start
const line = code.slice(0, offset).split('\n').length
compileResults.line = line
compileResults.offset = offset
}
else if (descriptor.scriptSetup) {
compileResults.script = descriptor.scriptSetup.content
const offset = descriptor.scriptSetup.start
const line = code.slice(0, offset).split('\n').length
compileResults.line = line
compileResults.offset = offset
}
}

return compileResults
}
catch (error) {
console.error(`[${PLUGIN_NAME}]`, error)
return {
script: '',
offset: 0,
line: 0,
}
}
}
45 changes: 45 additions & 0 deletions src/core/transform/compilers/vue3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { PLUGIN_NAME } from '../../constants'
import type { CompileResult, Context } from '../../../types'

export async function vue3Compiler(context: Context): Promise<CompileResult> {
try {
const { code, id } = context
const { parse } = await import('vue/compiler-sfc')

const compileResults = {
script: '',
line: 0,
offset: 0,
}

const { descriptor, errors } = parse(code, {
filename: id,
})

if (errors.length === 0) {
if (descriptor.script) {
compileResults.script = descriptor.script.content
const { line, offset } = descriptor.script.loc.start
compileResults.line = line - 1
compileResults.offset = offset
}

else if (descriptor.scriptSetup) {
compileResults.script = descriptor.scriptSetup.content
const { line, offset } = descriptor.scriptSetup.loc.start
compileResults.line = line - 1
compileResults.offset = offset
}
}

return compileResults
}
catch (error) {
console.error(`[${PLUGIN_NAME}]`, error)
return {
script: '',
offset: 0,
line: 0,
}
}
}
18 changes: 15 additions & 3 deletions src/core/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function transform(context: Context) {
const { code, id, options } = context
const magicString = new MagicString(code)

const compiler = getCompiler(id)
const compiler = await getCompiler(id)

if (!compiler) {
return {
Expand All @@ -31,7 +31,13 @@ export async function transform(context: Context) {
plugins: ['jsx', 'typescript'],
})

if (isPluginDisable({ comments: program.comments || [], originalLine: 1, id, type: 'top-file' })) {
if (isPluginDisable({
comments: program.comments || [],
originalLine: 1,
id,
type: 'top-file',
compiler,
})) {
return {
code: magicString.toString(),
map: magicString.generateMap({
Expand All @@ -58,7 +64,13 @@ export async function transform(context: Context) {
const originalLine = line + compileResult.line
const originalColumn = column

if (isPluginDisable({ comments: program.comments || [], originalLine: line, id, type: 'inline-file' }))
if (isPluginDisable({
comments: program.comments || [],
originalLine: line,
id,
type: 'inline-file',
compiler,
}))
return false

// @ts-expect-error any
Expand Down
25 changes: 20 additions & 5 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,28 @@ export function getExtendedPath(filePath: string, extendedPathFileNames?: string
return basename
}

export function getCompiler(id: string): Compiler | undefined {
export async function getCompiler(id: string): Promise<Compiler | undefined> {
const urlObject = new URL(id, 'file://')
const fileType = extname(urlObject.pathname)

switch (fileType) {
case '.vue':
return 'vue'
case '.vue': {
const Vue = await import('vue')
if (!Vue || typeof Vue.version !== 'string') {
console.warn(`[${PLUGIN_NAME}]: Vue is not installed`)
return undefined
}
else if (Vue.version.startsWith('2.')) {
return 'vue2'
}
else if (Vue.version.startsWith('3.')) {
return 'vue3'
}
else {
console.warn(`[${PLUGIN_NAME}]: Unsupported Vue version: ${Vue.version}`)
return undefined
}
}
case '.svelte':
return 'svelte'
case '.js':
Expand All @@ -87,14 +102,14 @@ export function isPluginDisable(meta: {
originalLine: number
id: string
type: 'top-file' | 'inline-file'
compiler: Compiler
}) {
const { comments, originalLine, id, type } = meta
const { comments, originalLine, type, compiler } = meta

if (comments?.length === 0)
return false

if (type === 'top-file') {
const compiler = getCompiler(id)
const startLine = compiler === 'vanilla' ? 1 : 2

const disablePluginComment = comments?.find(comment => comment.value.includes('turbo-console-disable'))
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface GenContext {
id: string
}

export type Compiler = 'vanilla' | 'vue' | 'svelte'
export type Compiler = 'vanilla' | 'vue3' | 'vue2' | 'svelte'

export interface Context {
code: string
Expand Down

0 comments on commit 58cf4ae

Please sign in to comment.