3

So I have a Svelte component that looks like this:

<div id="app">
  <h1>Page Title</h1>
  <p>Some text</p>
  <div><p>Some more text</p></div>
</div>


<style>
  div :global(p) {
    color: red;
  }
</style>

My expectation is that the p tags should be red, but that's not what's happening. I'm using webpack to build the app and the relevant config for Svelte is:

{
  test: /\.html$/,
  exclude: /node_modules/,
  use: 'svelte-loader'
}

The styles that are generated are:

    div.svelte-f5mkpg :global(p),
   .svelte-f5mkpg div :global(p){color:red}

I'm using Svelte 1.59.0 and svelte-loader 2.5.1. Any idea what's wrong here? I also see this behavior in the default Sapper app. The global CSS is actually in a global.css file and the :global styles don't seem to take.

4

1 回答 1

6

为了使用:global(...)修饰符,您需要显式地防止 CSS 级联:

{
  test: /\.html$/,
  exclude: /node_modules/,
  use: {
    loader: 'svelte-loader',
    options: {
      cascade: false
    }
  }
}

在第 2 版(希望即将推出)中,将始终防止级联,但有必要在此之前将其放在选项后面,以防止发生重大更改。

:global(...)如果options.cascade !== false——我已经打开了一个问题,编译器可能会发出警告。

于 2018-04-10T13:22:21.427 回答