@@ -464,10 +464,10 @@ class WorkerAndWorkletPlugin {
464464 /**
465465 * Handles a matched `new Worker(...)` expression.
466466 * @param {CallExpression } expr expression
467- * @param {boolean = } isGlobalWorker matched the global `new Worker()` syntax (eligible for universal rewrite)
467+ * @param {string = } workerSyntax matched global constructor syntax
468468 * @returns {boolean | void } true when handled
469469 */
470- const handleNewWorker = ( expr , isGlobalWorker = false ) => {
470+ const handleNewWorker = ( expr , workerSyntax ) => {
471471 const parsedUrl = resolveWorkerUrl ( expr ) ;
472472 if ( ! parsedUrl ) return ;
473473 const { url, range, needNewUrl, arg1, arg2 } = parsedUrl ;
@@ -547,9 +547,10 @@ class WorkerAndWorkletPlugin {
547547 const dep = new WorkerDependency ( url , range , {
548548 publicPath : this . _workerPublicPath ,
549549 needNewUrl,
550- workerConstructorRange : isGlobalWorker
551- ? /** @type {Range } */ ( expr . callee . range )
552- : undefined ,
550+ workerConstructorRange :
551+ workerSyntax === "Worker"
552+ ? /** @type {Range } */ ( expr . callee . range )
553+ : undefined ,
553554 resourceHint
554555 } ) ;
555556 dep . loc = parser . getLocation ( expr ) ;
@@ -588,18 +589,48 @@ class WorkerAndWorkletPlugin {
588589 const type = this . _module ? '"module"' : "undefined" ;
589590 // `{ ...(opts), type }` is equivalent to `Object.assign({}, opts, { type })`
590591 const useSpread = compilation . outputOptions . environment . spread ;
591- const dep1 = new ConstDependency (
592- useSpread ? "{ ...(" : "Object.assign({}, " ,
593- /** @type {Range } */ ( insertLocation ) [ 0 ]
594- ) ;
595- const dep2 = new ConstDependency (
596- useSpread ? `), type: ${ type } }` : `, { type: ${ type } })` ,
597- /** @type {Range } */ ( insertLocation ) [ 1 ]
598- ) ;
599- dep1 . loc = parser . getLocation ( expr ) ;
600- dep2 . loc = parser . getLocation ( expr ) ;
601- parser . state . module . addPresentationalDependency ( dep1 ) ;
602- parser . state . module . addPresentationalDependency ( dep2 ) ;
592+ let prefix = useSpread ? "{ ...(" : "Object.assign({}, " ;
593+ let suffix = useSpread
594+ ? `), type: ${ type } }`
595+ : `, { type: ${ type } })` ;
596+ if (
597+ workerSyntax !== undefined &&
598+ ( workerSyntax === "SharedWorker" ||
599+ workerSyntax . endsWith ( ".SharedWorker" ) )
600+ ) {
601+ const optionsExpression = /** @type {Expression } */ ( arg2 ) ;
602+ if (
603+ optionsExpression . type === "Literal" &&
604+ typeof optionsExpression . value === "string"
605+ ) {
606+ prefix = this . _module ? "{ name: " : "" ;
607+ suffix = this . _module ? `, type: ${ type } }` : "" ;
608+ } else {
609+ const stringOptions = this . _module
610+ ? `{ name: options, type: ${ type } }`
611+ : "options" ;
612+ // Evaluate the argument once, outside the wrapper's scope.
613+ prefix = `(${ compilation . runtimeTemplate . returningFunction (
614+ `typeof options === "string" ? ${ stringOptions } : ${ prefix } options${ suffix } ` ,
615+ "options"
616+ ) } )((`;
617+ suffix = "))" ;
618+ }
619+ }
620+ if ( prefix ) {
621+ const dep1 = new ConstDependency (
622+ prefix ,
623+ /** @type {Range } */ ( insertLocation ) [ 0 ]
624+ ) ;
625+ const dep2 = new ConstDependency (
626+ suffix ,
627+ /** @type {Range } */ ( insertLocation ) [ 1 ]
628+ ) ;
629+ dep1 . loc = parser . getLocation ( expr ) ;
630+ dep2 . loc = parser . getLocation ( expr ) ;
631+ parser . state . module . addPresentationalDependency ( dep1 ) ;
632+ parser . state . module . addPresentationalDependency ( dep2 ) ;
633+ }
603634 } else if ( insertType === "argument" && this . _module ) {
604635 const dep = new ConstDependency (
605636 ', { type: "module" }' ,
@@ -687,7 +718,7 @@ class WorkerAndWorkletPlugin {
687718 /**
688719 * Registers the parser hooks matching a single syntax item.
689720 * @param {string } item item
690- * @param {(expr: CallExpression, isGlobalWorker ?: boolean ) => boolean | void } handle handler invoked on a match
721+ * @param {(expr: CallExpression, workerSyntax ?: string ) => boolean | void } handle handler invoked on a match
691722 * @param {symbol } specifierTag tag used for `*variable` syntax
692723 */
693724 const processItem = ( item , handle , specifierTag ) => {
@@ -752,7 +783,7 @@ class WorkerAndWorkletPlugin {
752783 } else {
753784 parser . hooks . new
754785 . for ( item )
755- . tap ( PLUGIN_NAME , ( expr ) => handle ( expr , item === "Worker" ) ) ;
786+ . tap ( PLUGIN_NAME , ( expr ) => handle ( expr , item ) ) ;
756787 }
757788 }
758789 } ;
@@ -761,7 +792,7 @@ class WorkerAndWorkletPlugin {
761792 * Expands and registers a syntax list.
762793 * @param {string[] } syntaxList syntax list (may contain "...")
763794 * @param {string[] } defaultSyntax default syntax used for "..."
764- * @param {(expr: CallExpression, isGlobalWorker ?: boolean ) => boolean | void } handle handler
795+ * @param {(expr: CallExpression, workerSyntax ?: string ) => boolean | void } handle handler
765796 * @param {symbol } specifierTag tag used for `*variable` syntax
766797 */
767798 const processList = (
0 commit comments