Skip to content

Commit c50532c

Browse files
jakebaileykarthiknadig
authored andcommitted
Add webpack build for browser entrypoint (#16912)
1 parent 956c01f commit c50532c

File tree

9 files changed

+156
-0
lines changed

9 files changed

+156
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ debugpy*.log
4141
pydevd*.log
4242
nodeLanguageServer/**
4343
nodeLanguageServer.*/**
44+
dist/**
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
// @ts-check
5+
6+
'use strict';
7+
8+
const path = require('path');
9+
10+
const packageRoot = path.resolve(__dirname, '..', '..');
11+
const outDir = path.resolve(packageRoot, 'dist');
12+
13+
/** @type {(env: any, argv: { mode: 'production' | 'development' | 'none' }) => import('webpack').Configuration} */
14+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
15+
const nodeConfig = (_, { mode }) => ({
16+
context: packageRoot,
17+
entry: {
18+
extension: './src/client/browser/extension.ts',
19+
},
20+
target: 'webworker',
21+
output: {
22+
filename: '[name].browser.js',
23+
path: outDir,
24+
libraryTarget: 'commonjs2',
25+
devtoolModuleFilenameTemplate: '../../[resource-path]',
26+
},
27+
devtool: 'source-map',
28+
// stats: {
29+
// all: false,
30+
// errors: true,
31+
// warnings: true,
32+
// },
33+
resolve: {
34+
extensions: ['.ts', '.js'],
35+
},
36+
externals: {
37+
vscode: 'commonjs vscode',
38+
39+
// These dependencies are ignored because we don't use them, and App Insights has try-catch protecting their loading if they don't exist
40+
// See: https://github.com/microsoft/vscode-extension-telemetry/issues/41#issuecomment-598852991
41+
'applicationinsights-native-metrics': 'commonjs applicationinsights-native-metrics',
42+
'@opentelemetry/tracing': 'commonjs @opentelemetry/tracing',
43+
},
44+
module: {
45+
rules: [
46+
{
47+
test: /\.ts$/,
48+
loader: 'ts-loader',
49+
options: {
50+
configFile: 'tsconfig.browser.json',
51+
},
52+
},
53+
{
54+
test: /\.node$/,
55+
loader: 'node-loader',
56+
},
57+
],
58+
},
59+
// optimization: {
60+
// usedExports: true,
61+
// splitChunks: {
62+
// cacheGroups: {
63+
// defaultVendors: {
64+
// name: 'vendor',
65+
// test: /[\\/]node_modules[\\/]/,
66+
// chunks: 'all',
67+
// priority: -10,
68+
// },
69+
// },
70+
// },
71+
// },
72+
});
73+
74+
module.exports = nodeConfig;

gulpfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ gulp.task('webpack', async () => {
7575
await buildWebPackForDevOrProduction('./build/webpack/webpack.extension.dependencies.config.js', 'production');
7676
await buildWebPackForDevOrProduction('./build/webpack/webpack.startPage-ui-viewers.config.js', 'production');
7777
await buildWebPackForDevOrProduction('./build/webpack/webpack.extension.config.js', 'extension');
78+
await buildWebPackForDevOrProduction('./build/webpack/webpack.extension.browser.config.js', 'browser');
7879
});
7980

8081
gulp.task('addExtensionPackDependencies', async () => {
@@ -199,6 +200,8 @@ function getAllowedWarningsForWebPack(buildConfig) {
199200
'WARNING in ./node_modules/diagnostic-channel-publishers/dist/src/azure-coretracing.pub.js',
200201
'WARNING in ./node_modules/applicationinsights/out/AutoCollection/NativePerformance.js',
201202
];
203+
case 'browser':
204+
return [];
202205
default:
203206
throw new Error('Unknown WebPack Configuration');
204207
}

news/1 Enhancements/16869.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a basic web extension bundle.

news/1 Enhancements/16870.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add basic Pylance support to the web extension.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"workspaceContains:app.py"
103103
],
104104
"main": "./out/client/extension",
105+
"browser": "./dist/extension.browser.js",
105106
"contributes": {
106107
"walkthroughs": [
107108
{

src/client/browser/extension.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as vscode from 'vscode';
5+
import { LanguageClientOptions } from 'vscode-languageclient';
6+
import { LanguageClient } from 'vscode-languageclient/browser';
7+
import { ILSExtensionApi } from '../activation/node/languageServerFolderService';
8+
import { PYLANCE_EXTENSION_ID } from '../common/constants';
9+
10+
interface BrowserConfig {
11+
distUrl: string; // URL to Pylance's dist folder.
12+
}
13+
14+
export async function activate(context: vscode.ExtensionContext): Promise<void> {
15+
// Run in a promise and return early so that VS Code can go activate Pylance.
16+
runPylance(context);
17+
}
18+
19+
async function runPylance(context: vscode.ExtensionContext): Promise<void> {
20+
const pylanceExtension = vscode.extensions.getExtension<ILSExtensionApi>(PYLANCE_EXTENSION_ID);
21+
const pylanceApi = await pylanceExtension?.activate();
22+
if (!pylanceApi?.languageServerFolder) {
23+
throw new Error('Could not find Pylance extension');
24+
}
25+
26+
const { path: distUrl } = await pylanceApi.languageServerFolder();
27+
28+
try {
29+
const worker = new Worker(`${distUrl}/browser.server.bundle.js`);
30+
31+
// Pass the configuration as the first message to the worker so it can
32+
// have info like the URL of the dist folder early enough.
33+
//
34+
// This is the same method used by the TS worker:
35+
// https://github.com/microsoft/vscode/blob/90aa979bb75a795fd8c33d38aee263ea655270d0/extensions/typescript-language-features/src/tsServer/serverProcess.browser.ts#L55
36+
const config: BrowserConfig = {
37+
distUrl,
38+
};
39+
worker.postMessage(config);
40+
41+
const clientOptions: LanguageClientOptions = {
42+
// Register the server for python source files.
43+
documentSelector: [
44+
{
45+
language: 'python',
46+
},
47+
],
48+
synchronize: {
49+
// Synchronize the setting section to the server.
50+
configurationSection: ['python'],
51+
},
52+
};
53+
54+
const languageClient = new LanguageClient('python', 'Python Language Server', clientOptions, worker);
55+
const disposable = languageClient.start();
56+
57+
context.subscriptions.push(disposable);
58+
} catch (e) {
59+
console.log(e);
60+
}
61+
}

tsconfig.browser.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": [
4+
"./src/client/browser"
5+
]
6+
}

typings/webworker.fix.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
// Fake interfaces that are required for web workers to work around
5+
// tsconfig's DOM and WebWorker lib options being mutally exclusive.
6+
// https://github.com/microsoft/TypeScript/issues/20595
7+
8+
interface DedicatedWorkerGlobalScope {}

0 commit comments

Comments
 (0)