Skip to content

Commit 9dd7dbe

Browse files
committed
feat: filter package json field
1 parent 117163b commit 9dd7dbe

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,24 @@ It exports the information of the current `package.json`.
317317
import { name, version } from '~build/package';
318318
```
319319

320+
You can also **control which fields should be exported**.
321+
322+
```ts
323+
// vite.config.ts
324+
325+
import Info from 'unplugin-info/vite';
326+
327+
export default defineConfig({
328+
plugins: [
329+
Info({
330+
package: {
331+
dependencies: true
332+
}
333+
})
334+
]
335+
});
336+
```
337+
320338
## TypeScript
321339

322340
If you did not rename the virtual module, custom exported fields, or use `~build/meta`, you can just add `unplugin-info/client` to your `tsconfig.json` types, or add `/// <reference types="unplugin-info/client" />` to your `.d.ts` file.

examples/vite/env.d.ts

+24
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ declare module '~build/git' {
4545
export const isClean: boolean;
4646
}
4747

48+
declare module '~build/package' {
49+
/** Package name */
50+
export const name: string;
51+
52+
/** Package version */
53+
export const version: string;
54+
55+
/** Package description */
56+
export const description: string;
57+
58+
/** Package keywords */
59+
export const keywords: string[];
60+
61+
/** Package license */
62+
export const license: string;
63+
64+
/** Package author */
65+
export const author: string;
66+
67+
export const dependencies: Record<string, string>;
68+
69+
export const devDependencies: Record<string, string>;
70+
}
71+
4872
declare module '~build/meta' {
4973
export const message: string;
5074
}

examples/vite/main.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
isClean
1919
} from '~build/git';
2020
import { message } from '~build/meta';
21-
import { name, version } from '~build/package';
2221
import { isCI, name as ciName } from '~build/ci';
22+
import { name, version, dependencies, devDependencies } from '~build/package';
2323

2424
import { format } from 'date-fns';
2525

@@ -66,6 +66,10 @@ append('Message: ', message);
6666

6767
append('Package name: ', name);
6868
append('Package version: ', version);
69+
append(
70+
'Dependencies: ',
71+
[...Object.entries(dependencies), ...Object.entries(devDependencies)].map((d) => d[0]).join(' ,')
72+
);
6973
append('', '');
7074

7175
append('You can also open console and play around with it.', '');

examples/vite/vite.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export default defineConfig({
1212
return status.isClean();
1313
}
1414
},
15+
package: {
16+
dependencies: true,
17+
devDependencies: true
18+
},
1519
meta: { message: 'This is set from vite.config.ts' }
1620
})
1721
]

src/core/index.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ export const UnpluginInfo = createUnplugin<Options | undefined>((option) => {
103103
return body.join('\n');
104104
} else if (id === ModuleName.BuildPackage) {
105105
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf-8'));
106+
const defaults = ['name', 'version', 'description', 'keywords', 'license', 'author'];
107+
const keys = new Set(
108+
Array.isArray(option?.package)
109+
? [...defaults, ...option.package]
110+
: typeof option?.package === 'object'
111+
? Object.entries(
112+
Object.fromEntries([
113+
...defaults.map((d) => [d, true] as const),
114+
...Object.entries(option.package)
115+
])
116+
)
117+
.filter(([k, v]) => !!v)
118+
.map(([k, v]) => k)
119+
: defaults
120+
);
106121
const entries = Object.entries({
107122
name: '',
108123
version: '0.0.0',
@@ -111,9 +126,7 @@ export const UnpluginInfo = createUnplugin<Options | undefined>((option) => {
111126
license: '',
112127
author: '',
113128
...pkg
114-
}).filter(([key]) =>
115-
['name', 'version', 'description', 'keywords', 'license', 'author'].includes(key)
116-
);
129+
}).filter(([key]) => keys.has(key));
117130
return entries
118131
.map(([key, value]) => `export const ${key} = ${JSON.stringify(value, null, 2)};`)
119132
.join('\n');

src/core/types.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ export interface Options {
1717
git?: Record<string, (git: SimpleGit) => Promise<any> | any>;
1818

1919
/**
20-
* Custom virtual module prefix
21-
*
22-
* @default '~build'
20+
* Filter exported fields of package.json
2321
*/
24-
prefix?: string;
22+
package?: string[] | Record<string, boolean | null | undefined>;
2523

2624
/**
2725
* Pass some meta data to Vite app
2826
*
2927
* Notice: meta data will be serialized to JSON format
3028
*/
3129
meta?: Record<string | number | symbol, any>;
30+
31+
/**
32+
* Custom virtual module prefix
33+
*
34+
* @default '~build'
35+
*/
36+
prefix?: string;
3237
}

0 commit comments

Comments
 (0)