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: configurable excluded tags #97

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ interface VitePluginInspectorOptions {
* @default code (Visual Studio Code)
*/
launchEditor?: 'appcode' | 'atom' | 'atom-beta' | 'brackets' | 'clion' | 'code' | 'code-insiders' | 'codium' | 'emacs' | 'idea' | 'notepad++' | 'pycharm' | 'phpstorm' | 'rubymine' | 'sublime' | 'vim' | 'visualstudio' | 'webstorm'

/**
* Exclude .vue file tags from being treated as templates.
* By default, `style` and `i18n` tags are excluded.
*/
excludedTags?: string[]
}
```

Expand Down
6 changes: 6 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ interface VitePluginInspectorOptions {
* @default code (Visual Studio Code)
*/
launchEditor?: 'appcode' | 'atom' | 'atom-beta' | 'brackets' | 'clion' | 'code' | 'code-insiders' | 'codium' | 'emacs' | 'idea' | 'notepad++' | 'pycharm' | 'phpstorm' | 'rubymine' | 'sublime' | 'vim' | 'visualstudio' | 'webstorm'

/**
* Exclude .vue file tags from being treated as templates.
* By default, `style` and `i18n` tags are excluded.
*/
excludedTags?: string[]
}
```

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/compiler/template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import MagicString from 'magic-string'
import { parse as vueParse, transform as vueTransform } from '@vue/compiler-dom'
import { type PlainElementNode, parse as vueParse, transform as vueTransform } from '@vue/compiler-dom'
import { parse as babelParse, traverse as babelTraverse } from '@babel/core'
import vueJsxPlugin from '@vue/babel-plugin-jsx'
import typescriptPlugin from '@babel/plugin-transform-typescript'
Expand All @@ -16,16 +16,21 @@ interface CompileSFCTemplateOptions {
code: string
id: string
type: 'template' | 'jsx'
excludedTopLevelTags?: string[]
}
export async function compileSFCTemplate(
{ code, id, type }: CompileSFCTemplateOptions,
{ code, id, type, excludedTopLevelTags = [] }: CompileSFCTemplateOptions,
) {
const s = new MagicString(code)
const relativePath = normalizePath(path.relative(process.cwd(), id))
const result = await new Promise((resolve) => {
switch (type) {
case 'template': {
const ast = vueParse(code, { comments: true })

// Remove excluded top level tags - their content should not be mutated
ast.children = ast.children.filter((c: PlainElementNode) => !excludedTopLevelTags.includes(c.tag))

vueTransform(ast, {
nodeTransforms: [
(node) => {
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ export interface VitePluginInspectorOptions {
* @default process.env.LAUNCH_EDITOR ?? code (Visual Studio Code)
*/
launchEditor?: 'appcode' | 'atom' | 'atom-beta' | 'brackets' | 'clion' | 'code' | 'code-insiders' | 'codium' | 'emacs' | 'idea' | 'notepad++' | 'pycharm' | 'phpstorm' | 'rubymine' | 'sublime' | 'vim' | 'visualstudio' | 'webstorm' | 'rider' | string

/**
* Exclude .vue file tags from being treated as templates.
* By default, `style` and `i18n` tags are excluded.
*/
excludedTags?: string[]
}

const toggleComboKeysMap = {
Expand All @@ -136,6 +142,7 @@ export const DEFAULT_INSPECTOR_OPTIONS: VitePluginInspectorOptions = {
appendTo: '',
lazyLoad: false,
launchEditor: process.env.LAUNCH_EDITOR ?? 'code',
excludedTags: ['style', 'i18n'] as string[],
} as const

function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPECTOR_OPTIONS): PluginOption {
Expand Down Expand Up @@ -193,10 +200,10 @@ function VitePluginInspector(options: VitePluginInspectorOptions = DEFAULT_INSPE
const { filename, query } = parseVueRequest(id)

const isJsx = filename.endsWith('.jsx') || filename.endsWith('.tsx') || (filename.endsWith('.vue') && query.isJsx)
const isTpl = filename.endsWith('.vue') && query.type !== 'style' && !query.raw
const isTpl = filename.endsWith('.vue') && !normalizedOptions.excludedTags?.includes(query.type) && !query.raw

if (isJsx || isTpl)
return compileSFCTemplate({ code, id: filename, type: isJsx ? 'jsx' : 'template' })
return compileSFCTemplate({ code, id: filename, type: isJsx ? 'jsx' : 'template', excludedTopLevelTags: normalizedOptions.excludedTags })

if (!appendTo)
return
Expand Down
8 changes: 8 additions & 0 deletions packages/playground/vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ p
color: #35495d
cursor: pointer
</style>

<i18n lang="json">
{
"en": {
"somethingWithHtml": "This is <b>HTML</b> content"
}
}
</i18n>
6 changes: 6 additions & 0 deletions packages/unplugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ interface VitePluginInspectorOptions {
* @default code (Visual Studio Code)
*/
launchEditor?: 'appcode' | 'atom' | 'atom-beta' | 'brackets' | 'clion' | 'code' | 'code-insiders' | 'codium' | 'emacs' | 'idea' | 'notepad++' | 'pycharm' | 'phpstorm' | 'rubymine' | 'sublime' | 'vim' | 'visualstudio' | 'webstorm'

/**
* Exclude .vue file tags from being treated as templates.
* By default, `style` and `i18n` tags are excluded.
*/
excludedTags?: string[]
}
```

Expand Down