Svelte Swiper is causing unexpected @ in /swiper/swiper.scss

Viewed 577

So, I am building a site using svelte and I am using swiper for one of the functionalities but I get this error that states [!] Error: Unexpected character '@' (Note that you need plugins to import files that are not JavaScript). So this states I cannot use the @import directive in scss which from my research I found that my svelte app doesn't support scss files. Scss preprocessers are required in the rollup.config.js. But plot twist I have scss support and I use scss in my project instead of normal css. Here is my rollup.config.js

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

Now this is the part where I import this is the exact code in swiper's official docs for svelte (https://swiperjs.com/svelte):

<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>

Anyone please answer this question.

1 Answers

Hey I solved it myself but I am gonna share this here If someone else needs help.

So this is a workaround not a fullproof solution to enable scss. Even thouth scss is already there in my project but not working in swiper only. So the workaround is there are min.css files in swiper directories. For example: swiper/swiper.scss has a alternative swiper/swiper.bundle.min.css So you can change everything like this:

// 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]);
Related