Skip to content

Commit

Permalink
fix: compile .svelte.xxx.js/ts modules in Svelte 5
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Jun 6, 2024
1 parent 495600b commit 562db6a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ function getMajor() {
return Number(svelte.VERSION.split('.')[0]);
}

const svelte_module_regex = /\.svelte(\.[^.]+)*\.(js|ts)$/

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v16 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v18 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v14 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v14 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v18 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v16 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v18 on ubuntu-latest

Missing semicolon

Check failure on line 66 in index.js

View workflow job for this annotation

GitHub Actions / Test on Node v16 on ubuntu-latest

Missing semicolon

module.exports = function(source, map) {
this.cacheable();

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-loader",
"version": "3.2.1",
"version": "3.2.2",
"author": "Nico Rehwaldt <git_nikku@nixis.de>",
"description": "A webpack loader for svelte",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/file.svelte.foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @ts-nocheck
export const count = $state({ value: 0 }); // eslint-disable-line no-undef
15 changes: 14 additions & 1 deletion test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
},
{}
)
);
});
});

Expand Down

0 comments on commit 562db6a

Please sign in to comment.