From 562db6a1c5a410af71e98c8ae7850b50a8859643 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 6 Jun 2024 10:55:33 +0200 Subject: [PATCH] fix: compile .svelte.xxx.js/ts modules in Svelte 5 https://github.com/sveltejs/svelte/issues/11536 --- CHANGELOG.md | 4 ++++ index.js | 4 +++- package-lock.json | 4 ++-- package.json | 2 +- test/fixtures/file.svelte.foo.ts | 2 ++ test/loader.spec.js | 15 ++++++++++++++- 6 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/file.svelte.foo.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eaf0e54..1ea861c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # svelte-loader changelog +## 3.2.2 + +* Svelte 5: Also compile `.svelte.xxx.js/ts` files + ## 3.2.1 * Handle sourcemaps being falsy diff --git a/index.js b/index.js index fa123644..99101e7d 100644 --- a/index.js +++ b/index.js @@ -63,6 +63,8 @@ function getMajor() { return Number(svelte.VERSION.split('.')[0]); } +const svelte_module_regex = /\.svelte(\.[^.]+)*\.(js|ts)$/ + module.exports = function(source, map) { this.cacheable(); @@ -85,7 +87,7 @@ module.exports = function(source, map) { }; const handleWarning = warning => this.emitWarning(new Error(warning)); - if (getMajor() >= 5 && (this.resourcePath.endsWith('.svelte.js') || this.resourcePath.endsWith('.svelte.ts'))) { + if (getMajor() >= 5 && svelte_module_regex.test(this.resourcePath)) { try { const { js, warnings } = svelte.compileModule( source, diff --git a/package-lock.json b/package-lock.json index 2cd10481..8a4fa0c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "svelte-loader", - "version": "3.2.1", + "version": "3.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "svelte-loader", - "version": "3.2.1", + "version": "3.2.2", "license": "MIT", "dependencies": { "loader-utils": "^2.0.4", diff --git a/package.json b/package.json index 02c52e52..d2366549 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte-loader", - "version": "3.2.1", + "version": "3.2.2", "author": "Nico Rehwaldt ", "description": "A webpack loader for svelte", "license": "MIT", diff --git a/test/fixtures/file.svelte.foo.ts b/test/fixtures/file.svelte.foo.ts new file mode 100644 index 00000000..b1ac3c1d --- /dev/null +++ b/test/fixtures/file.svelte.foo.ts @@ -0,0 +1,2 @@ +// @ts-nocheck +export const count = $state({ value: 0 }); // eslint-disable-line no-undef diff --git a/test/loader.spec.js b/test/loader.spec.js index fdb9b982..9d302a46 100644 --- a/test/loader.spec.js +++ b/test/loader.spec.js @@ -465,7 +465,7 @@ describe('loader', () => { if (!isSvelte5Plus) return; it( - 'should compile .svelte.js/ts', + 'should compile .svelte.js', testLoader( 'test/fixtures/file.svelte.js', function(err, code, map) { @@ -476,6 +476,19 @@ describe('loader', () => { {} ) ); + + it( + 'should compile .svelte.foo.ts', + testLoader( + 'test/fixtures/file.svelte.foo.ts', + function(err, code, map) { + expect(err).not.to.exist; + + expect(code).not.to.contain('$state'); + }, + {} + ) + ); }); });