是什么原因导致此 svelte 应用程序无法编译 SASS?
What causes failure to compile SASS in this svelte applcation?
我正在使用 Svelte 和 material 设计库进行一个项目 Svelte Material UI.
这个 material 设计库需要 SASS,所以我用 npm install svelte-preprocess
安装了预处理器并在 rollup.config.js 中添加了 preprocess: autoPreprocess()
。所以我现在有:
plugins: [
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('public/build/bundle.css');
},
preprocess: autoPreprocess()
}),
routify({ singleBuild : true}),
replace({
// stringify the object
APPENV: JSON.stringify({
isProd: production,
...config().parsed // attached the .env config
}),
}),
// more stuff
]
我有一个文件 smui.js
,内容如下:
import Button from '@smui/button';
import Checkbox from '@smui/checkbox';
import Chips from '@smui/chips';
import Dialog from '@smui/dialog';
import FormField from '@smui/form-field';
import Select from '@smui/select';
export {
Button,
Checkbox,
Chips,
Dialog,
FormField,
Select
}
在我的 index.svelt
e 文件中,我以这种方式导入上面的内容:import * as Smui from "../smui.js";
.
我得到的不是带有应用程序应 运行 端口的成功消息:
[!] Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript)
node_modules\@smui\dialog\_index.scss (1:0)
1: @import "smui-theme";
^
2: @import "./style";
Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript)
我做错了什么?
我从未使用@import 从 NPM 包中导入组件,但在您引用的自述包中,它建议使用 'import x from" svelte-material'。另请注意,您引用的包不支持 svelte-preprocess,请查看自述文件:
要将其捆绑到您自己的代码中,请使用 Sass 处理器(不是 Sass Svelte 预处理器,而是 Sass 处理器)。
我遇到了同样的问题,我设法用 rollup-plugin-postcss
插件解决了这个问题。使用以下代码更新您的 rollup.config.js
,您的 sass 目录中应该有 _smui-theme.scss
。
import postcss from 'rollup-plugin-postcss'
...
plugins: [
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('public/build/bundle.css')
}
}),
postcss({
extensions: ['.css'],
extract: true,
minimize: true,
use: [['sass', { includePaths: ['./src/(yoursass-directory-name)', './node_modules'] }]]
})
我正在使用 Svelte 和 material 设计库进行一个项目 Svelte Material UI.
这个 material 设计库需要 SASS,所以我用 npm install svelte-preprocess
安装了预处理器并在 rollup.config.js 中添加了 preprocess: autoPreprocess()
。所以我现在有:
plugins: [
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('public/build/bundle.css');
},
preprocess: autoPreprocess()
}),
routify({ singleBuild : true}),
replace({
// stringify the object
APPENV: JSON.stringify({
isProd: production,
...config().parsed // attached the .env config
}),
}),
// more stuff
]
我有一个文件 smui.js
,内容如下:
import Button from '@smui/button';
import Checkbox from '@smui/checkbox';
import Chips from '@smui/chips';
import Dialog from '@smui/dialog';
import FormField from '@smui/form-field';
import Select from '@smui/select';
export {
Button,
Checkbox,
Chips,
Dialog,
FormField,
Select
}
在我的 index.svelt
e 文件中,我以这种方式导入上面的内容:import * as Smui from "../smui.js";
.
我得到的不是带有应用程序应 运行 端口的成功消息:
[!] Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript)
node_modules\@smui\dialog\_index.scss (1:0)
1: @import "smui-theme";
^
2: @import "./style";
Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript)
我做错了什么?
我从未使用@import 从 NPM 包中导入组件,但在您引用的自述包中,它建议使用 'import x from" svelte-material'。另请注意,您引用的包不支持 svelte-preprocess,请查看自述文件:
要将其捆绑到您自己的代码中,请使用 Sass 处理器(不是 Sass Svelte 预处理器,而是 Sass 处理器)。
我遇到了同样的问题,我设法用 rollup-plugin-postcss
插件解决了这个问题。使用以下代码更新您的 rollup.config.js
,您的 sass 目录中应该有 _smui-theme.scss
。
import postcss from 'rollup-plugin-postcss'
...
plugins: [
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('public/build/bundle.css')
}
}),
postcss({
extensions: ['.css'],
extract: true,
minimize: true,
use: [['sass', { includePaths: ['./src/(yoursass-directory-name)', './node_modules'] }]]
})