如何将 Material UI 集成到 Svelte 项目中

2024-01-04

我想整合材质用户界面 https://sveltematerialui.com/进入我的 Svelte 项目。

我尝试遵循官方文档here https://github.com/hperrin/svelte-material-ui,但我不知道为什么在尝试运行我的项目时遇到奇怪的错误:

loaded rollup.config.js with warnings
(!) Unused external imports
default imported from external module 'rollup-plugin-postcss' but never used
rollup v1.27.13
bundles src/main.js → public/build/bundle.js...
[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/views/App.css (1:0)
1: .footer.svelte-1xl6ht0{position:fixed;left:0;bottom:0;width:100%;background-color:#569e3e;color:white;text-align:center;height:15px}.footer.us.svelte-1xl6ht0,.footer.europe.svelte-1xl6ht0,.footer.central.svelte-1xl6ht0,.footer.south.svelte-1xl6ht0,.footer.apac.svelte-1xl6ht0,.footer.baldr.svelte-1xl6ht0{background-color:#ca4a4a}.footer
....

问题似乎与 CSS 有关。

In my src目录 我有一个名为theme其中包含一个名为_smui-theme.scss这是文件的内容:

@import "@material/theme/color-palette";

// Svelte Colors!
$mdc-theme-primary: #ff3e00;
$mdc-theme-secondary: #676778;
// Other Svelte color: #40b3ff

$mdc-theme-background: #fff;
$mdc-theme-surface: #fff;

$mdc-theme-error: $material-color-red-900;

这是我的rollup.config.json file:

import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import json from '@rollup/plugin-json';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js',
    },
    plugins: [
        json(),
        svelte({
            // Enables run-time checks when not in production.
            dev: !production,

            // Extracts any component CSS out into a separate file — better for performance.
            css: css => css.write('public/build/bundle.css'),

            // Emit CSS as "files" for other plugins to process
            emitCss: true,
        }),

        resolve({
            browser: true,
            dedupe: importee => importee === 'svelte' || importee.startsWith('svelte/')
        }),
        commonjs(),

        // In dev mode, call `npm run start` once the bundle has been generated
        !production && serve(),

        // Watches the `public` directory and refresh the browser on changes when not in production.
        !production && livereload('public'),

        // Minify for production.
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

function serve() {
    let started = false;

    return {
        writeBundle() {
            if (!started) {
                started = true;

                require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                    stdio: ['ignore', 'inherit', 'inherit'],
                    shell: true
                });
            }
        }
    };
}

为了解决这个问题postcss汇总需要插件。
我还添加了一个精简的预处理器(我认为这是可选的,但我想确定一下)。

确保安装此软件包npm or yarn:

rollup-plugin-postcss and svelte-preprocess

然后插件应该被添加到rollup.config.js像这样:

import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';      <<<------------- Add this
import autoPreprocess from 'svelte-preprocess';   <<<------------- Add this
import json from '@rollup/plugin-json';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js',
    },
    plugins: [
        json(),
        svelte({
            // Enables run-time checks when not in production.
            dev: !production,

            // Extracts any component CSS out into a separate file — better for performance.
            css: css => css.write('public/build/bundle.css'),

            // Emit CSS as "files" for other plugins to process
            emitCss: true,

            preprocess: autoPreprocess()          <<<------------- Add this
        }),

        resolve({
            browser: true,
            dedupe: importee => importee === 'svelte' || importee.startsWith('svelte/')
        }),
        commonjs(),

        postcss({                               <<<------------- Add this
            extract: true,
            minimize: true,
            use: [
                ['sass', {
                includePaths: [
                    './src/theme',
                    './node_modules'
                ]
                }]
            ]
        }),

        // In dev mode, call `npm run start` once the bundle has been generated
        !production && serve(),

        // Watches the `public` directory and refresh the browser on changes when not in production.
        !production && livereload('public'),

        // Minify for production.
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

function serve() {
    let started = false;

    return {
        writeBundle() {
            if (!started) {
                started = true;

                require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                    stdio: ['ignore', 'inherit', 'inherit'],
                    shell: true
                });
            }
        }
    };
}

现在一切都应该与 css 一起正常工作,并且可以使用 Material UI。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Material UI 集成到 Svelte 项目中 的相关文章

随机推荐

  • 从 Django 模型获取数据并使用 AJAX 在 html 表中显示

    我已经搜索并实现了很多不同的方法来显示 html 表 使用 AJAX 从 JsonResponse Django 但无济于事 目前 我得到的最远的是对网络控制台的响应 products model products product pk 2
  • 多列上的 SELECT COUNT(DISTINCT...) 错误?

    我有一个表 VehicleModelYear 其中包含列 id 年份 品牌和型号 以下两个查询按预期工作 SELECT DISTINCT make model FROM VehicleModelYear SELECT COUNT DISTI
  • JS Regex - 替换 Markdown 链接的内容

    我几乎已经解决了一个正则表达式问题 只是一件小事 我正在尝试得到这个 and so use chalk api red string options 进入这个 and so use chalk red string options 我有这个
  • matplotlib 图例中的两个相邻符号

    我想在图例中的同一行上识别两个不同的符号 具有不同的颜色 下面 我尝试使用代理艺术家执行此操作 但结果是它们在图例中彼此堆叠 我希望它们彼此相邻或一个在另一个之上 这样它们都是可见的 from pylab import import mat
  • C# - XML - 压缩

    我遇到过这样的情况 我正在生成要提交到 Web 服务的 XML 文件 有时由于数据量超过 30mb 或 50mb 我需要使用 c net Framework 4 0 来压缩文件 而不是使用拥有大部分数据的节点之一 我不知道我要怎么做 如果有
  • Tomcat HTTP Access 日志写入文件有延迟

    在 tomcat 中 http 访问日志 Valve 需要一些时间才能写入文件 请注意 我有 org apache catalina valves AccessLogValve 的默认配置 有什么办法可以改善延迟吗 造成这种延迟的主要原因是
  • 在 Windows 上通过 Jenkins 使用 chromedriver 和 chrome 捕获屏幕截图时,从渲染器接收消息超时:10.000

    操作系统 Windows 10 浏览器 Chrome 浏览器版本 版本73 0 3683 86 官方版本 32位 我正在运行 selenium cucumber BDD 项目 我正在验证一页的标题 我正在使用范围报告版本4 项目在本地运行成
  • afterAjaxUpdate 回调函数 CListView 显示未定义

    我试图调用一个在另一个 js 文件中通过文件名的 afterAjaxUpdate 参数定义的函数 但我在控制台中收到错误 该函数未定义
  • 在同一行显示图标和文本

    我试图让文本 主页 关于我们 奶酪 等显示在社交媒体图标的右侧 以便文本的底部与图标的底部对齐 并且它们显示在同一行 我该怎么做呢 我的代码在这里摆弄http jsfiddle net pnX3d http jsfiddle net pnX
  • /etc/rc.local 和 ~/.bashrc 有什么区别?

    这是一个与linux相关的问题 我四处搜寻但没有得到很好的解释 在我看来 这两个文件在我登录时都会配置设置 但是有什么区别吗 我注意到在决定哪些内容应该放入两个不同的文件中似乎有 一些规则 例如 如果我需要向 PATH 添加特定的搜索路径
  • 如何使用apache poi 3.6获取A1(单元格地址)的单元格值

    我有 Excel 单元格地址 如 A1 A2 那么 如何使用 poi 3 6 以编程方式访问此单元格 另一种方法是 row mySheet getRow cell row getCell 但我的地址格式为 A1 那么 我如何以编程方式访问这
  • 如何使用模板函数从缓冲区(T* 数据数组)创建 cv::Mat?

    我想编写一个模板函数来复制指针引用的数据T image to cv Mat 我很困惑如何概括 T 和 cv type 匹配 template
  • 将 Android studio 更新到 2.2 后出现“无法解决 x”错误

    Google 推出了 Android Studio 2 2 所以我将 Android studio 2 1 更新为 Android Studio 2 2 当我制作项目时 我收到这些错误 和更多 Error Failed to resolve
  • 添加apache用户组文件上传权限

    我有一个 php 脚本将文件上传到某个文件夹 目前它们作为 psacln 组上传 以便我可以通过 FTP 删除文件 当 PHP 作为 FastCGI 运行时 这一切都工作正常 我必须将 PHP 更改为作为 Apache 模块运行 以便让 p
  • 为什么Ul上方有空白

    当我放置无序列表时 then why top bar goes down I cannot find the reason behind this Please tell me how to fix this problem http js
  • Joomla MySQL 性能

    我一直在开发一个版本为2 5 11的Joomla网站 网站的流量将会非常高 我的问题是关于 MySQL 查询性能的 数据库包含大约 60000 行内容表 下面看到的查询 核心 com content 文章模型查询 执行时间约为 6 秒 非常
  • 如何显示尾随“%”符号?

    在 SO Line 的折扣百分比列中 我想在其中显示尾随 我尝试将显示格式设置为 P 但这是一场灾难 由于实际值是数据库中的百分比 因此不必乘以 100 感谢您的回答 您应该设置DisplayFormat to 0 00 结果你会得到以下结
  • Py2Exe 检测为病毒。备择方案?

    所以 我创建了一个python程序 使用 Py2Exe 转换为 exe 并尝试使用 PyInstaller 和 cx freeze 所有这些都会触发程序在virustotal 和我的本地计算机上被avast avg 和其他程序检测为病毒 我
  • Cython:编译独立的静态可执行文件

    我正在尝试编译一个不使用动态加载器的可执行文件 ELF 文件 我用了Cython将Python编译为C cython3 3 test py embed Then gcc test c otest pkg config libs cflags
  • 如何将 Material UI 集成到 Svelte 项目中

    我想整合材质用户界面 https sveltematerialui com 进入我的 Svelte 项目 我尝试遵循官方文档here https github com hperrin svelte material ui 但我不知道为什么在