【问题标题】:What causes failure to compile SASS in this svelte applcation?是什么导致在这个苗条的应用程序中编译 SASS 失败?
【发布时间】:2020-06-30 19:04:34
【问题描述】:

我正在使用 Svelte 和材料设计库 Svelte Material UI 开展一个项目。

这个材料设计库需要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.svelte 文件中,我以这种方式导入上述内容: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)

我做错了什么?

【问题讨论】:

  • 如何在 Svelte 组件中包含 sass?

标签: javascript svelte svelte-3


【解决方案1】:

我遇到了同样的问题,不知何故我设法用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'] }]]
        })

【讨论】:

  • 通过这一配置更改,我又遇到了一个问题。组件 CSS 和其他 CSS 样式不适用。我认为这个插件会覆盖 CSS,但我不确定
【解决方案2】:

我从未使用过@import 从 NPM 包中导入组件,但在您引用的自述包中,它建议使用“import x from”svelte-material。还要注意 svelte-preprocess 不会受您引用的软件包的支持,请查看自述文件:

要将其捆绑到您自己的代码中,请使用 Sass 处理器(不是 Sass Svelte 预处理器,而是 Sass 处理器)。

【讨论】:

  • 我在我的问题中添加了详细信息。请看一看。
猜你喜欢
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多