@@ -99,6 +99,30 @@ function aliasJest(options) {
99
99
}
100
100
}
101
101
102
+ function autoscan ( tasks ) {
103
+ const dirlist = dir =>
104
+ fs . readdirSync ( dir ) . filter (
105
+ file => fs . statSync ( path . resolve ( dir , file ) ) . isDirectory ( ) )
106
+ if ( ! Array . isArray ( tasks ) ) tasks = tasks ? [ tasks ] : [ ]
107
+ tasks = tasks . map ( task => ( task === task + '' ) ? { path : task } : task )
108
+ tasks = tasks . map ( task => ( {
109
+ prefix : '' ,
110
+ suffix : '' ,
111
+ ...task ,
112
+ } ) )
113
+ const aliasMap = tasks . map ( task => (
114
+ dirlist ( task . path ) . reduce (
115
+ ( a , alias ) => ( {
116
+ ...a ,
117
+ [ task . prefix + alias + task . suffix ] :
118
+ path . join ( task . path , alias )
119
+ } ) ,
120
+ { }
121
+ )
122
+ ) ) . reduce ( ( a , map ) => ( { ...a , ...map } ) , { } )
123
+ return aliasMap
124
+ }
125
+
102
126
function configFilePath ( configPath = '' ) {
103
127
if (
104
128
configPath . length > 0 && fs . existsSync ( path . resolve ( paths . appPath , configPath ) )
@@ -140,22 +164,28 @@ function configPathsRaw(confPath) {
140
164
function configPaths ( configPath = '' ) {
141
165
const confPath = configFilePath ( configPath )
142
166
const paths = configPathsRaw ( confPath )
143
- return Object . keys ( paths ) . reduce ( ( a , path ) => {
167
+ const aliasMap = Object . keys ( paths ) . reduce ( ( a , path ) => {
144
168
const value = paths [ path ]
145
169
const target = Array . isArray ( value ) ? value [ 0 ] : value
146
170
a [ path . replace ( / \/ \* $ / , '' ) ] = target . replace ( / \/ \* $ / , '' )
147
171
return a
148
172
} , { } )
173
+ return aliasMap
149
174
}
150
175
151
176
function defaultOptions ( options ) {
152
177
const configPath = configFilePath (
153
178
options . tsconfig || options . jsconfig
154
179
)
155
180
const aliasMap = options . alias || configPaths ( configPath )
181
+ const aliasAutoMap = autoscan ( options . autoscan )
182
+
156
183
const opts = {
157
184
...options ,
158
- aliasMap,
185
+ aliasMap : {
186
+ ...aliasAutoMap ,
187
+ ...aliasMap ,
188
+ } ,
159
189
}
160
190
return opts
161
191
}
@@ -182,6 +212,7 @@ const CracoAliasPlugin = {
182
212
module . exports = {
183
213
aliasWebpack,
184
214
aliasJest,
215
+ autoscan,
185
216
configFilePath,
186
217
configPathsRaw,
187
218
configPaths,
0 commit comments