Skip to content

Commit

Permalink
fix: jsconfig path alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Viijay-Kr committed Dec 24, 2024
1 parent a9e1ea7 commit 379f1a5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
9 changes: 6 additions & 3 deletions examples/jsconfig-react/jsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
7 changes: 4 additions & 3 deletions examples/jsconfig-react/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -15,7 +16,7 @@ function App() {
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<a className={Qst["class-1"]} href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const Page = () => {
return <span></span>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class-1 {
display: flex;
}
3 changes: 3 additions & 0 deletions examples/jsconfig-react/src/pages/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const Page = () => {
return <span></span>;
};
64 changes: 22 additions & 42 deletions src/store/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
// }
// ]
},
"exclude": ["examples"],
"exclude": ["examples/**/*"],
"include": ["src/**/*"]
}

0 comments on commit 379f1a5

Please sign in to comment.