Skip to content

Commit 98bd7e6

Browse files
committed
Make special behavior for workers based on the filename ending in .worker.js.
This allows us to keep the worker file located close to the file which uses it. It also makes it easier to have equivalent handling in esbuild.
1 parent f6db0d3 commit 98bd7e6

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

src/test/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ fetchMock.mockGlobal();
2525
(global as any).fetchMock = fetchMock;
2626

2727
// Mock the effects of the file-loader which our Webpack config defines
28-
// for JS files under res: The "default export" is the path to the file.
29-
jest.mock('firefox-profiler-res/gz-worker.js', () => './res/gz-worker.js');
28+
// for files ending in .worker.js: The "default export" is the path to the file.
29+
jest.mock('../utils/gz.worker.js', () => 'src/utils/gz.worker.js');
3030

3131
// Install a Worker class which is similar to the DOM Worker class.
3232
(global as any).Worker = NodeWorker;

src/types/globals/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Added by webpack's DefinePlugin
66
declare const AVAILABLE_STAGING_LOCALES: string[] | null;
77

8-
declare module 'firefox-profiler-res/*.js' {
8+
declare module '*.worker.js' {
99
const content: string;
1010
export default content;
1111
}

src/utils/gz.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import gzWorkerPath from 'firefox-profiler-res/gz-worker.js';
5+
import gzWorkerPath from './gz.worker.js';
66

77
function runGzWorker(
88
kind: 'compress' | 'decompress',
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function readableStreamToBuffer(stream) {
3131
}
3232

3333
onmessage = async (e) => {
34-
let data = e.data;
34+
const data = e.data;
3535
if (data.kind === 'compress') {
3636
// Create a gzip compression stream
3737
const compressionStream = new CompressionStream('gzip');
@@ -42,7 +42,7 @@ onmessage = async (e) => {
4242
writer.close();
4343

4444
// Read the compressed data back into a buffer
45-
let result = await readableStreamToBuffer(compressionStream.readable);
45+
const result = await readableStreamToBuffer(compressionStream.readable);
4646
postMessage(result, [result.buffer]);
4747
} else if (data.kind === 'decompress') {
4848
// Create a gzip compression stream
@@ -54,7 +54,7 @@ onmessage = async (e) => {
5454
writer.close();
5555

5656
// Read the compressed data back into a buffer
57-
let result = await readableStreamToBuffer(decompressionStream.readable);
57+
const result = await readableStreamToBuffer(decompressionStream.readable);
5858
postMessage(result, [result.buffer]);
5959
} else {
6060
throw new Error('unknown message');

webpack.config.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ const config = {
3232
devtool: 'source-map',
3333
module: {
3434
rules: [
35-
{
36-
test: /\.js$/,
37-
use: ['file-loader'],
38-
include: [path.join(__dirname, 'res')],
39-
},
4035
{
4136
test: /\.(js|ts|tsx)$/,
4237
use: ['babel-loader'],
43-
include: [path.join(__dirname, 'src')],
38+
include: includes,
39+
},
40+
{
41+
test: /\.worker\.js$/,
42+
use: ['file-loader'],
43+
include: includes,
4444
},
4545
{
4646
test: /\.json$/,
@@ -99,7 +99,6 @@ const config = {
9999
patterns: [
100100
'res/_headers',
101101
'res/_redirects',
102-
'res/gz-worker.js',
103102
'res/contribute.json',
104103
'res/robots.txt',
105104
'res/service-worker-compat.js',

0 commit comments

Comments
 (0)