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

fix:loader-module-webpack #2816

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
42 changes: 32 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,15 +78,19 @@
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) {

Check failure on line 81 in packages/web-worker/src/webpack-parts/loader.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Delete `·`

Check failure on line 81 in packages/web-worker/src/webpack-parts/loader.ts

View workflow job for this annotation

GitHub Actions / Test (Node 20, React 18)

Delete `·`
wrapperContent = this.fs.readFileSync(wrapperModule).toString();
moduleWrapperCache.set(wrapperModule, wrapperContent ?? false);
} else {
throw new Error("readFileSync is undefined");

Check failure on line 85 in packages/web-worker/src/webpack-parts/loader.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Replace `"readFileSync·is·undefined"` with `'readFileSync·is·undefined'`

Check failure on line 85 in packages/web-worker/src/webpack-parts/loader.ts

View workflow job for this annotation

GitHub Actions / Test (Node 20, React 18)

Replace `"readFileSync·is·undefined"` with `'readFileSync·is·undefined'`
}
} catch (error) {
moduleWrapperCache.set(wrapperModule, false);
}
}
}

Check failure on line 91 in packages/web-worker/src/webpack-parts/loader.ts

View workflow job for this annotation

GitHub Actions / Test (Node 18, React 18)

Delete `⏎`

Check failure on line 91 in packages/web-worker/src/webpack-parts/loader.ts

View workflow job for this annotation

GitHub Actions / Test (Node 20, React 18)

Delete `⏎`


if (wrapperContent) {
plugin.virtualModules.writeModule(
virtualModule,
Expand All @@ -90,9 +101,9 @@
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 +159,21 @@
);
}

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
Loading