File size: 926 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Based on https://github.com/webpack-contrib/worker-loader/tree/v2.0.0

const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');

module.exports.pitch = function (request) {
    // Technically this loader does work in other environments, but our use case does not want that.
    if (this.target !== 'web') {
        return 'throw new Error("Not supported in non-web environment");';
    }
    this.cacheable(false);
    const callback = this.async();
    const compiler = this._compilation.createChildCompiler('extension worker', {});
    new SingleEntryPlugin(this.context, `!!${request}`, 'extension worker').apply(compiler);
    compiler.runAsChild((err, entries, compilation) => {
        if (err) return callback(err);
        const file = entries[0].files[0];
        const source = `module.exports = ${JSON.stringify(compilation.assets[file].source())};`;
        return callback(null, source);
    });
};