@@ -121,23 +121,33 @@ export function selectCliRuntimeExternalDependencies(
121121export function findEsmImportsOfExternalPackages ( source : string ) : ReadonlyArray < string > {
122122 const specifiers = new Set < string > ( ) ;
123123 // `import x from`, `import "side-effect"`, and `export ... from` all load the
124- // module at evaluation time.
125- const staticImport = / ^ i m p o r t \s [ ^ ; ] * ?\s f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / gm;
126- const sideEffectImport = / ^ i m p o r t \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / gm;
127- const reExport = / ^ e x p o r t \s [ ^ ; ] * ?\s f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / gm;
124+ // module at evaluation time. Dynamic `import()` is deferred, which is the
125+ // only form under which the Bun-only entry points are acceptable: they sit
126+ // behind a runtime check Node never passes. Statically imported they would
127+ // fail at evaluation like any other file-backed package.
128+ const evaluatedPatterns = [
129+ / ^ i m p o r t \s [ ^ ; ] * ?\s f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / gm,
130+ / ^ i m p o r t \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / gm,
131+ / ^ e x p o r t \s [ ^ ; ] * ?\s f r o m \s + [ " ' ] ( [ ^ " ' ] + ) [ " ' ] / gm,
132+ ] ;
128133 const dynamicImport = / \b i m p o r t \( \s * [ " ' ] ( [ ^ " ' ] + ) [ " ' ] \s * [ , ) ] / g;
129- for ( const pattern of [ staticImport , sideEffectImport , reExport , dynamicImport ] ) {
134+ const collect = ( pattern : RegExp , allowBuildOnly : boolean ) => {
130135 for ( const match of source . matchAll ( pattern ) ) {
131136 const specifier = match [ 1 ] ;
132137 if ( specifier === undefined ) continue ;
133138 if ( NodeModule . isBuiltin ( specifier ) ) continue ;
134139 if ( specifier . startsWith ( "./" ) || specifier . startsWith ( "../" ) ) continue ;
135- if ( CLI_BUILD_ONLY_EXTERNAL_PREFIXES . some ( ( prefix ) => specifier . startsWith ( prefix ) ) ) {
140+ if (
141+ allowBuildOnly &&
142+ CLI_BUILD_ONLY_EXTERNAL_PREFIXES . some ( ( prefix ) => specifier . startsWith ( prefix ) )
143+ ) {
136144 continue ;
137145 }
138146 specifiers . add ( specifier ) ;
139147 }
140- }
148+ } ;
149+ for ( const pattern of evaluatedPatterns ) collect ( pattern , false ) ;
150+ collect ( dynamicImport , true ) ;
141151 return [ ...specifiers ] . sort ( ) ;
142152}
143153
0 commit comments