Skip to content

Commit 8621cb2

Browse files
committed
feat: Compile .svelte.js/ts files when using Svelte 5
1 parent 344f007 commit 8621cb2

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# svelte-loader changelog
22

3+
## 3.2.0
4+
5+
* Compile `.svelte.js/ts` files when using Svelte 5
6+
37
## 3.1.9
48

59
* Handle `emitCSS` to `css` option transformation correctly for Svelte 4

index.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const fs = require('fs');
33
const { getOptions } = require('loader-utils');
44
const { buildMakeHot } = require('./lib/make-hot.js');
5-
const { compile, preprocess, VERSION } = require('svelte/compiler');
5+
const svelte = require('svelte/compiler');
66

77
function posixify(file) {
88
return file.replace(/[/\\]/g, '/');
@@ -55,6 +55,10 @@ try {
5555

5656
let warned = false;
5757

58+
function getMajor() {
59+
return Number(svelte.VERSION.split('.')[0])
60+
}
61+
5862
module.exports = function(source, map) {
5963
this.cacheable();
6064

@@ -70,13 +74,37 @@ module.exports = function(source, map) {
7074

7175
const isServer = this.target === 'node' || (options.compilerOptions && options.compilerOptions.generate == 'ssr');
7276
const isProduction = this.minimize || process.env.NODE_ENV === 'production';
73-
7477
const compileOptions = {
7578
filename: this.resourcePath,
76-
css: VERSION[0] === '3' ? !options.emitCss : (options.emitCss ? 'external' : 'injected'),
79+
css: getMajor() === 3 ? !options.emitCss : (options.emitCss ? 'external' : 'injected'),
7780
...options.compilerOptions
7881
};
79-
if (VERSION[0] === '3') {
82+
const handleWarning = warning => this.emitWarning(new Error(warning));
83+
84+
if (getMajor() >= 5 && (this.resourcePath.endsWith('.svelte.js') || this.resourcePath.endsWith('.svelte.ts'))) {
85+
try {
86+
const { js, warnings } = svelte.compileModule(
87+
source,
88+
{ filename: this.resourcePath, dev: compileOptions.dev, generate: compileOptions.generate }
89+
);
90+
91+
warnings.forEach(
92+
options.onwarn
93+
? warning => options.onwarn(warning, handleWarning)
94+
: handleWarning
95+
);
96+
97+
callback(null, js.code, js.map);
98+
} catch (err) {
99+
// wrap error to provide correct
100+
// context when logging to console
101+
callback(new Error(`${err.name}: ${err.toString()}`));
102+
}
103+
104+
return;
105+
}
106+
107+
if (getMajor() === 3) {
80108
compileOptions.format = (options.compilerOptions && options.compilerOptions.format) || 'esm';
81109
} else {
82110
if (options.compilerOptions && options.compilerOptions.format && !warned) {
@@ -86,12 +114,10 @@ module.exports = function(source, map) {
86114
}
87115
}
88116

89-
const handleWarning = warning => this.emitWarning(new Error(warning));
90-
91117
options.preprocess = options.preprocess || {};
92118
options.preprocess.filename = compileOptions.filename;
93119

94-
preprocess(source, options.preprocess).then(processed => {
120+
svelte.preprocess(source, options.preprocess).then(processed => {
95121
if (processed.dependencies && this.addDependency) {
96122
for (let dependency of processed.dependencies) {
97123
this.addDependency(dependency);
@@ -100,7 +126,7 @@ module.exports = function(source, map) {
100126

101127
if (processed.map) compileOptions.sourcemap = processed.map;
102128

103-
const compiled = compile(processed.toString(), compileOptions);
129+
const compiled = svelte.compile(processed.toString(), compileOptions);
104130
let { js, css, warnings } = compiled;
105131

106132
if (!js.map.sourcesContent) {
@@ -121,7 +147,7 @@ module.exports = function(source, map) {
121147
js.code = makeHot(id, js.code, hotOptions, compiled, source, compileOptions);
122148
}
123149

124-
if (options.emitCss && css.code) {
150+
if (options.emitCss && css && css.code) {
125151
const resource = posixify(compileOptions.filename);
126152
const cssPath = `${resource}.${index++}.css`;
127153
css.code += '\n/*# sourceMappingURL=' + css.map.toUrl() + '*/';

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"svelte": "^3.0.0"
3030
},
3131
"peerDependencies": {
32-
"svelte": "^3.0.0 || ^4.0.0-next.0"
32+
"svelte": "^3.0.0 || ^4.0.0-next.0 || ^5.0.0-next.1"
3333
},
3434
"repository": {
3535
"type": "git",

test/fixtures/file.svelte.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const count = $state({ value: 0 });

test/loader.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,22 @@ describe('loader', () => {
355355
);
356356
});
357357
});
358+
359+
// needs Svelte 5
360+
describe.skip('Svelte 5', () => {
361+
it(
362+
'should compile .svelte.js/ts',
363+
testLoader(
364+
'test/fixtures/file.svelte.js',
365+
function(err, code, map) {
366+
expect(err).not.to.exist;
367+
368+
expect(code).not.to.contain('$state');
369+
},
370+
{}
371+
)
372+
);
373+
});
358374
});
359375

360376
function readFile(path) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy