1- import * as fs from 'fs-extra' ;
21import * as path from 'path' ;
32import * as babel from '@babel/core' ;
3+ import { glob } from 'glob' ;
44import { logger } from '@nx/devkit' ;
55import { createExecutor } from '../../utils/create-executor' ;
66import { writeFileText } from '../../utils/file-operations' ;
77import { removeDirectoryRespectingExclusions } from '../clean/clean.impl' ;
8+ import { toPosixPath } from '../../utils/path-resolver' ;
89import { StateManagerOptimizeExecutorSchema } from './schema' ;
910
1011const ESM_REEXPORT = "export * from './prod/index';" ;
11- const STATE_MANAGER_REL_PATH = path . join ( ' __internal' , ' core' , ' state_manager' ) ;
12+ const STATE_MANAGER_INDEX_GLOB = '**/ __internal/ core/ state_manager/index.js' ;
1213const ERROR_BABEL_NO_CODE = 'Babel returned no code for CJS state_manager index.js' ;
14+ const ERROR_NO_STATE_MANAGER_FOUND =
15+ 'No state_manager/index.js found in any configured transpiledDirs' ;
1316
14- const VARIANTS = [ 'esm' , 'cjs' ] as const ;
15- type Variant = ( typeof VARIANTS ) [ number ] ;
16- type ContentBuilder = ( indexPath : string ) => string ;
17-
18- function transformReexportToCjs ( indexPath : string ) : string {
17+ export function transformReexportToCjs ( indexPath : string ) : string {
1918 const result = babel . transformSync ( ESM_REEXPORT , {
2019 filename : indexPath ,
2120 plugins : [ [ '@babel/plugin-transform-modules-commonjs' ] ] ,
@@ -28,33 +27,33 @@ function transformReexportToCjs(indexPath: string): string {
2827 return result . code ;
2928}
3029
31- const CONTENT_BUILDERS : Record < Variant , ContentBuilder > = {
32- esm : ( ) => ESM_REEXPORT ,
33- cjs : ( indexPath ) => transformReexportToCjs ( indexPath ) ,
34- } ;
35-
36- async function optimizeVariant ( variant : Variant , transpiledRoot : string ) : Promise < void > {
37- const stateManagerDir = path . join ( transpiledRoot , variant , STATE_MANAGER_REL_PATH ) ;
38-
39- if ( ! fs . existsSync ( stateManagerDir ) ) {
40- logger . verbose ( `Skipping ${ variant } state_manager: ${ stateManagerDir } does not exist` ) ;
41- return ;
42- }
30+ function isCjsFile ( filePath : string ) : boolean {
31+ return toPosixPath ( filePath ) . includes ( '/cjs/' ) ;
32+ }
4333
44- const indexPath = path . join ( stateManagerDir , 'index.js' ) ;
45- if ( fs . existsSync ( indexPath ) ) {
46- await writeFileText ( indexPath , CONTENT_BUILDERS [ variant ] ( indexPath ) ) ;
47- }
34+ async function optimizeIndexFile ( indexPath : string ) : Promise < void > {
35+ const content = isCjsFile ( indexPath ) ? transformReexportToCjs ( indexPath ) : ESM_REEXPORT ;
36+ await writeFileText ( indexPath , content ) ;
4837
38+ const stateManagerDir = path . dirname ( indexPath ) ;
4939 await removeDirectoryRespectingExclusions ( stateManagerDir , [
5040 indexPath ,
5141 path . join ( stateManagerDir , 'prod' ) ,
5242 ] ) ;
5343}
5444
55- async function optimizeTranspiledDir ( transpiledDir : string , projectRoot : string ) : Promise < void > {
56- const transpiledRoot = path . join ( projectRoot , transpiledDir ) ;
57- await Promise . all ( VARIANTS . map ( ( variant ) => optimizeVariant ( variant , transpiledRoot ) ) ) ;
45+ async function optimizeTranspiledDir ( transpiledRoot : string ) : Promise < number > {
46+ const indexFiles = await glob ( STATE_MANAGER_INDEX_GLOB , {
47+ cwd : toPosixPath ( transpiledRoot ) ,
48+ absolute : true ,
49+ nodir : true ,
50+ } ) ;
51+
52+ logger . verbose ( `Found ${ indexFiles . length } state_manager index.js file(s) in ${ transpiledRoot } ` ) ;
53+
54+ await Promise . all ( indexFiles . map ( optimizeIndexFile ) ) ;
55+
56+ return indexFiles . length ;
5857}
5958
6059interface ResolvedStateManagerOptimize {
@@ -73,10 +72,18 @@ export default createExecutor<StateManagerOptimizeExecutorSchema, ResolvedStateM
7372 `Optimizing state_manager modules in ${ resolved . transpiledDirs . length } transpiled tree(s)` ,
7473 ) ;
7574
76- await Promise . all (
75+ const counts = await Promise . all (
7776 resolved . transpiledDirs . map ( ( transpiledDir ) =>
78- optimizeTranspiledDir ( transpiledDir , resolved . projectRoot ) ,
77+ optimizeTranspiledDir ( path . join ( resolved . projectRoot , transpiledDir ) ) ,
7978 ) ,
8079 ) ;
80+
81+ const totalFound = counts . reduce ( ( sum , count ) => sum + count , 0 ) ;
82+
83+ if ( totalFound === 0 ) {
84+ throw new Error (
85+ `${ ERROR_NO_STATE_MANAGER_FOUND } : ${ resolved . transpiledDirs . join ( ', ' ) } . Check transpile output layout or transpiledDirs option.` ,
86+ ) ;
87+ }
8188 } ,
8289} ) ;
0 commit comments