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

Commit

Permalink
Merge pull request #2816 from admirsaheta/fix/loader-module-webpack
Browse files Browse the repository at this point in the history
fix:loader-module-webpack
  • Loading branch information
jesstelford authored Sep 24, 2024
2 parents d6fd7d1 + 746fea9 commit 525342f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/spicy-panthers-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/web-worker': minor
---

- Allow `output.chunkFilename` to be a function or string.
- `compiler.options.output.filename` and `compiler.options.output.chunkFilename` now default to `[name].js` when undefined.
41 changes: 31 additions & 10 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,9 +78,12 @@ 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);
if (this.fs?.readFileSync) {
wrapperContent = this.fs.readFileSync(wrapperModule).toString();
moduleWrapperCache.set(wrapperModule, wrapperContent ?? false);
} else {
throw new Error('readFileSync is undefined');
}
} catch (error) {
moduleWrapperCache.set(wrapperModule, false);
}
Expand All @@ -90,9 +100,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 ?? '[name].js'),
chunkFilename: addWorkerSubExtension(
compiler.options.output.chunkFilename as string,
compiler.options.output.chunkFilename ?? '[name].js',
),
globalObject: (plugin && plugin.options.globalObject) || 'self',
};
Expand Down Expand Up @@ -148,10 +158,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 525342f

Please sign in to comment.