如何在SvelteKit中使用vitePreprocess和全局SCSS混合?

huangapple go评论72阅读模式
英文:

How to use vitePreprocess with global SCSS mixins in SvelteKit?

问题

在我的SvelteKit项目中,我正在使用SCSS,并为了包含在整个项目中使用的一些混合,我从vitePreprocess切换到svelte-preprocess,并在前面添加了scss.prependData。现在我的配置看起来像这样:

const config = {
  preprocess: preprocess({
    scss: {
      prependData: `@import './src/styles/prepend.scss';`,
    },
  }),
  // ..
  // 与问题无关的其他选项
}

由于官方的SvelteKit文档建议vitePreprocess可能更快,我想知道是否可以使用它来设置全局混合。

我尝试在我的根+layout.svelte文件中导入prepend.scss,但现在我收到一个Undefined mixin错误。

我能否在vitePreprocess中使用混合,还是目前只有使用svelte-preprocess才能实现这一点?

英文:

In my SvelteKit project I am using SCSS and in order to include some mixins that are used throughout the project, I switched from vitePreprocess to svelte-preprocess and prepended them with scss.prependData. Now my config looks like this:

const config = {
  preprocess: preprocess({
    scss: {
      prependData: `@import './src/styles/prepend.scss';`,
    },
  }),
  // ..
  // Other options not relevant to the question
}

Since the official SvelteKit docs suggest that vitePreprocess might be faster, I was wondering if I can set up global mixins with it.

I tried importing prepend.scss in my root +layout.svelte file but now I'm getting an Undefined mixin error.

Can I use mixins with vitePreprocess or is svelte-preprocess the only way to achieve this at the moment?

答案1

得分: 5

根据@SlavenIvanov的答案建议,您需要从vite.config.js导入变量混合

以下是在新的SvelteKit项目上配置Sass的完整示例:

  1. 创建项目(npm create svelte@latest my-app
  2. svelte.config.js中添加vitePreprocess(参见SvelteKit Preprocessors
  3. 使用npm install -D sass添加Sass支持
  4. vite.config.js导入变量混合(参见Vite文档
  5. 从主布局导入全局样式(如这里所述)
// svelte.config.js
import { vitePreprocess } from '@sveltejs/kit/vite';

export default {
  preprocess: [vitePreprocess()]
};
// vite.config.js
export default defineConfig({
  css: {
    preprocessorOptions: {
      sass: {
        additionalData: `
          @import '$lib/sass/variables'
          @import '$lib/sass/mixins'
        `,
      }
    },
  }
});
// src/lib/sass/_variables.sass
$red: #d34a
$green: #9c3
$blue: #0bc
// src/lib/sass/_mixins.sass
=tablet
  @media (min-width: 768px)
    @content

=desktop
  @media (min-width: 1024px)
    @content
// src/lib/sass/global.sass

html
  background: $red

  +tablet
    background: $blue
<!-- src/routes/+layout.svelte -->
<script>
  import '$lib/sass/global.sass'
</script>

<slot />
<!-- src/routes/+page.svelte -->
<h1>Hello world</h1>

<style lang="sass">
  h1
    color: $green

    +desktop
      color: $red
</style>

注意

  1. 我不知道为什么,但您必须在global.sass文件的开头添加一个额外的换行符(否则会崩溃)。
  2. 您可以通过取消选中“设置.../编辑器/检查/Sass/SCSS/缺少导入”中的框来禁用WebStorm警告,例如“元素仅通过名称解析...”。
英文:

As @SlavenIvanov's answer suggests, you have to import the variables and mixins from vite.config.js.


Here is a complete example of Sass configuration on a new SvelteKit project:
  1. Create the project (npm create svelte@latest my-app)
  2. Add vitePreprocess in svelte.config.js (see SvelteKit Preprocessors)
  3. Add Sass support with npm install -D sass
  4. Import variables and mixins from vite.config.js (see Vite documentation)
  5. Import global styles from main layout (like described here)
// svelte.config.js
import { vitePreprocess } from &#39;@sveltejs/kit/vite&#39;;
 
export default {
  preprocess: [vitePreprocess()]
};
// vite.config.js
export default defineConfig({
  css: {
    preprocessorOptions: {
      sass: {
        additionalData: `
          @import &#39;$lib/sass/variables&#39;
          @import &#39;$lib/sass/mixins&#39;
        `,
      }
    },
  }
});
// src/lib/sass/_variables.sass
$red: #d34a
$green: #9c3
$blue: #0bc
// src/lib/sass/_mixins.sass
=tablet
  @media (min-width: 768px)
    @content

=desktop
  @media (min-width: 1024px)
    @content
// src/lib/sass/global.sass

html
  background: $red

  +tablet
    background: $blue
&lt;!-- src/routes/+layout.svelte --&gt;
&lt;script&gt;
  import &#39;$lib/sass/global.sass&#39;
&lt;/script&gt;

&lt;slot /&gt;
&lt;!-- src/routes/+page.svelte --&gt;
&lt;h1&gt;Hello world&lt;/h1&gt;

&lt;style lang=&quot;sass&quot;&gt;
  h1
    color: $green

    +desktop
      color: $red
&lt;/style&gt;

Notes:

  1. I don't know why but you have to add an extra line break at the beginning of the global.sass file (otherwise it crashes)
  2. You can disable WebStorm warnings such as Element is resolved only by name... by unchecking the box in Settings... "Editor / Inspections / Sass/SCSS / Missing import"

答案2

得分: 2

以下是翻译好的部分:

一些好的答案在这里,但答案中仍然存在一些混淆,让我来澄清一下。

您需要更新两个文件,svelte.config.jsvite.config.js 以使其正常工作。

更新 svelte.config.js

import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite'; // 添加这一行

const config = {
	kit: {
		adapter: adapter()
	},
	preprocess: [vitePreprocess()] // 以及这一行
};

export default config;

更新 vite.config.js

这部分有些混乱,因为此配置因人而异,取决于您要做什么。

  • 您必须使用 .scss.sass 之一,找出您正在使用哪一个,并不要在配置中混合它们。
  • 找出您要在整个项目中包含的 .scss.sass 文件的路径。在这种情况下,它是 /src/lib/scss/,对于您来说可能是 /src//src/scss/
  • 使用此配置,vite 将包含所有的 sass/scss,如果其中一些是实际的样式而不仅仅是变量,这些样式将在最终包中重复。因此,我们将使用 @use 而不是 @import
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
	plugins: [sveltekit()],
	// 添加这一部分
	css: {
		preprocessorOptions: {
			// 如果使用 SCSS
			scss: {									
				additionalData: `
				@use '$lib/scss/variables' as *;	
				@use '$lib/scss/mixins' as *;
			`,
			},
			// 如果使用 SASS
			sass: {									
				additionalData: `
				@use '$lib/sass/variables' as *;	
				@use '$lib/sass/mixins' as *;
			`,
			},
		},
	}
});

使用

// src/lib/scss/_variables.scss

$color: red;

// src/lib/scss/_mixins.scss

@mixin big-font {
    font-size: 32px;
    font-weight: 900;
}

<!-- src/routes/+page.svelte -->

...

<style lang="scss">
	h2 {
		color: $color;
		@include big-font;
	}
</style>

要明确使用变量和混合,您可以为它们指定别名

scss: {
	additionalData: `
	@use '$lib/scss/variables' as vr;	
	@use '$lib/scss/mixins' as mx;
	`,
},
<!-- src/routes/+page.svelte -->

...

<style lang="scss">
	h2 {
		color: vr.$color;
		@include mx.big-font;
	}
</style>

英文:

Some good answers here, but there still seems to be some confusions in the answers, let me clear them up.

You need to update two files, svelte.config.js and vite.config.js to get this working.

Update svelte.config.js

import adapter from &#39;@sveltejs/adapter-auto&#39;;
import { vitePreprocess } from &#39;@sveltejs/kit/vite&#39;; // Add this


const config = {
	kit: {
		adapter: adapter()
	},
	preprocess: [vitePreprocess()] // And this
};

export default config;

Update vite.config.js

This is the confusing part, since this config will vary from person to person and what you are trying to do.

  • You must be using either .scss or .sass, figure out which one you are using and don't mix them in the config.
  • Figure out the path of your .scss or .sass files that you want to include throughout the project. In this case, it's /src/lib/scss/, for you it could be /src/ or /src/scss/
  • With this config vite will include all the sass/scss and if some of them are actual styles and not just variables, those styles will be duplicated in the final bundle. For the same reason, we will use @use and not @import.
import { sveltekit } from &#39;@sveltejs/kit/vite&#39;;
import { defineConfig } from &#39;vite&#39;;

export default defineConfig({
	plugins: [sveltekit()],
	// Add this
	css: {
		preprocessorOptions: {
			// if using SCSS
			scss: {									
				additionalData: `
				@use &#39;$lib/scss/variables&#39; as *;	
				@use &#39;$lib/scss/mixins&#39; as *;
			`,
			},
			// if using SASS
			sass: {									
				additionalData: `
				@use &#39;$lib/sass/variables&#39; as *;	
				@use &#39;$lib/sass/mixins&#39; as *;
			`,
			},
		},
	}
});

Usage

// src/lib/scss/_variables.scss

$color: red;

// src/lib/scss/_mixins.scss

@mixin big-font {
    font-size: 32px;
    font-weight: 900;
}

&lt;!-- src/routes/+page.svelte --&gt;

...

&lt;style lang=&quot;scss&quot;&gt;
	h2 {
		color: $color;
		@include big-font;
	}
&lt;/style&gt;

To explicitly use variables and mixins you can give them an alias

scss: {
	additionalData: `
	@use &#39;$lib/scss/variables&#39; as vr;	
	@use &#39;$lib/scss/mixins&#39; as mx;
	`,
},
&lt;!-- src/routes/+page.svelte --&gt;

...

&lt;style lang=&quot;scss&quot;&gt;
	h2 {
		color: vr.$color;
		@include mx.big-font;
	}
&lt;/style&gt;

答案3

得分: 0

你需要将你的 prependData 添加到 vite.config.ts 中,如这里所述。

应该是这样的:

// vite.config.ts

const config: UserConfig = {
    plugins: [sveltekit()],
    test: {
        include: ['src/**/*.{test,spec}.{js,ts}']
    },
    // &#128071; 这是魔法 &#128071;
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@import 'src/styles/prepend.scss';`
            }
        }
    }
}
英文:

You need to add your prependData to vite.config.ts as explained here.

It should look like this:

// vite.config.ts

const config: UserConfig = {
	plugins: [sveltekit()],
	test: {
		include: [&#39;src/**/*.{test,spec}.{js,ts}&#39;]
	},
// &#128071; This is the magic &#128071;
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: `@import &#39;./src/styles/prepend.scss&#39;;`
			}
		}
	}
}

huangapple
  • 本文由 发表于 2023年1月9日 19:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定