From 379f1a5ef8cf3793b4083ea7a304e420d7d50f00 Mon Sep 17 00:00:00 2001 From: Viijay-Kr Date: Mon, 23 Dec 2024 11:55:40 +0100 Subject: [PATCH] fix: jsconfig path alias --- eslint.config.mjs | 2 +- examples/jsconfig-react/jsconfig.json | 9 ++- examples/jsconfig-react/src/App.jsx | 7 +- .../common/Questions/QuestionComponent.jsx | 3 + .../Questions/QuestionStyles.module.scss | 3 + examples/jsconfig-react/src/pages/index.jsx | 3 + src/store/Store.ts | 64 +++++++------------ tsconfig.json | 2 +- 8 files changed, 43 insertions(+), 50 deletions(-) create mode 100644 examples/jsconfig-react/src/pages/common/Questions/QuestionComponent.jsx create mode 100644 examples/jsconfig-react/src/pages/common/Questions/QuestionStyles.module.scss create mode 100644 examples/jsconfig-react/src/pages/index.jsx diff --git a/eslint.config.mjs b/eslint.config.mjs index fb35aa5..cc5ebcc 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -3,7 +3,7 @@ import tsParser from "@typescript-eslint/parser"; export default [ { - ignores: ["**/out", "**/dist", "**/*.d.ts", "**/examples"], + ignores: ["**/out", "**/dist", "**/*.d.ts", "**/examples/**/*.jsx"], }, { plugins: { diff --git a/examples/jsconfig-react/jsconfig.json b/examples/jsconfig-react/jsconfig.json index dea08ce..3450aae 100644 --- a/examples/jsconfig-react/jsconfig.json +++ b/examples/jsconfig-react/jsconfig.json @@ -1,9 +1,12 @@ { "compilerOptions": { - "baseUrl": ".", + "baseUrl": "src", "paths": { - "@standard/*": ["src/styles/standard/*"] + "@src/*": ["."], + "@pages/*": ["pages/*"], + "@standard/*": ["styles/standard/*"], + "@common/*": ["pages/common/*"] } }, - "include": ["src/**/*"] + "include": ["src"] } diff --git a/examples/jsconfig-react/src/App.jsx b/examples/jsconfig-react/src/App.jsx index 8ba0c9a..a22cc05 100644 --- a/examples/jsconfig-react/src/App.jsx +++ b/examples/jsconfig-react/src/App.jsx @@ -3,8 +3,9 @@ import reactLogo from "./assets/react.svg"; import viteLogo from "/vite.svg"; import "./App.css"; import styles from "@standard/button.module.css"; -import rootStyles from "src/styles/root.module.css"; -import rootStyles2 from "src/styles/root2.module.css"; +import rootStyles from "styles/root.module.css"; +import rootStyles2 from "styles/root2.module.css"; +import Qst from "@common/Questions/QuestionStyles.module.scss"; function App() { const [count, setCount] = useState(0); @@ -15,7 +16,7 @@ function App() { Vite logo - + React logo diff --git a/examples/jsconfig-react/src/pages/common/Questions/QuestionComponent.jsx b/examples/jsconfig-react/src/pages/common/Questions/QuestionComponent.jsx new file mode 100644 index 0000000..deac6df --- /dev/null +++ b/examples/jsconfig-react/src/pages/common/Questions/QuestionComponent.jsx @@ -0,0 +1,3 @@ +export const Page = () => { + return ; +}; diff --git a/examples/jsconfig-react/src/pages/common/Questions/QuestionStyles.module.scss b/examples/jsconfig-react/src/pages/common/Questions/QuestionStyles.module.scss new file mode 100644 index 0000000..5e42292 --- /dev/null +++ b/examples/jsconfig-react/src/pages/common/Questions/QuestionStyles.module.scss @@ -0,0 +1,3 @@ +.class-1 { + display: flex; +} diff --git a/examples/jsconfig-react/src/pages/index.jsx b/examples/jsconfig-react/src/pages/index.jsx new file mode 100644 index 0000000..deac6df --- /dev/null +++ b/examples/jsconfig-react/src/pages/index.jsx @@ -0,0 +1,3 @@ +export const Page = () => { + return ; +}; diff --git a/src/store/Store.ts b/src/store/Store.ts index db7c5d6..1451a87 100644 --- a/src/store/Store.ts +++ b/src/store/Store.ts @@ -216,52 +216,32 @@ export class Store { path.dirname(this.getActiveTextDocument().fileName), ); for (const [, config] of this.tsJsConfig) { + const alias = normalizePath(path.dirname(source)); + const module_name = path.basename(source); + const paths = config.compilerOptions.paths; if (activeFileDir.includes(config.baseDir)) { - const alias = normalizePath(path.dirname(source)); - const module_name = path.basename(source); - const paths = config.compilerOptions.paths; - const dir = (paths?.[alias] ?? [""]).join(""); - const baseUrl = config.compilerOptions.baseUrl; - if (baseUrl) { - const final_path = normalizePath( - path.join( - config.baseDir, - config.compilerOptions.baseUrl ?? "", - !!alias.match(/^\@/g)?.[0] ? dir.replace("*", "") : alias, - module_name, - ), - ); - if (this.cssModules.has(final_path)) { - return final_path; + let aliasPath = undefined; + const match = alias.match(/^@\w+/g); + const a = match?.[0]; + for (const [key, val] of Object.entries(paths ?? {})) { + const b = key.match(/^@\w+/g)?.[0]; + if (b === a && !!b && !!a) { + const rest = alias.substring(a?.length); + aliasPath = path.join(val[0].replace("*", ""), rest); + break; } } - for (const [_path, values] of Object.entries(paths ?? {})) { - const alias_dir_path = normalizePath(path.dirname(_path)); - let final_path = ""; - const alias_value = values[0].replace("*", ""); - if (alias === alias_dir_path) { - final_path = normalizePath( - path.join( - config.baseDir, - config.compilerOptions.baseUrl ?? "", - alias_value, - module_name, - ), - ); - } else if (alias.indexOf(alias_dir_path) === 0) { - final_path = normalizePath( - path.join( - config.baseDir, - alias_value, - alias.replace(alias_dir_path, ""), - module_name, - ), - ); - } - if (this.cssModules.has(final_path)) { - return final_path; - } + const final_path = normalizePath( + path.join( + config.baseDir, + config.compilerOptions.baseUrl ?? "", + aliasPath ?? alias, + module_name, + ), + ); + if (this.cssModules.has(final_path)) { + return final_path; } } } diff --git a/tsconfig.json b/tsconfig.json index 4d0738a..08261c5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,6 @@ // } // ] }, - "exclude": ["examples"], + "exclude": ["examples/**/*"], "include": ["src/**/*"] }