Skip to content

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

@kl3sk

Description

@kl3sk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions