Skip to content

Commit

Permalink
Merge branch 'master' of github.com:GoogleChromeLabs/worker-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
andywer committed Jan 7, 2020
2 parents c433b8c + 9b3dcf1 commit c896c13
Showing 13 changed files with 230 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -75,6 +75,7 @@
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.8.0",
"memory-fs": "^0.4.1",
"microbundle": "^0.11.0",
"puppeteer": "^1.11.0",
"serve-handler": "^5.0.7",
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ export default class WorkerPlugin {
let workerId = 0;

compiler.hooks.normalModuleFactory.tap(NAME, factory => {
let workerId = 0;
for (const type of JS_TYPES) {
factory.hooks.parser.for(`javascript/${type}`).tap(NAME, parser => {
parser.hooks.new.for('imported var').tap(NAME, expr => {
2 changes: 1 addition & 1 deletion test/fixtures/basic/entry.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* the License.
*/

const worker = new Worker('./worker', { type: 'module' });
const worker = new Worker('./worker', { type: 'module', });
worker.onmessage = ({ data }) => {
console.log('page got data: ', data);
};
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/fixtures/watch/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';
21 changes: 21 additions & 0 deletions test/fixtures/watch/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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 worker = new Worker('./worker', { type: 'module' });
worker.onmessage = ({ data }) => {
console.log('page got data: ', data);
};
worker.postMessage('hello');
19 changes: 19 additions & 0 deletions test/fixtures/watch/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/watch/worker.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');

addEventListener('message', ({ data }) => {
console.log('worker got message', data);
if (data === 'hello') {
postMessage(foo);
}
});
29 changes: 28 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -44,7 +44,17 @@ describe('worker-plugin', () => {
expect(main).toMatch(/[^\n]*new\s+Worker\s*\([^)]*\)[^\n]*/g);

const workerInit = main.match(/[^\n]*new\s+Worker\s*\([^)]*\)[^\n]*/g)[0];
expect(workerInit).toMatch(/new\s+Worker\s*\(\s*__webpack__worker__\d\s*(,\s*\{\}\s*)?\)/g);
// As it replaces the value of the `type` property with `undefined`
// it will emit a string that contains line breaks, like:
// `{\n type: void 0 \n}`.
// We have to replace those line breaks thus it will become
// one-line string, like:
// `const worker = new Worker(__webpack__worker__0, { type: void 0 });`
const workerInitWithoutLineBreak = workerInit.replace(/\n/g, '');
// Match also the `type: void 0` string
expect(workerInitWithoutLineBreak).toMatch(
/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);
});
@@ -66,6 +76,23 @@ describe('worker-plugin', () => {
expect(main).toMatch(/module.exports = __webpack_require__\.p\s*\+\s*"1\.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: [

0 comments on commit c896c13

Please sign in to comment.