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

[Bug] Styles (.scss) inside React component not being processed #96

Closed
dhAlcojor opened this issue Oct 29, 2022 · 3 comments
Closed

[Bug] Styles (.scss) inside React component not being processed #96

dhAlcojor opened this issue Oct 29, 2022 · 3 comments

Comments

@dhAlcojor
Copy link

What's going on

I'm pretty sure it's probably my fault and I'm doing something wrong, but I have created a React component and styles are not working.

In ContentScript/index.tsx I'm adding a React component to the web page:

import React from 'react';
import ReactDOM from 'react-dom';
import ConventionsLink from './ConventionsLink';
...
const navigation: HTMLUListElement | null = document.querySelector(NAVIGATION_SELECTOR);
const conventions = document.createElement('li');
conventions.className = 'conventions';
navigation.append(conventions);
ReactDOM.render(<ConventionsLink />, conventions);

This is the code for the component:

import React from 'react';
import './ConventionsLink.scss';

const ConventionsLink: React.FC = () => {
  const onConventionsLink = (e: React.MouseEvent<HTMLAnchorElement>): void => {
    e.preventDefault();
    console.log('ConventionsMenuItem clicked');
  };

  return (
    <a
      id={'conventions-link'}
      href={'#'}
      onClick={($event): void => onConventionsLink($event)}
    >
      <span className={'icon'} />
      <span className={'title'}>Salones</span>
    </a>
  );
};

export default ConventionsLink;

And this is the file with the styles:

#conventions-link {
  color: orange;
}

I can't quite see what I might be doing wrong, but the styles are not working.

@dhAlcojor dhAlcojor changed the title [Bug] Styles inside React component not being processed [Bug] Styles (.scss) inside React component not being processed Feb 18, 2023
@laplandlearner
Copy link

laplandlearner commented Apr 25, 2023

Can you show me your webpack config?
I would like to confirm this. maybe scss rule is required in webpack config.
You can check webpack config here.
https://github.com/good-guy1218/frontdoor-extension

@dhAlcojor
Copy link
Author

dhAlcojor commented May 31, 2023

@yavis1218 The webpack.config.js is mostly the same as that can be found in the project.

I have made minor modifications like for example this one, to the port property (so I can test the extension while in development in Chrome and Firefox at the same time):

nodeEnv === 'development'
    ? new ExtensionReloader({
        port: targetBrowser === 'chrome' ? 9090 : 9091,
        reloadPage: true,
        entries: {
          // TODO: reload manifest on update
          contentScript: 'contentScript',
          background: 'background',
          extensionPage: ['popup', 'options'],
        },
      })

Or this one (using constants and path.join() to configure all paths):

const sourcePath = path.join(__dirname, 'src');
const scriptsPath = path.join(__dirname, 'src', 'scripts');
const destPath = path.join(__dirname, 'extension');

...

entry: {
  manifest: path.join(sourcePath, 'manifest.json'),
  background: path.join(scriptsPath, 'background.js'),
  contentScript: path.join(scriptsPath, 'contentScript.js'),
  popup: path.join(scriptsPath, 'popup.js'),
  options: path.join(scriptsPath, 'options.js'),
  bgServiceWorker: path.join(scriptsPath, 'service-worker.js'),
},

But the part about SCSS files is untouched:

{
  test: /\.(sa|sc|c)ss$/,
  use: [
    {
      loader: MiniCssExtractPlugin.loader, // It creates a CSS file per JS file which contains CSS
    },
    {
      loader: 'css-loader',
      options: {
        sourceMap: true,
      },
    },
    {
      loader: 'postcss-loader',
      options: {
        postcssOptions: {
          plugins: [
            [
              'autoprefixer',
              {
                // Options
              },
            ],
          ],
        },
      },
    },
    'resolve-url-loader',
    'sass-loader',
  ],
}

@laplandlearner
Copy link

Hello, @dhAlcojor
In this case, you need to use styled component or shadowdom.
I used it and it works well

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

2 participants