This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dev-only component (#7950)
- Loading branch information
1 parent
8e1bcae
commit b010e3e
Showing
4 changed files
with
85 additions
and
0 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
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,11 @@ | ||
import { defineComponent } from 'vue' | ||
|
||
export default defineComponent({ | ||
name: 'DevOnly', | ||
setup (_, props) { | ||
if (process.dev) { | ||
return () => props.slots.default?.() | ||
} | ||
return () => null | ||
} | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { pathToFileURL } from 'node:url' | ||
import { stripLiteral } from 'strip-literal' | ||
import { parseQuery, parseURL } from 'ufo' | ||
import MagicString from 'magic-string' | ||
import { createUnplugin } from 'unplugin' | ||
|
||
interface DevOnlyPluginOptions { | ||
sourcemap?: boolean | ||
} | ||
|
||
export const DevOnlyPlugin = createUnplugin((options: DevOnlyPluginOptions) => { | ||
const DEVONLY_COMP_RE = /<dev-?only>(:?[\s\S]*)<\/dev-?only>/gmi | ||
|
||
return { | ||
name: 'nuxt:server-devonly:transfrom', | ||
enforce: 'pre', | ||
transformInclude (id) { | ||
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href)) | ||
const { type } = parseQuery(search) | ||
|
||
// vue files | ||
if (pathname.endsWith('.vue') && (type === 'template' || !search)) { | ||
return true | ||
} | ||
}, | ||
transform (code, id) { | ||
if (!code.match(DEVONLY_COMP_RE)) { return } | ||
|
||
const s = new MagicString(code) | ||
const strippedCode = stripLiteral(code) | ||
for (const match of strippedCode.matchAll(DEVONLY_COMP_RE) || []) { | ||
s.remove(match.index!, match.index! + match[0].length) | ||
} | ||
|
||
if (s.hasChanged()) { | ||
return { | ||
code: s.toString(), | ||
map: options.sourcemap | ||
? s.generateMap({ source: id, includeContent: true }) | ||
: undefined | ||
} | ||
} | ||
} | ||
} | ||
}) |