Skip to content

Commit

Permalink
Change worker chunk name from <ID> to w<ID>
Browse files Browse the repository at this point in the history
Fixes GoogleChromeLabs#43, as it avoids conflicts with other number-based webpack
chunks.
Note that the conflict may not be on the filename (since this plugin
adds '.worker' to it) but on the internal key used by webpack in
referencing modules.
  • Loading branch information
gluck committed Mar 9, 2020
1 parent bf6a40c commit 56f2ca5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class WorkerPlugin {
return false;
}

const loaderOptions = { name: opts.name || workerId + '' };
const loaderOptions = { name: opts.name || 'w' + workerId };
const req = `require(${JSON.stringify(workerLoader + '?' + JSON.stringify(loaderOptions) + '!' + dep.string)})`;
const id = `__webpack__worker__${workerId++}`;
ParserHelpers.toConstantDependency(parser, id)(expr.arguments[0]);
Expand Down
16 changes: 8 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('worker-plugin', () => {

const assetNames = Object.keys(stats.assets);
expect(assetNames).toHaveLength(2);
expect(assetNames).toContainEqual('0.worker.js');
expect(assetNames).toContainEqual('w0.worker.js');

const main = stats.assets['main.js'];
expect(main).toMatch(/[^\n]*new\s+Worker\s*\([^)]*\)[^\n]*/g);
Expand All @@ -56,7 +56,7 @@ describe('worker-plugin', () => {
/new\s+Worker\s*\(\s*__webpack__worker__\d\s*(,\s*\{\s+type\:\svoid [0]\s+\}\s*)?\)/g
);

expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"0\.worker\.js"/g);
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"w0\.worker\.js"/g);
});

test('it replaces multiple Worker exports with __webpack_require__', async () => {
Expand All @@ -68,12 +68,12 @@ describe('worker-plugin', () => {

const assetNames = Object.keys(stats.assets);
expect(assetNames).toHaveLength(3);
expect(assetNames).toContainEqual('0.worker.js');
expect(assetNames).toContainEqual('1.worker.js');
expect(assetNames).toContainEqual('w0.worker.js');
expect(assetNames).toContainEqual('w1.worker.js');

const main = stats.assets['main.js'];
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"0\.worker\.js"/g);
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"1\.worker\.js"/g);
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"w0\.worker\.js"/g);
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"w1\.worker\.js"/g);
});

test('retainModule:true leaves {type:module} in worker init', async () => {
Expand Down Expand Up @@ -248,8 +248,8 @@ describe('worker-plugin', () => {
await sleep(1000);
stats = await ready;
await sleep(1000);
expect(Object.keys(stats.assets).sort()).toEqual(['0.worker.js', 'main.js']);
expect(stats.assets['0.worker.js']).toContain(`hello from worker ${i}`);
expect(Object.keys(stats.assets).sort()).toEqual(['main.js', 'w0.worker.js']);
expect(stats.assets['w0.worker.js']).toContain(`hello from worker ${i}`);
}
} finally {
watcher.close();
Expand Down

0 comments on commit 56f2ca5

Please sign in to comment.