|
| 1 | +import path from 'path'; |
| 2 | +import loaderUtils from 'loader-utils'; |
| 3 | + |
| 4 | +import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin'; |
| 5 | +import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'; |
| 6 | +import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin'; |
| 7 | + |
| 8 | +export default function loader() {} |
| 9 | + |
| 10 | +const CACHE = {}; |
| 11 | + |
| 12 | +loader.pitch = function(request) { |
| 13 | + this.cacheable(false); |
| 14 | + |
| 15 | + const options = loaderUtils.getOptions(this) || {}; |
| 16 | + |
| 17 | + const cb = this.async(); |
| 18 | + |
| 19 | + const filename = loaderUtils.interpolateName(this, `${options.name || '[hash]'}.worker.js`, { |
| 20 | + context: options.context || this.options.context, |
| 21 | + regExp: options.regExp |
| 22 | + }); |
| 23 | + |
| 24 | + const worker = {}; |
| 25 | + |
| 26 | + worker.options = { |
| 27 | + filename, |
| 28 | + chunkFilename: `[id].${filename}`, |
| 29 | + namedChunkFilename: null |
| 30 | + }; |
| 31 | + |
| 32 | + worker.compiler = this._compilation.createChildCompiler('worker', worker.options); |
| 33 | + |
| 34 | + worker.compiler.apply(new WebWorkerTemplatePlugin(worker.options)); |
| 35 | + |
| 36 | + if (this.target!=='webworker' && this.target!=='web') { |
| 37 | + worker.compiler.apply(new NodeTargetPlugin()); |
| 38 | + } |
| 39 | + |
| 40 | + worker.compiler.apply(new SingleEntryPlugin(this.context, `!!${path.resolve(__dirname, 'rpc-worker-loader.js')}!${request}`, 'main')); |
| 41 | + |
| 42 | + const subCache = `subcache ${__dirname} ${request}`; |
| 43 | + |
| 44 | + worker.compiler.plugin('compilation', (compilation, data) => { |
| 45 | + if (compilation.cache) { |
| 46 | + if (!compilation.cache[subCache]) compilation.cache[subCache] = {}; |
| 47 | + |
| 48 | + compilation.cache = compilation.cache[subCache]; |
| 49 | + } |
| 50 | + |
| 51 | + data.normalModuleFactory.plugin('parser', (parser, options) => { |
| 52 | + parser.plugin('export declaration', expr => { |
| 53 | + let decl = expr.declaration || expr, |
| 54 | + { compilation, current } = parser.state, |
| 55 | + entry = compilation.entries[0].resource; |
| 56 | + |
| 57 | + // only process entry exports |
| 58 | + if (current.resource!==entry) return; |
| 59 | + |
| 60 | + let exports = compilation.__workerizeExports || (compilation.__workerizeExports = {}); |
| 61 | + |
| 62 | + if (decl.id) { |
| 63 | + exports[decl.id.name] = true; |
| 64 | + } |
| 65 | + else if (decl.declarations) { |
| 66 | + for (let i=0; i<decl.declarations.length; i++) { |
| 67 | + exports[decl.declarations[i].id.name] = true; |
| 68 | + } |
| 69 | + } |
| 70 | + else { |
| 71 | + console.warn('[workerize] unknown export declaration: ', expr); |
| 72 | + } |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + worker.compiler.runAsChild((err, entries, compilation) => { |
| 78 | + if (err) return cb(err); |
| 79 | + |
| 80 | + if (entries[0]) { |
| 81 | + worker.file = entries[0].files[0]; |
| 82 | + |
| 83 | + let contents = compilation.assets[worker.file].source(); |
| 84 | + let exports = Object.keys(CACHE[worker.file] = compilation.__workerizeExports || CACHE[worker.file] || {}); |
| 85 | + |
| 86 | + // console.log('Workerized exports: ', exports.join(', ')); |
| 87 | + |
| 88 | + if (options.inline) { |
| 89 | + worker.url = `URL.createObjectURL(new Blob([${JSON.stringify(contents)}]))`; |
| 90 | + } |
| 91 | + else { |
| 92 | + worker.url = `__webpack_public_path__ + ${JSON.stringify(worker.file)}`; |
| 93 | + } |
| 94 | + |
| 95 | + if (options.fallback === false) { |
| 96 | + delete this._compilation.assets[worker.file]; |
| 97 | + } |
| 98 | + |
| 99 | + return cb(null, ` |
| 100 | + var addMethods = require(${loaderUtils.stringifyRequest(this, path.resolve(__dirname, 'rpc-wrapper.js'))}) |
| 101 | + var methods = ${JSON.stringify(exports)} |
| 102 | + module.exports = function() { |
| 103 | + var w = new Worker(${worker.url}, { name: ${JSON.stringify(filename)} }) |
| 104 | + addMethods(w, methods) |
| 105 | + ${ options.ready ? 'w.ready = new Promise(function(r) { w.addEventListener("ready", function(){ r(w) }) })' : '' } |
| 106 | + return w |
| 107 | + } |
| 108 | + `); |
| 109 | + } |
| 110 | + |
| 111 | + return cb(null, null); |
| 112 | + }); |
| 113 | +}; |
0 commit comments