MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/dependencies/WorkerDependency.js:112–152  ·  view source on GitHub ↗

* Applies the plugin by registering its hooks on the compiler. * @param {Dependency} dependency the dependency for which the template should be applied * @param {ReplaceSource} source the current replace source which can be modified * @param {DependencyTemplateContext} templateContext the cont

(dependency, source, templateContext)

Source from the content-addressed store, hash-verified

110 * @returns {void}
111 */
112 apply(dependency, source, templateContext) {
113 const { chunkGraph, moduleGraph, runtimeRequirements, runtimeTemplate } =
114 templateContext;
115 const dep = /** @type {WorkerDependency} */ (dependency);
116 const block = /** @type {AsyncDependenciesBlock} */ (
117 moduleGraph.getParentBlock(dependency)
118 );
119 const entrypoint = /** @type {Entrypoint} */ (
120 chunkGraph.getBlockChunkGroup(block)
121 );
122 const chunk = entrypoint.getEntrypointChunk();
123 // We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
124 const workerImportBaseUrl = dep.options.publicPath
125 ? `"${dep.options.publicPath}"`
126 : RuntimeGlobals.publicPath;
127
128 runtimeRequirements.add(RuntimeGlobals.publicPath);
129 runtimeRequirements.add(RuntimeGlobals.baseURI);
130 runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
131
132 const workerImportStr = `/* worker import */ ${workerImportBaseUrl} + ${
133 RuntimeGlobals.getChunkScriptFilename
134 }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`;
135
136 source.replace(
137 dep.range[0],
138 dep.range[1] - 1,
139 dep.options.needNewUrl ? `new URL(${workerImportStr})` : workerImportStr
140 );
141
142 // `Worker` is global only on the web, so route node (universal or node-only) through `worker_threads`
143 const { platform } = runtimeTemplate.compilation.compiler;
144 const ctorRange = dep.options.workerConstructorRange;
145 if (
146 ctorRange &&
147 (runtimeTemplate.isUniversalTarget() || (platform.node && !platform.web))
148 ) {
149 runtimeRequirements.add(RuntimeGlobals.worker);
150 source.replace(ctorRange[0], ctorRange[1] - 1, RuntimeGlobals.worker);
151 }
152 }
153};
154
155makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");

Callers

nothing calls this directly

Calls 6

getParentBlockMethod · 0.80
getBlockChunkGroupMethod · 0.80
getEntrypointChunkMethod · 0.80
addMethod · 0.45
replaceMethod · 0.45
isUniversalTargetMethod · 0.45

Tested by

no test coverage detected