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,47 +10,53 @@ 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() {
// Trick Papa Parse into thinking it's being imported by RequireJS if (!workerCode) {
// so we can capture the AMD callback. // Trick Papa Parse into thinking it's being imported by RequireJS
let factory: (() => unknown) | undefined; // so we can capture the AMD callback.
global.define = (_: unknown, callback: () => void) => { let factory: (() => unknown) | undefined;
factory = callback; global.define = (_: unknown, callback: () => void) => {
}; factory = callback;
global.define.amd = true; };
await import('papaparse'); global.define.amd = true;
delete global.define; await import('papaparse');
delete global.define;
if (!factory) { if (!factory) {
throw new Error('Failed to capture Papa Parse AMD callback'); throw new Error('Failed to capture Papa Parse AMD callback');
}
workerCode = `
var global = (function() {
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
return {};
})();
global.IS_PAPA_WORKER = true;
(${factory.toString()})();
`;
const result = await build({
stdin: {
contents: workerCode,
},
write: false,
minify: true,
});
workerCode = result.outputFiles[0].text;
} }
const workerCode = `
var global = (function() {
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
return {};
})();
global.IS_PAPA_WORKER = true;
(${factory.toString()})();`;
const result = await build({
stdin: {
contents: workerCode,
},
write: false,
minify: true,
});
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,
}); });
}, },