in fusion-cli/build/loaders/worker-loader.js [51:130]
module.exports.pitch = function(request /* : any*/) {
const options = loaderUtils.getOptions(this) || {};
if (!this.webpack) {
throw new WorkerLoaderError({
name: 'Worker Loader',
message: 'This loader is only usable with webpack',
});
}
this.cacheable(false);
const cb = this.async();
const filename = loaderUtils.interpolateName(
this,
options.name || '[hash].worker.js',
{
context: options.context || this.rootContext || this.options.context,
regExp: options.regExp,
}
);
const worker = {};
worker.options = {
filename,
chunkFilename: `[id].${filename}`,
globalObject: 'self',
namedChunkFilename: null,
};
worker.compiler = this._compilation.createChildCompiler(
'worker',
worker.options
);
new WebWorkerTemplatePlugin(worker.options).apply(worker.compiler);
new SingleEntryPlugin(this.context, `!!${request}`, 'main').apply(
worker.compiler
);
const subCache = `subcache ${__dirname} ${request}`;
worker.compilation = compilation => {
if (compilation.cache) {
if (!compilation.cache[subCache]) {
compilation.cache[subCache] = {};
}
compilation.cache = compilation.cache[subCache];
}
};
const plugin = {name: 'WorkerLoader'};
worker.compiler.hooks.compilation.tap(plugin, worker.compilation);
worker.compiler.runAsChild((err, entries, compilation) => {
if (err) return cb(err);
if (entries[0]) {
worker.file = entries[0].files[0];
worker.factory = getWorker(
worker.file,
compilation.assets[worker.file].source(),
options
);
if (options.fallback === false) {
delete this._compilation.assets[worker.file];
}
return cb(null, `module.exports = ${worker.factory};\n`);
}
return cb(null, null);
});
};