[Bug] Importing "virtual:pwa-register" could not be resolved

See original GitHub issue

Describe the bug

I’m building a PWA with Storybook and builder-vite. For convenience i wanted to add Vite-Plugin-PWA. To inform the user about an update a ReloadPrompt was added, which is also mentioned in the docs. The ReloadPrompt Component uses an import import { useRegisterSW } from "virtual:pwa-register/react"; but the builder does not seem to find the dependency.

Steps to reproduce the behavior

  1. Add Vite-Plugin-PWA to package.json
  2. Create the ReloadPrompt.tsx from here
  3. yarn start-storybook

Expected behavior

Builder-vite should successfully import the virtual module

Screenshots and/or logs

The following dependencies are imported but could not be resolved:
virtual:pwa-register/react (imported by C:/Projekte/PWA/src/components/ReloadPrompt/ReloadPrompt.tsx)

Environment

  • OS: [e.g. iOS] Windows
  • Node.js version: [e.g. v12.17.0] v14.18.0
  • Yarn version: [e.g. 6.14.4] 1.22.17
  • Browser (if applicable): [e.g. chrome, safari] Brave
  • Browser version (if applicable): [e.g. 22] 1.37.109

Additional context

When adding VitePluginPWA to the Vite config in Storybooks main.js the error is not shown, but Storybook wont build the iframe properly.

main.js:

const { VitePWA } = require("vite-plugin-pwa");

module.exports = {
    core: {
        builder: "@storybook/builder-vite",
    },
    stories: ["../src/**/**/*.stories.tsx"],
    addons: ["@storybook/addon-essentials"],
    // staticDirs: ['../public'],
    framework: "@storybook/react",

    async viteFinal(config, { configType }) {
        config.optimizeDeps.include = [
            "react-router-dom",
            "http-proxy-middleware",
            "vite",
            "vite-plugin-pwa",
            "fast-deep-equal",
            ...config.optimizeDeps.include,
        ];
        config.plugins.push(VitePWA({}));
        return config;
    },
};

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Hydraircommented, Apr 4, 2022

Yes, here it is: https://github.com/Hydrair/MWE-import-virtual Using yarn and yarn storybook it produces the same behavior like the original project. After optimizing dependencies.

0reactions
Hydraircommented, Apr 8, 2022

Alright fixed it with https://github.com/windicss/windicss/issues/267#issuecomment-947881136.

The main.js now looks like this:

const VitePWA = require("vite-plugin-pwa").VitePWA;
const { dirname } = require("path");

module.exports = {
    core: {
        builder: "@storybook/builder-vite",
    },
    stories: ["../src/**/**/*.stories.tsx"],
    addons: ["@storybook/addon-essentials"],
    framework: "@storybook/react",
    async viteFinal(config, { configType }) {
        config.optimizeDeps.include = [
            "react-router-dom",
            "vite",
            "vite-plugin-pwa",
            ...config.optimizeDeps.include,
        ];
        config.optimizeDeps.exclude = ["path", "fs"];
        config.plugins = config.plugins ?? [];
        config.plugins.push(
            VitePWA({
                root: dirname(__dirname),
            })
        );
        return config;
    },
};

Apparently by specifying the root Vite knows how to look for the exports.

Thanks for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parcel
$ parcel index.html Server running at http://localhost:1234 Build failed. @parcel/core: Cannot resolve 'ract' from './index.js' /dev/app/index.js: ...
Read more >
Plugins - esbuild
If the plugin doesn't provide one, esbuild's default behavior won't resolve any imports in this module. This directory will be passed to any...
Read more >
vite-plugin-pwa - npm
Start using vite-plugin-pwa in your project by running `npm i vite-plugin-pwa`. There are 26 other projects in the npm registry using ...
Read more >
Progressive Web Application (PWA) - îles
clientsClaim and skipWaiting will be enabled; registerSW.js won't be generated unless explicitly enabled. Using a prompt for updates. When using ...
Read more >
How do I implement a typescript service worker with vite ...
The first thing I did was to install the Vite plugin: npm i vite-plugin-pwa -D. then to the vite.config.ts I added the import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found