web/satellite: fix papa parse worker plugin issue in watch mode

This change fixes an issue that caused the Papa Parse worker plugin to
break during Vite's watch mode.

Change-Id: I84d03d38e2d0d6fe4b7536706638640d3ed259a6
This commit is contained in:
Jeremy Wharton 2023-11-18 07:39:43 -06:00 committed by Storj Robot
parent 6d22ec21d0
commit 866780bcde

View File

@ -10,11 +10,13 @@ export default function papaParseWorker(): Plugin {
const resolvedVirtualModuleId = '\0' + virtualModuleId; const resolvedVirtualModuleId = '\0' + virtualModuleId;
let refId = ''; let refId = '';
let workerCode = '';
return { return {
name, name,
async buildStart() { async buildStart() {
if (!workerCode) {
// Trick Papa Parse into thinking it's being imported by RequireJS // Trick Papa Parse into thinking it's being imported by RequireJS
// so we can capture the AMD callback. // so we can capture the AMD callback.
let factory: (() => unknown) | undefined; let factory: (() => unknown) | undefined;
@ -29,7 +31,7 @@ export default function papaParseWorker(): Plugin {
throw new Error('Failed to capture Papa Parse AMD callback'); throw new Error('Failed to capture Papa Parse AMD callback');
} }
const workerCode = ` workerCode = `
var global = (function() { var global = (function() {
if (typeof self !== 'undefined') { return self; } if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; } if (typeof window !== 'undefined') { return window; }
@ -37,7 +39,8 @@ export default function papaParseWorker(): Plugin {
return {}; return {};
})(); })();
global.IS_PAPA_WORKER = true; global.IS_PAPA_WORKER = true;
(${factory.toString()})();`; (${factory.toString()})();
`;
const result = await build({ const result = await build({
stdin: { stdin: {
@ -47,10 +50,13 @@ export default function papaParseWorker(): Plugin {
minify: true, minify: true,
}); });
workerCode = result.outputFiles[0].text;
}
refId = this.emitFile({ refId = this.emitFile({
type: 'asset', type: 'asset',
name: `papaparse-worker.js`, name: 'papaparse-worker.js',
source: result.outputFiles[0].text, source: workerCode,
}); });
}, },