@sveltejs/vite-plugin-svelte 2.3.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/package.json +9 -14
  2. package/src/{handle-hot-update.ts → handle-hot-update.js} +35 -20
  3. package/src/index.d.ts +215 -0
  4. package/src/{index.ts → index.js} +40 -69
  5. package/src/{preprocess.ts → preprocess.js} +37 -28
  6. package/src/types/compile.d.ts +48 -0
  7. package/src/types/id.d.ts +31 -0
  8. package/src/types/log.d.ts +24 -0
  9. package/src/types/options.d.ts +20 -0
  10. package/src/types/plugin-api.d.ts +11 -0
  11. package/src/types/vite-plugin-svelte-stats.d.ts +30 -0
  12. package/src/utils/{compile.ts → compile.js} +38 -64
  13. package/src/utils/{dependencies.ts → dependencies.js} +14 -11
  14. package/src/utils/{error.ts → error.js} +21 -14
  15. package/src/utils/{esbuild.ts → esbuild.js} +28 -19
  16. package/src/utils/{hash.ts → hash.js} +14 -3
  17. package/src/utils/{id.ts → id.js} +59 -60
  18. package/src/utils/{load-raw.ts → load-raw.js} +16 -16
  19. package/src/utils/{load-svelte-config.ts → load-svelte-config.js} +12 -10
  20. package/src/utils/{log.ts → log.js} +81 -48
  21. package/src/utils/{optimizer.ts → optimizer.js} +15 -7
  22. package/src/utils/{options.ts → options.js} +146 -296
  23. package/src/utils/{preprocess.ts → preprocess.js} +28 -12
  24. package/src/utils/{resolve.ts → resolve.js} +18 -9
  25. package/src/utils/{sourcemaps.ts → sourcemaps.js} +22 -14
  26. package/src/utils/svelte-version.js +6 -0
  27. package/src/utils/vite-plugin-svelte-cache.js +253 -0
  28. package/src/utils/{vite-plugin-svelte-stats.ts → vite-plugin-svelte-stats.js} +66 -62
  29. package/src/utils/{watch.ts → watch.js} +30 -22
  30. package/dist/index.d.ts +0 -193
  31. package/dist/index.js +0 -2272
  32. package/dist/index.js.map +0 -1
  33. package/src/__tests__/fixtures/preprocess/foo.scss +0 -3
  34. package/src/__tests__/preprocess.spec.ts +0 -51
  35. package/src/utils/__tests__/compile.spec.ts +0 -49
  36. package/src/utils/__tests__/sourcemaps.spec.ts +0 -79
  37. package/src/utils/__tests__/svelte-version.spec.ts +0 -102
  38. package/src/utils/svelte-version.ts +0 -37
  39. package/src/utils/vite-plugin-svelte-cache.ts +0 -182
  40. /package/src/utils/{constants.ts → constants.js} +0 -0
@@ -1,102 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { compareToSvelte, atLeastSvelte, parseVersion } from '../svelte-version';
3
- import { VERSION } from 'svelte/compiler';
4
- const svelteVersion = parseVersion(VERSION);
5
- describe('svelte-version', () => {
6
- describe('parseVersion', () => {
7
- it('should fill major,minor,patch', () => {
8
- expect(parseVersion('3')).toEqual([3, 0, 0]);
9
- expect(parseVersion('3.1')).toEqual([3, 1, 0]);
10
- });
11
- it('should ignore additional segments', () => {
12
- expect(parseVersion('1.2.3.4')).toEqual([1, 2, 3]);
13
- });
14
- });
15
- describe('compareToSvelte', () => {
16
- it('should return 0 for current', async () => {
17
- expect(compareToSvelte(VERSION)).toBe(0);
18
- });
19
-
20
- it('should return 1 for patch bump', async () => {
21
- const patch = svelteVersion.concat();
22
- patch[2] += 1;
23
- const patchBump = patch.join('.');
24
- expect(compareToSvelte(patchBump)).toBe(1);
25
- });
26
- it('should return 1 for minor bump', async () => {
27
- const minor = svelteVersion.concat();
28
- minor[1] += 1;
29
- const minorBump = minor.join('.');
30
- expect(compareToSvelte(minorBump)).toBe(1);
31
- });
32
- it('should return 1 for major bump', async () => {
33
- const major = svelteVersion.concat();
34
- major[0] += 1;
35
- const majorBump = major.join('.');
36
- expect(compareToSvelte(majorBump)).toBe(1);
37
- });
38
-
39
- it('should return -1 for lower patch', async () => {
40
- const patch = svelteVersion.concat();
41
- patch[2] -= 1;
42
- const lowerPatch = patch.join('.');
43
- expect(compareToSvelte(lowerPatch)).toBe(-1);
44
- });
45
- it('should return -1 for lower minor', async () => {
46
- const minor = svelteVersion.concat();
47
- minor[1] -= 1;
48
- const lowerMinor = minor.join('.');
49
- expect(compareToSvelte(lowerMinor)).toBe(-1);
50
- });
51
- it('should return -1 for lower major', async () => {
52
- const major = svelteVersion.concat();
53
- major[0] -= 1;
54
- const lowerMajor = major.join('.');
55
- expect(compareToSvelte(lowerMajor)).toBe(-1);
56
- });
57
- });
58
-
59
- describe('atLeastSvelte', () => {
60
- it('should return true for current', async () => {
61
- expect(atLeastSvelte(VERSION)).toBe(true);
62
- });
63
-
64
- it('should return false for higher patch', async () => {
65
- const patch = svelteVersion.concat();
66
- patch[2] += 1;
67
- const patchBump = patch.join('.');
68
- expect(atLeastSvelte(patchBump)).toBe(false);
69
- });
70
- it('should return false for higher minor', async () => {
71
- const minor = svelteVersion.concat();
72
- minor[1] += 1;
73
- const minorBump = minor.join('.');
74
- expect(atLeastSvelte(minorBump)).toBe(false);
75
- });
76
- it('should return false for higher major', async () => {
77
- const major = svelteVersion.concat();
78
- major[0] += 1;
79
- const majorBump = major.join('.');
80
- expect(atLeastSvelte(majorBump)).toBe(false);
81
- });
82
-
83
- it('should return true for lower patch', async () => {
84
- const patch = svelteVersion.concat();
85
- patch[2] -= 1;
86
- const lowerPatch = patch.join('.');
87
- expect(atLeastSvelte(lowerPatch)).toBe(true);
88
- });
89
- it('should return true for lower minor', async () => {
90
- const minor = svelteVersion.concat();
91
- minor[1] -= 1;
92
- const lowerMinor = minor.join('.');
93
- expect(atLeastSvelte(lowerMinor)).toBe(true);
94
- });
95
- it('should return true for lower major', async () => {
96
- const major = svelteVersion.concat();
97
- major[0] -= 1;
98
- const lowerMajor = major.join('.');
99
- expect(atLeastSvelte(lowerMajor)).toBe(true);
100
- });
101
- });
102
- });
@@ -1,37 +0,0 @@
1
- import { VERSION } from 'svelte/compiler';
2
- const svelteVersion = parseVersion(VERSION);
3
-
4
- export function parseVersion(version: string): number[] {
5
- const segments = version.split('.', 3).map((s) => parseInt(s, 10));
6
- while (segments.length < 3) {
7
- segments.push(0);
8
- }
9
- return segments;
10
- }
11
-
12
- /**
13
- * compare version with current svelte, only takes major.minor.patch into account.
14
- * If you don't pass all three, values will be filled with 0, ie `3` is equal to `3.0.0`
15
- * @param version
16
- * @returns 1 if passed version is larger than current, 0 if it is equal and -1 if it is lower
17
- */
18
- export function compareToSvelte(version: string): 1 | 0 | -1 {
19
- const parsedVersion = parseVersion(version);
20
- for (let i = 0; i < svelteVersion.length; i++) {
21
- const a = parsedVersion[i];
22
- const b = svelteVersion[i];
23
- if (a === b) {
24
- continue;
25
- } else if (a > b) {
26
- return 1;
27
- } else {
28
- return -1;
29
- }
30
- }
31
- return 0;
32
- }
33
-
34
- export function atLeastSvelte(version: string) {
35
- const result = compareToSvelte(version) <= 0;
36
- return result;
37
- }
@@ -1,182 +0,0 @@
1
- import { SvelteRequest } from './id';
2
- import { Code, CompileData } from './compile';
3
- import { readFileSync } from 'fs';
4
- import { dirname } from 'path';
5
- //eslint-disable-next-line node/no-missing-import
6
- import { findClosestPkgJsonPath } from 'vitefu';
7
- import { normalizePath } from 'vite';
8
-
9
- interface PackageInfo {
10
- name: string;
11
- version: string;
12
- svelte?: string;
13
- path: string;
14
- }
15
-
16
- export class VitePluginSvelteCache {
17
- private _css = new Map<string, Code>();
18
- private _js = new Map<string, Code>();
19
- private _dependencies = new Map<string, string[]>();
20
- private _dependants = new Map<string, Set<string>>();
21
- private _resolvedSvelteFields = new Map<string, string>();
22
- private _errors = new Map<string, any>();
23
- private _packageInfos: PackageInfo[] = [];
24
-
25
- public update(compileData: CompileData) {
26
- this._errors.delete(compileData.normalizedFilename);
27
- this.updateCSS(compileData);
28
- this.updateJS(compileData);
29
- this.updateDependencies(compileData);
30
- }
31
-
32
- public has(svelteRequest: SvelteRequest) {
33
- const id = svelteRequest.normalizedFilename;
34
- return this._errors.has(id) || this._js.has(id) || this._css.has(id);
35
- }
36
-
37
- public setError(svelteRequest: SvelteRequest, error: any) {
38
- // keep dependency info, otherwise errors in dependants would not trigger an update after fixing
39
- // because they are no longer watched
40
- this.remove(svelteRequest, true);
41
- this._errors.set(svelteRequest.normalizedFilename, error);
42
- }
43
-
44
- private updateCSS(compileData: CompileData) {
45
- this._css.set(compileData.normalizedFilename, compileData.compiled.css);
46
- }
47
-
48
- private updateJS(compileData: CompileData) {
49
- if (!compileData.ssr) {
50
- // do not cache SSR js
51
- this._js.set(compileData.normalizedFilename, compileData.compiled.js);
52
- }
53
- }
54
-
55
- private updateDependencies(compileData: CompileData) {
56
- const id = compileData.normalizedFilename;
57
- const prevDependencies = this._dependencies.get(id) || [];
58
- const dependencies = compileData.dependencies;
59
- this._dependencies.set(id, dependencies);
60
- const removed = prevDependencies.filter((d) => !dependencies.includes(d));
61
- const added = dependencies.filter((d) => !prevDependencies.includes(d));
62
- added.forEach((d) => {
63
- if (!this._dependants.has(d)) {
64
- this._dependants.set(d, new Set<string>());
65
- }
66
- this._dependants.get(d)!.add(compileData.filename);
67
- });
68
- removed.forEach((d) => {
69
- this._dependants.get(d)!.delete(compileData.filename);
70
- });
71
- }
72
-
73
- public remove(svelteRequest: SvelteRequest, keepDependencies: boolean = false): boolean {
74
- const id = svelteRequest.normalizedFilename;
75
- let removed = false;
76
- if (this._errors.delete(id)) {
77
- removed = true;
78
- }
79
- if (this._js.delete(id)) {
80
- removed = true;
81
- }
82
- if (this._css.delete(id)) {
83
- removed = true;
84
- }
85
- if (!keepDependencies) {
86
- const dependencies = this._dependencies.get(id);
87
- if (dependencies) {
88
- removed = true;
89
- dependencies.forEach((d) => {
90
- const dependants = this._dependants.get(d);
91
- if (dependants && dependants.has(svelteRequest.filename)) {
92
- dependants.delete(svelteRequest.filename);
93
- }
94
- });
95
- this._dependencies.delete(id);
96
- }
97
- }
98
-
99
- return removed;
100
- }
101
-
102
- public getCSS(svelteRequest: SvelteRequest) {
103
- return this._css.get(svelteRequest.normalizedFilename);
104
- }
105
-
106
- public getJS(svelteRequest: SvelteRequest) {
107
- if (!svelteRequest.ssr) {
108
- // SSR js isn't cached
109
- return this._js.get(svelteRequest.normalizedFilename);
110
- }
111
- }
112
-
113
- public getError(svelteRequest: SvelteRequest) {
114
- return this._errors.get(svelteRequest.normalizedFilename);
115
- }
116
-
117
- public getDependants(path: string): string[] {
118
- const dependants = this._dependants.get(path);
119
- return dependants ? [...dependants] : [];
120
- }
121
-
122
- public getResolvedSvelteField(name: string, importer?: string): string | void {
123
- return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
124
- }
125
-
126
- public hasResolvedSvelteField(name: string, importer?: string) {
127
- return this._resolvedSvelteFields.has(this._getResolvedSvelteFieldKey(name, importer));
128
- }
129
- public setResolvedSvelteField(
130
- importee: string,
131
- importer: string | undefined = undefined,
132
- resolvedSvelte: string
133
- ) {
134
- this._resolvedSvelteFields.set(
135
- this._getResolvedSvelteFieldKey(importee, importer),
136
- resolvedSvelte
137
- );
138
- }
139
-
140
- private _getResolvedSvelteFieldKey(importee: string, importer?: string): string {
141
- return importer ? `${importer} > ${importee}` : importee;
142
- }
143
-
144
- public async getPackageInfo(file: string): Promise<PackageInfo> {
145
- let info = this._packageInfos.find((pi) => file.startsWith(pi.path));
146
- if (!info) {
147
- info = await findPackageInfo(file);
148
- this._packageInfos.push(info);
149
- }
150
- return info;
151
- }
152
- }
153
-
154
- /**
155
- * utility to get some info from the closest package.json with a "name" set
156
- *
157
- * @param {string} file to find info for
158
- * @returns {PackageInfo}
159
- */
160
- async function findPackageInfo(file: string): Promise<PackageInfo> {
161
- const info: PackageInfo = {
162
- name: '$unknown',
163
- version: '0.0.0-unknown',
164
- path: '$unknown'
165
- };
166
- let path = await findClosestPkgJsonPath(file, (pkgPath) => {
167
- const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
168
- if (pkg.name != null) {
169
- info.name = pkg.name;
170
- if (pkg.version != null) {
171
- info.version = pkg.version;
172
- }
173
- info.svelte = pkg.svelte;
174
- return true;
175
- }
176
- return false;
177
- });
178
- // return normalized path with appended '/' so .startsWith works for future file checks
179
- path = normalizePath(dirname(path ?? file)) + '/';
180
- info.path = path;
181
- return info;
182
- }
File without changes