vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.1k stars 5.91k forks source link

Worker is built in legacy mode with the {type: "module"} option #6761

Open KubesDavid opened 2 years ago

KubesDavid commented 2 years ago

Describe the bug

After running vite build, Worker is built in legacy mode with the {type: "module"} option, which is undesirable in this case and results in the application breaking in older browser versions.

vite.config.ts

import vue from '@vitejs/plugin-vue';
import legacy from '@vitejs/plugin-legacy';
import { resolve } from 'path';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [vue(), legacy()],
  build: {
    reportCompressedSize: false,
  },
  worker: {
    format: 'iife',  // 'es' produces the same output
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, './src'),
    },
  },
});

index-legacy.057ce815.js

return new Worker("/assets/tv.worker.b73a6f8e.js", {type: "module"})

Reproduction

https://stackblitz.com/edit/node-kfimtt

System Info

System:
    OS: macOS 12.2
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 1.82 GB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.4.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.4.0 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 98.1.35.100
    Chrome: 60.0.3112.113
    Safari: 15.3
  npmPackages:
    @vitejs/plugin-legacy: ^1.6.4 => 1.6.4 
    @vitejs/plugin-vue: ^2.1.0 => 2.1.0 
    vite: ^2.8.0-beta.5 => 2.8.0-beta.5

Used Package Manager

npm

Logs

No response

Validations

vigneshpa commented 2 years ago

Yes, this must not be happening.

sapphi-red commented 2 years ago

For a workaround, you can use Import with Constructors.

okikio commented 2 years ago

@sapphi-red That doesn't solve the problem of workers always being built in iife mode even when the user has specifically chosen esm as their build format.