Skip to content

[question] Separate output path for resources #9

Closed

Description

@webdiscus
  1. I might have svg files in src/img folder. But I might also have svg files in src/fonts. I wanna seperate them in build time. src/img svg files to dist/img, src/fonts to dist/fonts. Like this.

  2. If i have folder structure like this "src/img/banners" and if i require these images from "src/img/banners", can i have dist folder structure like this "dist/img/banners"? Or just "dist/img"? Because i can have kind of folder structures in src. Like "src/fonts/OpenSans/..." or "src/img/banners/...". I wanna have dist folder like this "dist/fonts/OpenSans/..." or "dist/img/banners/...".

Activity

  1. webdiscus commented on Apr 18, 2022

    @webdiscus
    OwnerAuthor

    hello @myasarbardaklar,

    yes, it is possible.

    Common solution to hold same folder structure of asset resources in public path is using the property filename as the function.

    Add to the module.rules in webpack config:

    {
      test: /\.(png|jpg|jpeg|ico|svg|woff2)/, // filter for both images and fonts
      type: 'asset/resource',
      generator: {
        filename: (pathData) => {
          const { dir } = path.parse(pathData.filename); // get relative path started with `src/...`
          const outputPath = dir.replace('src/', ''); // remove the source dir from path
          return outputPath + '/[name].[hash:8][ext]'; // return output path with resource filename
        },
      },
    }

    The output path dist/ will be have same directory structure as src/.

    If you will use the responsive-loader then here is a bit complex. The option name is string. You can define here only some like [hash]-[width].[ext]. But exists the option outputPath that can be as function. Try to use this option like in example above.

  2. Repository owner locked and limited conversation to collaborators on Apr 19, 2022
  3. converted this issue into a discussion #14 on Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions