Skip to content

Commit 273581d

Browse files
authored
fix(extract/transpile): retains svelte(5+) dependencies used exclusively outside <script> sections (#1046)
## Description - retains svelte dependencies used exclusively outside <script> sections ## Motivation and Context addresses #1045 ## How Has This Been Tested? - [x] green ci ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] Documentation only change - [ ] Refactor (non-breaking change which fixes an issue without changing functionality) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist - [x] 📖 - My change doesn't require a documentation update, or ... - it _does_ and I have updated it - [x] ⚖️ - The contribution will be subject to [The MIT license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE), and I'm OK with that. - The contribution is my own original work. - I am ok with the stuff in [**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).
1 parent f86b5fa commit 273581d

3 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/extract/transpile/svelte-wrap.mjs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-inline-comments */
12
import preProcess from "./svelte-preprocess.mjs";
23
import tryImport from "#utl/try-import.mjs";
34
import meta from "#meta.cjs";
@@ -6,14 +7,19 @@ const { compile, VERSION } = await tryImport(
67
"svelte/compiler",
78
meta.supportedTranspilers.svelte,
89
);
10+
/* c8 ignore next */
11+
const MAJOR_VERSION = VERSION ? Number(VERSION.split(".")[0]) : 0;
912

1013
function getTranspiler(pTranspilerWrapper) {
1114
return (pSource, _pFileName, pTranspilerOptions) => {
12-
const lPreProcessedSource = preProcess(
13-
pSource,
14-
pTranspilerWrapper,
15-
pTranspilerOptions,
16-
);
15+
// svelte 5 natively supports typescript directly, without
16+
// the need for preprocessing.
17+
const lPreProcessedSource =
18+
// eslint-disable-next-line no-magic-numbers
19+
MAJOR_VERSION < 5
20+
? /* c8 ignore next */
21+
preProcess(pSource, pTranspilerWrapper, pTranspilerOptions)
22+
: pSource;
1723
// in svelte 5 one must provide the second argument
1824
// lest it throws because it's accessing a property
1925
// in it. In svelte 4 (which also takes compiler

test/extract/transpile/__fixtures__/svelte.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "svelte/internal/disclose-version";
22
import "svelte/internal/flags/legacy";
33
import * as $ from "svelte/internal/client";
44
import "./page.css";
5+
import Header from "./Header.svelte";
56

67
var root = $.from_html(
78
`<article><!> <section><h2>Pages in Storybook</h2> <img src="./my.png" alt="my alt"/></section></article>`,

test/extract/transpile/svelte-wrap.spec.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ describe("[I] svelte transpiler", () => {
1111
equal(wrap.isAvailable(), true);
1212
});
1313
[
14-
[
15-
"ts",
16-
(pSource) =>
17-
pSource.replace('import Header from "./Header.svelte";\n', ""),
18-
],
14+
["ts", (pSource) => pSource],
1915
["js", (pSource) => pSource],
2016
].forEach(([variant, transformExpected]) => {
2117
it(`'transpiles' svelte with "<script lang='${variant}'>"'`, async () => {

0 commit comments

Comments
 (0)