英文:
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的完整示例:
- 创建项目(
npm create svelte@latest my-app
) - 在
svelte.config.js
中添加vitePreprocess(参见SvelteKit Preprocessors) - 使用
npm install -D sass
添加Sass支持 - 从
vite.config.js
导入变量和混合(参见Vite文档) - 从主布局导入全局样式(如这里所述)
// 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>
注意:
- 我不知道为什么,但您必须在
global.sass
文件的开头添加一个额外的换行符(否则会崩溃)。 - 您可以通过取消选中“设置.../编辑器/检查/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:
- Create the project (
npm create svelte@latest my-app
) - Add vitePreprocess in
svelte.config.js
(see SvelteKit Preprocessors) - Add Sass support with
npm install -D sass
- Import variables and mixins from
vite.config.js
(see Vite documentation) - Import global styles from main layout (like described here)
// 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>
Notes:
- 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) - 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.js
和 vite.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 '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite'; // 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 '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()],
// Add this
css: {
preprocessorOptions: {
// if using SCSS
scss: {
additionalData: `
@use '$lib/scss/variables' as *;
@use '$lib/scss/mixins' as *;
`,
},
// if using SASS
sass: {
additionalData: `
@use '$lib/sass/variables' as *;
@use '$lib/sass/mixins' as *;
`,
},
},
}
});
Usage
// 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>
To explicitly use variables and mixins you can give them an alias
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>
答案3
得分: 0
你需要将你的 prependData
添加到 vite.config.ts
中,如这里所述。
应该是这样的:
// vite.config.ts
const config: UserConfig = {
plugins: [sveltekit()],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
},
// 👇 这是魔法 👇
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: ['src/**/*.{test,spec}.{js,ts}']
},
// 👇 This is the magic 👇
css: {
preprocessorOptions: {
scss: {
additionalData: `@import './src/styles/prepend.scss';`
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论