tomjs / vite-plugin-vscode Goto Github PK
View Code? Open in Web Editor NEW用 vue/react 来开发 vscode extension webview ,支持 esm 和 cjs。Use vue/react to develop vscode extension webview, supporting esm and cjs.
License: MIT License
用 vue/react 来开发 vscode extension webview ,支持 esm 和 cjs。Use vue/react to develop vscode extension webview, supporting esm and cjs.
License: MIT License
使能够在vscode扩展的代码中能够导入html内容
项目中已经包含了一个或多个html文件
能否实现直接导入html文件内容而不是在代码中内联html
直接用import的话可能会使热重载失效
提供一个工具函数也可以
这是我之前写的导入html内容的esbuild插件
import * as esbuild from 'esbuild'
import * as fs from 'node:fs'
import * as path from 'node:path'
import * as crypto from 'node:crypto'
import * as url from 'node:url'
interface HtmlPluginData {
resolveDir: string
fileName: string
fullPath: string
}
export const htmlCompiler = (): esbuild.Plugin => {
return {
name: 'esbuild-plugin-html',
setup: build => {
// 处理 *.vue 文件中的 script
build.onResolve({ filter: /\.html$/ }, async args => {
const filePath = args.path
const fileName = path.basename(filePath)
const fullPath = path.resolve(args.resolveDir, filePath)
return {
path: args.path,
namespace: 'vscode-html',
pluginData: {
...args.pluginData,
resolveDir: args.resolveDir,
fileName,
fullPath,
} as HtmlPluginData,
}
})
build.onLoad(
{ filter: /\.html$/, namespace: 'vscode-html' },
async args => {
const data = args.pluginData as HtmlPluginData
const content = await fs.promises.readFile(data.fullPath, 'utf8')
const code = new Array<string>()
code.push('export default (data) =>')
code.push(` Object.entries(data).reduce(`)
code.push(` (html, [property, value]) =>`)
code.push(
' html.replaceAll(`\\$\\{\\{${property}\\}\\}`, value),',
)
code.push(` ${JSON.stringify(content)},`)
code.push(' )')
const contents = code.join('\n')
return {
contents,
resolveDir: data.resolveDir,
loader: 'js',
watchFiles: [args.path],
}
},
)
},
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Security-Policy" content="${{csp}}" />
<title>Document</title>
<link rel="stylesheet" crossorigin href="${{stylesUri}}" />
</head>
<body>
<div id="app"></div>
<script
type="module"
crossorigin
nonce="${{nonce}}"
src="${{scriptUri}}"></script>
</body>
</html>
import html from './pages/ShpViewer.html'
// ...
const stylesUri = this.buildUri(`index.css`)
const scriptUri = this.buildUri(`index.js`)
const nonce = crypto.randomUUID()
const csp =
[
`default-src ${this.panel.webview.cspSource} data: blob:`,
`style-src ${this.panel.webview.cspSource} 'unsafe-inline'`,
`script-src ${this.panel.webview.cspSource} blob: 'unsafe-eval' 'nonce-${nonce}'`,
].join('; ') + ';'
this.panel.webview.html = html({
stylesUri: stylesUri,
scriptUri: scriptUri,
nonce: nonce,
csp: csp,
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.