Skip to content

Commit

Permalink
feat: compatible jsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
RSS1102 committed Jan 29, 2024
1 parent 50c6b47 commit 5bf79ec
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"node": ">=18.x"
},
"bugs": {
"url": "https://github.com/holy-two/vite-plugin-alias-from-types"
"url": "https://github.com/holy-two/vite-plugin-alias#readme"
},
"homepage": "https://github.com/holy-two/vite-plugin-alias-from-types#readme",
"homepage": "https://github.com/holy-two/vite-plugin-alias#readme",
"repository": {
"type": "git",
"url": "https://github.com/holy-two/vite-plugin-alias-from-types"
"url": "https://github.com/holy-two/vite-plugin-alias#readme"
},
"devDependencies": {
"typescript": "^5.3.3",
Expand Down
2 changes: 0 additions & 2 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
},
"include": [
"src/**/*.vue",
"src/**/*.ts",
"vite.config.ts"
],
}
30 changes: 21 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@ import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import stripJsonComments from 'strip-json-comments';

const filePath = join(process.cwd(), 'tsconfig.json');

const alias = () => {
const fileExists = existsSync(filePath);
if (!fileExists) {
return [{ find: '@', replacement: join(process.cwd(), 'src') }]
const tsconfigPath = join(process.cwd(), 'tsconfig.json');
const jsconfigPath = join(process.cwd(), 'jsconfig.json');

const tsconfigExists = existsSync(tsconfigPath);
const jsconfigExists = existsSync(jsconfigPath);

if (!tsconfigExists && !jsconfigExists) {
console.warn('tsconfig.json or jsconfig.json not found');
};

const tsConfigStr = readFileSync(filePath, 'utf8');
const tsConfig = JSON.parse(stripJsonComments(tsConfigStr.replace(/,\s*([\]}])/g, '$1')));
let configStr = '';
if (tsconfigExists) {
configStr = readFileSync(tsconfigPath, 'utf8');
} else {
configStr = readFileSync(jsconfigPath, 'utf8');
};
const tsConfig = JSON.parse(stripJsonComments(configStr.replace(/,\s*([\]}])/g, '$1')));
const paths: TsConfigPaths = tsConfig?.compilerOptions?.paths;

if (!paths) {
return [{ find: '@', replacement: join(process.cwd(), 'src') }]
console.warn("tsconfig.json's paths or jsconfig.json's paths not found");
};

// todo: 未处理 tsconfig.json 中的 baseUrl
// todo: 判断数据格式是否正确

const alias: AliasArr = [];
Object.entries(paths).forEach(([key, value]) => {
alias.push(({ find: key?.replace(/\/\*$/, ''), replacement: join(process.cwd(), value[0]?.replace(/\/\*$/, '')) }));
});

return alias;
};

Expand Down
17 changes: 12 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"lib": [
"ES2020",
"DOM",
"DOM.Iterable"
],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["playground/**/*.ts", "src/**/*.ts", "playground/**/*.vue"],
}
"include": [
"playground/**/*.ts",
"playground/**/*.vue",
"src/**/*.ts",
"vite.config.ts"
],
}

0 comments on commit 5bf79ec

Please sign in to comment.