1
1
'use strict' ;
2
2
3
- const path = require ( 'path' ) ;
3
+ /* eslint-disable no-for-of-loops/no-for-of-loops */
4
4
5
+ const path = require ( 'path' ) ;
6
+ const { promisify} = require ( 'util' ) ;
7
+ const glob = promisify ( require ( 'glob' ) ) ;
5
8
const { ESLint} = require ( 'eslint' ) ;
6
9
7
- const { bundles, getFilename, bundleTypes} = require ( '../bundles' ) ;
8
- const Packaging = require ( '../packaging' ) ;
9
-
10
- const {
11
- NODE_ES2015 ,
12
- NODE_ESM ,
13
- UMD_DEV ,
14
- UMD_PROD ,
15
- UMD_PROFILING ,
16
- NODE_DEV ,
17
- NODE_PROD ,
18
- NODE_PROFILING ,
19
- FB_WWW_DEV ,
20
- FB_WWW_PROD ,
21
- FB_WWW_PROFILING ,
22
- RN_OSS_DEV ,
23
- RN_OSS_PROD ,
24
- RN_OSS_PROFILING ,
25
- RN_FB_DEV ,
26
- RN_FB_PROD ,
27
- RN_FB_PROFILING ,
28
- } = bundleTypes ;
10
+ // Lint the final build artifacts. Helps catch bugs in our build pipeline.
29
11
30
- function getFormat ( bundleType ) {
31
- switch ( bundleType ) {
32
- case UMD_DEV :
33
- case UMD_PROD :
34
- case UMD_PROFILING :
35
- return 'umd' ;
36
- case NODE_ES2015 :
12
+ function getFormat ( filepath ) {
13
+ if ( filepath . includes ( 'facebook' ) ) {
14
+ if ( filepath . includes ( 'shims' ) ) {
15
+ // We don't currently lint these shims. We rely on the downstream Facebook
16
+ // repo to transform them.
17
+ // TODO: Should we lint them?
18
+ return null ;
19
+ }
20
+ return 'fb' ;
21
+ }
22
+ if ( filepath . includes ( 'react-native' ) ) {
23
+ if ( filepath . includes ( 'shims' ) ) {
24
+ // We don't currently lint these shims. We rely on the downstream Facebook
25
+ // repo to transform them.
26
+ // TODO: Should we we lint them?
27
+ return null ;
28
+ }
29
+ return 'rn' ;
30
+ }
31
+ if ( filepath . includes ( 'cjs' ) ) {
32
+ if (
33
+ filepath . includes ( 'react-server-dom-webpack-plugin' ) ||
34
+ filepath . includes ( 'react-server-dom-webpack-node-register' ) ||
35
+ filepath . includes ( 'react-suspense-test-utils' )
36
+ ) {
37
37
return 'cjs2015' ;
38
- case NODE_ESM :
39
- return 'esm' ;
40
- case NODE_DEV :
41
- case NODE_PROD :
42
- case NODE_PROFILING :
43
- return 'cjs' ;
44
- case FB_WWW_DEV :
45
- case FB_WWW_PROD :
46
- case FB_WWW_PROFILING :
47
- return 'fb' ;
48
- case RN_OSS_DEV :
49
- case RN_OSS_PROD :
50
- case RN_OSS_PROFILING :
51
- case RN_FB_DEV :
52
- case RN_FB_PROD :
53
- case RN_FB_PROFILING :
54
- return 'rn' ;
38
+ }
39
+ return 'cjs' ;
40
+ }
41
+ if ( filepath . includes ( 'esm' ) ) {
42
+ return 'esm' ;
43
+ }
44
+ if ( filepath . includes ( 'umd' ) ) {
45
+ return 'umd' ;
55
46
}
56
- throw new Error ( 'unknown bundleType' ) ;
47
+ if (
48
+ filepath . includes ( 'oss-experimental' ) ||
49
+ filepath . includes ( 'oss-stable' )
50
+ ) {
51
+ // If a file in one of the open source channels doesn't match an earlier,
52
+ // more specific rule, then assume it's CommonJS.
53
+ return 'cjs' ;
54
+ }
55
+ throw new Error ( 'Could not find matching lint format for file: ' + filepath ) ;
57
56
}
58
57
59
58
function getESLintInstance ( format ) {
@@ -64,51 +63,42 @@ function getESLintInstance(format) {
64
63
} ) ;
65
64
}
66
65
67
- const esLints = {
68
- cjs : getESLintInstance ( 'cjs' ) ,
69
- cjs2015 : getESLintInstance ( 'cjs2015' ) ,
70
- esm : getESLintInstance ( 'esm' ) ,
71
- rn : getESLintInstance ( 'rn' ) ,
72
- fb : getESLintInstance ( 'fb' ) ,
73
- umd : getESLintInstance ( 'umd' ) ,
74
- } ;
75
-
76
- // Performs sanity checks on bundles *built* by Rollup.
77
- // Helps catch Rollup regressions.
78
- async function lint ( bundle , bundleType ) {
79
- const filename = getFilename ( bundle , bundleType ) ;
80
- const format = getFormat ( bundleType ) ;
81
- const eslint = esLints [ format ] ;
82
-
83
- const packageName = Packaging . getPackageName ( bundle . entry ) ;
84
- const mainOutputPath = Packaging . getBundleOutputPath (
85
- bundleType ,
86
- filename ,
87
- packageName
88
- ) ;
89
-
90
- const results = await eslint . lintFiles ( [ mainOutputPath ] ) ;
66
+ async function lint ( eslint , filepaths ) {
67
+ const results = await eslint . lintFiles ( filepaths ) ;
91
68
if (
92
69
results . some ( result => result . errorCount > 0 || result . warningCount > 0 )
93
70
) {
94
71
process . exitCode = 1 ;
95
- console . log ( `Failed ${ mainOutputPath } ` ) ;
72
+ console . log ( `Lint failed ` ) ;
96
73
const formatter = await eslint . loadFormatter ( 'stylish' ) ;
97
74
const resultText = formatter . format ( results ) ;
98
75
console . log ( resultText ) ;
99
76
}
100
77
}
101
78
102
79
async function lintEverything ( ) {
103
- console . log ( `Linting known bundles...` ) ;
104
- let promises = [ ] ;
105
- // eslint-disable-next-line no-for-of-loops/no-for-of-loops
106
- for ( const bundle of bundles ) {
107
- // eslint-disable-next-line no-for-of-loops/no-for-of-loops
108
- for ( const bundleType of bundle . bundleTypes ) {
109
- promises . push ( lint ( bundle , bundleType ) ) ;
80
+ console . log ( `Linting build artifacts...` ) ;
81
+
82
+ const allFilepaths = await glob ( 'build2/**/*.js' ) ;
83
+
84
+ const pathsByFormat = new Map ( ) ;
85
+ for ( const filepath of allFilepaths ) {
86
+ const format = getFormat ( filepath ) ;
87
+ if ( format !== null ) {
88
+ const paths = pathsByFormat . get ( format ) ;
89
+ if ( paths === undefined ) {
90
+ pathsByFormat . set ( format , [ filepath ] ) ;
91
+ } else {
92
+ paths . push ( filepath ) ;
93
+ }
110
94
}
111
95
}
96
+
97
+ const promises = [ ] ;
98
+ for ( const [ format , filepaths ] of pathsByFormat ) {
99
+ const eslint = getESLintInstance ( format ) ;
100
+ promises . push ( lint ( eslint , filepaths ) ) ;
101
+ }
112
102
await Promise . all ( promises ) ;
113
103
}
114
104
0 commit comments