在构建中包含 GeoTiff.js 和 Web Worker 时出现 Rollup/Vite 构建错误

问题描述 投票:0回答:1

我正在使用 Web Worker 对 GeoTiff 文件进行计算。我从

getRidgePoints
调用函数
CalculateRidge.ts
,这个函数创建一个worker,将消息发送给worker,然后等待worker的结果,最后在得到结果后终止worker并返回结果.工作人员被包裹在“等待”功能中(我听说过 Comlink,但我找到了这个解决方案并且效果很好)。

工人有一个

addEventListener
执行函数
CreateRidge_Init
将进行计算。此函数加载 GeoTiff 文件,读取它并根据从该文件读取的值进行计算。

我的问题是:

此代码不会构建。尝试构建代码时出现以下错误:

Error [RollupError]: Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
    at error (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:2125:30)
    at validateOptionsForMultiChunkOutput (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:17493:16)
    at Bundle.generate (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:17384:17)
    at async file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:25683:27
    at async catchUnfinishedHookActions (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/rollup/dist/es/shared/node-entry.js:24770:20)
    at async serialBundleWorkerEntry (file:///C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/node_modules/vite/dist/node/chunks/dep-d305c21f.js:22316:61) {
  code: 'PLUGIN_ERROR',
  url: 'https://rollupjs.org/configuration-options/#output-format',
  pluginCode: 'INVALID_OPTION',
  plugin: 'vite:worker',
  hook: 'transform',
  id: 'C:/Users/ono/Documents/Programmering/solar-analysis-faroe-island/src/lib/Functions/CalculateRidgeWorker.ts?worker&url',
  watchFiles: [
    ... basicly all the files used in this project ...
]

我测试了不同的东西,我注意到它是同时使用 GeoTiff.js(

fromArrayBuffer()
.getImage()
)和导致错误的 Web Worker 的组合:

  • 如果我绕过 Web Worker 并在主线程上执行
    CreateRidge_Init
    ,构建工作。
  • 如果我不在 Web Worker 中使用
    fromArrayBuffer()
    .getImage()
    ,构建工作。

我尝试尝试使用 build.rollupOptions 设置,但构建中没有错误更改。我想补充一点,代码在开发中工作得很好。

计算岭.ts:

import workerUrl from "src/lib/Functions/CalculateRidgeWorker?worker&url";
import type { Pos, Point } from "../Stores";

// The function calling the worker.
export async function getRidgePoints(pos: Pos) {

  // Create a worker
  const worker = new Worker(workerUrl, { type: 'module' })

  // Send a value to the worker.
  worker.postMessage(pos)

  // Wait for the worker to process the sent value and recieve the calculated value.
  const result = await runWorker(worker);

  // Terminate the worker.
  worker.terminate();

  return result
}

// An await function that will wait for a message from the worker
function runWorker(worker: Worker): Promise<Point[]> {
  return new Promise(resolve => {
    worker.onmessage = (e:MessageEvent<Point[]>) => {
      resolve(e.data)
    }
  })
}

CalculateRidgeWorker.ts:

addEventListener('message', async (e:MessageEvent<Pos>) => {
  let pos = e.data;
  let points = await getRidgePoints_Init(pos)
  postMessage(points)
})

// If i bypass the web worker, and execute this function on
// the main thread, the code does build succesfully.
async function getRidgePoints_Init(pos_m: Pos) {

  const DSM_Base = new URL(import.meta.url)
  const DSM_25M = new URL('FO_DSM_2017_FOTM_25M_DEFLATE_UInt16.tif', DSM_Base.origin);
  const DSM_5M = new URL('FO_DSM_2017_FOTM_5M_DEFLATE_UInt16.tif', DSM_Base.origin);

  // Load the 25M resolution map.
  let image_25M = await fetch(url)
    .then(response  => response.arrayBuffer())
    .then(tiff      => fromArrayBuffer(tiff)) // If i comment out these two lines when using a
    .then(result    => result.getImage())     // web worker, the code does build succesfully.

  ... Does alot of calculations based on this file ...

  return result
}

我当前的vite.config.ts文件:

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  base: "/solar-analysis-faroe-island/",
  plugins: [svelte()],

  build: {
    rollupOptions: {
      output: {
        format: "esm",
        inlineDynamicImports: true,
      }
    }
  },

  resolve: {
    alias: {
      src: path.resolve('src/'),
    }
  },
})
typescript vite web-worker rollupjs geotiff
1个回答
0
投票

我试图在汇总选项中更改输出格式,但我应该更改工作人员本身的格式(我不知道有 .worker 选项)。这个 vite.config.js 文件解决了错误,现在项目成功构建并运行:

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import path, { resolve } from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  base: "/solar-analysis-faroe-island/",
  plugins: [svelte()],

  worker: {
    format: "es"
  },

  resolve: {
    alias: {
      src: path.resolve('src/'),
    }
  },

})
© www.soinside.com 2019 - 2024. All rights reserved.