@varlet/vite-plugins 2.6.0-alpha.1673285309604
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/LICENCE +21 -0
- package/lib/html.d.ts +5 -0
- package/lib/html.js +12 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/inlineCss.d.ts +7 -0
- package/lib/inlineCss.js +26 -0
- package/lib/markdown.d.ts +5 -0
- package/lib/markdown.js +110 -0
- package/package.json +42 -0
- package/src/html.ts +20 -0
- package/src/index.ts +3 -0
- package/src/inlineCss.ts +41 -0
- package/src/markdown.ts +141 -0
- package/tsconfig.json +7 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 varlet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/html.d.ts
ADDED
package/lib/html.js
ADDED
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/inlineCss.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import fse from 'fs-extra';
|
|
2
|
+
const { pathExistsSync, writeFileSync, readFileSync } = fse;
|
|
3
|
+
export function inlineCss(options) {
|
|
4
|
+
return {
|
|
5
|
+
name: 'vite-plugin-varlet-inline-css',
|
|
6
|
+
apply: 'build',
|
|
7
|
+
closeBundle() {
|
|
8
|
+
const { cssFile, jsFile, onEnd } = options;
|
|
9
|
+
if (!pathExistsSync(cssFile)) {
|
|
10
|
+
this.error('css file cannot found');
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (!pathExistsSync(jsFile)) {
|
|
14
|
+
this.error('js file cannot found');
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const cssCode = readFileSync(cssFile, 'utf-8');
|
|
18
|
+
const jsCode = readFileSync(jsFile, 'utf-8');
|
|
19
|
+
const injectCode = `;(function(){var style=document.createElement('style');style.type='text/css';\
|
|
20
|
+
style.rel='stylesheet';style.appendChild(document.createTextNode(\`${cssCode.replace(/\\/g, '\\\\')}\`));\
|
|
21
|
+
var head=document.querySelector('head');head.appendChild(style)})();`;
|
|
22
|
+
writeFileSync(jsFile, `${injectCode}${jsCode}`);
|
|
23
|
+
onEnd === null || onEnd === void 0 ? void 0 : onEnd();
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
package/lib/markdown.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import markdownIt from 'markdown-it';
|
|
2
|
+
import hljs from 'highlight.js';
|
|
3
|
+
import { kebabCase } from '@varlet/shared';
|
|
4
|
+
function htmlWrapper(html) {
|
|
5
|
+
const hGroup = html.replace(/<h3/g, ':::<h3').replace(/<h2/g, ':::<h2').split(':::');
|
|
6
|
+
const cardGroup = hGroup
|
|
7
|
+
.map((fragment) => (fragment.includes('<h3') ? `<div class="card">${fragment}</div>` : fragment))
|
|
8
|
+
.join('');
|
|
9
|
+
return cardGroup.replace(/<code>/g, '<code v-pre>');
|
|
10
|
+
}
|
|
11
|
+
function extractComponents(source) {
|
|
12
|
+
const componentRE = /import (.+) from ['"].+['"]/;
|
|
13
|
+
const importRE = /import .+ from ['"].+['"]/g;
|
|
14
|
+
const vueRE = /```vue((.|\r|\n)*?)```/g;
|
|
15
|
+
const imports = [];
|
|
16
|
+
const components = [];
|
|
17
|
+
source = source.replace(vueRE, (_, p1) => {
|
|
18
|
+
const partImports = p1.match(importRE);
|
|
19
|
+
const partComponents = partImports === null || partImports === void 0 ? void 0 : partImports.map((importer) => {
|
|
20
|
+
importer = importer.replace(/(\n|\r)/g, '');
|
|
21
|
+
const component = importer.replace(componentRE, '$1');
|
|
22
|
+
!imports.includes(importer) && imports.push(importer);
|
|
23
|
+
!components.includes(component) && components.push(component);
|
|
24
|
+
return `<${kebabCase(component)} />`;
|
|
25
|
+
});
|
|
26
|
+
return partComponents ? `<div class="varlet-component-preview">${partComponents.join('\n')}</div>` : '';
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
imports,
|
|
30
|
+
components,
|
|
31
|
+
source,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function injectCodeExample(source) {
|
|
35
|
+
const codeRE = /(<pre class="hljs">(.|\r|\n)*?<\/pre>)/g;
|
|
36
|
+
return source.replace(codeRE, (str) => {
|
|
37
|
+
const flags = [
|
|
38
|
+
'// playground-ignore\n',
|
|
39
|
+
'<span class="hljs-meta">#</span><span class="bash"> playground-ignore</span>\n',
|
|
40
|
+
'<span class="hljs-comment">// playground-ignore</span>\n',
|
|
41
|
+
'<span class="hljs-comment">/* playground-ignore */</span>\n',
|
|
42
|
+
'<span class="hljs-comment"><!-- playground-ignore --></span>\n',
|
|
43
|
+
];
|
|
44
|
+
const attr = flags.some((flag) => str.includes(flag)) ? 'playground-ignore' : '';
|
|
45
|
+
str = flags.reduce((str, flag) => str.replace(flag, ''), str);
|
|
46
|
+
return `<var-site-code-example ${attr}>${str}</var-site-code-example>`;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function highlight(str, lang, style) {
|
|
50
|
+
let link = '';
|
|
51
|
+
if (style) {
|
|
52
|
+
link = '<link class="hljs-style" rel="stylesheet" href="' + style + '"/>';
|
|
53
|
+
}
|
|
54
|
+
if (lang && hljs.getLanguage(lang)) {
|
|
55
|
+
return ('<pre class="hljs"><code>' +
|
|
56
|
+
link +
|
|
57
|
+
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
|
|
58
|
+
'</code></pre>');
|
|
59
|
+
}
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
function markdownToVue(source, options) {
|
|
63
|
+
const { source: vueSource, imports, components } = extractComponents(source);
|
|
64
|
+
const md = markdownIt({
|
|
65
|
+
html: true,
|
|
66
|
+
highlight: (str, lang) => highlight(str, lang, options.style),
|
|
67
|
+
});
|
|
68
|
+
let templateString = htmlWrapper(md.render(vueSource));
|
|
69
|
+
templateString = templateString.replace(/process.env/g, '<span>process.env</span>');
|
|
70
|
+
templateString = injectCodeExample(templateString);
|
|
71
|
+
return `
|
|
72
|
+
<template><div class="varlet-site-doc">${templateString}</div></template>
|
|
73
|
+
|
|
74
|
+
<script>
|
|
75
|
+
${imports.join('\n')}
|
|
76
|
+
|
|
77
|
+
export default {
|
|
78
|
+
components: {
|
|
79
|
+
${components.join(',')}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
</script>
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
export function markdown(options) {
|
|
86
|
+
return {
|
|
87
|
+
name: 'vite-plugin-varlet-markdown',
|
|
88
|
+
enforce: 'pre',
|
|
89
|
+
transform(source, id) {
|
|
90
|
+
if (!/\.md$/.test(id)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
return markdownToVue(source, options);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
this.error(e);
|
|
98
|
+
return '';
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
async handleHotUpdate(ctx) {
|
|
102
|
+
if (!/\.md$/.test(ctx.file))
|
|
103
|
+
return;
|
|
104
|
+
const readSource = ctx.read;
|
|
105
|
+
ctx.read = async function () {
|
|
106
|
+
return markdownToVue(await readSource(), options);
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@varlet/vite-plugins",
|
|
3
|
+
"version": "2.6.0-alpha.1673285309604",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "vite plugins of varlet",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"module": "lib/index.js",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"vite",
|
|
10
|
+
"plugins",
|
|
11
|
+
"varlet"
|
|
12
|
+
],
|
|
13
|
+
"author": "haoziqaq <357229046@qq.com>",
|
|
14
|
+
"homepage": "https://github.com/varletjs/varlet#readme",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/varletjs/varlet.git"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/varletjs/varlet/issues"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"ejs": "^3.1.8",
|
|
25
|
+
"fs-extra": "^9.0.1",
|
|
26
|
+
"highlight.js": "^10.7.2",
|
|
27
|
+
"markdown-it": "^12.2.3",
|
|
28
|
+
"@varlet/shared": "2.6.0-alpha.1673285309604"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"vite": "^4.0.4",
|
|
32
|
+
"typescript": "^4.4.4",
|
|
33
|
+
"@types/node": "^18.7.20",
|
|
34
|
+
"@types/ejs": "^3.1.1",
|
|
35
|
+
"@types/markdown-it": "^12.2.3",
|
|
36
|
+
"@types/fs-extra": "^9.0.2"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"dev": "tsc --watch",
|
|
40
|
+
"build": "tsc"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/html.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import ejs from 'ejs'
|
|
2
|
+
import type { Plugin } from 'vite'
|
|
3
|
+
|
|
4
|
+
export interface HtmlOptions {
|
|
5
|
+
data?: Record<string, string>
|
|
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
|
+
transform(html) {
|
|
16
|
+
return ejs.render(html, options.data)
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/index.ts
ADDED
package/src/inlineCss.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Plugin } from 'vite'
|
|
2
|
+
import fse from 'fs-extra'
|
|
3
|
+
|
|
4
|
+
const { pathExistsSync, writeFileSync, readFileSync } = 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.error('css file cannot found')
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!pathExistsSync(jsFile)) {
|
|
27
|
+
this.error('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
|
+
onEnd?.()
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/markdown.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import markdownIt from 'markdown-it'
|
|
2
|
+
import hljs from 'highlight.js'
|
|
3
|
+
import { kebabCase } from '@varlet/shared'
|
|
4
|
+
import type { Plugin } from 'vite'
|
|
5
|
+
|
|
6
|
+
function htmlWrapper(html: string) {
|
|
7
|
+
const hGroup = html.replace(/<h3/g, ':::<h3').replace(/<h2/g, ':::<h2').split(':::')
|
|
8
|
+
|
|
9
|
+
const cardGroup = hGroup
|
|
10
|
+
.map((fragment) => (fragment.includes('<h3') ? `<div class="card">${fragment}</div>` : fragment))
|
|
11
|
+
.join('')
|
|
12
|
+
|
|
13
|
+
return cardGroup.replace(/<code>/g, '<code v-pre>')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function extractComponents(source: string) {
|
|
17
|
+
const componentRE = /import (.+) from ['"].+['"]/
|
|
18
|
+
const importRE = /import .+ from ['"].+['"]/g
|
|
19
|
+
const vueRE = /```vue((.|\r|\n)*?)```/g
|
|
20
|
+
const imports: string[] = []
|
|
21
|
+
const components: string[] = []
|
|
22
|
+
|
|
23
|
+
source = source.replace(vueRE, (_, p1) => {
|
|
24
|
+
const partImports = p1.match(importRE)
|
|
25
|
+
|
|
26
|
+
const partComponents = partImports?.map((importer: string) => {
|
|
27
|
+
importer = importer.replace(/(\n|\r)/g, '')
|
|
28
|
+
const component = importer.replace(componentRE, '$1')
|
|
29
|
+
!imports.includes(importer) && imports.push(importer)
|
|
30
|
+
!components.includes(component) && components.push(component)
|
|
31
|
+
|
|
32
|
+
return `<${kebabCase(component)} />`
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return partComponents ? `<div class="varlet-component-preview">${partComponents.join('\n')}</div>` : ''
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
imports,
|
|
40
|
+
components,
|
|
41
|
+
source,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function injectCodeExample(source: string) {
|
|
46
|
+
const codeRE = /(<pre class="hljs">(.|\r|\n)*?<\/pre>)/g
|
|
47
|
+
|
|
48
|
+
return source.replace(codeRE, (str) => {
|
|
49
|
+
const flags = [
|
|
50
|
+
'// playground-ignore\n',
|
|
51
|
+
'<span class="hljs-meta">#</span><span class="bash"> playground-ignore</span>\n',
|
|
52
|
+
'<span class="hljs-comment">// playground-ignore</span>\n',
|
|
53
|
+
'<span class="hljs-comment">/* playground-ignore */</span>\n',
|
|
54
|
+
'<span class="hljs-comment"><!-- playground-ignore --></span>\n',
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
const attr = flags.some((flag) => str.includes(flag)) ? 'playground-ignore' : ''
|
|
58
|
+
|
|
59
|
+
str = flags.reduce((str, flag) => str.replace(flag, ''), str)
|
|
60
|
+
|
|
61
|
+
return `<var-site-code-example ${attr}>${str}</var-site-code-example>`
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function highlight(str: string, lang: string, style?: string) {
|
|
66
|
+
let link = ''
|
|
67
|
+
|
|
68
|
+
if (style) {
|
|
69
|
+
link = '<link class="hljs-style" rel="stylesheet" href="' + style + '"/>'
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (lang && hljs.getLanguage(lang)) {
|
|
73
|
+
return (
|
|
74
|
+
'<pre class="hljs"><code>' +
|
|
75
|
+
link +
|
|
76
|
+
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
|
|
77
|
+
'</code></pre>'
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return ''
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function markdownToVue(source: string, options: MarkdownOptions) {
|
|
85
|
+
const { source: vueSource, imports, components } = extractComponents(source)
|
|
86
|
+
const md = markdownIt({
|
|
87
|
+
html: true,
|
|
88
|
+
highlight: (str, lang) => highlight(str, lang, options.style),
|
|
89
|
+
})
|
|
90
|
+
let templateString = htmlWrapper(md.render(vueSource))
|
|
91
|
+
templateString = templateString.replace(/process.env/g, '<span>process.env</span>')
|
|
92
|
+
templateString = injectCodeExample(templateString)
|
|
93
|
+
|
|
94
|
+
return `
|
|
95
|
+
<template><div class="varlet-site-doc">${templateString}</div></template>
|
|
96
|
+
|
|
97
|
+
<script>
|
|
98
|
+
${imports.join('\n')}
|
|
99
|
+
|
|
100
|
+
export default {
|
|
101
|
+
components: {
|
|
102
|
+
${components.join(',')}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
</script>
|
|
106
|
+
`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface MarkdownOptions {
|
|
110
|
+
style?: string
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function markdown(options: MarkdownOptions): Plugin {
|
|
114
|
+
return {
|
|
115
|
+
name: 'vite-plugin-varlet-markdown',
|
|
116
|
+
|
|
117
|
+
enforce: 'pre',
|
|
118
|
+
|
|
119
|
+
transform(source, id) {
|
|
120
|
+
if (!/\.md$/.test(id)) {
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
return markdownToVue(source, options)
|
|
126
|
+
} catch (e: any) {
|
|
127
|
+
this.error(e)
|
|
128
|
+
return ''
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
async handleHotUpdate(ctx) {
|
|
133
|
+
if (!/\.md$/.test(ctx.file)) return
|
|
134
|
+
|
|
135
|
+
const readSource = ctx.read
|
|
136
|
+
ctx.read = async function () {
|
|
137
|
+
return markdownToVue(await readSource(), options)
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
}
|