Code Monkey home page Code Monkey logo

vite-plugin-vscode's Introduction

@tomjs/vite-plugin-vscode

npm node-current (scoped) NPM Docs

English | 中文

Use vue/react to develop vscode extension webview, supporting esm and cjs.

In development mode, inject the code of @tomjs/vscode-extension-webview into vscode extension code and web page code, use To support HMR; during production build, the final generated index.html code is injected into vscode extension code to reduce the workload.

Features

  • Use tsup to quickly build extension code
  • Simple configuration, focus on business
  • Support esm and cjs
  • Support webview HMR
  • Support Multi-Page App
  • Supports vue and react and other frameworks supported by vite

Install

# pnpm
pnpm add @tomjs/vite-plugin-vscode -D

# yarn
yarn add @tomjs/vite-plugin-vscode -D

# npm
npm i @tomjs/vite-plugin-vscode --save-dev

Usage

Recommended

Setting recommended will modify some preset configurations. See PluginOptions and recommended parameter descriptions in detail.

Directory Structure

  • By default, recommended:true will be based on the following directory structure as a convention.
|--extension      // extension code
|  |--index.ts
|--src            // front-end code
|  |--App.vue
|  |--main.ts
|--index.html
  • Zero configuration, default dist output directory
|--dist
|  |--extension
|  |  |--index.js
|  |  |--index.js.map
|  |--webview
|  |  |--index.html
  • If you want to modify the extension source code directory to src, you can set { extension: { entry: 'src/index.ts' } }
|--src            // extension code
|  |--index.ts
|--webview        // front-end code
|  |--App.vue
|  |--main.ts
|--index.html

extension

code snippet, more code see examples

const panel = window.createWebviewPanel('showHelloWorld', 'Hello World', ViewColumn.One, {
  enableScripts: true,
  localResourceRoots: [Uri.joinPath(extensionUri, 'dist/webview')],
});

// Vite development mode and production mode inject different webview codes to reduce development work
panel.webview.html = process.env.VITE_DEV_SERVER_URL
  ? __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL)
  : __getWebviewHtml__(webview, context);
  • package.json
{
  "main": "dist/extension/index.js"
}

vue

  • vite.config.ts
import { defineConfig } from 'vite';
import vscode from '@tomjs/vite-plugin-vscode';
import vue from '@vitejs/plugin-vue';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag: string) => tag.startsWith('vscode-'),
        },
      },
    }),
    vscode(),
    // Modify the extension source code entry path, and also modify the `index.html` entry file path
    // vscode({ extension: { entry: 'src/index.ts' } }),
  ],
});

react

  • vite.config.ts
import { defineConfig } from 'vite';
import vscode from '@tomjs/vite-plugin-vscode';
import react from '@vitejs/plugin-react-swc';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), vscode()],
});

Multi-page application

See vue-import example

  • vite.config.ts
import path from 'node:path';
import vscode from '@tomjs/vite-plugin-vscode';

export default defineConfig({
  build: {
    plugins: [vscode()]
    rollupOptions: {
      // https://cn.vitejs.dev/guide/build.html#multi-page-app
      input: [path.resolve(__dirname, 'index.html'), path.resolve(__dirname, 'index2.html')],
      // You can also customize the name
      // input:{
      //   'index': path.resolve(__dirname, 'index.html'),
      //   'index2': path.resolve(__dirname, 'index2.html'),
      // }
    },
  },
});
  • page one
process.env.VITE_DEV_SERVER_URL
  ? __getWebviewHtml__(process.env.VITE_DEV_SERVER_URL)
  : __getWebviewHtml__(webview, context);
  • page two
process.env.VITE_DEV_SERVER_URL
  ? __getWebviewHtml__(`${process.env.VITE_DEV_SERVER_URL}/index2.html`)
  : __getWebviewHtml__(webview, context, 'index2');

getWebviewHtml Description

/**
 *  `[vite serve]` Gets the html of webview in development mode.
 * @param options serverUrl: The url of the vite dev server.
 */
function __getWebviewHtml__(options?: string | { serverUrl: string }): string;

/**
 *  `[vite build]` Gets the html of webview in production mode.
 * @param webview The WebviewPanel instance of the extension.
 * @param context The ExtensionContext instance of the extension.
 * @param inputName vite build.rollupOptions.input name. Default is `index`.
 */
function __getWebviewHtml__(
  webview: Webview,
  context: ExtensionContext,
  inputName?: string,
): string;

Documentation

Parameters

PluginOptions

Property Type Default Description
recommended boolean true This option is intended to provide recommended default parameters and behavior.
extension ExtensionOptions Configuration options for the vscode extension.
webview boolean false Inject @tomjs/vscode-extension-webview into vscode extension code and web client code, so that webview can support HMR during the development stage.

Notice

The recommended option is used to set the default configuration and behavior, which can be used with almost zero configuration. The default is true. If you want to customize the configuration, set it to false. The following default prerequisites are to use the recommended project structure.

  • The output directory is based on the build.outDir parameter of vite, and outputs extension and src to dist/extension and dist/webview respectively.
  • Other behaviors to be implemented

ExtensionOptions

Based on Options of tsup, some default values are added for ease of use.

Property Type Default Description
entry string extension/index.ts The vscode extension entry file.
outDir string dist-extension/main The output directory for the vscode extension file
onSuccess () => Promise<void | undefined | (() => void | Promise<void>)> undefined A function that will be executed after the build succeeds.

Additional Information

  • Default values for extension when the relevant parameters are not configured
Parameter Development Mode Default Production Mode Default
sourcemap true false
minify false true

Environment Variables

Vite plugin variables

vscode extension use.

  • development mode
Variable Description
VITE_DEV_SERVER_URL The url of the vite dev server
  • production mode
Variable Description
VITE_WEBVIEW_DIST vite webview page output path

Debug

Run Debug Extension through vscode to debug. For debugging tools, refer to Official Documentation

launch.json is configured as follows:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Extension",
      "type": "extensionHost",
      "request": "launch",
      "args": ["--extensionDevelopmentPath=${workspaceFolder}"],
      "outFiles": ["${workspaceFolder}/dist/extension/*.js"],
      "preLaunchTask": "npm: dev"
    },
    {
      "name": "Preview Extension",
      "type": "extensionHost",
      "request": "launch",
      "args": ["--extensionDevelopmentPath=${workspaceFolder}"],
      "outFiles": ["${workspaceFolder}/dist/extension/*.js"],
      "preLaunchTask": "npm: build"
    }
  ]
}

tasks.json is configured as follows:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "npm",
      "script": "dev",
      "problemMatcher": {
        "owner": "typescript",
        "fileLocation": "relative",
        "pattern": {
          "regexp": "^([a-zA-Z]\\:/?([\\w\\-]/?)+\\.\\w+):(\\d+):(\\d+): (ERROR|WARNING)\\: (.*)$",
          "file": 1,
          "line": 3,
          "column": 4,
          "code": 5,
          "message": 6
        },
        "background": {
          "activeOnStart": true,
          "beginsPattern": "^.*extension build start*$",
          "endsPattern": "^.*extension (build|rebuild) success.*$"
        }
      },
      "isBackground": true,
      "presentation": {
        "reveal": "never"
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "type": "npm",
      "script": "build",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": []
    }
  ]
}

Examples

First execute the following command to install dependencies and generate library files:

pnpm install
pnpm build

Open the examples directory, there are vue and react examples.

  • react: Simple react example.
  • vue: Simple vue example.
  • vue-import: Dynamic import() and multi-page examples.

vite-plugin-vscode's People

Contributors

tomgao365 avatar

Stargazers

 avatar  avatar  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.