@@ -104,6 +104,36 @@ function holdsFunction(value, seen = new Set()) {
104104 ) ;
105105}
106106
107+ /**
108+ * Whether a value holds a regular expression anywhere inside it.
109+ * Child-process workers on runtimes without worker threads use the default
110+ * process IPC serializer, which does not preserve regular expressions.
111+ * @param {unknown } value what a worker would be handed
112+ * @param {Set<unknown>= } seen values already walked
113+ * @returns {boolean } true when a regular expression is in there
114+ */
115+ function holdsRegExp ( value , seen = new Set ( ) ) {
116+ if ( value instanceof RegExp ) {
117+ return true ;
118+ }
119+ if ( ! value || typeof value !== "object" || seen . has ( value ) ) {
120+ return false ;
121+ }
122+ seen . add ( value ) ;
123+
124+ if ( value instanceof Map ) {
125+ return [ ...value ] . some (
126+ ( [ key , one ] ) => holdsRegExp ( key , seen ) || holdsRegExp ( one , seen ) ,
127+ ) ;
128+ }
129+ if ( value instanceof Set ) {
130+ return [ ...value ] . some ( ( one ) => holdsRegExp ( one , seen ) ) ;
131+ }
132+ return Object . values ( /** @type {Record<string, unknown> } */ ( value ) ) . some (
133+ ( one ) => holdsRegExp ( one , seen ) ,
134+ ) ;
135+ }
136+
107137/**
108138 * True when every `minimizer.implementation` is a module path (`string` or
109139 * `{ path, export }`). Inline minify functions keep `transform`. When
@@ -112,9 +142,10 @@ function holdsFunction(value, seen = new Set()) {
112142 * whole asset task, even if that asset's own matched minimizers are paths.
113143 * @template T
114144 * @param {import("./index.js").InternalOptions<T> } options options
145+ * @param {{ enableWorkerThreads?: boolean }= } capabilities worker capabilities
115146 * @returns {boolean } whether `worker.minify` can run without `transform`
116147 */
117- function canMinifyByPath ( options ) {
148+ function canMinifyByPath ( options , capabilities = { } ) {
118149 /**
119150 * @param {unknown } implementation implementation
120151 * @returns {boolean } true when a module path is known
@@ -138,6 +169,13 @@ function canMinifyByPath(options) {
138169 ) {
139170 return false ;
140171 }
172+ if (
173+ capabilities . enableWorkerThreads === false &&
174+ ( holdsRegExp ( options . extractComments ) ||
175+ holdsRegExp ( options . minimizer . options ) )
176+ ) {
177+ return false ;
178+ }
141179
142180 if ( ! options . embedded ) {
143181 return true ;
@@ -146,6 +184,12 @@ function canMinifyByPath(options) {
146184 if ( holdsFunction ( options . embedded . options ) ) {
147185 return false ;
148186 }
187+ if (
188+ capabilities . enableWorkerThreads === false &&
189+ holdsRegExp ( options . embedded . options )
190+ ) {
191+ return false ;
192+ }
149193
150194 const embedded = Array . isArray ( options . embedded . implementation )
151195 ? options . embedded . implementation
0 commit comments