Skip to content

Commit b6bf9ce

Browse files
committed
fix: webview error
1 parent 917ae46 commit b6bf9ce

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"publisher": "zhangmo8",
33
"name": "git-panel",
44
"displayName": "Git Panel",
5-
"version": "0.0.1",
5+
"version": "0.0.2",
66
"private": true,
77
"packageManager": "[email protected]",
88
"description": "",
@@ -128,14 +128,14 @@
128128
}
129129
},
130130
"scripts": {
131-
"build": "cross-env NODE_ENV=production vite build && tsup src/index.ts --external vscode",
132-
"dev": "concurrently \"vite\" \"cross-env NODE_ENV=development tsup src/index.ts --external vscode --watch\"",
131+
"build": "vite build && tsup src/index.ts --external vscode",
132+
"dev": "concurrently \"vite\" \"tsup src/index.ts --external vscode --watch\"",
133133
"prepare": "nr update",
134134
"update": "vscode-ext-gen --output src/generated/meta.ts",
135135
"lint": "eslint .",
136136
"vscode:prepublish": "nr build",
137-
"publish": "vsce publish --no-dependencies",
138-
"pack": "vsce package --no-dependencies",
137+
"publish": "vsce publish",
138+
"pack": "vsce package",
139139
"test": "vitest",
140140
"typecheck": "tsc --noEmit",
141141
"release": "bumpp && nr publish"

src/views/webview.ts

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import process from 'node:process'
21
import type * as vscode from 'vscode'
32
import { Uri, commands } from 'vscode'
43

@@ -92,36 +91,54 @@ export class GitPanelViewProvider implements vscode.WebviewViewProvider {
9291
}
9392

9493
private _getHtmlForWebview(webview: vscode.Webview) {
95-
const scriptUri = process.env.NODE_ENV === 'development'
96-
? 'http://localhost:5173/src/views/history/index.ts'
97-
: webview.asWebviewUri(
98-
Uri.joinPath(this._extensionUri, 'views.es.js'),
99-
)
94+
// const scriptUri = process.env.NODE_ENV === 'development'
95+
// ? 'http://localhost:5173/src/views/history/index.ts'
96+
// : webview.asWebviewUri(
97+
// Uri.joinPath(this._extensionUri, 'views.es.js'),
98+
// )
99+
100+
const scriptUri = webview.asWebviewUri(
101+
Uri.joinPath(this._extensionUri, 'views.es.js'),
102+
)
103+
104+
const styleUri = webview.asWebviewUri(
105+
Uri.joinPath(this._extensionUri, 'views.css'),
106+
)
107+
108+
const nonce = getNonce()
100109

101110
return `<!doctype html>
102-
<html lang="en">
103-
<head>
104-
<meta charset="UTF-8">
105-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
106-
<title>Git Panel</title>
107-
<style>
108-
body {
109-
margin: 0;
110-
padding: 0;
111-
}
112-
113-
* {
114-
box-sizing: border-box;
115-
-moz-box-sizing: border-box;
116-
-webkit-box-sizing: border-box;
117-
}
118-
</style>
119-
</head>
120-
<body>
121-
<div id="app"></div>
122-
<script crossorigin type="module" src="${scriptUri}"></script>
123-
</body>
124-
</html>
125-
`
111+
<html lang="en">
112+
<head>
113+
<meta charset="UTF-8">
114+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
115+
<title>Git Panel</title>
116+
<link rel="stylesheet" type="text/css" href="${styleUri}">
117+
<style>
118+
body {
119+
margin: 0;
120+
padding: 0;
121+
}
122+
123+
* {
124+
box-sizing: border-box;
125+
-moz-box-sizing: border-box;
126+
-webkit-box-sizing: border-box;
127+
}
128+
</style>
129+
</head>
130+
<body>
131+
<div id="app"></div>
132+
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
133+
</body>
134+
</html>`
126135
}
127136
}
137+
138+
function getNonce() {
139+
let text = ''
140+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
141+
for (let i = 0; i < 32; i++)
142+
text += possible.charAt(Math.floor(Math.random() * possible.length))
143+
return text
144+
}

tsup.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ export default defineConfig({
66
'src/index.ts',
77
],
88
format: ['cjs'],
9-
shims: false,
9+
shims: true,
1010
dts: false,
1111
external: [
1212
'vscode',
1313
],
14+
noExternal: [
15+
'simple-git'
16+
],
1417
esbuildOptions(options) {
1518
options.alias = {
1619
'@': resolve(__dirname, './src'),

vite.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import vue from '@vitejs/plugin-vue'
66
// https://vite.dev/config/
77
export default defineConfig({
88
base: './',
9+
define: {
10+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
11+
'__VUE_OPTIONS_API__': false,
12+
'__VUE_PROD_DEVTOOLS__': false,
13+
},
914
plugins: [vue()],
1015
resolve: {
1116
alias: {
@@ -26,6 +31,12 @@ export default defineConfig({
2631
formats: ['es'],
2732
fileName: format => `views.${format}.js`,
2833
},
34+
cssCodeSplit: false,
35+
rollupOptions: {
36+
output: {
37+
assetFileNames: 'views.css'
38+
}
39+
},
2940
emptyOutDir: false,
3041
outDir: 'dist',
3142
},

0 commit comments

Comments
 (0)