Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset URL changes when lib rebuilds in watch mode #92

Open
kanonieer opened this issue Sep 6, 2024 · 7 comments
Open

Asset URL changes when lib rebuilds in watch mode #92

kanonieer opened this issue Sep 6, 2024 · 7 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@kanonieer
Copy link

Hi, TLDR I have issue that the url to assets get changed in output js files on rebuild in watch mode

Little project setup background:

/library
 /myComponent
  - myComponent.tsx
  - image.svg
/frontend-app

in /library I have a component that imports an svg for background image like

import image from './image.svg`

The image.svg is correctly bundled into dist/assets/image.svg and is correctly linked in myComponent.js bundled file as

import image from '../../assets/image.svg`

The issue I have occurs in dev mode in which I have the library built via vite build --watch - when I update code of library it gets rebuild and the link to asset get updated to

import image from `./assets/image.svg`

which no longer correctly links to asset folder (assets still exist under previous url)

here is how my vite.config looks like

export default defineConfig({
  plugins: [
    libInjectCss(),
    dts({
      insertTypesEntry: true,
    }),
    viteTsconfigPaths(),
    libAssets({
      name: '[name].[ext]',
    }),
  ],
  esbuild: {
    minifyIdentifiers: false,
  },
  build: {
    lib: {
      entry: resolve(__dirname, 'src/index.tsx'),
    },
    rollupOptions: {
      preserveEntrySignatures: 'strict',
      external: pkg.peers,
      input: Object.fromEntries(
        glob.sync('src/**/*.{ts,tsx}').map((file) => [
          // The name of the entry point
          // src/nested/foo.ts becomes nested/foo
          relative('src', file.slice(0, file.length - extname(file).length)),
          // The absolute path to the entry file
          // src/nested/foo.ts becomes /project/src/nested/foo.ts
          fileURLToPath(new URL(file, import.meta.url)),
        ]),
      ),
      output: [
        {
          format: 'es',
          // Put chunk files at <output>/chunks
          chunkFileNames: 'chunks/[name].[hash].js',
          // Put chunk styles at <output>/assets
          assetFileNames: 'assets/[name][extname]',
          entryFileNames: '[name].js',
        },
      ],
    },
  },
});

The issue seems to be similar to the one described there #89

PS. I was logging asset within outputPath function and I noticed it's only called during first build and not when the lib rebuilds - could it be the issue?

Thanks in advance for any help or advice 🙏

@coder-layne
Copy link
Contributor

@kanonieer Under watch mode, due to the build cache, unchanged files are not re-processed by the plugin, and the assets within them aren't recognized. As a result, it's expected that the outputPath function won't be called on some assets during rebuilds.
To emit complete assets during rebuilds, this plugin caches all emitted assets and re-emits them during the writeBundle phase. However, when emitting these cached assets, the import paths in importers are not processed, leading to this bug.

@coder-layne
Copy link
Contributor

@kanonieer I just released v0.5.25 under the next tag, you can try it to see if it solves your problem.

@rbatistajs
Copy link

@coder-layne in version v0.5.25 after changing any file the assets folder is deleted.

In version v0.5.24 I have an error after changing any file and the folder is also deleted.

image

@kanonieer
Copy link
Author

kanonieer commented Sep 23, 2024

@kanonieer I just released v0.5.25 under the next tag, you can try it to see if it solves your problem.

@coder-layne The fix you made resolved the issue I had - many thanks for the help ❤️ 🙇‍♂️

@coder-layne
Copy link
Contributor

@rbatistajs Thanks for your feedback, and can you provide a minimal reproducible version?

@mdsadiqueinam
Copy link

Getting error failed to preprocessCSS [sass] Undefined variable even the variable is defined

@coder-layne
Copy link
Contributor

@mdsadiqueinam Can you open a new issue and provide a minimal reproducible example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants