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

Use external CSS files from .styles.scss source #5489

Merged
merged 1 commit into from
May 16, 2023

Conversation

hannajones
Copy link
Collaborator

This PR provides a potential solution for using external stylesheets in our wizard web components. The webpack config changes basically:

  • takes .scss source code and translates it to CSS using sass-loader
  • specifies type: "asset/resource" which will emit a separate file and export the URL of that generated file by default
  • specifies that the naming structure for the file that gets emitted to ensure it's a .css file

Once we've generated the CSS file we can import the .scss source in our web component to get the URL of the built file, then create a link pointing to that URL.

This whole process doesn't seem to work if we run things through MiniCssExtractPlugin.loader, so I've given the file a .styles.scss extension so that we can specify different Webpack rules for CSS we want to load into web components. (This was inspired by this article - I'm open to other naming conventions.)

I'm not 100% sure this is going to work in production environments, but it's working well for me locally, so seems like a starting point at least.

--

As a little experiment to check that we could load Protocol styles, I copied the contents of _protocol.scss into form-wizard.styles.scss and added an import for the protocol button component. This was the result:

Screenshot 2023-05-16 at 12 48 09 PM
Copy link
Collaborator

@mikeconley mikeconley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great for me! Let's hope it works on staging!

Copy link
Contributor

@escattone escattone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love it! Really nice work @hannajones, thank you! This should work great on stage/prod. I ran npm run webpack:build:prod locally and used dev tools to check where the shadow DOM elements were getting their CSS, and it was using form-wizard.styles.6b347252648b278f.css as expected.

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