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

Svelte-loader custom component and global CSS/SCSS #1112

Open
kl3sk opened this issue Apr 21, 2022 · 2 comments
Open

Svelte-loader custom component and global CSS/SCSS #1112

kl3sk opened this issue Apr 21, 2022 · 2 comments
Labels
Status: Waiting Feedback Needs feedback from the author

Comments

@kl3sk
Copy link

kl3sk commented Apr 21, 2022

Hello,

I setup Encore to use svelte-loader as such:

...
    .addLoader({
            test: /\.svelte$/,
            loader: 'svelte-loader',
            options: {
                preprocess: sveltePreprocess(),
                emitCss: true,
                compilerOptions: {
                    css: true,
                    dev: !Encore.isProduction(),
                    customElement: true,
                }
            }
        },
        {
            // required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
            test: /node_modules\/svelte\/.*\.mjs$/,
            resolve: {
                fullySpecified: false
            }
        }
    )
...
let config = Encore.getWebpackConfig()
config.resolve.mainFields = ['svelte', 'browser', 'module', 'main']
config.resolve.extensions = ['.mjs', '.js', '.svelte']

let svelte = config.module.rules.pop()
config.module.rules.unshift(svelte)

module.exports = config

Svelete works as expected if I disable customElement options.

With this option enabled, I manage to have a working custom element, unless I use styles for nested component.

As I understand, it looks like not possible to style nested components inside it.

A workaround is to use :global inside parent element, but with my configuration it doesn't work.

Is there a specific webpack config for it I failed ?

Thanks

@kl3sk kl3sk changed the title Svelete loader custom component and global CSS/SCSS Svelte-loader custom component and global CSS/SCSS Apr 21, 2022
@mandrasch
Copy link

mandrasch commented Apr 28, 2023

Hey, do you still have the problem? I use the following config and styling worked fine for me?

const preprocess = require('svelte-preprocess');

/* ... */

  // enables Sass/SCSS support
  .enableSassLoader();

  .addLoader({
    test: /\.svelte$/,
    loader: "svelte-loader",
    options: {
      preprocess: preprocess(),
      emitCss: true,
      compilerOptions: {
        css: true,
        dev: !Encore.isProduction()
      }
    }
  }, {
    // https://github.com/symfony/webpack-encore/issues/1112
    // required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
    test: /node_modules\/svelte\/.*\.mjs$/,
    resolve: {
      fullySpecified: false
    }
  })

/* At the end */

// https://www.twilio.com/blog/develop-a-symfony-app-using-svelte-and-webpack-encore-to-manage-your-twilio-message-history 
let config = Encore.getWebpackConfig();

// https://github.com/sveltejs/svelte-loader
config.resolve = {
  alias: {
    svelte: path.resolve('node_modules', 'svelte')
  },
  extensions: ['.mjs', '.js', '.svelte'],
  mainFields: ['svelte', 'browser', 'module', 'main'],
  conditionNames: ['svelte']
};

module.exports = config;

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@Kocal Kocal added the Status: Waiting Feedback Needs feedback from the author label Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Waiting Feedback Needs feedback from the author
Projects
None yet
Development

No branches or pull requests

4 participants