Skip to content

Commit

Permalink
fix: Avoid downstream need node polyfill for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 31, 2023
1 parent f5141bd commit 860aec2
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 26 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"chrome": "vscode-test-web --browserType=chromium --extensionDevelopmentPath=./extensions/vscode ../volar-starter"
},
"devDependencies": {
"@types/node": "latest",
"@volar/language-service": "~1.10.9",
"typescript": "latest",
"vite": "latest",
Expand Down
3 changes: 3 additions & 0 deletions packages/component-meta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"typescript": {
"optional": true
}
},
"devDependencies": {
"@types/node": "latest"
}
}
18 changes: 9 additions & 9 deletions packages/component-meta/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export type ComponentMetaChecker = ReturnType<typeof baseCreate>;
const windowsPathReg = /\\/g;

export function createComponentMetaCheckerByJsonConfig(
root: string,
_rootPath: string,
json: any,
checkerOptions: MetaCheckerOptions = {},
ts: typeof import('typescript/lib/tsserverlibrary') = require('typescript'),
) {
const rootPath = (root as path.OsPath).replace(windowsPathReg, '/') as path.PosixPath;
const rootPath = _rootPath.replace(windowsPathReg, '/') as path.PosixPath;
return createComponentMetaCheckerWorker(
() => vue.createParsedCommandLineByJson(ts, ts.sys, root, json),
() => vue.createParsedCommandLineByJson(ts, ts.sys, rootPath, json),
checkerOptions,
rootPath,
path.join(rootPath, 'jsconfig.json.global.vue' as path.PosixPath),
Expand All @@ -38,13 +38,13 @@ export function createComponentMetaCheckerByJsonConfig(
}

export function createComponentMetaChecker(
tsconfigPath: string,
_tsconfig: string,
checkerOptions: MetaCheckerOptions = {},
ts: typeof import('typescript/lib/tsserverlibrary') = require('typescript'),
) {
const tsconfig = (tsconfigPath as path.OsPath).replace(windowsPathReg, '/') as path.PosixPath;
const tsconfig = _tsconfig.replace(windowsPathReg, '/') as path.PosixPath;
return createComponentMetaCheckerWorker(
() => vue.createParsedCommandLine(ts, ts.sys, tsconfigPath),
() => vue.createParsedCommandLine(ts, ts.sys, tsconfig),
checkerOptions,
path.dirname(tsconfig),
tsconfig + '.global.vue',
Expand Down Expand Up @@ -152,11 +152,11 @@ export function baseCreate(
return _host[prop as keyof typeof _host];
},
}) as vue.TypeScriptLanguageHost;
const vueLanguages = ts ? vue.createLanguages(
const vueLanguages = vue.createLanguages(
ts,
host.getCompilationSettings(),
vueCompilerOptions,
ts,
) : [];
);
const core = vue.createLanguageContext(host, vueLanguages);
const tsLsHost = createLanguageServiceHost(core, ts, ts.sys);
const tsLs = ts.createLanguageService(tsLsHost);
Expand Down
3 changes: 3 additions & 0 deletions packages/language-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
"computeds": "^0.0.1",
"minimatch": "^9.0.3",
"muggle-string": "^0.3.1",
"path-browserify": "^1.0.1",
"vue-template-compiler": "^2.7.14"
},
"devDependencies": {
"@types/minimatch": "^5.1.2",
"@types/node": "latest",
"@types/path-browserify": "^1.0.1",
"@vue/compiler-sfc": "^3.3.0"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FileRangeCapabilities, MirrorBehaviorCapabilities } from '@volar/langua
import * as SourceMaps from '@volar/source-map';
import { Segment, getLength } from '@volar/source-map';
import * as muggle from 'muggle-string';
import { posix as path } from 'path';
import * as path from 'path-browserify';
import type * as ts from 'typescript/lib/tsserverlibrary';
import type * as templateGen from '../generators/template';
import type { ScriptRanges } from '../parsers/scriptRanges';
Expand Down
4 changes: 2 additions & 2 deletions packages/language-core/src/languageModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Language } from '@volar/language-core';
import { posix as path } from 'path';
import * as path from 'path-browserify';
import { getDefaultVueLanguagePlugins } from './plugins';
import { VueFile } from './virtualFile/vueFile';
import { VueCompilerOptions, VueLanguagePlugin } from './types';
Expand Down Expand Up @@ -118,9 +118,9 @@ export function createVueLanguage(
* @deprecated planed to remove in 2.0, please use createVueLanguage instead of
*/
export function createLanguages(
ts: typeof import('typescript/lib/tsserverlibrary'),
compilerOptions: ts.CompilerOptions = {},
vueCompilerOptions: Partial<VueCompilerOptions> = {},
ts: typeof import('typescript/lib/tsserverlibrary') = require('typescript'),
codegenStack: boolean = false,
): Language[] {
return [
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/src/utils/ts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import * as path from 'path-browserify';
import type { RawVueCompilerOptions, VueCompilerOptions, VueLanguagePlugin } from '../types';

export type ParsedCommandLine = ts.ParsedCommandLine & {
Expand Down
1 change: 1 addition & 0 deletions packages/language-plugin-pug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"directory": "packages/language-plugin-pug"
},
"devDependencies": {
"@types/node": "latest",
"@vue/language-core": "1.8.22"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/languageServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export function createServerPlugin(connection: Connection) {
}

return vue.resolveConfig(
ts,
config,
ctx?.host.getCompilationSettings() ?? {},
vueOptions,
ts,
initOptions.codegenStack,
);

Expand Down
1 change: 1 addition & 0 deletions packages/language-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"vscode-languageserver-textdocument": "^1.0.11"
},
"devDependencies": {
"@types/node": "latest",
"@volar/kit": "~1.10.9",
"vscode-languageserver-protocol": "^3.17.5",
"vscode-uri": "^3.0.8"
Expand Down
4 changes: 2 additions & 2 deletions packages/language-service/src/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export interface Settings {
}

export function resolveConfig(
ts: typeof import('typescript/lib/tsserverlibrary'),
config: Config,
compilerOptions: ts.CompilerOptions = {},
vueCompilerOptions: Partial<VueCompilerOptions> = {},
ts: typeof import('typescript/lib/tsserverlibrary') = require('typescript'),
codegenStack: boolean = false,
) {

const resolvedVueCompilerOptions = resolveVueCompilerOptions(vueCompilerOptions);
const vueLanguageModules = createLanguages(compilerOptions, resolvedVueCompilerOptions, ts, codegenStack);
const vueLanguageModules = createLanguages(ts, compilerOptions, resolvedVueCompilerOptions, codegenStack);

config.languages = Object.assign({}, vueLanguageModules, config.languages);
config.services = resolvePlugins(config.services, resolvedVueCompilerOptions);
Expand Down
2 changes: 1 addition & 1 deletion packages/language-service/tests/utils/createTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function createTester(root: string) {
},
}
},
resolveConfig({}),
resolveConfig(ts, {}),
host,
);

Expand Down
3 changes: 2 additions & 1 deletion packages/language-service/tests/utils/format.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expect, it } from 'vitest';
import * as kit from '@volar/kit';
import { resolveConfig } from '../../out';
import * as ts from 'typescript';

const formatter = kit.createFormatter(resolveConfig({}));
const formatter = kit.createFormatter(resolveConfig(ts as any, {}));

export function defineFormatTest(options: {
title: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/tsc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
},
"peerDependencies": {
"typescript": "*"
},
"devDependencies": {
"@types/node": "latest"
}
}
4 changes: 2 additions & 2 deletions packages/tsc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ export function createProgram(options: ts.CreateProgramOptions) {
const languageContext = vue.createLanguageContext(
languageHost,
vue.createLanguages(
ts,
languageHost.getCompilationSettings(),
vueCompilerOptions,
ts,
),
);
const languageServiceHost = volarTs.createLanguageServiceHost(languageContext, ts, ts.sys);
Expand All @@ -97,7 +97,7 @@ export function createProgram(options: ts.CreateProgramOptions) {
function getVueCompilerOptions(): Partial<vue.VueCompilerOptions> {
const tsConfig = ctx.options.options.configFilePath;
if (typeof tsConfig === 'string') {
return vue.createParsedCommandLine(ts as any, ts.sys, tsConfig).vueOptions;
return vue.createParsedCommandLine(ts as any, ts.sys, tsConfig.replace(windowsPathReg, '/')).vueOptions;
}
return {};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/typescript-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vue from '@vue/language-core';
import type * as ts from 'typescript/lib/tsserverlibrary';

const externalFiles = new WeakMap<ts.server.Project, string[]>();
const windowsPathReg = /\\/g;

const init: ts.server.PluginModuleFactory = (modules) => {
const { typescript: ts } = modules;
Expand All @@ -11,9 +12,9 @@ const init: ts.server.PluginModuleFactory = (modules) => {

const virtualFiles = vue.createVirtualFiles(
vue.createLanguages(
ts,
info.languageServiceHost.getCompilationSettings(),
getVueCompilerOptions(),
ts,
),
);

Expand All @@ -35,7 +36,7 @@ const init: ts.server.PluginModuleFactory = (modules) => {
function getVueCompilerOptions() {
if (info.project.projectKind === ts.server.ProjectKind.Configured) {
const tsconfig = info.project.getProjectName();
return vue.createParsedCommandLine(ts, ts.sys, tsconfig).vueOptions;
return vue.createParsedCommandLine(ts, ts.sys, tsconfig.replace(windowsPathReg, '/')).vueOptions;
}
else {
return vue.createParsedCommandLineByJson(ts, ts.sys, info.languageServiceHost.getCurrentDirectory(), {}).vueOptions;
Expand Down
30 changes: 27 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 860aec2

Please sign in to comment.