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

Angular SVG as templates do not load in client #16438

Open
chris-elta opened this issue Oct 21, 2021 · 10 comments
Open

Angular SVG as templates do not load in client #16438

chris-elta opened this issue Oct 21, 2021 · 10 comments

Comments

@chris-elta
Copy link

chris-elta commented Oct 21, 2021

Describe the bug
Angular allows the use of *.svg files for component templates, however they do not load in storybook.

To Reproduce

  1. Add an SVG file to an angular component i.e., home-icon.svg
  2. Set the SVG url as the templateUrl for the angular component i.e., templateUrl: './home-icon.svg'
@Component({
	selector: 'home-icon',
	templateUrl: '../svg/home-icon.svg',
})

System
Displays static/media/**/src/icons/svg/home-icon..svg instead of the icon presentation

Version
v6.3.9

@DmitryShashkov
Copy link

Issue still present as of now with Storybook v6.5.16

@shilman
Copy link
Member

shilman commented Mar 31, 2023

Please try upgrading to the latest prerelease. Is it still a problem?

Migration guide: https://storybook.js.org/migration-guides/7.0

@lacolaco
Copy link

lacolaco commented Jun 20, 2023

@shilman Yes, it is. sb 7.0.22 + Angular v16.1.

@twardzikfilip
Copy link

I can confirm, it's present on latest Storybook + Angular v14.15.

@atomicrobokid
Copy link

@shilman - Hey, any update on this? Experiencing this issue now and will stop us rolling out a major design system update as we cannot document any icons or components that use them. This is on 7.1.1

@lukket
Copy link

lukket commented Aug 3, 2023

It may be too obvious, but as a workaround you can just change the file ending from .svg to .html.

As every SVG is also a valid Angular template, the file ending doesn't seem to make any real difference. Storybook seems to treat all *.svg files as asset. If you follow the path that Stroybook displays under static/media/... you'll see that the file exists.

@JonET
Copy link

JonET commented Aug 29, 2023

That workaround doesn't really help me as the SVG assets are coming from an external npm repository. I'd really need to have this fixed in storybook if I want the documentation to not be busted.

@metalhaze
Copy link

BUMP

Experiencing this same issue. Any ETA on when this might be fixed?

@valentinpalkovic
Copy link
Contributor

valentinpalkovic commented Jan 25, 2024

Hi there,

It seems like the issue is related to the configuration of asset resources in Storybook's default webpack configuration. The current configuration includes SVG files being processed as asset resources.

As a workaround, you must modify the asset resource rule by adding a webpackFinal section in your .storybook/main.ts file. Here's the updated code:

// .storybook/main.ts
const config = {
  framework: {
    name: "@storybook/angular",
    options: {},
  },
  webpackFinal: async (config) => {
    config.module!.rules = config.module!.rules!.map((rule) => {
      if (rule && typeof rule === "object" && "type" in rule && rule.type === "asset/resource") {
        return {
          ...rule,
          test: new RegExp(
            rule.test!.toString().replace("svg|", "").slice(1, -1)
          ),
        };
      } else {
        return rule;
      }
    });
    
    return config;
  }
}

export default config;

I am curious about your feedback. Please let me know if this solution is sufficient enough as a workaround. If it works out, we could built-in a proper solution.

@metalhaze
Copy link

@valentinpalkovic This works for our needs!!! Thank you so much!
CleanShot 2024-01-25 at 10 48 16

@vanessayuenn vanessayuenn removed this from the 8.0-Stable milestone Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests