@@ -15,15 +15,31 @@ const path = require('path');
15
15
const crypto = require ( 'crypto' ) ;
16
16
const fs = require ( 'fs' ) ;
17
17
const { threadId} = require ( 'worker_threads' ) ;
18
+ const glob = require ( 'glob' ) ;
18
19
19
- const cacheDir = path . join ( __dirname , '..' , 'node_modules' , '.cache' , 'intl' ) ;
20
+ const intlCacheDir = path . join ( __dirname , '..' , 'node_modules' , '.cache' , 'intl' ) ;
21
+ const globCacheDir = path . join ( __dirname , '..' , 'node_modules' , '.cache' , 'glob' ) ;
20
22
try {
21
- fs . mkdirSync ( cacheDir , { recursive : true } ) ;
23
+ fs . mkdirSync ( intlCacheDir , { recursive : true } ) ;
24
+ fs . mkdirSync ( globCacheDir , { recursive : true } ) ;
22
25
} catch ( err ) {
23
26
// ignore
24
27
}
25
28
26
29
module . exports = ( request , options ) => {
30
+ // Handle glob imports.
31
+ if ( request . endsWith ( '*.json' ) ) {
32
+ let files = glob . sync ( request , { cwd : options . basedir } ) ;
33
+ let source = '' ;
34
+ for ( let file of files ) {
35
+ source += `exports['${ path . basename ( file , '.json' ) } '] = require('${ path . join ( options . basedir , file ) } ');\n` ;
36
+ }
37
+ let hash = crypto . createHash ( 'md5' ) ;
38
+ hash . update ( source ) ;
39
+ let cacheFile = path . join ( globCacheDir , hash . digest ( 'hex' ) ) ;
40
+ return writeCacheFile ( cacheFile , source ) ;
41
+ }
42
+
27
43
let resolved = options . defaultResolver ( request , {
28
44
...options ,
29
45
// https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149
@@ -40,19 +56,22 @@ module.exports = (request, options) => {
40
56
41
57
let hash = crypto . createHash ( 'md5' ) ;
42
58
hash . update ( res ) ;
43
- let cacheFile = path . join ( cacheDir , hash . digest ( 'hex' ) + '.js' ) ;
59
+ let cacheFile = path . join ( intlCacheDir , hash . digest ( 'hex' ) + '.js' ) ;
60
+ return writeCacheFile ( cacheFile , res ) ;
61
+ }
44
62
45
- // If cache file already exists, return it.
46
- if ( fs . existsSync ( cacheFile ) ) {
47
- return cacheFile ;
48
- }
63
+ return resolved ;
64
+ } ;
49
65
50
- // Otherwise, write it atomically to avoid race conditions between threads/processes.
51
- let tmpFile = cacheFile + '.' + process . pid + ( threadId != null ? '.' + threadId : '' ) ;
52
- fs . writeFileSync ( tmpFile , res ) ;
53
- fs . renameSync ( tmpFile , cacheFile ) ;
66
+ function writeCacheFile ( cacheFile , res ) {
67
+ // If cache file already exists, return it.
68
+ if ( fs . existsSync ( cacheFile ) ) {
54
69
return cacheFile ;
55
70
}
56
71
57
- return resolved ;
58
- } ;
72
+ // Otherwise, write it atomically to avoid race conditions between threads/processes.
73
+ let tmpFile = cacheFile + '.' + process . pid + ( threadId != null ? '.' + threadId : '' ) ;
74
+ fs . writeFileSync ( tmpFile , res ) ;
75
+ fs . renameSync ( tmpFile , cacheFile ) ;
76
+ return cacheFile ;
77
+ }
0 commit comments