VSCode 扩展 webview 加载 json 文件问题
VSCode Extension webview load json file problem
我正在尝试构建一个项目,其中有一个 json 文件,我必须在主文件中解析该文件。但我不能将它包含在主文件中。在终端中,main.ts 和 main.js 都没有错误。 Webview 面板显示 html 中的内容,但没有显示主文件中的内容。如果我通过开发人员工具进行检查,则会显示错误。我在 main.ts 中导入 json,main.js 是 main.ts 的编译文件。我需要这两个文件,但其中任何一个都出现错误。
我尝试过不同的组合
组合 1:
import json from "./test.json"; //in main.ts file
"module": "commonjs" // in tsconfig.json file
错误是"exports is not defined at main.js file"
组合二:
const json = require("./test.json"); //in main.ts file
"module": "commonjs" // in tsconfig.json file
错误是"require is not defined at main.ts"
组合 3:
const json = require("./test.json"); //in main.ts file
"module": "es2015" // in tsconfig.json file
错误是"require is not defined at main.ts"
组合4:
import json from "./test.json"; //in main.ts file
"module": "es2015" // in tsconfig.json file
错误是"Cannot use import statement outside a module"
下面是我完整的例子 tsconfig.json
{
"compilerOptions": {
"module": "es2015",
"target": "es5",
"outDir": "out",
"sourceMap": true,
"strict": true,
"rootDir": "src",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"strictNullChecks":false,
"lib": ["dom","es2015","es5","es6"]
},
"exclude": ["node_modules", ".vscode-test"],
"include" : [
"src/**/*"
]
}
我做错了什么?请帮忙。提前致谢。
webview 沙箱运行简单 javascript,所以除非你做一些额外的事情,node.js require()
或 模块 的概念是无法使用。
只有来自配置位置的文件和资源才能加载。看
https://code.visualstudio.com/api/extension-guides/webview#loading-local-content
关于 VS Code Webview,我建议将逻辑保留在您的扩展代码中,将 Webview 保留为仅可视化逻辑,并使用此处描述的消息传递来回通信:https://code.visualstudio.com/api/extension-guides/webview#scripts-and-message-passing 并通过调用您的扩展命令 .
这样,您可以在创建 Webview 的打字稿代码中加载 json 文件,然后在发生事件(主体加载事件或用户按下按钮)时,javascript 在您的 Webview html 中应向您的扩展程序传递一条消息,请求 json 数据。您的扩展程序返回一条包含 json 数据作为负载的消息。
示例扩展代码:
const json = require("./test.json");
// Send a message to our webview.
// You can send any JSON serializable data.
currentPanel.webview.postMessage({ command: 'load', jsonData: json });
Webview 示例 javascript 代码:
window.addEventListener('message', event => {
const message = event.data; // The command and JSON data our extension sent
switch (message.command) {
case 'load':
// todo: do something with the json data
console.log(message.jsonData)
break;
}
});
我正在尝试构建一个项目,其中有一个 json 文件,我必须在主文件中解析该文件。但我不能将它包含在主文件中。在终端中,main.ts 和 main.js 都没有错误。 Webview 面板显示 html 中的内容,但没有显示主文件中的内容。如果我通过开发人员工具进行检查,则会显示错误。我在 main.ts 中导入 json,main.js 是 main.ts 的编译文件。我需要这两个文件,但其中任何一个都出现错误。
我尝试过不同的组合
组合 1:
import json from "./test.json"; //in main.ts file
"module": "commonjs" // in tsconfig.json file
错误是"exports is not defined at main.js file"
组合二:
const json = require("./test.json"); //in main.ts file
"module": "commonjs" // in tsconfig.json file
错误是"require is not defined at main.ts"
组合 3:
const json = require("./test.json"); //in main.ts file
"module": "es2015" // in tsconfig.json file
错误是"require is not defined at main.ts"
组合4:
import json from "./test.json"; //in main.ts file
"module": "es2015" // in tsconfig.json file
错误是"Cannot use import statement outside a module"
下面是我完整的例子 tsconfig.json
{
"compilerOptions": {
"module": "es2015",
"target": "es5",
"outDir": "out",
"sourceMap": true,
"strict": true,
"rootDir": "src",
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true,
"strictNullChecks":false,
"lib": ["dom","es2015","es5","es6"]
},
"exclude": ["node_modules", ".vscode-test"],
"include" : [
"src/**/*"
]
}
我做错了什么?请帮忙。提前致谢。
webview 沙箱运行简单 javascript,所以除非你做一些额外的事情,node.js require()
或 模块 的概念是无法使用。
只有来自配置位置的文件和资源才能加载。看
https://code.visualstudio.com/api/extension-guides/webview#loading-local-content
关于 VS Code Webview,我建议将逻辑保留在您的扩展代码中,将 Webview 保留为仅可视化逻辑,并使用此处描述的消息传递来回通信:https://code.visualstudio.com/api/extension-guides/webview#scripts-and-message-passing 并通过调用您的扩展命令 .
这样,您可以在创建 Webview 的打字稿代码中加载 json 文件,然后在发生事件(主体加载事件或用户按下按钮)时,javascript 在您的 Webview html 中应向您的扩展程序传递一条消息,请求 json 数据。您的扩展程序返回一条包含 json 数据作为负载的消息。
示例扩展代码:
const json = require("./test.json");
// Send a message to our webview.
// You can send any JSON serializable data.
currentPanel.webview.postMessage({ command: 'load', jsonData: json });
Webview 示例 javascript 代码:
window.addEventListener('message', event => {
const message = event.data; // The command and JSON data our extension sent
switch (message.command) {
case 'load':
// todo: do something with the json data
console.log(message.jsonData)
break;
}
});