+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
{hello}
+
+
+ {#if AValue && val}
+
There is a value: {AValue}
+ {/if}
+
+ {#if val && isTest1(val) && AValue && true && "test"}
+
There is a value: {AValue}
+ {:else if obj.val && obj.fn() && isTest1(obj.val)}
+
There is a value: {AValue}
+ {:else}
+ Else
+ {/if}
+
+ {#each arr as item (item)}
+
{item}
+ {/each}
+
+ {#each arr as item}
+
{item}
+ {:else}
+
No items
+ {/each}
+
+ {#await prom}
+ Loading...
+ {:then value}
+
inputVal = e.currentTarget.value} />
+ {:catch err}
+
Error: {err}
+ {/await}
+
+ {#await prom then value}
+
{value}
+ {/await}
+
+ {#key val}
+
Keyed {val}
+ {/key}
+
+
+ {inputVal}
+
+
+ {@html val}
+ {@debug val, inputVal}
+
+
+
diff --git a/test/fixtures/TypeScriptImportsModule.svelte b/test/fixtures/TypeScriptImportsModule.svelte
new file mode 100644
index 00000000..b885f8eb
--- /dev/null
+++ b/test/fixtures/TypeScriptImportsModule.svelte
@@ -0,0 +1,10 @@
+
+
+
+
+{val} {aValue}
\ No newline at end of file
diff --git a/test/fixtures/TypeScriptTypesOnly.svelte b/test/fixtures/TypeScriptTypesOnly.svelte
new file mode 100644
index 00000000..950e16fc
--- /dev/null
+++ b/test/fixtures/TypeScriptTypesOnly.svelte
@@ -0,0 +1,5 @@
+
diff --git a/test/modules/modules.test.ts b/test/modules/modules.test.ts
index 51d4fcb5..5a48e330 100644
--- a/test/modules/modules.test.ts
+++ b/test/modules/modules.test.ts
@@ -59,6 +59,7 @@ describe(`get tag information`, () => {
it('should only include src files if content is empty', async () => {
let parsedFile = await getTagInfo({
content: '',
+ markup: '',
attributes: { src: './fixtures/style.scss' },
filename: getTestAppFilename(),
});
diff --git a/test/transformers/globalStyle.test.ts b/test/transformers/globalStyle.test.ts
index dd8a4b22..8009c995 100644
--- a/test/transformers/globalStyle.test.ts
+++ b/test/transformers/globalStyle.test.ts
@@ -6,15 +6,24 @@ describe('transformer - globalStyle', () => {
// todo: why it isn't generating a sourcemap?
it('adds sourceMap with { sourceMap: true }', async () => {
const template = ``;
- const opts = autoProcess({
- globalStyle: {
- sourceMap: true,
- },
- });
- const preprocessed = await preprocess(template, opts);
+ const preprocessedWithMap: any = await preprocess(
+ template,
+ autoProcess({
+ globalStyle: {
+ sourceMap: true,
+ },
+ }),
+ );
- expect(preprocessed.toString()).toContain(`sourceMappingURL`);
+ const preprocessedWithoutMap: any = await preprocess(
+ template,
+ autoProcess(),
+ );
+
+ expect(preprocessedWithMap.map.mappings).not.toEqual(
+ preprocessedWithoutMap.map?.mappings,
+ );
});
it('wraps selector in :global(...) modifier', async () => {
@@ -127,15 +136,24 @@ describe('transformer - globalStyle', () => {
describe('global selector', () => {
it('adds sourceMap with { sourceMap: true }', async () => {
const template = ``;
- const opts = autoProcess({
- globalStyle: {
- sourceMap: true,
- },
- });
- const preprocessed = await preprocess(template, opts);
+ const preprocessedWithMap: any = await preprocess(
+ template,
+ autoProcess({
+ globalStyle: {
+ sourceMap: true,
+ },
+ }),
+ );
- expect(preprocessed.toString()).toContain(`sourceMappingURL`);
+ const preprocessedWithoutMap: any = await preprocess(
+ template,
+ autoProcess(),
+ );
+
+ expect(preprocessedWithMap.map.mappings).not.toEqual(
+ preprocessedWithoutMap.map?.mappings,
+ );
});
it('wraps selector in :global(...) modifier', async () => {
diff --git a/test/transformers/typescript.test.ts b/test/transformers/typescript.test.ts
index 95342039..563bb534 100644
--- a/test/transformers/typescript.test.ts
+++ b/test/transformers/typescript.test.ts
@@ -29,6 +29,7 @@ const autoProcessTS = (content: string, compilerOptions?: any) => {
return opts.script({
content,
+ markup: ``,
attributes: { type: 'text/typescript' },
filename: resolve(__dirname, '..', 'App.svelte'),
}) as Processed & { diagnostics: Diagnostic[] };
@@ -107,7 +108,70 @@ describe('transformer - typescript', () => {
const { code } = await preprocess(template, opts);
- return expect(code).toContain(getFixtureContent('script.js'));
+ expect(code).toContain(getFixtureContent('script.js'));
+ });
+
+ it('should strip unused and type imports', async () => {
+ const tpl = getFixtureContent('TypeScriptImports.svelte');
+
+ const opts = sveltePreprocess({
+ typescript: { tsconfigFile: false },
+ });
+
+ const { code } = await preprocess(tpl, opts);
+
+ // Test that imports are properly preserved
+ expect(code).toContain(`import { fly } from "svelte/transition"`);
+ expect(code).toContain(`import { flip } from "svelte/animate"`);
+ expect(code).toContain(`import Nested from "./Nested.svelte"`);
+ expect(code).toContain(`import { hello } from "./script"`);
+ expect(code).toContain(`import { AValue } from "./types"`);
+ expect(code).toContain(
+ `import { storeTemplateOnly, storeScriptOnly } from "./store"`,
+ );
+ expect(code).toContain(
+ `import { onlyUsedInModuleScript } from "./modulescript";`,
+ );
+ // Test that comments are properly preserved
+ expect(code).toContain('');
+ });
+
+ it('should deal with empty transpilation result', async () => {
+ const tpl = getFixtureContent('TypeScriptTypesOnly.svelte');
+
+ const opts = sveltePreprocess({
+ typescript: { tsconfigFile: false },
+ sourceMap: true,
+ });
+
+ const { code } = await preprocess(tpl, opts);
+
+ expect(code).toBe(``);
+ });
+
+ it('should strip unused and type imports in context="module" tags', async () => {
+ const tpl = getFixtureContent('TypeScriptImportsModule.svelte');
+
+ const opts = sveltePreprocess({
+ typescript: { tsconfigFile: false },
+ });
+
+ const { code } = await preprocess(tpl, opts);
+
+ expect(code).toContain(`import { AValue } from "./types";`);
+ });
+
+ it('should produce sourcemap', async () => {
+ const tpl = getFixtureContent('TypeScriptImportsModule.svelte');
+
+ const opts = sveltePreprocess({
+ typescript: { tsconfigFile: false },
+ sourceMap: true,
+ });
+
+ const { map } = await preprocess(tpl, opts);
+
+ expect(map).toHaveProperty('sources', ['App.svelte']);
});
it('supports extends field', () => {
diff --git a/tsconfig.json b/tsconfig.json
index 9d0f8b52..1a536fa4 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -8,6 +8,7 @@
"target": "es2018",
"lib": ["es2018"],
"declaration": true,
+ "skipLibCheck": true,
"types": ["jest", "node"],
"outDir": "dist"
},
diff --git a/yarn.lock b/yarn.lock
index c559e791..8676a7a2 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2008,6 +2008,11 @@ bser@2.1.1:
dependencies:
node-int64 "^0.4.0"
+buffer-crc32@^0.2.5:
+ version "0.2.13"
+ resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
+ integrity sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=
+
buffer-from@1.x, buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@@ -2860,6 +2865,11 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"
+es6-promise@^3.1.2:
+ version "3.3.1"
+ resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613"
+ integrity sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM=
+
escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
@@ -3594,6 +3604,11 @@ graceful-fs@^4.1.2, graceful-fs@^4.2.4:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==
+graceful-fs@^4.1.3:
+ version "4.2.6"
+ resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
+ integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==
+
growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
@@ -4928,6 +4943,13 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"
+magic-string@^0.25.7:
+ version "0.25.7"
+ resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
+ integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
+ dependencies:
+ sourcemap-codec "^1.4.4"
+
make-dir@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
@@ -6355,7 +6377,7 @@ reusify@^1.0.4:
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==
-rimraf@2:
+rimraf@2, rimraf@^2.5.2:
version "2.7.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
@@ -6408,6 +6430,16 @@ safe-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
+sander@^0.5.0:
+ version "0.5.1"
+ resolved "https://registry.yarnpkg.com/sander/-/sander-0.5.1.tgz#741e245e231f07cafb6fdf0f133adfa216a502ad"
+ integrity sha1-dB4kXiMfB8r7b98PEzrfohalAq0=
+ dependencies:
+ es6-promise "^3.1.2"
+ graceful-fs "^4.1.3"
+ mkdirp "^0.5.1"
+ rimraf "^2.5.2"
+
sane@^4.0.3:
version "4.1.0"
resolved "https://registry.yarnpkg.com/sane/-/sane-4.1.0.tgz#ed881fd922733a6c461bc189dc2b6c006f3ffded"
@@ -6622,6 +6654,16 @@ snapdragon@^0.8.1:
source-map-resolve "^0.5.0"
use "^3.1.0"
+sorcery@^0.10.0:
+ version "0.10.0"
+ resolved "https://registry.yarnpkg.com/sorcery/-/sorcery-0.10.0.tgz#8ae90ad7d7cb05fc59f1ab0c637845d5c15a52b7"
+ integrity sha1-iukK19fLBfxZ8asMY3hF1cFaUrc=
+ dependencies:
+ buffer-crc32 "^0.2.5"
+ minimist "^1.2.0"
+ sander "^0.5.0"
+ sourcemap-codec "^1.3.0"
+
source-map-resolve@^0.5.0, source-map-resolve@^0.5.2:
version "0.5.3"
resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a"
@@ -6668,6 +6710,11 @@ source-map@^0.7.3:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==
+sourcemap-codec@^1.3.0, sourcemap-codec@^1.4.4:
+ version "1.4.8"
+ resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4"
+ integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==
+
spdx-correct@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.1.tgz#dece81ac9c1e6713e5f7d1b6f17d468fa53d89a9"
@@ -6983,10 +7030,10 @@ supports-hyperlinks@^2.0.0:
has-flag "^4.0.0"
supports-color "^7.0.0"
-svelte@^3.23.0:
- version "3.31.0"
- resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.31.0.tgz#13966e5f55b975bc86675469bb2c58dd0e558d97"
- integrity sha512-r+n8UJkDqoQm1b+3tA3Lh6mHXKpcfOSOuEuIo5gE2W9wQYi64RYX/qE6CZBDDsP/H4M+N426JwY7XGH4xASvGQ==
+svelte@^3.42.0:
+ version "3.42.2"
+ resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.42.2.tgz#0246c175c820c1aeca07300c48573a15aae3c1e4"
+ integrity sha512-FOyNYKXb8wdE0Ot+Ctt2/OyDLsNBP8+V6PUE9ag6ZKeLslIou0LnMu1fhtWUA+HjzKTbAM1yj+4PFLtg/3pMJA==
symbol-tree@^3.2.2:
version "3.2.4"