Skip to content

Commit

Permalink
Merge pull request #49 from arturovt/fix/issue-25
Browse files Browse the repository at this point in the history
fix: replace `type` property with `undefined` except of replacing with empty string
  • Loading branch information
developit authored Jan 4, 2020
2 parents 7c353d5 + 6823558 commit 9b3dcf1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ export default class WorkerPlugin {
if (this.options.workerType) {
ParserHelpers.toConstantDependency(parser, JSON.stringify(this.options.workerType))(typeModuleExpr.value);
} else if (this.options.preserveTypeModule !== true) {
ParserHelpers.toConstantDependency(parser, '')(typeModuleExpr);
// Options object can contain comma at the end e.g. `{ type: 'module', }`.
// Previously, `type` property was replaced with an empty string
// that left this comma.
// Currently the `type` property value is replaced with `undefined`.
ParserHelpers.toConstantDependency(parser, 'type:undefined')(typeModuleExpr);
}

return ParserHelpers.addParsedVariableToModule(parser, id, req);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
12 changes: 11 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 9b3dcf1

Please sign in to comment.