Coder Social home page Coder Social logo

tomjs / vite-plugin-vscode Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 767 KB

用 vue/react 来开发 vscode extension webview ,支持 esm 和 cjs。Use vue/react to develop vscode extension webview, supporting esm and cjs.

License: MIT License

JavaScript 2.87% Shell 1.80% TypeScript 95.33%
extension hmr hot plugin vite vscode webview react vue

vite-plugin-vscode's People

Contributors

tomgao365 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bluryar

vite-plugin-vscode's Issues

不能导入模块

vite打包如果有输出多个文件的话 在vscode里是用不了的

image
image

支持导入html文件

使能够在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,
    })

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.