aboutsummaryrefslogtreecommitdiff
path: root/packages/config/vite/relAlias.ts
diff options
context:
space:
mode:
authorPauline <git@ethanlibs.co>2023-11-26 17:13:07 -0500
committerPauline <git@ethanlibs.co>2023-11-26 17:13:07 -0500
commite9d485fe3b5db8c426ac03b30ed8917df0baa62d (patch)
tree69a684d53abce6b636620cc0000a6e84846246d7 /packages/config/vite/relAlias.ts
parent85c31ee8d278ac6fa1f0ba143b78d65e5f665f32 (diff)
downloadNexus-e9d485fe3b5db8c426ac03b30ed8917df0baa62d.tar.gz
Nexus-e9d485fe3b5db8c426ac03b30ed8917df0baa62d.tar.bz2
Nexus-e9d485fe3b5db8c426ac03b30ed8917df0baa62d.zip
feat(lint): switch to eslint config and formatting (use vscode for autoformat)
Diffstat (limited to 'packages/config/vite/relAlias.ts')
-rw-r--r--packages/config/vite/relAlias.ts33
1 files changed, 21 insertions, 12 deletions
diff --git a/packages/config/vite/relAlias.ts b/packages/config/vite/relAlias.ts
index 1a502b5..7bafe74 100644
--- a/packages/config/vite/relAlias.ts
+++ b/packages/config/vite/relAlias.ts
@@ -1,10 +1,11 @@
-import fs from 'fs/promises';
-import path from 'path';
-import { Alias } from 'vite';
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import type { Alias } from 'vite';
const projectPath = path.resolve(__dirname, '../../../');
const pkgJsonCache = new Map();
+// /src/ or \src\, depending on platform
const SRC_DIR_PATH = `${path.sep}src${path.sep}`;
const resolver: Alias = {
@@ -13,15 +14,18 @@ const resolver: Alias = {
async customResolver(source, importer) {
let root: null | string = null;
- if (importer) importer = path.normalize(importer);
+ if (importer)
+ importer = path.normalize(importer);
+ // source is the path imported on typescript, which always use / as path separator
const [_, sourcePath] = source.split('~/');
const relativeImporter = importer?.replace(projectPath, '');
if (relativeImporter && relativeImporter.includes(SRC_DIR_PATH)) {
const [pkg] = relativeImporter.split(SRC_DIR_PATH);
root = path.join(projectPath, pkg, 'src');
- } else if (importer) {
+ }
+ else if (importer) {
const pathObj = path.parse(importer);
let parent = pathObj.dir;
@@ -30,13 +34,15 @@ const resolver: Alias = {
let hasPkgJson = pkgJsonCache.get(parent);
- if (hasPkgJson === undefined)
+ if (hasPkgJson === undefined) {
try {
await fs.stat(path.join(parent, 'package.json'));
pkgJsonCache.set(parent, (hasPkgJson = true));
- } catch {
+ }
+ catch {
pkgJsonCache.set(parent, (hasPkgJson = false));
}
+ }
if (hasPkgJson) {
root = parent;
@@ -46,7 +52,8 @@ const resolver: Alias = {
if (root === null)
throw new Error(`Failed to resolve import path ${source} in file ${importer}`);
- } else {
+ }
+ else {
throw new Error(`Failed to resolve import path ${source} in file ${importer}`);
}
@@ -54,7 +61,8 @@ const resolver: Alias = {
const folderItems = await fs.readdir(path.join(absolutePath, '..'));
- const item = folderItems.find((i) => i.startsWith(sourcePath.split('/').at(-1)!))!;
+ // sourcePath is derived from the path imported on typescript, which always use / as path separator
+ const item = folderItems.find(i => i.startsWith(sourcePath.split('/').at(-1)!))!;
const fullPath = absolutePath + path.extname(item);
@@ -63,15 +71,16 @@ const resolver: Alias = {
if (stats.isDirectory()) {
const directoryItems = await fs.readdir(absolutePath + path.extname(item));
- const indexFile = directoryItems.find((i) => i.startsWith('index'));
+ const indexFile = directoryItems.find(i => i.startsWith('index'));
if (!indexFile)
throw new Error(`Failed to resolve import path ${source} in file ${importer}`);
return path.join(absolutePath, indexFile);
- } else {
+ }
+ else {
return fullPath;
}
- }
+ },
};
export default resolver;