-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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,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, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,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, | ||
} | ||
} | ||
} |
This file contains 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,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, | ||
} | ||
} | ||
} |
This file contains 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 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 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