所以,我正在使用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
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)文档中的代码:
<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>请任何人回答这个问题。
发布于 2021-08-14 13:07:57
嘿,我自己解决的,但如果有人需要帮助,我会在这里分享这一点。
因此,这是一种变通方法,而不是启用scss的完全可靠的解决方案。尽管scss已经在我的项目中,但不是只在swiper中工作。因此,解决方法是在swiper目录中有min.css文件。例如: swiper/swiper.scss有一个替代的swiper/swiper.bundle.min.css,所以你可以像这样改变一切:
// 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]);https://stackoverflow.com/questions/68783490
复制相似问题