@sveltejs/vite-plugin-svelte 7.0.0-next.1 → 7.1.0
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.
- package/package.json +2 -2
- package/src/index.js +7 -3
- package/src/plugins/compile-module.js +17 -11
- package/src/plugins/compile.js +10 -5
- package/src/plugins/configure.js +10 -5
- package/src/plugins/hot-update.js +10 -5
- package/src/plugins/inspector/index.js +7 -3
- package/src/plugins/inspector/options.js +6 -3
- package/src/plugins/load-compiled-css.js +5 -2
- package/src/plugins/load-custom.js +6 -3
- package/src/plugins/preprocess.js +21 -14
- package/src/plugins/setup-optimizer.js +60 -59
- package/src/preprocess.js +34 -29
- package/src/utils/compile.js +10 -6
- package/src/utils/constants.js +32 -10
- package/src/utils/error.js +13 -8
- package/src/utils/id.js +16 -13
- package/src/utils/load-svelte-config.js +8 -5
- package/src/utils/log.js +24 -19
- package/src/utils/options.js +49 -45
- package/src/utils/vite-plugin-svelte-stats.js +14 -14
- package/src/utils/watch.js +5 -2
- package/types/index.d.ts +4 -4
- package/types/index.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/vite-plugin-svelte",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"src",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@types/debug": "^4.1.12",
|
|
47
47
|
"sass": "^1.94.2",
|
|
48
48
|
"svelte": "^5.45.4",
|
|
49
|
-
"vite": "^8.0.
|
|
49
|
+
"vite": "^8.0.9"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"check:publint": "publint --strict",
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** @import { Options } from './public.js' */
|
|
2
|
+
/** @import { PluginAPI } from './types/plugin-api.js' */
|
|
3
|
+
/** @import { Plugin } from 'vite' */
|
|
4
|
+
|
|
1
5
|
import process from 'node:process';
|
|
2
6
|
import { log } from './utils/log.js';
|
|
3
7
|
import { configure } from './plugins/configure.js';
|
|
@@ -14,14 +18,14 @@ import { hotUpdate } from './plugins/hot-update.js';
|
|
|
14
18
|
* returns a list of plugins to handle svelte files
|
|
15
19
|
* plugins are named `vite-plugin-svelte:<task>`
|
|
16
20
|
*
|
|
17
|
-
* @param {Partial<
|
|
18
|
-
* @returns {
|
|
21
|
+
* @param {Partial<Options>} [inlineOptions]
|
|
22
|
+
* @returns {Plugin[]}
|
|
19
23
|
*/
|
|
20
24
|
export function svelte(inlineOptions) {
|
|
21
25
|
if (process.env.DEBUG != null) {
|
|
22
26
|
log.setLevel('debug');
|
|
23
27
|
}
|
|
24
|
-
/** @type {
|
|
28
|
+
/** @type {PluginAPI} */
|
|
25
29
|
// @ts-expect-error initialize empty to guard against early use
|
|
26
30
|
const api = {}; // initialized by configure plugin, used in others
|
|
27
31
|
return [
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/** @import { ModuleIdParser } from '../types/id.js' */
|
|
2
|
+
/** @import { ResolvedOptions } from '../types/options.js' */
|
|
3
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
4
|
+
/** @import { CompileOptions, ModuleCompileOptions } from 'svelte/compiler' */
|
|
5
|
+
/** @import { Plugin } from 'vite' */
|
|
6
|
+
|
|
1
7
|
import { buildModuleIdFilter, buildModuleIdParser } from '../utils/id.js';
|
|
2
8
|
import * as svelteCompiler from 'svelte/compiler';
|
|
3
9
|
import { log, logCompilerWarnings } from '../utils/log.js';
|
|
@@ -5,25 +11,25 @@ import { toRollupError } from '../utils/error.js';
|
|
|
5
11
|
import { isSvelteWithAsync } from '../utils/svelte-version.js';
|
|
6
12
|
|
|
7
13
|
/**
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
14
|
+
* @param {PluginAPI} api
|
|
15
|
+
* @returns {Plugin}
|
|
10
16
|
*/
|
|
11
17
|
export function compileModule(api) {
|
|
12
18
|
/**
|
|
13
|
-
* @type {
|
|
19
|
+
* @type {ResolvedOptions}
|
|
14
20
|
*/
|
|
15
21
|
let options;
|
|
16
22
|
/**
|
|
17
|
-
* @type {
|
|
23
|
+
* @type {ModuleIdParser}
|
|
18
24
|
*/
|
|
19
25
|
let idParser;
|
|
20
26
|
|
|
21
27
|
/**
|
|
22
|
-
* @type {
|
|
28
|
+
* @type {ModuleCompileOptions}
|
|
23
29
|
*/
|
|
24
30
|
let staticModuleCompileOptions;
|
|
25
31
|
|
|
26
|
-
/** @type {
|
|
32
|
+
/** @type {Plugin} */
|
|
27
33
|
const plugin = {
|
|
28
34
|
name: 'vite-plugin-svelte:compile-module',
|
|
29
35
|
enforce: 'post',
|
|
@@ -42,7 +48,7 @@ export function compileModule(api) {
|
|
|
42
48
|
return;
|
|
43
49
|
}
|
|
44
50
|
const filename = moduleRequest.filename;
|
|
45
|
-
/** @type {
|
|
51
|
+
/** @type {CompileOptions} */
|
|
46
52
|
const compileOptions = {
|
|
47
53
|
...staticModuleCompileOptions,
|
|
48
54
|
dev: !this.environment.config.isProduction,
|
|
@@ -96,11 +102,11 @@ export function compileModule(api) {
|
|
|
96
102
|
|
|
97
103
|
/**
|
|
98
104
|
*
|
|
99
|
-
* @param {
|
|
100
|
-
* @return {
|
|
105
|
+
* @param {CompileOptions} compilerOptions
|
|
106
|
+
* @return {ModuleCompileOptions}
|
|
101
107
|
*/
|
|
102
108
|
function filterNonModuleCompilerOptions(compilerOptions) {
|
|
103
|
-
/** @type {Array<keyof
|
|
109
|
+
/** @type {Array<keyof ModuleCompileOptions>} */
|
|
104
110
|
const knownModuleCompileOptionNames = ['dev', 'generate', 'filename', 'rootDir', 'warningFilter'];
|
|
105
111
|
if (isSvelteWithAsync) {
|
|
106
112
|
knownModuleCompileOptionNames.push('experimental');
|
|
@@ -108,7 +114,7 @@ function filterNonModuleCompilerOptions(compilerOptions) {
|
|
|
108
114
|
// not typed but this is temporary until svelte itself ignores CompileOptions passed to compileModule
|
|
109
115
|
const experimentalModuleCompilerOptionNames = ['async'];
|
|
110
116
|
|
|
111
|
-
/** @type {
|
|
117
|
+
/** @type {ModuleCompileOptions} */
|
|
112
118
|
const filtered = filterByPropNames(compilerOptions, knownModuleCompileOptionNames);
|
|
113
119
|
if (filtered.experimental) {
|
|
114
120
|
filtered.experimental = filterByPropNames(
|
package/src/plugins/compile.js
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
|
+
/** @import { CompileSvelte } from '../types/compile.js' */
|
|
2
|
+
/** @import { ResolvedOptions } from '../types/options.js' */
|
|
3
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
4
|
+
/** @import { Plugin } from 'vite' */
|
|
5
|
+
|
|
1
6
|
import { toRollupError } from '../utils/error.js';
|
|
2
7
|
import { logCompilerWarnings } from '../utils/log.js';
|
|
3
8
|
|
|
4
9
|
/**
|
|
5
|
-
* @param {
|
|
6
|
-
* @returns {
|
|
10
|
+
* @param {PluginAPI} api
|
|
11
|
+
* @returns {Plugin}
|
|
7
12
|
*/
|
|
8
13
|
export function compile(api) {
|
|
9
14
|
/**
|
|
10
|
-
* @type {
|
|
15
|
+
* @type {ResolvedOptions}
|
|
11
16
|
*/
|
|
12
17
|
let options;
|
|
13
18
|
|
|
14
19
|
/**
|
|
15
|
-
* @type {
|
|
20
|
+
* @type {CompileSvelte}
|
|
16
21
|
*/
|
|
17
22
|
let compileSvelte;
|
|
18
|
-
/** @type {
|
|
23
|
+
/** @type {Plugin} */
|
|
19
24
|
const plugin = {
|
|
20
25
|
name: 'vite-plugin-svelte:compile',
|
|
21
26
|
configResolved() {
|
package/src/plugins/configure.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/** @import { Options } from '../public.js' */
|
|
2
|
+
/** @import { PreResolvedOptions } from '../types/options.js' */
|
|
3
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
4
|
+
/** @import { Plugin } from 'vite' */
|
|
5
|
+
|
|
1
6
|
import process from 'node:process';
|
|
2
7
|
import { isDebugNamespaceEnabled, log } from '../utils/log.js';
|
|
3
8
|
import { VitePluginSvelteStats } from '../utils/vite-plugin-svelte-stats.js';
|
|
@@ -13,19 +18,19 @@ import { buildIdFilter, buildIdParser } from '../utils/id.js';
|
|
|
13
18
|
import { createCompileSvelte } from '../utils/compile.js';
|
|
14
19
|
|
|
15
20
|
/**
|
|
16
|
-
* @param {Partial<
|
|
17
|
-
* @param {
|
|
18
|
-
* @returns {
|
|
21
|
+
* @param {Partial<Options>} [inlineOptions]
|
|
22
|
+
* @param {PluginAPI} api
|
|
23
|
+
* @returns {Plugin}
|
|
19
24
|
*/
|
|
20
25
|
export function configure(api, inlineOptions) {
|
|
21
26
|
validateInlineOptions(inlineOptions);
|
|
22
27
|
|
|
23
28
|
/**
|
|
24
|
-
* @type {
|
|
29
|
+
* @type {PreResolvedOptions}
|
|
25
30
|
*/
|
|
26
31
|
let preOptions;
|
|
27
32
|
|
|
28
|
-
/** @type {
|
|
33
|
+
/** @type {Plugin} */
|
|
29
34
|
return {
|
|
30
35
|
name: 'vite-plugin-svelte:config',
|
|
31
36
|
api,
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
+
/** @import { IdParser } from '../types/id.js' */
|
|
2
|
+
/** @import { ResolvedOptions } from '../types/options.js' */
|
|
3
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
4
|
+
/** @import { Plugin } from 'vite' */
|
|
5
|
+
|
|
1
6
|
import { log } from '../utils/log.js';
|
|
2
7
|
import { setupWatchers } from '../utils/watch.js';
|
|
3
8
|
import { SVELTE_VIRTUAL_STYLE_ID_REGEX } from '../utils/constants.js';
|
|
4
9
|
|
|
5
10
|
/**
|
|
6
|
-
* @param {
|
|
7
|
-
* @returns {
|
|
11
|
+
* @param {PluginAPI} api
|
|
12
|
+
* @returns {Plugin}
|
|
8
13
|
*/
|
|
9
14
|
export function hotUpdate(api) {
|
|
10
15
|
/**
|
|
11
|
-
* @type {
|
|
16
|
+
* @type {ResolvedOptions}
|
|
12
17
|
*/
|
|
13
18
|
let options;
|
|
14
19
|
/**
|
|
15
|
-
* @type {
|
|
20
|
+
* @type {IdParser}
|
|
16
21
|
*/
|
|
17
22
|
let idParser;
|
|
18
23
|
|
|
@@ -22,7 +27,7 @@ export function hotUpdate(api) {
|
|
|
22
27
|
*/
|
|
23
28
|
const transformResultCache = new Map();
|
|
24
29
|
|
|
25
|
-
/** @type {
|
|
30
|
+
/** @type {Plugin} */
|
|
26
31
|
const plugin = {
|
|
27
32
|
name: 'vite-plugin-svelte:hot-update',
|
|
28
33
|
enforce: 'post',
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** @import { InspectorOptions } from '../../public.js' */
|
|
2
|
+
/** @import { PluginAPI } from '../../types/plugin-api.js' */
|
|
3
|
+
/** @import { Plugin } from 'vite' */
|
|
4
|
+
|
|
1
5
|
import { normalizePath } from 'vite';
|
|
2
6
|
import fs from 'node:fs';
|
|
3
7
|
import path from 'node:path';
|
|
@@ -22,14 +26,14 @@ function getInspectorPath() {
|
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
/**
|
|
25
|
-
* @param {
|
|
26
|
-
* @returns {
|
|
29
|
+
* @param {PluginAPI} api
|
|
30
|
+
* @returns {Plugin}
|
|
27
31
|
*/
|
|
28
32
|
export function svelteInspector(api) {
|
|
29
33
|
const inspectorPath = getInspectorPath();
|
|
30
34
|
log.debug(`svelte inspector path: ${inspectorPath}`, null, 'inspector');
|
|
31
35
|
|
|
32
|
-
/** @type {
|
|
36
|
+
/** @type {InspectorOptions} */
|
|
33
37
|
let inspectorOptions;
|
|
34
38
|
let disabled = false;
|
|
35
39
|
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
/** @import { InspectorOptions } from '../../public.js' */
|
|
2
|
+
/** @import { ResolvedConfig } from 'vite' */
|
|
3
|
+
|
|
1
4
|
import process from 'node:process';
|
|
2
5
|
import { loadEnv } from 'vite';
|
|
3
6
|
import { log } from '../../utils/log.js';
|
|
4
7
|
|
|
5
|
-
/** @type {
|
|
8
|
+
/** @type {InspectorOptions} */
|
|
6
9
|
export const defaultInspectorOptions = {
|
|
7
10
|
toggleKeyCombo: 'alt-x',
|
|
8
11
|
navKeys: { parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' },
|
|
@@ -15,8 +18,8 @@ export const defaultInspectorOptions = {
|
|
|
15
18
|
};
|
|
16
19
|
|
|
17
20
|
/**
|
|
18
|
-
* @param {
|
|
19
|
-
* @returns {Partial<
|
|
21
|
+
* @param {ResolvedConfig} config
|
|
22
|
+
* @returns {Partial<InspectorOptions> | boolean | void}
|
|
20
23
|
*/
|
|
21
24
|
export function parseEnvironmentOptions(config) {
|
|
22
25
|
const env = loadEnv(config.mode, config.envDir ?? process.cwd(), 'SVELTE_INSPECTOR');
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
2
|
+
/** @import { Plugin } from 'vite' */
|
|
3
|
+
|
|
1
4
|
import { log } from '../utils/log.js';
|
|
2
5
|
import { SVELTE_VIRTUAL_STYLE_ID_REGEX } from '../utils/constants.js';
|
|
3
6
|
|
|
4
7
|
const filter = { id: SVELTE_VIRTUAL_STYLE_ID_REGEX };
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
|
-
* @param {
|
|
8
|
-
* @returns {
|
|
10
|
+
* @param {PluginAPI} api
|
|
11
|
+
* @returns {Plugin}
|
|
9
12
|
*/
|
|
10
13
|
export function loadCompiledCss(api) {
|
|
11
14
|
let useLocalCache = false;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
2
|
+
/** @import { Plugin } from 'vite' */
|
|
3
|
+
|
|
1
4
|
import fs from 'node:fs';
|
|
2
5
|
import { log } from '../utils/log.js';
|
|
3
6
|
|
|
@@ -5,11 +8,11 @@ import { log } from '../utils/log.js';
|
|
|
5
8
|
* if svelte config includes files that vite treats as assets (e.g. .svg)
|
|
6
9
|
* we have to manually load them to avoid getting urls
|
|
7
10
|
*
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
11
|
+
* @param {PluginAPI} api
|
|
12
|
+
* @returns {Plugin}
|
|
10
13
|
*/
|
|
11
14
|
export function loadCustom(api) {
|
|
12
|
-
/** @type {
|
|
15
|
+
/** @type {Plugin} */
|
|
13
16
|
const plugin = {
|
|
14
17
|
name: 'vite-plugin-svelte:load-custom',
|
|
15
18
|
enforce: 'pre', // must come before vites own asset handling or custom extensions like .svg won't work
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/** @import { PreprocessSvelte } from '../types/compile.js' */
|
|
2
|
+
/** @import { SvelteRequest } from '../types/id.js' */
|
|
3
|
+
/** @import { ResolvedOptions } from '../types/options.js' */
|
|
4
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
5
|
+
/** @import { PreprocessorGroup } from 'svelte/compiler' */
|
|
6
|
+
/** @import { Plugin, ResolvedConfig, Rollup, ViteDevServer } from 'vite' */
|
|
7
|
+
|
|
1
8
|
import { toRollupError } from '../utils/error.js';
|
|
2
9
|
import { mapToRelative } from '../utils/sourcemaps.js';
|
|
3
10
|
import * as svelte from 'svelte/compiler';
|
|
@@ -7,12 +14,12 @@ import fs from 'node:fs';
|
|
|
7
14
|
import path from 'node:path';
|
|
8
15
|
|
|
9
16
|
/**
|
|
10
|
-
* @param {
|
|
11
|
-
* @returns {
|
|
17
|
+
* @param {PluginAPI} api
|
|
18
|
+
* @returns {Plugin}
|
|
12
19
|
*/
|
|
13
20
|
export function preprocess(api) {
|
|
14
21
|
/**
|
|
15
|
-
* @type {
|
|
22
|
+
* @type {ResolvedOptions}
|
|
16
23
|
*/
|
|
17
24
|
let options;
|
|
18
25
|
|
|
@@ -22,11 +29,11 @@ export function preprocess(api) {
|
|
|
22
29
|
let dependenciesCache;
|
|
23
30
|
|
|
24
31
|
/**
|
|
25
|
-
* @type {
|
|
32
|
+
* @type {PreprocessSvelte}
|
|
26
33
|
*/
|
|
27
34
|
let preprocessSvelte;
|
|
28
35
|
|
|
29
|
-
/** @type {
|
|
36
|
+
/** @type {Plugin} */
|
|
30
37
|
const plugin = {
|
|
31
38
|
name: 'vite-plugin-svelte:preprocess',
|
|
32
39
|
enforce: 'pre',
|
|
@@ -73,7 +80,7 @@ export function preprocess(api) {
|
|
|
73
80
|
}
|
|
74
81
|
}
|
|
75
82
|
|
|
76
|
-
/** @type {
|
|
83
|
+
/** @type {Rollup.SourceDescription}*/
|
|
77
84
|
const result = { code: preprocessed.code };
|
|
78
85
|
if (preprocessed.map) {
|
|
79
86
|
// @ts-expect-error type differs but should work
|
|
@@ -89,12 +96,12 @@ export function preprocess(api) {
|
|
|
89
96
|
return plugin;
|
|
90
97
|
}
|
|
91
98
|
/**
|
|
92
|
-
* @param {
|
|
93
|
-
* @param {
|
|
94
|
-
* @returns {
|
|
99
|
+
* @param {ResolvedOptions} options
|
|
100
|
+
* @param {ResolvedConfig} resolvedConfig
|
|
101
|
+
* @returns {PreprocessSvelte}
|
|
95
102
|
*/
|
|
96
103
|
function createPreprocessSvelte(options, resolvedConfig) {
|
|
97
|
-
/** @type {Array<
|
|
104
|
+
/** @type {Array<PreprocessorGroup>} */
|
|
98
105
|
const preprocessors = arraify(options.preprocess);
|
|
99
106
|
|
|
100
107
|
for (const preprocessor of preprocessors) {
|
|
@@ -103,7 +110,7 @@ function createPreprocessSvelte(options, resolvedConfig) {
|
|
|
103
110
|
}
|
|
104
111
|
}
|
|
105
112
|
|
|
106
|
-
/** @type {
|
|
113
|
+
/** @type {PreprocessSvelte} */
|
|
107
114
|
return async function preprocessSvelte(svelteRequest, code) {
|
|
108
115
|
const { filename } = svelteRequest;
|
|
109
116
|
let preprocessed;
|
|
@@ -133,11 +140,11 @@ class DependenciesCache {
|
|
|
133
140
|
/** @type {Map<string, Set<string>>} */
|
|
134
141
|
#dependants = new Map();
|
|
135
142
|
|
|
136
|
-
/** @type {
|
|
143
|
+
/** @type {ViteDevServer} */
|
|
137
144
|
#server;
|
|
138
145
|
/**
|
|
139
146
|
*
|
|
140
|
-
* @param {
|
|
147
|
+
* @param {ViteDevServer} server
|
|
141
148
|
*/
|
|
142
149
|
constructor(server) {
|
|
143
150
|
this.#server = server;
|
|
@@ -184,7 +191,7 @@ class DependenciesCache {
|
|
|
184
191
|
|
|
185
192
|
/**
|
|
186
193
|
*
|
|
187
|
-
* @param {
|
|
194
|
+
* @param {SvelteRequest} svelteRequest
|
|
188
195
|
* @param {string[]} dependencies
|
|
189
196
|
*/
|
|
190
197
|
update(svelteRequest, dependencies) {
|
|
@@ -1,57 +1,61 @@
|
|
|
1
|
+
/** @import { Code } from '../types/compile.js' */
|
|
2
|
+
/** @import { ResolvedOptions } from '../types/options.js' */
|
|
3
|
+
/** @import { PluginAPI } from '../types/plugin-api.js' */
|
|
4
|
+
/** @import { StatCollection } from '../types/vite-plugin-svelte-stats.js' */
|
|
5
|
+
/** @import { CompileOptions } from 'svelte/compiler' */
|
|
6
|
+
/** @import { Plugin, ResolvedConfig, Rolldown, UserConfig } from 'vite' */
|
|
7
|
+
|
|
1
8
|
import fs from 'node:fs/promises';
|
|
2
9
|
import path from 'node:path';
|
|
3
10
|
import * as svelte from 'svelte/compiler';
|
|
4
11
|
import { log } from '../utils/log.js';
|
|
5
12
|
import { toRollupError } from '../utils/error.js';
|
|
13
|
+
import { SVELTE_IMPORTS } from '../utils/constants.js';
|
|
14
|
+
import { isDepExcluded } from 'vitefu';
|
|
6
15
|
|
|
7
16
|
/**
|
|
8
|
-
* @typedef {NonNullable<
|
|
17
|
+
* @typedef {NonNullable<Rolldown.Plugin>} RollupPlugin
|
|
9
18
|
*/
|
|
10
19
|
|
|
11
20
|
const optimizeSveltePluginName = 'vite-plugin-svelte:optimize';
|
|
12
21
|
const optimizeSvelteModulePluginName = 'vite-plugin-svelte:optimize-module';
|
|
13
22
|
|
|
14
23
|
/**
|
|
15
|
-
* @param {
|
|
16
|
-
* @returns {
|
|
24
|
+
* @param {PluginAPI} api
|
|
25
|
+
* @returns {Plugin}
|
|
17
26
|
*/
|
|
18
27
|
export function setupOptimizer(api) {
|
|
19
|
-
/** @type {
|
|
28
|
+
/** @type {ResolvedConfig} */
|
|
20
29
|
let viteConfig;
|
|
21
30
|
|
|
22
31
|
return {
|
|
23
32
|
name: 'vite-plugin-svelte:setup-optimizer',
|
|
24
33
|
apply: 'serve',
|
|
25
|
-
config
|
|
26
|
-
|
|
34
|
+
configEnvironment(name, config) {
|
|
35
|
+
// fall back to vite behavior when consumer isn't set
|
|
36
|
+
const consumer = (config.consumer ?? name === 'client') ? 'client' : 'server';
|
|
37
|
+
/** @type {UserConfig['optimizeDeps']} */
|
|
27
38
|
const optimizeDeps = {
|
|
28
39
|
// Experimental Vite API to allow these extensions to be scanned and prebundled
|
|
29
40
|
extensions: ['.svelte']
|
|
30
41
|
};
|
|
31
|
-
// Add optimizer plugins to prebundle Svelte files.
|
|
32
|
-
// Currently, a placeholder as more information is needed after Vite config is resolved,
|
|
33
|
-
// the added plugins are patched in configResolved below
|
|
34
42
|
|
|
43
|
+
// Add optimizer plugins to prebundle Svelte files.
|
|
35
44
|
optimizeDeps.rolldownOptions = {
|
|
36
45
|
plugins: [
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
rolldownOptimizerPlugin(api, consumer, true),
|
|
47
|
+
rolldownOptimizerPlugin(api, consumer, false)
|
|
39
48
|
]
|
|
40
49
|
};
|
|
41
50
|
|
|
51
|
+
if (consumer === 'server' && !isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
|
|
52
|
+
optimizeDeps.include = [...SVELTE_IMPORTS];
|
|
53
|
+
}
|
|
54
|
+
|
|
42
55
|
return { optimizeDeps };
|
|
43
56
|
},
|
|
44
57
|
configResolved(c) {
|
|
45
58
|
viteConfig = c;
|
|
46
|
-
const optimizeDeps = c.optimizeDeps;
|
|
47
|
-
const plugins =
|
|
48
|
-
// @ts-expect-error not typed
|
|
49
|
-
optimizeDeps.rolldownOptions?.plugins?.filter((p) =>
|
|
50
|
-
[optimizeSveltePluginName, optimizeSvelteModulePluginName].includes(p.name)
|
|
51
|
-
) ?? [];
|
|
52
|
-
for (const plugin of plugins) {
|
|
53
|
-
patchRolldownOptimizerPlugin(plugin, api.options);
|
|
54
|
-
}
|
|
55
59
|
},
|
|
56
60
|
async buildStart() {
|
|
57
61
|
if (!api.options.prebundleSvelteLibraries) return;
|
|
@@ -66,21 +70,29 @@ export function setupOptimizer(api) {
|
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
/**
|
|
69
|
-
* @param {
|
|
70
|
-
* @param {
|
|
73
|
+
* @param {import('../types/plugin-api.d.ts').PluginAPI} api
|
|
74
|
+
* @param {'server'|'client'} consumer
|
|
75
|
+
* @param {boolean} components
|
|
76
|
+
* @return {Rolldown.Plugin}
|
|
71
77
|
*/
|
|
72
|
-
function
|
|
73
|
-
const
|
|
78
|
+
function rolldownOptimizerPlugin(api, consumer, components) {
|
|
79
|
+
const name = components ? optimizeSveltePluginName : optimizeSvelteModulePluginName;
|
|
74
80
|
const compileFn = components ? compileSvelte : compileSvelteModule;
|
|
75
81
|
const statsName = components ? 'prebundle library components' : 'prebundle library modules';
|
|
76
82
|
const includeRe = components ? /^[^?#]+\.svelte(?:[?#]|$)/ : /^[^?#]+\.svelte\.[jt]s(?:[?#]|$)/;
|
|
77
|
-
|
|
83
|
+
const generate = consumer === 'server' ? 'server' : 'client';
|
|
84
|
+
/** @type {StatCollection | undefined} */
|
|
78
85
|
let statsCollection;
|
|
79
86
|
|
|
87
|
+
/**@type {Rolldown.Plugin}*/
|
|
88
|
+
const plugin = {
|
|
89
|
+
name
|
|
90
|
+
};
|
|
91
|
+
|
|
80
92
|
plugin.options = (opts) => {
|
|
81
93
|
// @ts-expect-error plugins is an array here
|
|
82
94
|
const isScanner = opts.plugins.some(
|
|
83
|
-
(/** @type {{ name: string; }} */ p) => p
|
|
95
|
+
(/** @type {{ name: string; } | undefined} */ p) => p?.name === 'vite:dep-scan:resolve'
|
|
84
96
|
);
|
|
85
97
|
if (isScanner) {
|
|
86
98
|
delete plugin.buildStart;
|
|
@@ -95,14 +107,14 @@ function patchRolldownOptimizerPlugin(plugin, options) {
|
|
|
95
107
|
*/
|
|
96
108
|
async handler(code, filename) {
|
|
97
109
|
try {
|
|
98
|
-
return await compileFn(options, { filename, code }, statsCollection);
|
|
110
|
+
return await compileFn(api.options, { filename, code }, generate, statsCollection);
|
|
99
111
|
} catch (e) {
|
|
100
|
-
throw toRollupError(e, options);
|
|
112
|
+
throw toRollupError(e, api.options);
|
|
101
113
|
}
|
|
102
114
|
}
|
|
103
115
|
};
|
|
104
116
|
plugin.buildStart = () => {
|
|
105
|
-
statsCollection = options.stats?.startCollection(statsName, {
|
|
117
|
+
statsCollection = api.options.stats?.startCollection(statsName, {
|
|
106
118
|
logResult: (c) => c.stats.length > 1
|
|
107
119
|
});
|
|
108
120
|
};
|
|
@@ -111,27 +123,30 @@ function patchRolldownOptimizerPlugin(plugin, options) {
|
|
|
111
123
|
};
|
|
112
124
|
}
|
|
113
125
|
};
|
|
126
|
+
|
|
127
|
+
return plugin;
|
|
114
128
|
}
|
|
115
129
|
|
|
116
130
|
/**
|
|
117
|
-
* @param {
|
|
131
|
+
* @param {ResolvedOptions} options
|
|
118
132
|
* @param {{ filename: string, code: string }} input
|
|
119
|
-
* @param {
|
|
120
|
-
* @
|
|
133
|
+
* @param {'client'|'server'} generate
|
|
134
|
+
* @param {StatCollection} [statsCollection]
|
|
135
|
+
* @returns {Promise<Code>}
|
|
121
136
|
*/
|
|
122
|
-
async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
137
|
+
async function compileSvelte(options, { filename, code }, generate, statsCollection) {
|
|
123
138
|
let css = options.compilerOptions.css;
|
|
124
139
|
if (css !== 'injected') {
|
|
125
140
|
// TODO ideally we'd be able to externalize prebundled styles too, but for now always put them in the js
|
|
126
141
|
css = 'injected';
|
|
127
142
|
}
|
|
128
|
-
/** @type {
|
|
143
|
+
/** @type {CompileOptions} */
|
|
129
144
|
const compileOptions = {
|
|
130
145
|
dev: true, // default to dev: true because prebundling is only used in dev
|
|
131
146
|
...options.compilerOptions,
|
|
132
147
|
css,
|
|
133
148
|
filename,
|
|
134
|
-
generate
|
|
149
|
+
generate
|
|
135
150
|
};
|
|
136
151
|
|
|
137
152
|
let preprocessed;
|
|
@@ -180,17 +195,18 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
|
|
|
180
195
|
}
|
|
181
196
|
|
|
182
197
|
/**
|
|
183
|
-
* @param {
|
|
198
|
+
* @param {ResolvedOptions} options
|
|
184
199
|
* @param {{ filename: string; code: string }} input
|
|
185
|
-
* @param {
|
|
186
|
-
* @
|
|
200
|
+
* @param {'client'|'server'} generate
|
|
201
|
+
* @param {StatCollection} [statsCollection]
|
|
202
|
+
* @returns {Promise<Code>}
|
|
187
203
|
*/
|
|
188
|
-
async function compileSvelteModule(options, { filename, code }, statsCollection) {
|
|
204
|
+
async function compileSvelteModule(options, { filename, code }, generate, statsCollection) {
|
|
189
205
|
const endStat = statsCollection?.start(filename);
|
|
190
206
|
const compiled = svelte.compileModule(code, {
|
|
191
207
|
dev: options.compilerOptions?.dev ?? true, // default to dev: true because prebundling is only used in dev
|
|
192
208
|
filename,
|
|
193
|
-
generate
|
|
209
|
+
generate
|
|
194
210
|
});
|
|
195
211
|
if (endStat) {
|
|
196
212
|
endStat();
|
|
@@ -202,7 +218,7 @@ async function compileSvelteModule(options, { filename, code }, statsCollection)
|
|
|
202
218
|
}
|
|
203
219
|
|
|
204
220
|
// List of options that changes the prebundling result
|
|
205
|
-
/** @type {(keyof
|
|
221
|
+
/** @type {(keyof ResolvedOptions)[]} */
|
|
206
222
|
const PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
207
223
|
'compilerOptions',
|
|
208
224
|
'configFile',
|
|
@@ -215,7 +231,7 @@ const PREBUNDLE_SENSITIVE_OPTIONS = [
|
|
|
215
231
|
* stores svelte metadata in cache dir and compares if it has changed
|
|
216
232
|
*
|
|
217
233
|
* @param {string} cacheDir
|
|
218
|
-
* @param {
|
|
234
|
+
* @param {ResolvedOptions} options
|
|
219
235
|
* @returns {Promise<boolean>} Whether the Svelte metadata has changed
|
|
220
236
|
*/
|
|
221
237
|
async function svelteMetadataChanged(cacheDir, options) {
|
|
@@ -241,23 +257,8 @@ async function svelteMetadataChanged(cacheDir, options) {
|
|
|
241
257
|
}
|
|
242
258
|
|
|
243
259
|
/**
|
|
244
|
-
*
|
|
245
|
-
* @
|
|
246
|
-
* @returns {import('vite').Rollup.Plugin}
|
|
247
|
-
*/
|
|
248
|
-
function placeholderRolldownOptimizerPlugin(name) {
|
|
249
|
-
return {
|
|
250
|
-
name,
|
|
251
|
-
options() {},
|
|
252
|
-
buildStart() {},
|
|
253
|
-
buildEnd() {},
|
|
254
|
-
transform: { filter: { id: /^$/ }, handler() {} }
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* @param {import('../types/options.d.ts').ResolvedOptions} options
|
|
260
|
-
* @returns {Partial<import('../types/options.d.ts').ResolvedOptions>}
|
|
260
|
+
* @param {ResolvedOptions} options
|
|
261
|
+
* @returns {Partial<ResolvedOptions>}
|
|
261
262
|
*/
|
|
262
263
|
function generateSvelteMetadata(options) {
|
|
263
264
|
/** @type {Record<string, any>} */
|