@@ -14,47 +14,68 @@ export interface Options {
14
14
}
15
15
16
16
// vite resolves first before passing to the rollup plugin
17
- export const appConfigImportRegex = / ( a p p - c o n f i g | a p p - c o n f i g - m a i n ) \/ d i s t ( \/ e s ) ? \/ i n d e x \. j s / ;
17
+ export const appConfigImportRegex =
18
+ / ( a p p - c o n f i g | a p p - c o n f i g - m a i n | @ a p p - c o n f i g \/ m a i n ) \/ d i s t ( \/ e s ) ? \/ i n d e x \. j s / ;
18
19
19
- export default function appConfigRollup ( {
20
- useGlobalNamespace,
21
- loadingOptions,
22
- schemaLoadingOptions,
23
- injectValidationFunction,
24
-
25
- readGlobal,
26
- } : Options = { } ) : Plugin & { currentFilePaths ?: string [ ] } {
20
+ export default function appConfigRollup (
21
+ options : Options = { } ,
22
+ ) : Plugin & { currentFilePaths ?: string [ ] } {
27
23
const currentFilePaths : string [ ] = [ ] ;
28
24
29
25
return {
30
26
name : '@app-config/rollup' ,
31
27
currentFilePaths,
32
- resolveId ( source ) {
33
- if ( packageNameRegex . exec ( source ) || appConfigImportRegex . exec ( source ) ) {
34
- return '.config-placeholder' ;
28
+ resolveId ( id ) {
29
+ if ( shouldTransform ( id ) ) {
30
+ return { id , moduleSideEffects : false , external : false } ;
35
31
}
36
-
37
- return null ;
38
32
} ,
39
33
async load ( id ) {
40
- if ( packageNameRegex . exec ( id ) || appConfigImportRegex . exec ( id ) ) {
41
- const { fullConfig, environment, validationFunctionCode, filePaths } =
42
- await loadValidatedConfig ( loadingOptions , schemaLoadingOptions ) ;
43
-
44
- if ( filePaths ) {
45
- currentFilePaths . length = 0 ;
46
- currentFilePaths . push ( ...filePaths ) ;
47
- }
48
-
49
- return generateModuleText ( fullConfig , {
50
- environment,
51
- useGlobalNamespace : useGlobalNamespace ?? readGlobal ?? true ,
52
- validationFunctionCode : injectValidationFunction ? validationFunctionCode : undefined ,
53
- esmValidationCode : true ,
54
- } ) ;
34
+ if ( shouldTransform ( id ) ) {
35
+ return loadConfig ( options , currentFilePaths ) ;
55
36
}
56
-
57
- return null ;
58
37
} ,
38
+ async transform ( _ , id ) {
39
+ if ( shouldTransform ( id ) ) {
40
+ return loadConfig ( options , currentFilePaths ) ;
41
+ }
42
+ } ,
43
+ } ;
44
+ }
45
+
46
+ function shouldTransform ( id : string ) {
47
+ return ! ! packageNameRegex . exec ( id ) || ! ! appConfigImportRegex . exec ( id ) ;
48
+ }
49
+
50
+ async function loadConfig (
51
+ {
52
+ useGlobalNamespace,
53
+ loadingOptions,
54
+ schemaLoadingOptions,
55
+ injectValidationFunction,
56
+ readGlobal,
57
+ } : Options ,
58
+ currentFilePaths : string [ ] ,
59
+ ) {
60
+ const { fullConfig, environment, validationFunctionCode, filePaths } = await loadValidatedConfig (
61
+ loadingOptions ,
62
+ schemaLoadingOptions ,
63
+ ) ;
64
+
65
+ if ( filePaths ) {
66
+ currentFilePaths . splice ( 0 ) ;
67
+ currentFilePaths . push ( ...filePaths ) ;
68
+ }
69
+
70
+ const code = generateModuleText ( fullConfig , {
71
+ environment,
72
+ useGlobalNamespace : useGlobalNamespace ?? readGlobal ?? true ,
73
+ validationFunctionCode : injectValidationFunction ? validationFunctionCode : undefined ,
74
+ esmValidationCode : true ,
75
+ } ) ;
76
+
77
+ return {
78
+ code,
79
+ moduleSideEffects : false ,
59
80
} ;
60
81
}
0 commit comments