Skip to content

Commit

Permalink
add tsconfig.json support
Browse files Browse the repository at this point in the history
  • Loading branch information
FogelAI committed Sep 7, 2023
1 parent 959032a commit fb2d37e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "babel-plugin-transform-barrels",
"version": "1.0.2",
"version": "1.0.5",
"description": "This Babel plugin transforms indirect imports through a barrel file (index.js) into direct imports.",
"homepage": "https://github.com/fogelori/babel-plugin-transform-barrels",
"homepage": "https://github.com/FogelAI/babel-plugin-transform-barrels",
"main": "src/barrel.js",
"repository": {
"type": "git",
"url": "fogelori/babel-plugin-transform-barrels"
"url": "FogelAI/babel-plugin-transform-barrels"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
14 changes: 10 additions & 4 deletions src/barrel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ class PathFunctions {
return importModulePath.match(/\.(js|mjs|jsx|ts|tsx)$/);
}

static getBaseUrlFromTsconfig() {
static getBaseUrlFromJTsconfig() {
try {
const filename = ospath.resolve("jsconfig.json");
const content = JSON.parse(fs.readFileSync(filename, "utf-8"));
let content;
const jsconfig = ospath.resolve("jsconfig.json");
const tsconfig = ospath.resolve("tsconfig.json");
if (fs.existsSync(jsconfig)) {
content = JSON.parse(fs.readFileSync(jsconfig, "utf-8"));
} else if (fs.existsSync(tsconfig)) {
content = JSON.parse(fs.readFileSync(tsconfig, "utf-8"));
}
return content?.["compilerOptions"]?.["baseUrl"] || "./";
} catch (error) {
throw error;
Expand All @@ -39,7 +45,7 @@ class PathFunctions {
try {
const filenameDir = ospath.dirname(filenameImportFrom);
const basePath = PathFunctions.isRelativePath(modulePath) ?
filenameDir : ospath.resolve(PathFunctions.getBaseUrlFromTsconfig());
filenameDir : ospath.resolve(PathFunctions.getBaseUrlFromJTsconfig());
return require.resolve(ospath.resolve(basePath, modulePath));
} catch {
try {
Expand Down

0 comments on commit fb2d37e

Please sign in to comment.