Skip to content

Commit

Permalink
refactor: adjust files structure to adapt web build
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 16, 2022
1 parent a95695a commit 75c48db
Show file tree
Hide file tree
Showing 23 changed files with 312 additions and 236 deletions.
27 changes: 23 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch Client",
"type": "extensionHost",
"request": "launch",
"name": "Launch Client",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
Expand All @@ -22,29 +22,48 @@
}
},
{
"name": "Launch Web Client",
"type": "pwa-extensionHost",
"debugWebWorkerHost": true,
"request": "launch",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}/extensions/vscode-vue-language-features",
"--extensionDevelopmentKind=web"
],
"outFiles": [
"${workspaceRoot}/extensions/*/out/**/*.js",
"${workspaceRoot}/packages/*/out/**/*.js"
],
"preLaunchTask": {
"type": "npm",
"script": "watch"
}
},
{
"name": "Attach to Server - API",
"type": "node",
"request": "attach",
"name": "Attach to Server - API",
"port": 6009,
"restart": true,
"outFiles": [
"${workspaceRoot}/packages/*/out/**/*.js"
]
},
{
"name": "Attach to Server - Document",
"type": "node",
"request": "attach",
"name": "Attach to Server - Document",
"port": 6010,
"restart": true,
"outFiles": [
"${workspaceRoot}/packages/*/out/**/*.js"
]
},
{
"name": "Attach to Server - HTML",
"type": "node",
"request": "attach",
"name": "Attach to Server - HTML",
"port": 6011,
"restart": true,
"outFiles": [
Expand Down
4 changes: 2 additions & 2 deletions extensions/vscode-vue-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@
"scripts": {
"vscode:prepublish": "npm run build",
"prebuild": "cd ../.. && npm run build",
"build": "npm run build:node -- --minify foo=bar && npm run build:browser -- --minify",
"build": "npm run build:node -- --minify && npm run build:browser -- --minify",
"watch": "npm run build:node -- --watch & npm run build:browser -- --watch",
"build:node": "node scripts/build-node",
"build:browser": "esbuild client=./node_modules/@volar/client/out/browserClientMain.js --bundle --outdir=out/browser --external:vscode --format=esm --platform=browser --tsconfig=../../tsconfig.build.json",
"build:browser": "node scripts/build-browser",
"pack": "vsce package",
"release": "vsce publish"
},
Expand Down
14 changes: 14 additions & 0 deletions extensions/vscode-vue-language-features/scripts/build-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('esbuild').build({
entryPoints: {
client: './node_modules/@volar/client/out/browserClientMain.js',
// server: './node_modules/@volar/server/out/browser.js',
},
bundle: true,
outdir: './out/browser',
external: ['vscode'],
format: 'cjs',
platform: 'browser',
tsconfig: '../../tsconfig.build.json',
minify: process.argv.includes('--minify'),
watch: process.argv.includes('--watch'),
}).catch(() => process.exit(1))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"release:vue-language-features": "cd extensions/vscode-vue-language-features && npm run release",
"release:typescript-vue-plugin": "cd extensions/vscode-typescript-vue-plugin && npm run release",
"test": "jest",
"chrome": "npm run build && vscode-test-web --browserType=chromium --extensionDevelopmentPath=./extensions/vscode-vue-language-features ../volar-starter"
"chrome": "vscode-test-web --browserType=chromium --extensionDevelopmentPath=./extensions/vscode-vue-language-features ../volar-starter"
},
"devDependencies": {
"@types/jest": "latest",
Expand Down
16 changes: 8 additions & 8 deletions packages/client/src/features/createWorkspaceSnippets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as fs from '../utils/fs';

export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('volar.action.createWorkspaceSnippets', async () => {
Expand All @@ -10,14 +9,15 @@ export async function activate(context: vscode.ExtensionContext) {
const volar = vscode.extensions.getExtension('johnsoncodehk.volar');
if (!volar) return;

const templatePath = path.join(volar.extensionPath, 'templates', 'vue.code-snippets');
const newTemplatePath = path.join(rootPath.uri.fsPath, '.vscode', 'vue.code-snippets');
const templateUri = vscode.Uri.joinPath(volar.extensionUri, 'templates', 'vue.code-snippets');
const newTemplateUri = vscode.Uri.joinPath(rootPath.uri, '.vscode', 'vue.code-snippets');

if (!fs.existsSync(newTemplatePath)) {
const template = fs.readFileSync(templatePath);
fs.writeFileSync(newTemplatePath, template);
if (!await fs.exists(newTemplateUri)) {
const template = await vscode.workspace.fs.readFile(templateUri);
vscode.workspace.fs.writeFile(newTemplateUri, template)
}
const document = await vscode.workspace.openTextDocument(templatePath);

const document = await vscode.workspace.openTextDocument(newTemplateUri);
await vscode.window.showTextDocument(document);
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/client/src/features/preview.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import { compile, NodeTypes } from '@vue/compiler-dom';
import * as path from 'upath';
import * as fs from 'fs';
import * as fs from '../utils/fs';
import * as shared from '@volar/shared';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { htmlLs } from './splitEditors';
Expand Down Expand Up @@ -115,7 +115,7 @@ export async function activate(context: vscode.ExtensionContext) {

const port = await shared.getLocalHostAvaliablePort(vscode.workspace.getConfiguration('volar').get('preview.port') ?? 3333);
const terminal = vscode.window.createTerminal('volar-finder');
const viteDir = getViteDir(fileName);
const viteDir = await getViteDir(fileName);
if (viteDir) {
terminal.sendText(`cd ${viteDir}`);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export async function activate(context: vscode.ExtensionContext) {

const port = await shared.getLocalHostAvaliablePort(vscode.workspace.getConfiguration('volar').get('preview.port') ?? 3333);
const terminal = vscode.window.createTerminal('volar-previewer');
const viteDir = getViteDir(fileName);
const viteDir = await getViteDir(fileName);
if (viteDir) {
terminal.sendText(`cd ${viteDir}`);
}
Expand All @@ -194,13 +194,13 @@ export async function activate(context: vscode.ExtensionContext) {
return port;
}

function getViteDir(fileName: string) {
async function getViteDir(fileName: string) {
let dir = path.dirname(fileName);
let viteConfigDir: string | undefined;
while (true) {
const configTs = path.join(dir, 'vite.config.ts');
const configJs = path.join(dir, 'vite.config.js');
if (fs.existsSync(configTs) || fs.existsSync(configJs)) {
if (await fs.exists(vscode.Uri.file(configTs)) || await fs.exists(vscode.Uri.file(configJs))) {
viteConfigDir = dir;
break;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/client/src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as vscode from 'vscode';

export async function exists(uri: vscode.Uri) {
try {
await vscode.workspace.fs.stat(uri);
return true;
}
catch {
return false;
}
}
19 changes: 18 additions & 1 deletion packages/server/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import * as vscode from 'vscode-languageserver/browser';
import { createLanguageServer } from './common';
import { configure as configureHttpRequests } from 'request-light';
import httpSchemaRequestHandler from './schemaRequestHandlers/http';

const messageReader = new vscode.BrowserMessageReader(self);
const messageWriter = new vscode.BrowserMessageWriter(self);
const connection = vscode.createConnection(messageReader, messageWriter);

createLanguageServer(connection);
createLanguageServer(connection, {
loadTypescript(options) {
throw 'loadTypescript';
return {} as any;
},
loadTypescriptLocalized(options) {
throw 'loadTypescriptLocalized';
},
schemaRequestHandlers: {
http: httpSchemaRequestHandler,
https: httpSchemaRequestHandler,
},
onDidChangeConfiguration(settings) {
configureHttpRequests(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
},
});
30 changes: 12 additions & 18 deletions packages/server/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as shared from '@volar/shared';
import * as fs from 'fs';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'upath';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as vscode from 'vscode-languageserver';
import { URI } from 'vscode-uri';
Expand All @@ -11,7 +8,14 @@ import { getInferredCompilerOptions } from './inferredCompilerOptions';
import { createProjects } from './projects';
import * as tsConfigs from './tsConfigs';

export function createLanguageServer(connection: vscode.Connection) {
export interface RuntimeEnvironment {
loadTypescript: (initOptions: shared.ServerInitializationOptions) => typeof import('typescript/lib/tsserverlibrary'),
loadTypescriptLocalized: (initOptions: shared.ServerInitializationOptions) => any,
schemaRequestHandlers: { [schema: string]: (uri: string, encoding?: BufferEncoding) => Promise<string> },
onDidChangeConfiguration?: (settings: any) => void,
}

export function createLanguageServer(connection: vscode.Connection, runtimeEnv: RuntimeEnvironment) {

connection.onInitialize(onInitialize);
connection.listen();
Expand All @@ -26,7 +30,6 @@ export function createLanguageServer(connection: vscode.Connection) {
}
return undefined;
});
connection.onRequest(shared.DepsRequest.type, () => Object.keys(require.cache));

async function onInitialize(params: vscode.InitializeParams) {

Expand Down Expand Up @@ -57,7 +60,7 @@ export function createLanguageServer(connection: vscode.Connection) {

if (options.documentFeatures) {

const ts = loadTypescript(options.typescript.serverPath);
const ts = runtimeEnv.loadTypescript(options);
const formatters = await import('./formatters');
const noStateLs = vue.getDocumentLanguageService(
{ typescript: ts },
Expand All @@ -83,7 +86,7 @@ export function createLanguageServer(connection: vscode.Connection) {
let projects: ReturnType<typeof createProjects> | undefined;
const lsConfigs = params.capabilities.workspace?.configuration ? createLsConfigs(folders, connection) : undefined;

const ts = loadTypescript(options.typescript.serverPath);
const ts = runtimeEnv.loadTypescript(options);

(await import('./features/customFeatures')).register(connection, documents, () => projects);
(await import('./features/languageFeatures')).register(ts, connection, configuration, documents, () => projects, options.languageFeatures, lsConfigs, params);
Expand All @@ -92,13 +95,14 @@ export function createLanguageServer(connection: vscode.Connection) {
connection.onInitialized(async () => {

const inferredCompilerOptions = await getInferredCompilerOptions(ts, configuration);
const tsLocalized = options.typescript.localizedPath ? loadTypescriptLocalized(options.typescript.localizedPath) : undefined;
const tsLocalized = runtimeEnv.loadTypescriptLocalized(options);

if (params.capabilities.workspace?.didChangeConfiguration?.dynamicRegistration) { // TODO
connection.client.register(vscode.DidChangeConfigurationNotification.type);
}

projects = createProjects(
runtimeEnv,
folders,
ts,
tsLocalized,
Expand All @@ -119,13 +123,3 @@ export function createLanguageServer(connection: vscode.Connection) {
return result;
}
}

function loadTypescript(tsPath: string): typeof import('typescript/lib/tsserverlibrary') {
return require(path.toUnix(tsPath));
}

function loadTypescriptLocalized(tsPath: string): ts.MapLike<string> | undefined {
if (fs.existsSync(tsPath)) {
return require(path.toUnix(tsPath));
}
}
25 changes: 22 additions & 3 deletions packages/server/src/features/customFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as shared from '@volar/shared';
import * as fs from 'fs';
import * as path from 'upath';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as vscode from 'vscode-languageserver';
Expand Down Expand Up @@ -37,11 +36,31 @@ export function register(
if (!ls) continue;
const localTypes = ls.__internal__.getLocalTypesFiles(lsType);
for (const fileName of localTypes.fileNames) {
fs.writeFile(fileName, localTypes.code, () => { });
connection.workspace.applyEdit({
edit: {
documentChanges: [
vscode.CreateFile.create(shared.fsPathToUri(fileName)),
vscode.TextDocumentEdit.create(
vscode.OptionalVersionedTextDocumentIdentifier.create(shared.fsPathToUri(fileName), null),
[{ range: vscode.Range.create(0, 0, 0, 0), newText: localTypes.code }],
),
]
}
});
}
const { sourceFiles } = await ls.__internal__.getContext();
for (const [_, doc] of sourceFiles.getTsDocuments(lsType)) {
fs.writeFile(shared.uriToFsPath(doc.uri), doc.getText(), () => { });
connection.workspace.applyEdit({
edit: {
documentChanges: [
vscode.CreateFile.create(doc.uri),
vscode.TextDocumentEdit.create(
vscode.OptionalVersionedTextDocumentIdentifier.create(doc.uri, null),
[{ range: vscode.Range.create(0, 0, 0, 0), newText: doc.getText() }],
),
]
}
});
}
}
}
Expand Down
29 changes: 24 additions & 5 deletions packages/server/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
/**
* NOTE! This file will be rename to node.ts in future
*/

import * as vscode from 'vscode-languageserver/node';
import { createLanguageServer } from './common';
import { configure as configureHttpRequests } from 'request-light';
import fileSchemaRequestHandler from './schemaRequestHandlers/file';
import httpSchemaRequestHandler from './schemaRequestHandlers/http';
import * as path from 'upath';

const connection = vscode.createConnection(vscode.ProposedFeatures.all);

createLanguageServer(connection);
createLanguageServer(connection, {
loadTypescript(options) {
return require(path.toUnix(options.typescript.serverPath));
},
loadTypescriptLocalized(options) {
if (options.typescript.localizedPath) {
try {
return require(path.toUnix(options.typescript.localizedPath));
} catch { }
}
},
schemaRequestHandlers: {
file: fileSchemaRequestHandler,
http: httpSchemaRequestHandler,
https: httpSchemaRequestHandler,
},
onDidChangeConfiguration(settings) {
configureHttpRequests(settings.http && settings.http.proxy, settings.http && settings.http.proxyStrictSSL);
},
});
Loading

0 comments on commit 75c48db

Please sign in to comment.