@@ -10,81 +10,89 @@ import { BuildGitModule } from './modules/git';
10
10
import { LegacyInfoModule } from './modules/info' ;
11
11
import { BuildCIModule } from './modules/ci' ;
12
12
import { BuildMetaModule } from './modules/meta' ;
13
+ import { BuildEnvModule } from './modules/env' ;
13
14
import { BuildPackageModule } from './modules/package' ;
14
15
15
16
export * from './types' ;
16
17
17
- export const UnpluginInfo = createUnplugin < Options | undefined > ( ( options = { } ) => {
18
- const root = path . resolve ( options ?. root ?? process . cwd ( ) ) ;
18
+ export const UnpluginInfo = /* #__PURE__ */ createUnplugin < Options | undefined > (
19
+ ( options = { } , meta ) => {
20
+ const root = path . resolve ( options ?. root ?? process . cwd ( ) ) ;
19
21
20
- const modules = {
21
- Time : new BuildTimeModule ( root , options ) ,
22
- Git : new BuildGitModule ( root , options ) ,
23
- Info : new LegacyInfoModule ( root , options ) ,
24
- CI : new BuildCIModule ( root , options ) ,
25
- Meta : new BuildMetaModule ( root , options ) ,
26
- Package : new BuildPackageModule ( root , options )
27
- } ;
22
+ const modules = {
23
+ Time : new BuildTimeModule ( root , options ) ,
24
+ Git : new BuildGitModule ( root , options ) ,
25
+ Info : new LegacyInfoModule ( root , options ) ,
26
+ CI : new BuildCIModule ( root , options ) ,
27
+ Meta : new BuildMetaModule ( root , options ) ,
28
+ Env : new BuildEnvModule ( root , options ) ,
29
+ Package : new BuildPackageModule ( root , options )
30
+ } ;
28
31
29
- return {
30
- name : 'unplugin-info' ,
31
- async buildStart ( ) {
32
- await Promise . all ( Object . values ( modules ) . map ( ( mod ) => mod . buildStart ( this ) ) ) ;
33
- } ,
34
- async buildEnd ( ) {
35
- await Promise . all ( Object . values ( modules ) . map ( ( mod ) => mod . buildEnd ( this ) ) ) ;
36
- } ,
37
- resolveId ( id ) {
38
- if (
39
- Object . values ( modules )
32
+ return {
33
+ name : 'unplugin-info' ,
34
+ async buildStart ( ) {
35
+ await Promise . all ( Object . values ( modules ) . map ( ( mod ) => mod . buildStart ( this ) ) ) ;
36
+ } ,
37
+ async buildEnd ( ) {
38
+ await Promise . all ( Object . values ( modules ) . map ( ( mod ) => mod . buildEnd ( this ) ) ) ;
39
+ } ,
40
+ resolveId ( id ) {
41
+ if (
42
+ Object . values ( modules )
43
+ . map ( ( m ) => m . name )
44
+ . includes ( id )
45
+ ) {
46
+ return `\0${ id } ` ;
47
+ }
48
+ } ,
49
+ loadInclude ( id ) {
50
+ if ( ! id . startsWith ( '\0' ) ) return false ;
51
+ id = id . slice ( 1 ) ;
52
+ return Object . values ( modules )
40
53
. map ( ( m ) => m . name )
41
- . includes ( id )
42
- ) {
43
- return `\0${ id } ` ;
44
- }
45
- } ,
46
- loadInclude ( id ) {
47
- if ( ! id . startsWith ( '\0' ) ) return false ;
48
- id = id . slice ( 1 ) ;
49
- return Object . values ( modules )
50
- . map ( ( m ) => m . name )
51
- . includes ( id ) ;
52
- } ,
53
- async load ( id ) {
54
- if ( ! id . startsWith ( '\0' ) ) return ;
55
- id = id . slice ( 1 ) ;
54
+ . includes ( id ) ;
55
+ } ,
56
+ async load ( id ) {
57
+ if ( ! id . startsWith ( '\0' ) ) return ;
58
+ id = id . slice ( 1 ) ;
56
59
57
- for ( const mod of Object . values ( modules ) ) {
58
- if ( id === mod . name ) {
59
- if ( id === modules . Info . name ) {
60
- this . warn (
61
- `${ modules . Info . name } is deprecated, please migrate to ${ modules . Git . name } and ${ modules . CI . name } `
62
- ) ;
63
- }
60
+ for ( const mod of Object . values ( modules ) ) {
61
+ if ( id === mod . name ) {
62
+ if ( id === modules . Info . name ) {
63
+ this . warn (
64
+ `${ modules . Info . name } is deprecated, please migrate to ${ modules . Git . name } and ${ modules . CI . name } .`
65
+ ) ;
66
+ }
67
+ if ( id === modules . Env . name && meta . framework !== 'vite' ) {
68
+ this . warn ( `${ modules . Env . name } is only supported in Vite.` ) ;
69
+ return ;
70
+ }
64
71
65
- return mod . load ( this , id ) ;
72
+ return mod . load ( this , id ) ;
73
+ }
66
74
}
67
- }
68
- } ,
69
- vite : {
70
- handleHotUpdate ( { file, server } ) {
71
- // HMR: package.json
72
- if ( file === normalizePath ( path . resolve ( root , 'package.json' ) ) ) {
73
- const module = server . moduleGraph . getModuleById ( '\0' + modules . Package . name ) ;
74
- if ( module ) {
75
- // Invalidate module for reloading
76
- server . moduleGraph . invalidateModule ( module ) ;
75
+ } ,
76
+ vite : {
77
+ handleHotUpdate ( { file, server } ) {
78
+ // HMR: package.json
79
+ if ( file === normalizePath ( path . resolve ( root , 'package.json' ) ) ) {
80
+ const module = server . moduleGraph . getModuleById ( '\0' + modules . Package . name ) ;
81
+ if ( module ) {
82
+ // Invalidate module for reloading
83
+ server . moduleGraph . invalidateModule ( module ) ;
77
84
78
- // Reload client
79
- server . ws . send ( {
80
- type : 'full-reload'
81
- } ) ;
85
+ // Reload client
86
+ server . ws . send ( {
87
+ type : 'full-reload'
88
+ } ) ;
89
+ }
82
90
}
83
91
}
84
92
}
85
- }
86
- } ;
87
- } ) ;
93
+ } ;
94
+ }
95
+ ) ;
88
96
89
97
function normalizePath ( filename : string ) {
90
98
return filename . split ( path . win32 . sep ) . join ( path . posix . sep ) ;
0 commit comments