44
55import { INJECTED_FILE } from '@dd/core/constants' ;
66import retry from 'async-retry' ;
7+ import type { PluginBuild } from 'esbuild' ;
78import fsp from 'fs/promises' ;
89import fs from 'fs' ;
10+ import { glob } from 'glob' ;
911import path from 'path' ;
1012import type { RequestInit } from 'undici-types' ;
1113
12- import type { RequestOpts } from './types' ;
14+ import type { GlobalContext , Logger , RequestOpts , ResolvedEntry } from './types' ;
1315
1416// Format a duration 0h 0m 0s 0ms
1517export const formatDuration = ( duration : number ) => {
@@ -25,12 +27,72 @@ export const formatDuration = (duration: number) => {
2527 } ${ milliseconds ? `${ milliseconds } ms` : '' } `. trim ( ) ;
2628} ;
2729
28- export const getResolvedPath = ( filepath : string ) => {
29- try {
30- return require . resolve ( filepath ) ;
31- } catch ( e ) {
32- return filepath ;
30+ // https://esbuild.github.io/api/#glob-style-entry-points
31+ const getAllEntryFiles = ( filepath : string ) : string [ ] => {
32+ if ( ! filepath . includes ( '*' ) ) {
33+ return [ filepath ] ;
3334 }
35+
36+ const files = glob . sync ( filepath ) ;
37+ return files ;
38+ } ;
39+
40+ // Parse, resolve and return all the entries of esbuild.
41+ export const getEsbuildEntries = async (
42+ build : PluginBuild ,
43+ context : GlobalContext ,
44+ log : Logger ,
45+ ) : Promise < ResolvedEntry [ ] > => {
46+ const entries : { name ?: string ; resolved : string ; original : string } [ ] = [ ] ;
47+ const entryPoints = build . initialOptions . entryPoints ;
48+ const entryPaths : { name ?: string ; path : string } [ ] = [ ] ;
49+ const resolutionErrors : string [ ] = [ ] ;
50+
51+ if ( Array . isArray ( entryPoints ) ) {
52+ for ( const entry of entryPoints ) {
53+ const fullPath = entry && typeof entry === 'object' ? entry . in : entry ;
54+ entryPaths . push ( { path : fullPath } ) ;
55+ }
56+ } else if ( entryPoints && typeof entryPoints === 'object' ) {
57+ entryPaths . push (
58+ ...Object . entries ( entryPoints ) . map ( ( [ name , filepath ] ) => ( { name, path : filepath } ) ) ,
59+ ) ;
60+ }
61+
62+ // Resolve all the paths.
63+ const proms = entryPaths
64+ . flatMap ( ( entry ) =>
65+ getAllEntryFiles ( entry . path ) . map < [ { name ?: string ; path : string } , string ] > ( ( p ) => [
66+ entry ,
67+ p ,
68+ ] ) ,
69+ )
70+ . map ( async ( [ entry , p ] ) => {
71+ const result = await build . resolve ( p , {
72+ kind : 'entry-point' ,
73+ resolveDir : context . cwd ,
74+ } ) ;
75+
76+ if ( result . errors . length ) {
77+ resolutionErrors . push ( ...result . errors . map ( ( e ) => e . text ) ) ;
78+ }
79+
80+ if ( result . path ) {
81+ // Store them for later use.
82+ entries . push ( {
83+ name : entry . name ,
84+ resolved : result . path ,
85+ original : entry . path ,
86+ } ) ;
87+ }
88+ } ) ;
89+
90+ for ( const resolutionError of resolutionErrors ) {
91+ log . error ( resolutionError ) ;
92+ }
93+
94+ await Promise . all ( proms ) ;
95+ return entries ;
3496} ;
3597
3698export const ERROR_CODES_NO_RETRY = [ 400 , 403 , 413 ] ;
@@ -169,3 +231,6 @@ export const readJsonSync = (filepath: string) => {
169231 const data = fs . readFileSync ( filepath , { encoding : 'utf-8' } ) ;
170232 return JSON . parse ( data ) ;
171233} ;
234+
235+ let index = 0 ;
236+ export const getUniqueId = ( ) => `${ Date . now ( ) } .${ performance . now ( ) } .${ ++ index } ` ;
0 commit comments