sapphi-red / vite-plugin-static-copy

rollup-plugin-copy with dev server support.
MIT License
299 stars 32 forks source link

How to copy wasm files for vite-plugin-comlink #72

Closed goldenratio closed 11 months ago

goldenratio commented 11 months ago

Hey,

I am using comlink plugin to load workers and the workers inturn load .wasm files

In my vite plugins config, I have: (solution from https://stackoverflow.com/a/75616365/23147840)

 plugins: [
            viteStaticCopy({
                targets: [
                    {
                        src: "public/wasm/mozjpeg_wasm.wasm",
                        dest: "src/internals/services/image-service/mozjpeg/internal\:comlink\:./",
                    },
                ]
            }),
  1. This works fine in dev server
  2. Fails when I make build
16:49:45 [ERROR] [vite] [vite-plugin-static-copy:build] Path contains invalid characters: C:\Projects\dist\src\internals\services\image-service\mozjpeg\internal:comlink:.
Path contains invalid characters: C:\Projects\dist\src\internals\services\image-service\mozjpeg\internal:comlink:.
  Stack trace:
    at checkPath (C:\Projects\node_modules\fs-extra\lib\mkdirs\utils.js:16:21)
    at C:\Projects\node_modules\universalify\index.js:18:45
    at async copyAll (file:///C:/Projects/node_modules/vite-plugin-static-copy/dist/index.js:563:7)
    at async Promise.all (index 1)
    at async file:///C:/Projects/node_modules/rollup/dist/es/shared/node-entry.js:19526:13

any help is appreciated.

goldenratio commented 11 months ago

I fixed this by changing the target path based on NODE_ENV

const mode = process.env.NODE_ENV;

const mozJpegWASMCopy = ((pMode) => {
    if (pMode === 'production') {
        return {
            src: "public/wasm/mozjpeg_wasm.wasm",
            dest: "assets/",
        };
    }
    return {
        src: "public/wasm/mozjpeg_wasm.wasm",
        dest: "src/internals/services/image-service/mozjpeg/internal\:comlink\:./",
    };
})(mode);
plugins: [
            viteStaticCopy({
                targets: [
                    mozJpegWASMCopy
                ]
            }),