Loading node_modules Resources in VSC WebView
Recently, while developing the gitlink plugin, I encountered an issue loading node_modules resources in WebView. Although CDN resources could be used directly, loading local resources is faster. Here’s how to solve the loading of node_modules resources.
Configuration
- Install npm dependency
1 | npm install @highlightjs |
Note that this should be a dependency, not a dev dependency.
- Configure localResourceRoots in webview
1 | const panel = vscode.window.createWebviewPanel( |
Here, we use extensionContext.extensionUri as localResourceRoots, allowing us to access node_modules resources within the extension.
- Load resources in webview
1 | <link rel="stylesheet" href="node_modules/@highlightjs/cdn-assets/styles/github-dark.min.css"> |
Please note that relative path syntax is preferred over absolute path syntax. Absolute paths are not supported, but web addresses are.
- Handle HTML href paths
1 | const readHtml = async (htmlPath: string, panel: vscode.WebviewPanel) => |
This is how I handle it - processing all resources in the HTML based on the HTML’s location to obtain the path relative to the extension for node_modules resources.
Note
- Don’t add the node_modules folder in the .vscodeignore file. Otherwise, the package won’t include node_modules.
At the end
Following these steps solves the issue of loading node_modules resources in WebView. When updates are needed in the future, run the npm update and repackage the extension.