【问题标题】:How to integrate Material UI into Svelte project如何将 Material UI 集成到 Svelte 项目中
【发布时间】:2020-05-16 13:16:51
【问题描述】:

我想将Material UI 集成到我的 Svelte 项目中。

我尝试遵循here 的官方文档,但我不知道为什么我在尝试运行我的项目时遇到了一个奇怪的错误:

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 有关。

在我的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 文件:

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
                });
            }
        }
    };
}

【问题讨论】:

    标签: material-ui svelte rollupjs


    【解决方案1】:

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

    确保使用npm 或yarn 安装此软件包:

    rollup-plugin-postcss 和 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。

    【讨论】:

    • 嗨,按照你的方法,我得到:`意外字符'@'(请注意,你需要插件来导入不是 JavaScript 的文件)1:@import "smui-theme";`你遇到过吗这个错误之前?你是怎么过去的?
    • 嗨,@import 是什么?在我看来它不是一个好的 js 语法
    • 我改用了模板。在 sapper 上设置材料设计时,在遵循与您类似的方法(使用汇总文件)后,我收到了该错误,但没有像这样导入的地方。无论如何,问题现在已经消失了
    猜你喜欢
    • 2017-12-23
    • 2020-10-01
    • 2016-08-25
    • 1970-01-01
    • 2017-11-15
    • 2022-04-09
    • 2019-08-02
    • 2023-01-24
    • 2015-06-03
    相关资源
    最近更新 更多