Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
fix:loader-module-webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
admirsaheta authored and jesstelford committed Sep 24, 2024
1 parent d6fd7d1 commit 8abafcf
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions packages/web-worker/src/webpack-parts/loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as path from 'path';

import type {LoaderContext, Compilation, Compiler, Chunk} from 'webpack';
import type {
LoaderContext,
Compilation,
Compiler,
Chunk,
PathData,
AssetInfo,
} from 'webpack';
import {EntryPlugin, webworker, web} from 'webpack';

import {WebWorkerPlugin} from './plugin';
Expand Down Expand Up @@ -71,7 +78,6 @@ export function pitch(this: LoaderContext<Options>, request: string) {
wrapperContent = cachedContent;
} else if (cachedContent == null) {
try {
// @ts-expect-error readFileSync is available here
wrapperContent = this.fs.readFileSync(wrapperModule).toString();
moduleWrapperCache.set(wrapperModule, wrapperContent ?? false);
} catch (error) {
Expand All @@ -90,9 +96,9 @@ export function pitch(this: LoaderContext<Options>, request: string) {
const workerOptions = {
filename:
plugin.options.filename ??
addWorkerSubExtension(compiler.options.output.filename as string),
addWorkerSubExtension(compiler.options.output.filename ?? '[worker].js'),
chunkFilename: addWorkerSubExtension(
compiler.options.output.chunkFilename as string,
compiler.options.output.chunkFilename ?? '[worker].js',
),
globalObject: (plugin && plugin.options.globalObject) || 'self',
};
Expand Down Expand Up @@ -148,10 +154,21 @@ export function pitch(this: LoaderContext<Options>, request: string) {
);
}

function addWorkerSubExtension(file: string) {
return file.includes('[name]')
? file.replace(/\.([a-z]+)$/i, '.worker.$1')
: file.replace(/\.([a-z]+)$/i, '.[name].worker.$1');
function addWorkerSubExtension(
file: string | ((pathData: PathData, assetInfo?: AssetInfo) => string),
): string | ((pathData: PathData) => string) {
if (typeof file === 'function') {
return (pathData: PathData) => {
const result = file(pathData);
return result.includes('[name]')
? result.replace(/\.([a-z]+)$/i, '.worker.$1')
: result.replace(/\.([a-z]+)$/i, '.[name].worker.$1');
};
} else {
return file.includes('[name]')
? file.replace(/\.([a-z]+)$/i, '.worker.$1')
: file.replace(/\.([a-z]+)$/i, '.[name].worker.$1');
}
}

const loader = {
Expand Down

0 comments on commit 8abafcf

Please sign in to comment.