From f48e67b13c287437204c9d5a40bb6a58036ea4e4 Mon Sep 17 00:00:00 2001 From: Andrew Stein Date: Sun, 17 Nov 2024 23:09:07 -0500 Subject: [PATCH] Fix (another) minification issue, make troublemaker minification function disableable Signed-off-by: Andrew Stein --- docs/src/components/DocItem/browser.js | 5 +- examples/blocks/src/raycasting/index.js | 2 +- packages/perspective-esbuild-plugin/index.js | 6 +- packages/perspective-esbuild-plugin/wasm.js | 67 ++++++++++--------- packages/perspective-esbuild-plugin/worker.js | 23 ------- 5 files changed, 45 insertions(+), 58 deletions(-) diff --git a/docs/src/components/DocItem/browser.js b/docs/src/components/DocItem/browser.js index 275d586e30..f5993c38a3 100644 --- a/docs/src/components/DocItem/browser.js +++ b/docs/src/components/DocItem/browser.js @@ -10,11 +10,12 @@ // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ -import "@finos/perspective-viewer"; -import "@finos/perspective-viewer-datagrid"; import { SUPERSTORE_TABLE } from "@site/src/data/superstore.js"; export async function main(colorMode) { + await import("@finos/perspective-viewer", { type: "module" }); + await import("@finos/perspective-viewer-datagrid"); + await customElements.whenDefined("perspective-viewer"); const viewers = document.querySelectorAll( "perspective-viewer:not(.nosuperstore)" diff --git a/examples/blocks/src/raycasting/index.js b/examples/blocks/src/raycasting/index.js index 90c12f7dbe..78963f49e5 100644 --- a/examples/blocks/src/raycasting/index.js +++ b/examples/blocks/src/raycasting/index.js @@ -55,7 +55,7 @@ for (var j := 0; j <= radialSegments; j += 1) { vs[i0] := vs[i0] * bcos - vs[i0 + 2] * bsin; vs[i0 + 2] := b * bsin + vs[i0 + 2] * bcos; } -} +}; // Render scene var scale := resolution / (tan(fov / 2) * 400); diff --git a/packages/perspective-esbuild-plugin/index.js b/packages/perspective-esbuild-plugin/index.js index f97436d1ba..5328ada8a2 100644 --- a/packages/perspective-esbuild-plugin/index.js +++ b/packages/perspective-esbuild-plugin/index.js @@ -16,7 +16,11 @@ const { WorkerPlugin } = require("./worker.js"); exports.PerspectiveEsbuildPlugin = function PerspectiveEsbuildPlugin( options = {} ) { - const wasm_plugin = WasmPlugin(!!options.wasm?.inline); + const wasm_plugin = WasmPlugin( + !!options.wasm?.inline, + !options.wasm?.webpack_hack + ); + const worker_plugin = WorkerPlugin({ targetdir: options.worker?.targetdir, }); diff --git a/packages/perspective-esbuild-plugin/wasm.js b/packages/perspective-esbuild-plugin/wasm.js index 76a24be4a9..e55a107292 100644 --- a/packages/perspective-esbuild-plugin/wasm.js +++ b/packages/perspective-esbuild-plugin/wasm.js @@ -13,7 +13,7 @@ const fs = require("fs"); const path = require("path"); -exports.WasmPlugin = function WasmPlugin(inline) { +exports.WasmPlugin = function WasmPlugin(inline, webpack_hack) { function setup(build) { const options = build.initialOptions; options.metafile = true; @@ -62,12 +62,14 @@ exports.WasmPlugin = function WasmPlugin(inline) { .toString() .slice(2)}__`; KEYSET.push(key); + const url = webpack_hack ? `${key}(wasm)` : `wasm`; + return { pluginData: args.pluginData, contents: ` import wasm from ${JSON.stringify(args.path)}; export default function() { - return fetch(new URL(${key}(wasm), import.meta.url)); + return fetch(new URL(${url}, import.meta.url)); }; `, }; @@ -84,39 +86,42 @@ exports.WasmPlugin = function WasmPlugin(inline) { }); build.onEnd(({ metafile }) => { - for (const file of Object.keys(metafile.outputs)) { - if (file.endsWith(".js")) { - let contents = fs.readFileSync(file).toString(); - let updated = false; - for (const key of KEYSET) { - const symbol = contents.match( - new RegExp(`${key}\\(([a-zA-Z0-9_\$]+?)\\)`) - ); - - if (symbol?.[1]) { - updated = true; - const escapedSymbol = symbol[1].replace( - /\$/g, - "\\$" - ); - const filename = contents.match( - new RegExp( - `${escapedSymbol}\\s*?=\\s*?\\"(.+?)\\"` - ) + if (webpack_hack) { + for (const file of Object.keys(metafile.outputs)) { + if (file.endsWith(".js")) { + let contents = fs.readFileSync(file).toString(); + let updated = false; + for (const key of KEYSET) { + const symbol = contents.match( + new RegExp(`${key}\\(([a-zA-Z0-9_\$]+?)\\)`) ); - contents = contents.replace( - new RegExp( - `${key}\\(([a-zA-Z0-9_\$]+?)\\)`, - "g" - ), - `"${filename[1]}"` - ); + if (symbol?.[1]) { + updated = true; + const escapedSymbol = symbol[1].replace( + /\$/g, + "\\$" + ); + + const filename = contents.match( + new RegExp( + `(? { - for (const file of Object.keys(metafile.outputs)) { - if (file.endsWith(".js")) { - let contents = fs.readFileSync(file).toString(); - const symbol = contents.match( - /__PSP_INLINE_WORKER__\(([a-zA-Z0-9_\$]+?)\)/ - ); - if (symbol?.[1]) { - const filename = contents.match( - new RegExp(`${symbol[1]}\\s*?=\\s*?\\"(.+?)\\"`) - ); - - contents = contents.replace( - /__PSP_INLINE_WORKER__\([a-zA-Z0-9_\$]+?\)/, - `"${filename[1]}"` - ); - - fs.writeFileSync(file, contents); - } - } - } - }); } return {