From 295cf0bb6397fe134e5503cbef92db2ae97cd4f5 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 6 Jun 2024 11:03:05 +0200 Subject: [PATCH] fix: compile svelte.xxx.js/ts files in Svelte 5 https://github.com/sveltejs/svelte/issues/11536 --- CHANGELOG.md | 4 ++++ index.js | 4 +++- package-lock.json | 4 ++-- package.json | 2 +- test/filename-test2/src/main.js | 1 + test/filename-test2/src/runes.svelte.foo.ts | 2 ++ test/index.js | 4 +++- 7 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 test/filename-test2/src/runes.svelte.foo.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d724f15..c747f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # rollup-plugin-svelte changelog +## 7.2.2 + +- Svelte 5: Compile `.svelte.xxx.js/ts` files + ## 7.2.1 - Silence Svelte 5 version of unused CSS warning code if `emitCss` is `false` diff --git a/index.js b/index.js index b00c385..6e32b7e 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,8 @@ const plugin_options = new Set([ 'preprocess', ]); +const svelte_module_regex = /\.svelte(\.[^./\\]+)*\.(js|ts)$/; + let warned = false; /** @@ -130,7 +132,7 @@ module.exports = function (options = {}) { async transform(code, id) { if (!filter(id)) return null; - if (majorVersion > 4 && (id.endsWith('.svelte.js') || id.endsWith('.svelte.ts'))) { + if (majorVersion > 4 && svelte_module_regex.test(id)) { const compiled = svelte.compileModule(code, { filename: id, dev: compilerOptions.dev, diff --git a/package-lock.json b/package-lock.json index 47604fc..aed4ba3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rollup-plugin-svelte", - "version": "7.2.1", + "version": "7.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "rollup-plugin-svelte", - "version": "7.2.1", + "version": "7.2.2", "license": "MIT", "dependencies": { "@rollup/pluginutils": "^4.1.0", diff --git a/package.json b/package.json index d637279..4352660 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "7.2.1", + "version": "7.2.2", "name": "rollup-plugin-svelte", "description": "Compile Svelte components with Rollup", "repository": "sveltejs/rollup-plugin-svelte", diff --git a/test/filename-test2/src/main.js b/test/filename-test2/src/main.js index 724ec04..c8630a4 100644 --- a/test/filename-test2/src/main.js +++ b/test/filename-test2/src/main.js @@ -1 +1,2 @@ export { counter } from './runes.svelte.js'; +export { counter2 } from './runes.svelte.foo.ts'; diff --git a/test/filename-test2/src/runes.svelte.foo.ts b/test/filename-test2/src/runes.svelte.foo.ts new file mode 100644 index 0000000..346435f --- /dev/null +++ b/test/filename-test2/src/runes.svelte.foo.ts @@ -0,0 +1,2 @@ +// @ts-nocheck +export const counter2 = $state({ value: 0 }); diff --git a/test/index.js b/test/index.js index 0854a1a..de84b59 100644 --- a/test/index.js +++ b/test/index.js @@ -319,7 +319,9 @@ test('handles filenames that happen to contain ".svelte"', async () => { }); // Needs Svelte 5 -test.skip('handles ".svelte.ts/js" files', async () => { +test('handles ".svelte.ts/js" files', async () => { + if (!isSvelte5Plus) return; + sander.rimrafSync('test/filename-test2/dist'); sander.mkdirSync('test/filename-test2/dist');