From ff099b31734ab6a5c030ea0af088c619e589fec2 Mon Sep 17 00:00:00 2001 From: so1ve Date: Tue, 31 Oct 2023 14:26:51 +0800 Subject: [PATCH] feat: make options optional --- README.md | 29 ++++++++++++++++++++--------- src/core/index.ts | 6 ++++-- src/core/types.ts | 2 +- src/core/utils.ts | 8 ++++++++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8ab771a..94bbe01 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,11 @@ $ pnpm add -D unplugin-vue-complex-types import VueComplexTypes from "unplugin-vue-complex-types/vite"; export default defineConfig({ - plugins: [VueComplexTypes({})], + plugins: [ + VueComplexTypes({ + /* Options */ + }), + ], }); ``` @@ -44,7 +48,7 @@ import VueComplexTypes from "unplugin-vue-complex-types/rollup"; export default { plugins: [ VueComplexTypes({ - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }), // other plugins ], @@ -62,7 +66,7 @@ module.exports = { /* ... */ plugins: [ require("unplugin-vue-complex-types/webpack")({ - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }), ], }; @@ -78,7 +82,7 @@ module.exports = { export default defineNuxtConfig({ modules: ["unplugin-vue-complex-types/nuxt"], complexTypes: { - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }, }); ``` @@ -94,7 +98,7 @@ module.exports = { configureWebpack: { plugins: [ require("unplugin-vue-complex-types/webpack")({ - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }), ], }, @@ -113,7 +117,7 @@ module.exports = { [ "unplugin-vue-complex-types/vite", { - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }, ], ], @@ -129,7 +133,7 @@ module.exports = { chainWebpack(chain) { chain.plugin("unplugin-vue-complex-types").use( VueComplexTypesPlugin({ - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }), ); }, @@ -150,7 +154,7 @@ build({ /* ... */ plugins: [ require("unplugin-vue-complex-types/esbuild")({ - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }), ], }); @@ -168,7 +172,7 @@ import VueComplexTypes from "unplugin-vue-complex-types/astro"; export default defineConfig({ integrations: [ VueComplexTypes({ - tsconfigPath: "tsconfig.json", // Path to your tsconfig.json + /* Options */ }), ], }); @@ -176,6 +180,13 @@ export default defineConfig({
+## 📚 Options + +### `tsconfigPath` + +- Type: `string` +- Default: `path.join(process.cwd(), "tsconfig.json")` + ## 📝 License [MIT](./LICENSE). Made with ❤️ by [Ray](https://github.com/so1ve) diff --git a/src/core/index.ts b/src/core/index.ts index f09ed13..a52a3d7 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -3,13 +3,15 @@ import { createUnplugin } from "unplugin"; import { ensureLanguage } from "./language"; import { transform } from "./transform"; import type { Options } from "./types"; +import { resolveOptions } from "./utils"; export { ensureLanguage, getLanguage } from "./language"; -export default createUnplugin((options) => ({ +export default createUnplugin((options = {}) => ({ name: "unplugin-vue-complex-types", buildStart() { - ensureLanguage(options.tsconfigPath); + const resolvedOptions = resolveOptions(options); + ensureLanguage(resolvedOptions.tsconfigPath); }, transform(code, id) { if (!id.endsWith(".vue")) { diff --git a/src/core/types.ts b/src/core/types.ts index fde4e3a..31ab617 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -1,3 +1,3 @@ export interface Options { - tsconfigPath: string; + tsconfigPath?: string; } diff --git a/src/core/utils.ts b/src/core/utils.ts index ca5f437..b8e85fe 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -1,2 +1,10 @@ +import { join } from "node:path"; + +import type { Options } from "./types"; + const windowsPathReg = /\\/g; export const normalizePath = (id: string) => id.replace(windowsPathReg, "/"); + +export const resolveOptions = (rawOptions: Options): Required => ({ + tsconfigPath: rawOptions.tsconfigPath ?? join(process.cwd(), "tsconfig.json"), +});