首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Svelte Swiper导致意外的@ in /swiper/swiper.scss

Svelte Swiper导致意外的@ in /swiper/swiper.scss
EN

Stack Overflow用户
提问于 2021-08-14 12:56:36
回答 1查看 341关注 0票数 2

所以,我正在使用svelte构建一个站点,我正在使用swiper来实现其中一个功能,但我得到了这个错误,声明为[!] Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript)。这说明我不能在scss中使用@import指令,从我的研究中我发现我的svelte应用程序不支持scss文件。rollup.config.js中需要Scss预处理器。但是plot twist,我有scss支持,我在我的项目中使用scss,而不是普通的css。这是我的rollup.config.js

代码语言:javascript
复制
import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import css from 'rollup-plugin-css-only';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;

    function toExit() {
        if (server) server.kill(0);
    }

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

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.ts',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            preprocess: sveltePreprocess({ sourceMap: !production }),
            compilerOptions: {
                // 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({ output: 'bundle.css' }),

        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),
        typescript({
            sourceMap: !production,
            inlineSources: !production
        }),

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

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

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

这是我要导入的部分,这是swiper的官方svelte (https://swiperjs.com/svelte)文档中的代码:

代码语言:javascript
复制
<script>
    // import Swiper core and required modules
    import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
    import { Swiper, SwiperSlide } from 'swiper/svelte';
    // Import Swiper styles
    import 'swiper/swiper.scss';
    import 'swiper/components/navigation/navigation.scss';
    import 'swiper/components/pagination/pagination.scss';
    import 'swiper/components/scrollbar/scrollbar.scss';
    // install Swiper modules
    SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]);
</script>

请任何人回答这个问题。

EN

回答 1

Stack Overflow用户

发布于 2021-08-14 13:07:57

嘿,我自己解决的,但如果有人需要帮助,我会在这里分享这一点。

因此,这是一种变通方法,而不是启用scss的完全可靠的解决方案。尽管scss已经在我的项目中,但不是只在swiper中工作。因此,解决方法是在swiper目录中有min.css文件。例如: swiper/swiper.scss有一个替代的swiper/swiper.bundle.min.css,所以你可以像这样改变一切:

代码语言:javascript
复制
// import Swiper core and required modules
    import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from 'swiper';
    import { Swiper, SwiperSlide } from 'swiper/svelte';
    // Import Swiper styles
    import 'swiper/swiper-bundle.min.css';
    import 'swiper/components/navigation/navigation.min.css';
    import 'swiper/components/pagination/pagination.min.css';
    import 'swiper/components/scrollbar/scrollbar.min.css';
    // install Swiper modules
    SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68783490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档