Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default class WorkerPlugin {
return false;
}

let loaderOptions = opts.name && { name: opts.name };
const req = `require(${JSON.stringify(workerLoader + (loaderOptions ? ('?' + JSON.stringify(loaderOptions)) : '') + '!' + dep.string)})`;
const loaderOptions = { name: opts.name || 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
17 changes: 17 additions & 0 deletions test/fixtures/multiple/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

export const foo = 'bar';
28 changes: 28 additions & 0 deletions test/fixtures/multiple/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

const worker0 = new Worker('./worker-0', { type: 'module' });
const worker1 = new Worker('./worker-1', { type: 'module' });

worker0.onmessage = ({ data }) => {
console.log('page got data: ', data);
};
worker0.postMessage('hello 0');

worker1.onmessage = ({ data }) => {
console.log('page got data: ', data);
};
worker1.postMessage('hello 1');
19 changes: 19 additions & 0 deletions test/fixtures/multiple/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html><body>
<!--
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
-->
<script src="dist/main.js"></script>
</body></html>
26 changes: 26 additions & 0 deletions test/fixtures/multiple/worker-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { foo } from './dep';

console.log('hello from worker 0');

addEventListener('message', ({ data }) => {
console.log('worker 0 got message', data);
if (data === 'hello 0') {
postMessage(foo);
}
});
26 changes: 26 additions & 0 deletions test/fixtures/multiple/worker-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

import { foo } from './dep';

console.log('hello from worker 1');

addEventListener('message', ({ data }) => {
console.log('worker 1 got message', data);
if (data === 'hello 1') {
postMessage(foo);
}
});
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ describe('worker-plugin', () => {
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"0\.worker\.js"/g);
});

test('it replaces multiple Worker exports with __webpack_require__', async () => {
const stats = await runWebpack('multiple', {
plugins: [
new WorkerPlugin()
]
});

const assetNames = Object.keys(stats.assets);
expect(assetNames).toHaveLength(3);
expect(assetNames).toContainEqual('0.worker.js');
expect(assetNames).toContainEqual('1.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);
});

test('retainModule:true leaves {type:module} in worker init', async () => {
const { assets } = await runWebpack('basic', {
plugins: [
Expand Down