1
1
/* eslint-disable no-console */
2
2
const fse = require ( 'fs-extra' ) ;
3
+ const path = require ( 'path' ) ;
3
4
const glob = require ( 'glob' ) ;
4
- const path = require ( 'path' )
5
+ const getDynamicModuleMap = require ( './parse-dynamic-modules' ) ;
5
6
6
7
const root = process . cwd ( ) ;
7
8
const packageJson = require ( `${ root } /package.json` ) ;
8
9
9
- if ( ! ( process . argv . includes ( '--config' ) && process . argv . indexOf ( '--config' ) + 1 < process . argv . length ) ) {
10
+ if ( ! ( process . argv . includes ( '--config' ) && process . argv . indexOf ( '--config' ) + 1 < process . argv . length ) ) {
10
11
console . log ( '--config is required followed by the config file name' ) ;
11
12
process . exit ( 1 ) ;
12
13
}
13
14
14
15
const configJson = require ( `${ root } /${ process . argv [ process . argv . indexOf ( '--config' ) + 1 ] } ` ) ;
15
16
16
- const foldersExclude = configJson . exclude ? configJson . exclude : [ ]
17
+ const foldersExclude = configJson . exclude ? configJson . exclude : [ ] ;
17
18
18
- let moduleGlob = configJson . moduleGlob
19
- if ( moduleGlob && ! Array . isArray ( moduleGlob ) ) {
20
- moduleGlob = [ moduleGlob ]
19
+ let moduleGlob = configJson . moduleGlob ;
20
+
21
+ if ( moduleGlob && ! Array . isArray ( moduleGlob ) ) {
22
+ moduleGlob = [ moduleGlob ] ;
21
23
} else if ( ! moduleGlob ) {
22
- moduleGlob = [ '/dist/esm/*/*/**/index.js' ]
24
+ moduleGlob = [ '/dist/esm/*/*/**/index.js' ] ;
23
25
}
26
+
24
27
const components = {
25
28
// need the /*/*/ to avoid grabbing top level index files
26
29
/**
27
30
* We don't want the /index.js or /components/index.js to be have packages
28
31
* These files will not help with tree shaking in module federation environments
29
32
*/
30
- files : moduleGlob . map ( pattern => glob
31
- . sync ( `${ root } ${ pattern } ` )
32
- . filter ( ( item ) => ! foldersExclude . some ( ( name ) => item . includes ( name ) ) )
33
- . map ( ( name ) => name . replace ( / \/ $ / , '' ) ) )
34
- . flat ( ) ,
35
- }
36
-
37
-
33
+ files : moduleGlob
34
+ . map ( ( pattern ) =>
35
+ glob
36
+ . sync ( `${ root } ${ pattern } ` )
37
+ . filter ( ( item ) => ! foldersExclude . some ( ( name ) => item . includes ( name ) ) )
38
+ . map ( ( name ) => name . replace ( / \/ $ / , '' ) )
39
+ )
40
+ . flat ( )
41
+ } ;
38
42
39
43
async function createPackage ( component ) {
40
44
const cmds = [ ] ;
41
45
let destFile = component . replace ( / [ ^ / ] + \. j s $ / g, 'package.json' ) . replace ( '/dist/esm/' , '/dist/dynamic/' ) ;
42
- if ( component . match ( / i n d e x \. j s $ / ) ) {
46
+
47
+ if ( component . match ( / i n d e x \. j s $ / ) ) {
43
48
destFile = component . replace ( / [ ^ / ] + \. j s $ / g, 'package.json' ) . replace ( '/dist/esm/' , '/dist/dynamic/' ) ;
44
49
} else {
45
50
destFile = component . replace ( / \. j s $ / g, '/package.json' ) . replace ( '/dist/esm/' , '/dist/dynamic/' ) ;
46
51
}
52
+
47
53
const pathAsArray = component . split ( '/' ) ;
48
- const destDir = destFile . replace ( / p a c k a g e \. j s o n $ / , '' )
49
- const esmRelative = path . relative ( destDir , component )
50
- const cjsRelative = path . relative ( destDir , component . replace ( '/dist/esm/' , '/dist/js/' ) )
51
- const typesRelative = path . relative ( destDir , component . replace ( / \. j s $ / , '.d.ts' ) )
54
+ const destDir = destFile . replace ( / p a c k a g e \. j s o n $ / , '' ) ;
55
+ const esmRelative = path . relative ( destDir , component ) ;
56
+ const cjsRelative = path . relative ( destDir , component . replace ( '/dist/esm/' , '/dist/js/' ) ) ;
57
+ const typesRelative = path . relative ( destDir , component . replace ( / \. j s $ / , '.d.ts' ) ) ;
52
58
53
59
const packageName = configJson . packageName ;
60
+
54
61
if ( ! packageName ) {
55
- console . log ( " packageName is required!" )
62
+ console . log ( ' packageName is required!' ) ;
56
63
process . exit ( 1 ) ;
57
64
}
58
65
59
66
let componentName = pathAsArray [ pathAsArray . length - ( component . match ( / i n d e x \. j s $ / ) ? 2 : 1 ) ] ;
60
- if ( pathAsArray . includes ( "next" ) ) {
67
+
68
+ if ( pathAsArray . includes ( 'next' ) ) {
61
69
componentName = `${ componentName . toLowerCase ( ) } -next-dynamic` ;
62
- } else if ( pathAsArray . includes ( " deprecated" ) ) {
70
+ } else if ( pathAsArray . includes ( ' deprecated' ) ) {
63
71
componentName = `${ componentName . toLowerCase ( ) } -deprecated-dynamic` ;
64
72
} else {
65
73
componentName = `${ componentName . toLowerCase ( ) } -dynamic` ;
@@ -75,25 +83,39 @@ async function createPackage(component) {
75
83
} ;
76
84
77
85
// use ensureFile to not having to create all the directories
78
- fse . ensureDirSync ( destDir )
79
- cmds . push ( fse . writeJSON ( destFile , content ) )
86
+ fse . ensureDirSync ( destDir ) ;
87
+ cmds . push ( fse . writeJSON ( destFile , content ) ) ;
80
88
81
89
return Promise . all ( cmds ) ;
82
90
}
83
91
84
- async function generatePackages ( components , dist ) {
85
- const cmds = components . map ( ( component ) => createPackage ( component , dist ) ) ;
92
+ async function generatePackages ( components ) {
93
+ const cmds = components . map ( ( component ) => createPackage ( component ) ) ;
86
94
return Promise . all ( cmds ) ;
87
95
}
88
96
89
- async function run ( components ) {
97
+ async function generateDynamicModuleMap ( ) {
98
+ const moduleMap = getDynamicModuleMap ( root ) ;
99
+
100
+ if ( Object . keys ( moduleMap ) . length === 0 ) {
101
+ return Promise . resolve ( ) ;
102
+ }
103
+
104
+ const moduleMapSorted = Object . keys ( moduleMap )
105
+ . sort ( )
106
+ . reduce ( ( acc , key ) => ( { ...acc , [ key ] : moduleMap [ key ] } ) , { } ) ;
107
+
108
+ return fse . writeJSON ( path . resolve ( root , 'dist/dynamic-modules.json' ) , moduleMapSorted , { spaces : 2 } ) ;
109
+ }
110
+
111
+ async function run ( ) {
90
112
try {
91
- await generatePackages ( components ) ;
113
+ await generatePackages ( components . files ) ;
114
+ await generateDynamicModuleMap ( ) ;
92
115
} catch ( error ) {
93
- console . log ( error )
116
+ console . log ( error ) ;
94
117
process . exit ( 1 ) ;
95
118
}
96
119
}
97
120
98
- run ( components . files ) ;
99
-
121
+ run ( ) ;
0 commit comments