Skip to content

Commit

Permalink
refactor: use jsonc-parser instead of strip-json-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RSS1102 committed Feb 3, 2024
1 parent 071b191 commit aa0e2a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
9 changes: 6 additions & 3 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

export default {
import { defineBuildConfig } from "unbuild";
export default defineBuildConfig({
entries: ["./src/index"],
rollup: {
emitCJS: true,
esbuild: {
minify: true,
},
},
externals: ['vite'],
declaration: true,
};
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@
"url": "https://github.com/holy-two/vite-plugin-alias"
},
"devDependencies": {
"@holy-two/vite-plugin-alias": "workspace:*",
"@rollup/plugin-commonjs": "^25.0.7",
"typescript": "^5.3.3",
"unbuild": "^2.0.0",
"vite": "^5.0.12"
},
"peerDependencies": {
"vite": ">=4.0.0"
},
"dependencies": {
"@types/node": "^20.11.9",
"strip-json-comments": "^5.0.1"
"jsonc-parser": "^3.2.1"
}
}
28 changes: 14 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Plugin } from 'vite';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import stripJsonComments from 'strip-json-comments';
import { parse } from 'jsonc-parser';

const alias = () => {
const tsconfigPath = join(process.cwd(), 'tsconfig.json');
Expand All @@ -20,11 +20,11 @@ const alias = () => {
} else {
configStr = readFileSync(jsconfigPath, 'utf8');
};
const tsConfig = JSON.parse(stripJsonComments(configStr.replace(/,\s*([\]}])/g, '$1')));
const tsConfig = parse(configStr);
const paths: TsConfigPaths = tsConfig?.compilerOptions?.paths;

if (!paths) {
console.warn("tsconfig.json's paths or jsconfig.json's paths not found");
console.error("tsconfig.json's paths or jsconfig.json's paths not found");
};

// todo: 未处理 tsconfig.json 中的 baseUrl
Expand Down

0 comments on commit aa0e2a8

Please sign in to comment.