@@ -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,44 @@ 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 ( workerSyntax === "SharedWorker" ) {
597+ const optionsExpression = /** @type {Expression } */ ( arg2 ) ;
598+ if (
599+ optionsExpression . type === "Literal" &&
600+ typeof optionsExpression . value === "string"
601+ ) {
602+ prefix = this . _module ? "{ name: " : "" ;
603+ suffix = this . _module ? `, type: ${ type } }` : "" ;
604+ } else {
605+ const stringOptions = this . _module
606+ ? `{ name: options, type: ${ type } }`
607+ : "options" ;
608+ // Evaluate the argument once, outside the wrapper's scope.
609+ prefix = `(${ compilation . runtimeTemplate . returningFunction (
610+ `typeof options === "string" ? ${ stringOptions } : ${ prefix } options${ suffix } ` ,
611+ "options"
612+ ) } )((`;
613+ suffix = "))" ;
614+ }
615+ }
616+ if ( prefix ) {
617+ const dep1 = new ConstDependency (
618+ prefix ,
619+ /** @type {Range } */ ( insertLocation ) [ 0 ]
620+ ) ;
621+ const dep2 = new ConstDependency (
622+ suffix ,
623+ /** @type {Range } */ ( insertLocation ) [ 1 ]
624+ ) ;
625+ dep1 . loc = parser . getLocation ( expr ) ;
626+ dep2 . loc = parser . getLocation ( expr ) ;
627+ parser . state . module . addPresentationalDependency ( dep1 ) ;
628+ parser . state . module . addPresentationalDependency ( dep2 ) ;
629+ }
603630 } else if ( insertType === "argument" && this . _module ) {
604631 const dep = new ConstDependency (
605632 ', { type: "module" }' ,
@@ -687,7 +714,7 @@ class WorkerAndWorkletPlugin {
687714 /**
688715 * Registers the parser hooks matching a single syntax item.
689716 * @param {string } item item
690- * @param {(expr: CallExpression, isGlobalWorker ?: boolean ) => boolean | void } handle handler invoked on a match
717+ * @param {(expr: CallExpression, workerSyntax ?: string ) => boolean | void } handle handler invoked on a match
691718 * @param {symbol } specifierTag tag used for `*variable` syntax
692719 */
693720 const processItem = ( item , handle , specifierTag ) => {
@@ -752,7 +779,7 @@ class WorkerAndWorkletPlugin {
752779 } else {
753780 parser . hooks . new
754781 . for ( item )
755- . tap ( PLUGIN_NAME , ( expr ) => handle ( expr , item === "Worker" ) ) ;
782+ . tap ( PLUGIN_NAME , ( expr ) => handle ( expr , item ) ) ;
756783 }
757784 }
758785 } ;
@@ -761,7 +788,7 @@ class WorkerAndWorkletPlugin {
761788 * Expands and registers a syntax list.
762789 * @param {string[] } syntaxList syntax list (may contain "...")
763790 * @param {string[] } defaultSyntax default syntax used for "..."
764- * @param {(expr: CallExpression, isGlobalWorker ?: boolean ) => boolean | void } handle handler
791+ * @param {(expr: CallExpression, workerSyntax ?: string ) => boolean | void } handle handler
765792 * @param {symbol } specifierTag tag used for `*variable` syntax
766793 */
767794 const processList = (
0 commit comments