Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use pathe instead of path-browserify #5088

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extensions/vscode/scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
const path = require('path');
const fs = require('fs');
const fs = require('node:fs');
const path = require('node:path');

require('esbuild').context({
entryPoints: {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
"@tsslint/config": "latest",
"typescript": "latest",
"vitest": "latest"
},
"pnpm": {
"patchedDependencies": {
"pathe": "patches/pathe.patch"
}
}
}
8 changes: 4 additions & 4 deletions packages/component-meta/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
First of all, you need to create a component meta checker using `createChecker`:

```ts
import * as url from 'url'
import path from 'path'
import * as path from 'node:path'
import * as url from 'node:url'

import type { MetaCheckerOptions } from 'vue-component-meta'
import { createChecker } from 'vue-component-meta'
Expand All @@ -31,8 +31,8 @@ const tsconfigChecker = createChecker(
Now, you can extract the component meta using `getComponentMeta` method of checker:

```ts
import * as url from 'url'
import path from 'path'
import * as path from 'node:path'
import * as url from 'node:url'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

Expand Down
16 changes: 7 additions & 9 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TypeScriptProjectHost, createLanguageServiceHost, resolveFileLanguageId } from '@volar/typescript';
import * as vue from '@vue/language-core';
import { posix as path } from 'path-browserify';
import path from 'pathe';
import type * as ts from 'typescript';
import { code as typeHelpersCode } from 'vue-component-type-helpers';
import { code as vue2TypeHelpersCode } from 'vue-component-type-helpers/vue2';
Expand All @@ -18,15 +18,13 @@

export * from './types';

const windowsPathReg = /\\/g;

export function createCheckerByJsonConfigBase(
ts: typeof import('typescript'),
rootDir: string,
json: any,
checkerOptions: MetaCheckerOptions = {}
) {
rootDir = rootDir.replace(windowsPathReg, '/');
rootDir = path.normalize(rootDir);
return baseCreate(
ts,
() => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json, undefined, true),
Expand All @@ -41,7 +39,7 @@
tsconfig: string,
checkerOptions: MetaCheckerOptions = {}
) {
tsconfig = tsconfig.replace(windowsPathReg, '/');
tsconfig = path.normalize(tsconfig);
return baseCreate(
ts,
() => vue.createParsedCommandLine(ts, ts.sys, tsconfig, true),
Expand All @@ -59,14 +57,14 @@
globalComponentName: string
) {
let commandLine = getCommandLine();
let fileNames = commandLine.fileNames.map(path => path.replace(windowsPathReg, '/'));
let fileNames = commandLine.fileNames.map(path.normalize);
let projectVersion = 0;

const projectHost: TypeScriptProjectHost = {
getCurrentDirectory: () => rootPath,
getProjectVersion: () => projectVersion.toString(),
getCompilationSettings: () => commandLine.options,
getScriptFileNames: () => fileNames,

Check failure on line 67 in packages/component-meta/lib/base.ts

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

Type 'unknown[]' is not assignable to type 'string[]'.

Check failure on line 67 in packages/component-meta/lib/base.ts

View workflow job for this annotation

GitHub Actions / build (18, ubuntu-latest)

Type 'unknown[]' is not assignable to type 'string[]'.

Check failure on line 67 in packages/component-meta/lib/base.ts

View workflow job for this annotation

GitHub Actions / build

Type 'unknown[]' is not assignable to type 'string[]'.
getProjectReferences: () => commandLine.projectReferences,
};
const globalComponentSnapshot = ts.ScriptSnapshot.fromString('<script setup lang="ts"></script>');
Expand Down Expand Up @@ -188,18 +186,18 @@
getExportNames,
getComponentMeta,
updateFile(fileName: string, text: string) {
fileName = fileName.replace(windowsPathReg, '/');
fileName = path.normalize(fileName);
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(text));
projectVersion++;
},
deleteFile(fileName: string) {
fileName = fileName.replace(windowsPathReg, '/');
fileName = path.normalize(fileName);
fileNames = fileNames.filter(f => f !== fileName);
projectVersion++;
},
reload() {
commandLine = getCommandLine();
fileNames = commandLine.fileNames.map(path => path.replace(windowsPathReg, '/'));
fileNames = commandLine.fileNames.map(path.normalize);
this.clearCache();
},
clearCache() {
Expand Down
5 changes: 2 additions & 3 deletions packages/component-meta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"@volar/typescript": "~2.4.11",
"@vue/language-core": "2.2.0",
"path-browserify": "^1.0.1",
"pathe": "^1.1.2",
"vue-component-type-helpers": "2.2.0"
},
"peerDependencies": {
Expand All @@ -27,7 +27,6 @@
}
},
"devDependencies": {
"@types/node": "latest",
"@types/path-browserify": "latest"
"@types/node": "latest"
}
}
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/componentSelf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path-browserify';
import path from 'pathe';
import type { Code } from '../../types';
import type { TemplateCodegenContext } from '../template/context';
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Mapping } from '@volar/language-core';
import * as path from 'path-browserify';
import path from 'pathe';
import type * as ts from 'typescript';
import type { ScriptRanges } from '../../parsers/scriptRanges';
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path-browserify';
import path from 'pathe';
import type { Code } from '../../types';
import { getSlotsPropertyName, hyphenateTag } from '../../utils/shared';
import { TemplateCodegenContext, createTemplateCodegenContext } from '../template/context';
Expand Down
2 changes: 1 addition & 1 deletion packages/language-core/lib/utils/ts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { camelize } from '@vue/shared';
import { posix as path } from 'path-browserify';
import path from 'pathe';
import type * as ts from 'typescript';
import { generateGlobalTypes } from '../codegen/globalTypes';
import { getAllExtensions } from '../languagePlugin';
Expand Down
3 changes: 1 addition & 2 deletions packages/language-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
"alien-signals": "^0.4.9",
"minimatch": "^9.0.3",
"muggle-string": "^0.4.1",
"path-browserify": "^1.0.1"
"pathe": "^1.1.2"
},
"devDependencies": {
"@types/minimatch": "^5.1.2",
"@types/node": "latest",
"@types/path-browserify": "^1.0.1",
"@volar/typescript": "~2.4.11",
"@vue/compiler-sfc": "^3.5.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/language-server/lib/hybridModeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createLanguageServiceEnvironment } from '@volar/language-server/lib/pro
import { createLanguage } from '@vue/language-core';
import { createLanguageService, createUriMap, LanguageService } from '@vue/language-service';
import { configuredServers, getBestServer, inferredServers, onServerReady } from '@vue/typescript-plugin/lib/utils';
import path from 'pathe';
import { URI } from 'vscode-uri';

export function createHybridModeProject(
Expand Down Expand Up @@ -86,7 +87,7 @@ export function createHybridModeProject(
return project;

function asFileName(uri: URI) {
return uri.fsPath.replace(/\\/g, '/');
return path.normalize(uri.fsPath);
}

async function createLs(server: LanguageServer, tsconfig: string | undefined) {
Expand Down
1 change: 1 addition & 0 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@vue/language-core": "2.2.0",
"@vue/language-service": "2.2.0",
"@vue/typescript-plugin": "2.2.0",
"pathe": "^1.1.2",
"vscode-languageserver-protocol": "^3.17.5",
"vscode-uri": "^3.0.8"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { LanguageServiceContext, LanguageServicePlugin } from '@volar/language-service';
import { hyphenateAttr } from '@vue/language-core';
import path from 'pathe';
import type * as ts from 'typescript';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -69,7 +70,7 @@ export function create(
let sourceCodeOffset = document.offsetAt(selection);

const fileName = context.project.typescript?.uriConverter.asFileName(sourceScript.id)
?? sourceScript.id.fsPath.replace(/\\/g, '/');
?? path.normalize(sourceScript.id.fsPath);

if (sourceScript.generated) {
const serviceScript = sourceScript.generated.languagePlugin.typescript?.getServiceScript(sourceScript.generated.root);
Expand Down
4 changes: 2 additions & 2 deletions packages/language-service/lib/plugins/vue-document-drop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VueVirtualCode, forEachEmbeddedCode } from '@vue/language-core';
import { camelize, capitalize, hyphenate } from '@vue/shared';
import { posix as path } from 'path-browserify';
import path from 'pathe';
import { getUserPreferences } from 'volar-service-typescript/lib/configs/getUserPreferences';
import type * as vscode from 'vscode-languageserver-protocol';
import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -70,7 +70,7 @@
const code = [...forEachEmbeddedCode(root)].find(code => code.id === (sfc.scriptSetup ? 'scriptsetup_raw' : 'script_raw'))!;
const lastImportNode = getLastImportNode(ts, script.ast);
const incomingFileName = context.project.typescript?.uriConverter.asFileName(URI.parse(importUri))
?? URI.parse(importUri).fsPath.replace(/\\/g, '/');
?? path.normalize(URI.parse(importUri).fsPath);

let importPath: string | undefined;

Expand All @@ -89,7 +89,7 @@
importPath = path.relative(path.dirname(root.fileName), incomingFileName)
|| importUri.slice(importUri.lastIndexOf('/') + 1);

if (!importPath.startsWith('./') && !importPath.startsWith('../')) {

Check failure on line 92 in packages/language-service/lib/plugins/vue-document-drop.ts

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

'importPath' is possibly 'undefined'.

Check failure on line 92 in packages/language-service/lib/plugins/vue-document-drop.ts

View workflow job for this annotation

GitHub Actions / build (18, macos-latest)

'importPath' is possibly 'undefined'.

Check failure on line 92 in packages/language-service/lib/plugins/vue-document-drop.ts

View workflow job for this annotation

GitHub Actions / build (18, ubuntu-latest)

'importPath' is possibly 'undefined'.

Check failure on line 92 in packages/language-service/lib/plugins/vue-document-drop.ts

View workflow job for this annotation

GitHub Actions / build (18, ubuntu-latest)

'importPath' is possibly 'undefined'.

Check failure on line 92 in packages/language-service/lib/plugins/vue-document-drop.ts

View workflow job for this annotation

GitHub Actions / build

'importPath' is possibly 'undefined'.

Check failure on line 92 in packages/language-service/lib/plugins/vue-document-drop.ts

View workflow job for this annotation

GitHub Actions / build

'importPath' is possibly 'undefined'.
importPath = './' + importPath;
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/language-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@vue/shared": "^3.5.0",
"@vue/typescript-plugin": "2.2.0",
"alien-signals": "^0.4.9",
"path-browserify": "^1.0.1",
"pathe": "^1.1.2",
"volar-service-css": "0.0.62",
"volar-service-emmet": "0.0.62",
"volar-service-html": "0.0.62",
Expand All @@ -40,7 +40,6 @@
},
"devDependencies": {
"@types/node": "latest",
"@types/path-browserify": "latest",
"@volar/kit": "~2.4.11",
"vscode-languageserver-protocol": "^3.17.5"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/language-service/scripts/update-html-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const langs = [
{
name: 'en',
Expand Down
10 changes: 10 additions & 0 deletions patches/pathe.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
diff --git a/dist/index.d.ts b/dist/index.d.ts
index 531b6906507206b4594ff0ff830d605c4cf6a16a..a7419c0c83af0880c6a41180f46581c5922bc947 100644
--- a/dist/index.d.ts
+++ b/dist/index.d.ts
@@ -1,4 +1,4 @@
-import path$1 from 'node:path';
+import * as path$1 from 'node:path';

declare const sep = "/";
declare const delimiter = ":";
Loading
Loading