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

add outdir option per image proposal #15

Open
userquin opened this issue Aug 25, 2023 · 6 comments
Open

add outdir option per image proposal #15

userquin opened this issue Aug 25, 2023 · 6 comments

Comments

@userquin
Copy link
Member

By default the pwa assets will be generated in the image source folder, you can use a assetsName callback to change the output dir to create the assets in subfolders or in another path (via relative path using ..).

Since the pwa assets will use node path resolve with the image source folder, we can add a new option (optional) and resolve the asset against the new option or the image folder.

Hint:

We need to change the images signature to (and the usage from the cli-start module):

images: string | string[] | ImageData | ImageData[]
...
export interface ImageData {
  path: string
  outputDir: string
}

https://github.com/vite-pwa/assets-generator/blob/main/src/build.ts#L39 will be:

const folder = typeof image === 'string' ? dirname(imagePath) :  resolve(buildOptions.root, image.outputDir) : 

From this comment #13 (comment)

@DamianGlowala
Copy link

DamianGlowala commented Aug 25, 2023

To add for the record:

I thought that adding a relative path to the assetName would be a sufficient workaround to change the output location of the generated files. It indeed worked for majority of them. However, I discovered that when a favicon (favicon.ico) is generated, it remains in the input image's directory. Hence I see this addition as essential (and, subjectively speaking, a lot cleaner!).

@userquin
Copy link
Member Author

userquin commented Aug 25, 2023

However, I discovered that when a favicon (favicon.ico) is generated, it remains in the input image's directory.

Uhmm, you can provide the favicon names with the relative paths, if using defaults the corresponding names will be plain names. You can override favicons with relative paths:

export const minimalPreset: Preset = {
  transparent: {
    sizes: [64, 192, 512],
    favicons: [[64, '../../../public/favicon.ico']], // <== this one: by default [[64, 'favicon.ico']],
  },
  maskable: {
    sizes: [512],
  },
  apple: {
    sizes: [180],
  },
}

@DamianGlowala
Copy link

Ahh, you're totally right, that's the way to go in this case ;) appreciate your help

@benkroeger
Copy link

if you need the path change for all the generated assets, add an assetName function to your preset:

import { AssetType, ResolvedAssetSize, defaultAssetName, defineConfig } from '@vite-pwa/assets-generator/config';

export default defineConfig({
  preset: {
    transparent: {
      sizes: [64, 192, 512],
      favicons: [[64, 'icons/favicons/favicon.ico']],
    },
    maskable: {
      sizes: [512],
    },
    apple: {
      sizes: [180],
    },
    assetName: (type: AssetType, size: ResolvedAssetSize) => {
      return `icons/favicons/${defaultAssetName(type, size)}`;
    },
  },
  images: ['public/logo.svg'],
});

@egtwp
Copy link

egtwp commented Feb 22, 2024

Hello all,
any progress on this?

Regards,
Emanuele.

@holtwick
Copy link

if you need the path change for all the generated assets, add an assetName function to your preset:

import { AssetType, ResolvedAssetSize, defaultAssetName, defineConfig } from '@vite-pwa/assets-generator/config';

export default defineConfig({
  preset: {
    transparent: {
      sizes: [64, 192, 512],
      favicons: [[64, 'icons/favicons/favicon.ico']],
    },
    maskable: {
      sizes: [512],
    },
    apple: {
      sizes: [180],
    },
    assetName: (type: AssetType, size: ResolvedAssetSize) => {
      return `icons/favicons/${defaultAssetName(type, size)}`;
    },
  },
  images: ['public/logo.svg'],
});

This worked for me. Thanks @benkroeger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants