@varlet/vite-plugins 3.8.7 → 3.8.8-alpha.1740224609678

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/vite-plugins",
3
- "version": "3.8.7",
3
+ "version": "3.8.8-alpha.1740224609678",
4
4
  "description": "vite plugins of varlet",
5
5
  "keywords": [
6
6
  "vite",
@@ -20,13 +20,16 @@
20
20
  "type": "module",
21
21
  "main": "lib/index.js",
22
22
  "module": "lib/index.js",
23
+ "files": [
24
+ "lib"
25
+ ],
23
26
  "dependencies": {
24
27
  "ejs": "^3.1.8",
25
28
  "fs-extra": "^9.0.1",
26
29
  "highlight.js": "^10.7.2",
27
30
  "markdown-it": "^12.2.3",
28
31
  "pinyin-pro": "3.17.0",
29
- "@varlet/shared": "3.8.7"
32
+ "@varlet/shared": "3.8.8-alpha.1740224609678"
30
33
  },
31
34
  "devDependencies": {
32
35
  "@types/ejs": "^3.1.1",
@@ -34,8 +37,10 @@
34
37
  "@types/markdown-it": "^12.2.3",
35
38
  "@types/node": "^18.7.20",
36
39
  "tsup": "7.2.0",
37
- "typescript": "5.3.3",
38
- "vite": "5.4.6"
40
+ "typescript": "5.3.3"
41
+ },
42
+ "peerDependencies": {
43
+ "vite": "6.1.1"
39
44
  },
40
45
  "scripts": {
41
46
  "build": "tsup src/index.ts --format esm --out-dir=lib --dts --clean",
package/src/copy.ts DELETED
@@ -1,30 +0,0 @@
1
- import fse from 'fs-extra'
2
- import type { Plugin } from 'vite'
3
-
4
- const { copySync, copyFileSync } = fse
5
-
6
- export interface CopyPath {
7
- from: string
8
- to: string
9
- type: 'folder' | 'file'
10
- }
11
-
12
- export interface CopyOptions {
13
- paths: CopyPath[]
14
- }
15
-
16
- export function copy(options: CopyOptions): Plugin {
17
- return {
18
- name: 'vite-plugin-varlet-copy',
19
-
20
- buildStart() {
21
- options.paths.forEach((copyPath) => {
22
- try {
23
- ;(copyPath.type === 'folder' ? copySync : copyFileSync)(copyPath.from, copyPath.to)
24
- } catch (e: any) {
25
- this.warn(e)
26
- }
27
- })
28
- },
29
- }
30
- }
package/src/html.ts DELETED
@@ -1,20 +0,0 @@
1
- import ejs from 'ejs'
2
- import type { Plugin } from 'vite'
3
-
4
- export interface HtmlOptions {
5
- data?: Record<string, string | string[] | Record<string, any>>
6
- }
7
-
8
- export function html(options: HtmlOptions): Plugin {
9
- return {
10
- name: 'vite-plugin-varlet-html',
11
-
12
- transformIndexHtml: {
13
- order: 'pre',
14
-
15
- handler(html) {
16
- return ejs.render(html, options.data)
17
- },
18
- },
19
- }
20
- }
package/src/index.ts DELETED
@@ -1,4 +0,0 @@
1
- export * from './markdown.js'
2
- export * from './html.js'
3
- export * from './inlineCss.js'
4
- export * from './copy.js'
package/src/inlineCss.ts DELETED
@@ -1,42 +0,0 @@
1
- import fse from 'fs-extra'
2
- import type { Plugin } from 'vite'
3
-
4
- const { pathExistsSync, writeFileSync, readFileSync, removeSync } = fse
5
-
6
- export interface InlineCssOptions {
7
- cssFile: string
8
- jsFile: string
9
- onEnd?(): void
10
- }
11
-
12
- export function inlineCss(options: InlineCssOptions): Plugin {
13
- return {
14
- name: 'vite-plugin-varlet-inline-css',
15
-
16
- apply: 'build',
17
-
18
- closeBundle() {
19
- const { cssFile, jsFile, onEnd } = options
20
-
21
- if (!pathExistsSync(cssFile)) {
22
- this.warn('css file cannot found')
23
- return
24
- }
25
-
26
- if (!pathExistsSync(jsFile)) {
27
- this.warn('js file cannot found')
28
- return
29
- }
30
-
31
- const cssCode = readFileSync(cssFile, 'utf-8')
32
- const jsCode = readFileSync(jsFile, 'utf-8')
33
- const injectCode = `;(function(){var style=document.createElement('style');style.type='text/css';\
34
- style.rel='stylesheet';style.appendChild(document.createTextNode(\`${cssCode.replace(/\\/g, '\\\\')}\`));\
35
- var head=document.querySelector('head');head.appendChild(style)})();`
36
-
37
- writeFileSync(jsFile, `${injectCode}${jsCode}`)
38
- removeSync(cssFile)
39
- onEnd?.()
40
- },
41
- }
42
- }
package/src/markdown.ts DELETED
@@ -1,155 +0,0 @@
1
- import { kebabCase } from '@varlet/shared'
2
- import hljs from 'highlight.js'
3
- import markdownIt from 'markdown-it'
4
- import { pinyin } from 'pinyin-pro'
5
- import type { Plugin } from 'vite'
6
-
7
- const includeChinese = (value: string) => /[\u4e00-\u9fa5]/.test(value)
8
-
9
- function transformHash(hash: string) {
10
- return (includeChinese(hash) ? pinyin(hash, { toneType: 'num' }) : hash).replaceAll(' ', '')
11
- }
12
-
13
- function htmlWrapper(html: string) {
14
- const matches = html.matchAll(/<h3>(.*?)<\/h3>/g)
15
- const hGroup = html
16
- .replace(/<h3>/g, () => {
17
- const hash = transformHash(matches.next().value![1])
18
-
19
- return `:::<h3 id="${hash}"><router-link to="#${hash}">#</router-link>`
20
- })
21
- .replace(/<h2/g, ':::<h2')
22
- .split(':::')
23
-
24
- const cardGroup = hGroup
25
- .map((fragment) => (fragment.includes('<h3') ? `<div class="card">${fragment}</div>` : fragment))
26
- .join('')
27
-
28
- return cardGroup.replace(/<code>/g, '<code v-pre>')
29
- }
30
-
31
- function extractComponents(source: string) {
32
- const componentRE = /import (.+) from ['"].+['"]/
33
- const importRE = /import .+ from ['"].+['"]/g
34
- const vueRE = /```vue((.|\r|\n)*?)```/g
35
- const imports: string[] = []
36
- const components: string[] = []
37
-
38
- source = source.replace(vueRE, (_, p1) => {
39
- const partImports = p1.match(importRE)
40
-
41
- const partComponents = partImports?.map((importer: string) => {
42
- importer = importer.replace(/(\n|\r)/g, '')
43
- const component = importer.replace(componentRE, '$1')
44
- !imports.includes(importer) && imports.push(importer)
45
- !components.includes(component) && components.push(component)
46
-
47
- return `<${kebabCase(component)} />`
48
- })
49
-
50
- return partComponents ? `<div class="varlet-component-preview">${partComponents.join('\n')}</div>` : ''
51
- })
52
-
53
- return {
54
- imports,
55
- components,
56
- source,
57
- }
58
- }
59
-
60
- function injectCodeExample(source: string) {
61
- const codeRE = /(<pre class="hljs">(.|\r|\n)*?<\/pre>)/g
62
-
63
- return source.replace(codeRE, (str) => {
64
- const flags = [
65
- '// playground-ignore\n',
66
- '<span class="hljs-meta">#</span><span class="bash"> playground-ignore</span>\n',
67
- '<span class="hljs-comment">// playground-ignore</span>\n',
68
- '<span class="hljs-comment">/* playground-ignore */</span>\n',
69
- '<span class="hljs-comment">&lt;!-- playground-ignore --&gt;</span>\n',
70
- ]
71
-
72
- const attr = flags.some((flag) => str.includes(flag)) ? 'playground-ignore' : ''
73
-
74
- str = flags.reduce((str, flag) => str.replace(flag, ''), str)
75
-
76
- return `<var-site-code-example ${attr}>${str}</var-site-code-example>`
77
- })
78
- }
79
-
80
- function highlight(str: string, lang: string, style?: string) {
81
- let link = ''
82
-
83
- if (style) {
84
- link = '<link class="hljs-style" rel="stylesheet" href="' + style + '"/>'
85
- }
86
-
87
- if (lang && hljs.getLanguage(lang)) {
88
- return (
89
- '<pre class="hljs"><code>' +
90
- link +
91
- hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
92
- '</code></pre>'
93
- )
94
- }
95
-
96
- return ''
97
- }
98
-
99
- function markdownToVue(source: string, options: MarkdownOptions) {
100
- const { source: vueSource, imports, components } = extractComponents(source)
101
- const md = markdownIt({
102
- html: true,
103
- highlight: (str, lang) => highlight(str, lang, options.style),
104
- })
105
- let templateString = htmlWrapper(md.render(vueSource))
106
- templateString = templateString.replace(/process.env/g, '<span>process.env</span>')
107
- templateString = injectCodeExample(templateString)
108
-
109
- return `
110
- <template><div class="varlet-site-doc">${templateString}</div></template>
111
-
112
- <script>
113
- ${imports.join('\n')}
114
-
115
- export default {
116
- components: {
117
- ${components.join(',')}
118
- }
119
- }
120
- </script>
121
- `
122
- }
123
-
124
- export interface MarkdownOptions {
125
- style?: string
126
- }
127
-
128
- export function markdown(options: MarkdownOptions): Plugin {
129
- return {
130
- name: 'vite-plugin-varlet-markdown',
131
-
132
- enforce: 'pre',
133
-
134
- transform(source, id) {
135
- if (!/\.md$/.test(id)) {
136
- return
137
- }
138
-
139
- try {
140
- return markdownToVue(source, options)
141
- } catch (e: any) {
142
- this.error(e)
143
- }
144
- },
145
-
146
- handleHotUpdate(ctx) {
147
- if (!/\.md$/.test(ctx.file)) {
148
- return
149
- }
150
-
151
- const { read } = ctx
152
- ctx.read = async () => markdownToVue(await read(), options)
153
- },
154
- }
155
- }
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "target": "esnext",
5
- "outDir": "./lib"
6
- },
7
- "include": ["src/**/*"]
8
- }