Skip to content

Commit

Permalink
core: Svelte 5 work
Browse files Browse the repository at this point in the history
- use Svelte 4 in repository for testing now
- get tests passing using Svelte 5
- Silence Svelte 5 version of unused CSS warning code if `emitCss` is `false`
  • Loading branch information
dummdidumm committed Jun 5, 2024
1 parent 719f351 commit 1e01360
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# rollup-plugin-svelte changelog

## 7.2.1

- Silence Svelte 5 version of unused CSS warning code if `emitCss` is `false`

## 7.2.0

- Support compiling `svelte.js/ts` files in Svelte 5
Expand Down
27 changes: 17 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const svelte = require('svelte/compiler');

const PREFIX = '[rollup-plugin-svelte]';

const majorVersion = Number(svelte.VERSION.split('.')[0]);

const plugin_options = new Set([
'emitCss',
'exclude',
Expand All @@ -26,7 +28,7 @@ module.exports = function (options = {}) {
const extensions = rest.extensions || ['.svelte'];
const filter = createFilter(rest.include, rest.exclude);

if (svelte.VERSION[0] === '3') {
if (majorVersion === 3) {
compilerOptions.format = 'esm';
}

Expand All @@ -42,8 +44,7 @@ module.exports = function (options = {}) {
const { onwarn, emitCss = true } = rest;

if (emitCss) {
const [majorVer] = svelte.VERSION.split('.');
const cssOptionValue = majorVer > 3 ? 'external' : false;
const cssOptionValue = majorVersion > 3 ? 'external' : false;
if (compilerOptions.css) {
console.warn(
`${PREFIX} Forcing \`"compilerOptions.css": ${
Expand Down Expand Up @@ -129,10 +130,7 @@ module.exports = function (options = {}) {
async transform(code, id) {
if (!filter(id)) return null;

if (
Number(svelte.VERSION.split('.')[0]) >= 5 &&
(id.endsWith('.svelte.js') || id.endsWith('.svelte.ts'))
) {
if (majorVersion > 4 && (id.endsWith('.svelte.js') || id.endsWith('.svelte.ts'))) {
const compiled = svelte.compileModule(code, {
filename: id,
dev: compilerOptions.dev,
Expand Down Expand Up @@ -164,9 +162,18 @@ module.exports = function (options = {}) {
const compiled = svelte.compile(code, svelte_options);

(compiled.warnings || []).forEach((warning) => {
if (!emitCss && warning.code === 'css-unused-selector') return;
if (onwarn) onwarn(warning, this.warn);
else this.warn(warning);
if (
!emitCss &&
(warning.code === 'css-unused-selector' || warning.code === 'css_unused_selector')
) {
return;
}

if (onwarn) {
onwarn(warning, this.warn);
} else {
this.warn(warning);
}
});

if (emitCss && compiled.css && compiled.css.code) {
Expand Down
Loading

0 comments on commit 1e01360

Please sign in to comment.