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;
let refId = '';
let workerCode = '';
return {
name,
async buildStart() {
// Trick Papa Parse into thinking it's being imported by RequireJS
// so we can capture the AMD callback.
let factory: (() => unknown) | undefined;
global.define = (_: unknown, callback: () => void) => {
factory = callback;
};
global.define.amd = true;
await import('papaparse');
delete global.define;
if (!workerCode) {
// Trick Papa Parse into thinking it's being imported by RequireJS
// so we can capture the AMD callback.
let factory: (() => unknown) | undefined;
global.define = (_: unknown, callback: () => void) => {
factory = callback;
};
global.define.amd = true;
await import('papaparse');
delete global.define;
if (!factory) {
throw new Error('Failed to capture Papa Parse AMD callback');
if (!factory) {
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({
type: 'asset',
name: `papaparse-worker.js`,
source: result.outputFiles[0].text,
name: 'papaparse-worker.js',
source: workerCode,
});
},