vite-plugin-pwa: Error running plugin hook closeBundle for vite-plugin-pwa:build, expected a function hook.

App build fails with this error. image

vite.config.ts

// ...
    build: {
      outDir: 'build',
      emptyOutDir: true,
    },
    plugins: [
      react(),
      svgrPlugin({ svgrOptions: { icon: true } }),
      tsconfigPaths(),
      VitePWA({
        devOptions: { enabled: !isBuild },
        registerType: 'autoUpdate',
        manifest,
        workbox,
        outDir: 'build',
      }),
    ],
// ...

yarn dev works fine, but dev-dist/sw.js only precaches index.html no other js,css files.

yarn build fails with above error. rest of the build works fine, bundles are generated but no sw.js gets generated in the build folder.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

@RohitKaushal7 you can try with v0.12.8 without upgrading Vite to 3.1+: "vite-plugin-pwa": "0.12.8"

@ls so something installed on your local, here my deps:

pnpm list rollup -D --depth 2 
Legend: production dependency, optional only, dev only

xxxxx@1.0.0 /..../xxxxx

devDependencies:
astro 1.6.2
├── rollup 2.79.1
├─┬ vite 3.2.2
│ └── rollup 2.79.1
└─┬ vitefu 0.1.1
  └─┬ vite 3.2.2 peer
    └── rollup 2.79.1
vite 3.2.2
└── rollup 2.79.1
vite-plugin-compression 0.5.1
└─┬ vite 3.2.2 peer
  └── rollup 2.79.1
vite-plugin-pwa 0.13.1
├── rollup 2.79.1
├─┬ vite 3.2.2 peer
│ └── rollup 2.79.1
└─┬ workbox-build 6.5.4
  └── rollup 2.79.1

Well, it looks like I’m going to have to figure out why this is here. Thanks for helping debug!

We have a yarn resolution to use this version of rollup… no idea why that is yet.

head  node_modules/rollup/package.json
{
  "name": "rollup",
  "version": "2.74.1",
  "description": "Next-generation ES module bundler",
  "main": "dist/rollup.js",
  "module": "dist/es/rollup.js",
  "typings": "dist/rollup.d.ts",
  "bin": {
    "rollup": "dist/bin/rollup"
  },

Hey, I fixed those issues with vite 3.1.5. Thank you.

I am using NodeGlobalsPolyfillPlugin to access Buffer and process.

changed these imports to import from dist otherwise vite was importing from src or esm folder and then crying Cannot use import outside module.

- import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
+ import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill/dist/index.js'

- import replace from 'rollup-plugin-re'
+ import replace from 'rollup-plugin-re/dist/rollup-plugin-re.cjs'