Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support webpack 5 #101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
71 changes: 8 additions & 63 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,6 @@ import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlug

export default function loader() {}

const CACHE = {};
const tapName = 'workerize-loader';

function compilationHook(compiler, handler) {
if (compiler.hooks) {
return compiler.hooks.compilation.tap(tapName, handler);
}
return compiler.plugin('compilation', handler);
}

function parseHook(data, handler) {
if (data.normalModuleFactory.hooks) {
return data.normalModuleFactory.hooks.parser.for('javascript/auto').tap(tapName, handler);
}
return data.normalModuleFactory.plugin('parser', handler);
}

function exportDeclarationHook(parser, handler) {
if (parser.hooks) {
return parser.hooks.exportDeclaration.tap(tapName, handler);
}
return parser.plugin('export declaration', handler);
}

loader.pitch = function(request) {
this.cacheable(false);

Expand Down Expand Up @@ -84,51 +60,20 @@ loader.pitch = function(request) {
}

(new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, 'main')).apply(worker.compiler);

const subCache = `subcache ${__dirname} ${request}`;

compilationHook(worker.compiler, (compilation, data) => {
if (compilation.cache) {
if (!compilation.cache[subCache]) compilation.cache[subCache] = {};

compilation.cache = compilation.cache[subCache];
}
parseHook(data, (parser, options) => {
exportDeclarationHook(parser, expr => {
let decl = expr.declaration || expr,
{ compilation, current } = parser.state,
entry = compilation.entries[0].resource;

// only process entry exports
if (current.resource!==entry) return;

let key = current.nameForCondition();
let exports = CACHE[key] || (CACHE[key] = {});

if (decl.id) {
exports[decl.id.name] = true;
}
else if (decl.declarations) {
for (let i=0; i<decl.declarations.length; i++) {
exports[decl.declarations[i].id.name] = true;
}
}
else {
console.warn('[workerize] unknown export declaration: ', expr);
}
});
});
});

worker.compiler.runAsChild((err, entries, compilation) => {
if (err) return cb(err);

if (entries[0]) {
worker.file = entries[0].files[0];
worker.file = Array.from(entries[0].files)[0];

let key = entries[0].entryModule.nameForCondition();
let contents = compilation.assets[worker.file].source();
let exports = Object.keys(CACHE[key] || {});

if (entries[0].entryModule.buildMeta.providedExports === true) {
// Can also occur if doing `export * from 'common js module'`
throw new Error('Attempted to load a worker implemented in CommonJS');
}

let exports = entries[0].entryModule.buildMeta.providedExports;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that as per webpack/webpack#8576 (landed on Webpack 5.0), buildMeta.providedExports is no longer there

To retrieve the provided exports like in webpack 4, we would need to call compilation.moduleGraph.getProvidedExports, i.e. let providedExports = compilation.moduleGraph.getProvidedExports(entries[0].entryModule);


// console.log('Workerized exports: ', exports.join(', '));

Expand Down
9 changes: 9 additions & 0 deletions test/src/and-the-other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function otherOtherFoo() {
return 2;
}

export function anyFoo() {
return 4;
}

export const otherOtherBar = 3;
3 changes: 3 additions & 0 deletions test/src/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.tragedy = function tragedy() {
return 'common';
};
21 changes: 21 additions & 0 deletions test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@ describe('worker', () => {

it('should be an instance of Worker', () => {
worker = new Worker();
// eslint-disable-next-line jest/no-jasmine-globals
expect(worker).toEqual(jasmine.any(window.Worker));
});

it('worker.foo()', async () => {
expect(await worker.foo()).toBe(1);
});

it('worker.otherFoo()', async () => {
expect(await worker.otherFoo()).toBe(-1);
});

it('worker.otherOtherFoo()', async () => {
expect(await worker.otherOtherFoo()).toBe(2);
});
it('worker.andTheOtherFoo()', async () => {
expect(await worker.andTheOtherFoo()).toBe(2);
});

it('worker.anyFoo()', async () => {
expect(await worker.anyFoo()).toBe(4);
});

it('worker.tragedy()', async () => {
expect(await worker.tragedy()).toBe('common');
});

it('worker.bar()', async () => {
let out = await worker.bar('a', 'b');
expect(out).toEqual('a [bar:3] b');
Expand Down Expand Up @@ -102,6 +122,7 @@ describe('?import option', () => {

it('should be an instance of Worker', () => {
worker = new ImportWorker();
// eslint-disable-next-line jest/no-jasmine-globals
expect(worker).toEqual(jasmine.any(window.Worker));
});

Expand Down
6 changes: 4 additions & 2 deletions test/src/other.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export function otherFoo() {}
export function otherFoo() {
return -1;
}

export const otherBar = 3;
export const otherBar = 3;
6 changes: 6 additions & 0 deletions test/src/worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { otherFoo, otherBar } from './other';

export { otherFoo };
export { otherOtherFoo as andTheOtherFoo } from './and-the-other';
export * from './and-the-other';

export { tragedy } from './common.js';

export function foo() {
return 1;
}

export const value = 3;

export function throwError() {
throw new Error('Error in worker.js');
}
Expand Down