문제 상황을 알려주세요!


문제가 된 코드

// src/vite-config.ts
import { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';

export default defineConfig({
  plugins: [
    VitePWA({
      registerType: 'autoUpdate',
      workbox: {
        globPatterns: ['**/*.{js,css,html,png,svg,ico,json}'],
        cleanupOutdatedCaches: true,
      },
      filename: 'service-worker.js', // public/service-worker.js를 등록
    }),
  ],
});

에러 상황

Uncaught SyntaxError: Cannot use import statement outside a module (at service-worker.js:1:1)
error during build:
Could not resolve entry module "public/service-worker.js".
at getRollupError (file:///Users/son-youngjin/Development/softeer/Team6-DuBu/frontend/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:396:41)
at error (file:///Users/son-youngjin/Development/softeer/Team6-DuBu/frontend/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:392:42)
at ModuleLoader.loadEntryModule (file:///Users/son-youngjin/Development/softeer/Team6-DuBu/frontend/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:20237:20)
at async Promise.all (index 0)
 ELIFECYCLE  Command failed with exit code 1.

문제 원인이 무엇인가요?


Vite에서 vite-plugin-pwa를 사용할 경우, 서비스 워커 파일을 public이 아니라 src에 둬야 합니다.

🚨 이유 1: public 폴더의 파일은 번들링되지 않음

🚨 이유 2: vite-plugin-pwasrcDir 을 설정하지 않으면 해당 파일을 public 내에서 찾음

해결