@sveltejs/vite-plugin-svelte 1.0.0-next.9 → 1.0.2
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/README.md +16 -50
- package/dist/index.cjs +2014 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +231 -214
- package/dist/index.js +1522 -395
- package/dist/index.js.map +1 -0
- package/package.json +48 -33
- package/src/handle-hot-update.ts +117 -0
- package/src/index.ts +242 -0
- package/src/ui/inspector/Inspector.svelte +245 -0
- package/src/ui/inspector/load-inspector.js +15 -0
- package/src/ui/inspector/plugin.ts +106 -0
- package/src/utils/__tests__/dependencies.spec.ts +43 -0
- package/src/utils/__tests__/sourcemap.spec.ts +25 -0
- package/src/utils/compile.ts +159 -0
- package/src/utils/constants.ts +20 -0
- package/src/utils/dependencies.ts +241 -0
- package/src/utils/error.ts +95 -0
- package/src/utils/esbuild.ts +84 -0
- package/src/utils/hash.ts +32 -0
- package/src/utils/id.ts +135 -0
- package/src/utils/load-svelte-config.ts +115 -0
- package/src/utils/log.ts +211 -0
- package/src/utils/optimizer.ts +45 -0
- package/src/utils/options.ts +707 -0
- package/src/utils/preprocess.ts +252 -0
- package/src/utils/resolve.ts +57 -0
- package/src/utils/sourcemap.ts +58 -0
- package/src/utils/vite-plugin-svelte-cache.ts +127 -0
- package/src/utils/watch.ts +110 -0
- package/CHANGELOG.md +0 -44
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
import {
|
|
3
|
+
ConfigEnv,
|
|
4
|
+
DepOptimizationOptions,
|
|
5
|
+
ResolvedConfig,
|
|
6
|
+
UserConfig,
|
|
7
|
+
ViteDevServer,
|
|
8
|
+
normalizePath
|
|
9
|
+
} from 'vite';
|
|
10
|
+
import { log } from './log';
|
|
11
|
+
import { loadSvelteConfig } from './load-svelte-config';
|
|
12
|
+
import { SVELTE_HMR_IMPORTS, SVELTE_IMPORTS, SVELTE_RESOLVE_MAIN_FIELDS } from './constants';
|
|
13
|
+
// eslint-disable-next-line node/no-missing-import
|
|
14
|
+
import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
|
|
15
|
+
import type {
|
|
16
|
+
MarkupPreprocessor,
|
|
17
|
+
Preprocessor,
|
|
18
|
+
PreprocessorGroup,
|
|
19
|
+
Processed
|
|
20
|
+
// eslint-disable-next-line node/no-missing-import
|
|
21
|
+
} from 'svelte/types/compiler/preprocess';
|
|
22
|
+
|
|
23
|
+
import path from 'path';
|
|
24
|
+
import { findRootSvelteDependencies, needsOptimization, SvelteDependency } from './dependencies';
|
|
25
|
+
import { createRequire } from 'module';
|
|
26
|
+
import { esbuildSveltePlugin, facadeEsbuildSveltePluginName } from './esbuild';
|
|
27
|
+
import { addExtraPreprocessors } from './preprocess';
|
|
28
|
+
import deepmerge from 'deepmerge';
|
|
29
|
+
|
|
30
|
+
const allowedPluginOptions = new Set([
|
|
31
|
+
'include',
|
|
32
|
+
'exclude',
|
|
33
|
+
'emitCss',
|
|
34
|
+
'hot',
|
|
35
|
+
'ignorePluginPreprocessors',
|
|
36
|
+
'disableDependencyReinclusion',
|
|
37
|
+
'experimental'
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
const knownRootOptions = new Set(['extensions', 'compilerOptions', 'preprocess', 'onwarn']);
|
|
41
|
+
|
|
42
|
+
const allowedInlineOptions = new Set([
|
|
43
|
+
'configFile',
|
|
44
|
+
'kit', // only for internal use by sveltekit
|
|
45
|
+
...allowedPluginOptions,
|
|
46
|
+
...knownRootOptions
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
export function validateInlineOptions(inlineOptions?: Partial<Options>) {
|
|
50
|
+
const invalidKeys = Object.keys(inlineOptions || {}).filter(
|
|
51
|
+
(key) => !allowedInlineOptions.has(key)
|
|
52
|
+
);
|
|
53
|
+
if (invalidKeys.length) {
|
|
54
|
+
log.warn(`invalid plugin options "${invalidKeys.join(', ')}" in inline config`, inlineOptions);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function convertPluginOptions(config?: Partial<SvelteOptions>): Partial<Options> | undefined {
|
|
59
|
+
if (!config) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const invalidRootOptions = Object.keys(config).filter((key) => allowedPluginOptions.has(key));
|
|
63
|
+
if (invalidRootOptions.length > 0) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Invalid options in svelte config. Move the following options into 'vitePlugin:{...}': ${invalidRootOptions.join(
|
|
66
|
+
', '
|
|
67
|
+
)}`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
if (!config.vitePlugin) {
|
|
71
|
+
return config;
|
|
72
|
+
}
|
|
73
|
+
const pluginOptions = config.vitePlugin;
|
|
74
|
+
const pluginOptionKeys = Object.keys(pluginOptions);
|
|
75
|
+
|
|
76
|
+
const rootOptionsInPluginOptions = pluginOptionKeys.filter((key) => knownRootOptions.has(key));
|
|
77
|
+
if (rootOptionsInPluginOptions.length > 0) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Invalid options in svelte config under vitePlugin:{...}', move them to the config root : ${rootOptionsInPluginOptions.join(
|
|
80
|
+
', '
|
|
81
|
+
)}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const duplicateOptions = pluginOptionKeys.filter((key) =>
|
|
85
|
+
Object.prototype.hasOwnProperty.call(config, key)
|
|
86
|
+
);
|
|
87
|
+
if (duplicateOptions.length > 0) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
`Invalid duplicate options in svelte config under vitePlugin:{...}', they are defined in root too and must only exist once: ${duplicateOptions.join(
|
|
90
|
+
', '
|
|
91
|
+
)}`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const unknownPluginOptions = pluginOptionKeys.filter((key) => !allowedPluginOptions.has(key));
|
|
95
|
+
if (unknownPluginOptions.length > 0) {
|
|
96
|
+
log.warn(
|
|
97
|
+
`ignoring unknown plugin options in svelte config under vitePlugin:{...}: ${unknownPluginOptions.join(
|
|
98
|
+
', '
|
|
99
|
+
)}`
|
|
100
|
+
);
|
|
101
|
+
unknownPluginOptions.forEach((unkownOption) => {
|
|
102
|
+
// @ts-ignore
|
|
103
|
+
delete pluginOptions[unkownOption];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const result: Options = {
|
|
108
|
+
...config,
|
|
109
|
+
...pluginOptions
|
|
110
|
+
};
|
|
111
|
+
// @ts-expect-error it exists
|
|
112
|
+
delete result.vitePlugin;
|
|
113
|
+
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// used in config phase, merges the default options, svelte config, and inline options
|
|
118
|
+
export async function preResolveOptions(
|
|
119
|
+
inlineOptions: Partial<Options> = {},
|
|
120
|
+
viteUserConfig: UserConfig,
|
|
121
|
+
viteEnv: ConfigEnv
|
|
122
|
+
): Promise<PreResolvedOptions> {
|
|
123
|
+
const viteConfigWithResolvedRoot: UserConfig = {
|
|
124
|
+
...viteUserConfig,
|
|
125
|
+
root: resolveViteRoot(viteUserConfig)
|
|
126
|
+
};
|
|
127
|
+
const defaultOptions: Partial<Options> = {
|
|
128
|
+
extensions: ['.svelte'],
|
|
129
|
+
emitCss: true
|
|
130
|
+
};
|
|
131
|
+
const svelteConfig = convertPluginOptions(
|
|
132
|
+
await loadSvelteConfig(viteConfigWithResolvedRoot, inlineOptions)
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const extraOptions: Partial<PreResolvedOptions> = {
|
|
136
|
+
root: viteConfigWithResolvedRoot.root!,
|
|
137
|
+
isBuild: viteEnv.command === 'build',
|
|
138
|
+
isServe: viteEnv.command === 'serve',
|
|
139
|
+
isDebug: process.env.DEBUG != null
|
|
140
|
+
};
|
|
141
|
+
const merged = mergeConfigs<Partial<PreResolvedOptions> | undefined>(
|
|
142
|
+
defaultOptions,
|
|
143
|
+
svelteConfig,
|
|
144
|
+
inlineOptions,
|
|
145
|
+
extraOptions
|
|
146
|
+
);
|
|
147
|
+
// configFile of svelteConfig contains the absolute path it was loaded from,
|
|
148
|
+
// prefer it over the possibly relative inline path
|
|
149
|
+
if (svelteConfig?.configFile) {
|
|
150
|
+
merged.configFile = svelteConfig.configFile;
|
|
151
|
+
}
|
|
152
|
+
return merged;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function mergeConfigs<T>(...configs: T[]): ResolvedOptions {
|
|
156
|
+
let result = {};
|
|
157
|
+
for (const config of configs.filter(Boolean)) {
|
|
158
|
+
result = deepmerge<T>(result, config, {
|
|
159
|
+
// replace arrays
|
|
160
|
+
arrayMerge: (target: any[], source: any[]) => source ?? target
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return result as ResolvedOptions;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// used in configResolved phase, merges a contextual default config, pre-resolved options, and some preprocessors.
|
|
167
|
+
// also validates the final config.
|
|
168
|
+
export function resolveOptions(
|
|
169
|
+
preResolveOptions: PreResolvedOptions,
|
|
170
|
+
viteConfig: ResolvedConfig
|
|
171
|
+
): ResolvedOptions {
|
|
172
|
+
const defaultOptions: Partial<Options> = {
|
|
173
|
+
hot: viteConfig.isProduction ? false : { injectCss: !preResolveOptions.emitCss },
|
|
174
|
+
compilerOptions: {
|
|
175
|
+
css: !preResolveOptions.emitCss,
|
|
176
|
+
dev: !viteConfig.isProduction
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
const extraOptions: Partial<ResolvedOptions> = {
|
|
180
|
+
root: viteConfig.root,
|
|
181
|
+
isProduction: viteConfig.isProduction
|
|
182
|
+
};
|
|
183
|
+
const merged: ResolvedOptions = mergeConfigs(defaultOptions, preResolveOptions, extraOptions);
|
|
184
|
+
|
|
185
|
+
removeIgnoredOptions(merged);
|
|
186
|
+
addSvelteKitOptions(merged);
|
|
187
|
+
addExtraPreprocessors(merged, viteConfig);
|
|
188
|
+
enforceOptionsForHmr(merged);
|
|
189
|
+
enforceOptionsForProduction(merged);
|
|
190
|
+
return merged;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function enforceOptionsForHmr(options: ResolvedOptions) {
|
|
194
|
+
if (options.hot) {
|
|
195
|
+
if (!options.compilerOptions.dev) {
|
|
196
|
+
log.warn('hmr is enabled but compilerOptions.dev is false, forcing it to true');
|
|
197
|
+
options.compilerOptions.dev = true;
|
|
198
|
+
}
|
|
199
|
+
if (options.emitCss) {
|
|
200
|
+
if (options.hot !== true && options.hot.injectCss) {
|
|
201
|
+
log.warn('hmr and emitCss are enabled but hot.injectCss is true, forcing it to false');
|
|
202
|
+
options.hot.injectCss = false;
|
|
203
|
+
}
|
|
204
|
+
if (options.compilerOptions.css) {
|
|
205
|
+
log.warn(
|
|
206
|
+
'hmr and emitCss are enabled but compilerOptions.css is true, forcing it to false'
|
|
207
|
+
);
|
|
208
|
+
options.compilerOptions.css = false;
|
|
209
|
+
}
|
|
210
|
+
} else {
|
|
211
|
+
if (options.hot === true || !options.hot.injectCss) {
|
|
212
|
+
log.warn(
|
|
213
|
+
'hmr with emitCss disabled requires option hot.injectCss to be enabled, forcing it to true'
|
|
214
|
+
);
|
|
215
|
+
if (options.hot === true) {
|
|
216
|
+
options.hot = { injectCss: true };
|
|
217
|
+
} else {
|
|
218
|
+
options.hot.injectCss = true;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (!options.compilerOptions.css) {
|
|
222
|
+
log.warn(
|
|
223
|
+
'hmr with emitCss disabled requires compilerOptions.css to be enabled, forcing it to true'
|
|
224
|
+
);
|
|
225
|
+
options.compilerOptions.css = true;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function enforceOptionsForProduction(options: ResolvedOptions) {
|
|
232
|
+
if (options.isProduction) {
|
|
233
|
+
if (options.hot) {
|
|
234
|
+
log.warn('options.hot is enabled but does not work on production build, forcing it to false');
|
|
235
|
+
options.hot = false;
|
|
236
|
+
}
|
|
237
|
+
if (options.compilerOptions.dev) {
|
|
238
|
+
log.warn(
|
|
239
|
+
'you are building for production but compilerOptions.dev is true, forcing it to false'
|
|
240
|
+
);
|
|
241
|
+
options.compilerOptions.dev = false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function removeIgnoredOptions(options: ResolvedOptions) {
|
|
247
|
+
const ignoredCompilerOptions = ['generate', 'format', 'filename'];
|
|
248
|
+
if (options.hot && options.emitCss) {
|
|
249
|
+
ignoredCompilerOptions.push('cssHash');
|
|
250
|
+
}
|
|
251
|
+
const passedCompilerOptions = Object.keys(options.compilerOptions || {});
|
|
252
|
+
const passedIgnored = passedCompilerOptions.filter((o) => ignoredCompilerOptions.includes(o));
|
|
253
|
+
if (passedIgnored.length) {
|
|
254
|
+
log.warn(
|
|
255
|
+
`The following Svelte compilerOptions are controlled by vite-plugin-svelte and essential to its functionality. User-specified values are ignored. Please remove them from your configuration: ${passedIgnored.join(
|
|
256
|
+
', '
|
|
257
|
+
)}`
|
|
258
|
+
);
|
|
259
|
+
passedIgnored.forEach((ignored) => {
|
|
260
|
+
// @ts-expect-error string access
|
|
261
|
+
delete options.compilerOptions[ignored];
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// some SvelteKit options need compilerOptions to work, so set them here.
|
|
267
|
+
function addSvelteKitOptions(options: ResolvedOptions) {
|
|
268
|
+
// @ts-expect-error kit is not typed to avoid dependency on sveltekit
|
|
269
|
+
if (options?.kit != null) {
|
|
270
|
+
// @ts-expect-error kit is not typed to avoid dependency on sveltekit
|
|
271
|
+
const kit_browser_hydrate = options.kit.browser?.hydrate;
|
|
272
|
+
const hydratable = kit_browser_hydrate !== false;
|
|
273
|
+
if (
|
|
274
|
+
options.compilerOptions.hydratable != null &&
|
|
275
|
+
options.compilerOptions.hydratable !== hydratable
|
|
276
|
+
) {
|
|
277
|
+
log.warn(
|
|
278
|
+
`Conflicting values "compilerOptions.hydratable: ${options.compilerOptions.hydratable}" and "kit.browser.hydrate: ${kit_browser_hydrate}" in your svelte config. You should remove "compilerOptions.hydratable".`
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
log.debug(`Setting compilerOptions.hydratable: ${hydratable} for SvelteKit`);
|
|
282
|
+
options.compilerOptions.hydratable = hydratable;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// vite passes unresolved `root`option to config hook but we need the resolved value, so do it here
|
|
287
|
+
// https://github.com/sveltejs/vite-plugin-svelte/issues/113
|
|
288
|
+
// https://github.com/vitejs/vite/blob/43c957de8a99bb326afd732c962f42127b0a4d1e/packages/vite/src/node/config.ts#L293
|
|
289
|
+
function resolveViteRoot(viteConfig: UserConfig): string | undefined {
|
|
290
|
+
return normalizePath(viteConfig.root ? path.resolve(viteConfig.root) : process.cwd());
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function buildExtraViteConfig(
|
|
294
|
+
options: PreResolvedOptions,
|
|
295
|
+
config: UserConfig
|
|
296
|
+
): Partial<UserConfig> {
|
|
297
|
+
// extra handling for svelte dependencies in the project
|
|
298
|
+
const svelteDeps = findRootSvelteDependencies(options.root);
|
|
299
|
+
const extraViteConfig: Partial<UserConfig> = {
|
|
300
|
+
resolve: {
|
|
301
|
+
mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
|
|
302
|
+
dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS]
|
|
303
|
+
}
|
|
304
|
+
// this option is still awaiting a PR in vite to be supported
|
|
305
|
+
// see https://github.com/sveltejs/vite-plugin-svelte/issues/60
|
|
306
|
+
// @ts-ignore
|
|
307
|
+
// knownJsSrcExtensions: options.extensions
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(
|
|
311
|
+
svelteDeps,
|
|
312
|
+
options,
|
|
313
|
+
config.optimizeDeps
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
if (options.experimental?.prebundleSvelteLibraries) {
|
|
317
|
+
extraViteConfig.optimizeDeps = {
|
|
318
|
+
...extraViteConfig.optimizeDeps,
|
|
319
|
+
// Experimental Vite API to allow these extensions to be scanned and prebundled
|
|
320
|
+
// @ts-ignore
|
|
321
|
+
extensions: options.extensions ?? ['.svelte'],
|
|
322
|
+
// Add esbuild plugin to prebundle Svelte files.
|
|
323
|
+
// Currently a placeholder as more information is needed after Vite config is resolved,
|
|
324
|
+
// the real Svelte plugin is added in `patchResolvedViteConfig()`
|
|
325
|
+
esbuildOptions: {
|
|
326
|
+
plugins: [{ name: facadeEsbuildSveltePluginName, setup: () => {} }]
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// @ts-ignore
|
|
332
|
+
extraViteConfig.ssr = buildSSROptionsForSvelte(svelteDeps, options, config, extraViteConfig);
|
|
333
|
+
|
|
334
|
+
return extraViteConfig;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function buildOptimizeDepsForSvelte(
|
|
338
|
+
svelteDeps: SvelteDependency[],
|
|
339
|
+
options: PreResolvedOptions,
|
|
340
|
+
optimizeDeps?: DepOptimizationOptions
|
|
341
|
+
): DepOptimizationOptions {
|
|
342
|
+
// include svelte imports for optimization unless explicitly excluded
|
|
343
|
+
const include: string[] = [];
|
|
344
|
+
const exclude: string[] = ['svelte-hmr'];
|
|
345
|
+
const isIncluded = (dep: string) => include.includes(dep) || optimizeDeps?.include?.includes(dep);
|
|
346
|
+
const isExcluded = (dep: string) => {
|
|
347
|
+
return (
|
|
348
|
+
exclude.includes(dep) ||
|
|
349
|
+
// vite optimizeDeps.exclude works for subpackages too
|
|
350
|
+
// see https://github.com/vitejs/vite/blob/c87763c1418d1ba876eae13d139eba83ac6f28b2/packages/vite/src/node/optimizer/scan.ts#L293
|
|
351
|
+
optimizeDeps?.exclude?.some((id: string) => dep === id || id.startsWith(`${dep}/`))
|
|
352
|
+
);
|
|
353
|
+
};
|
|
354
|
+
if (!isExcluded('svelte')) {
|
|
355
|
+
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== 'svelte/ssr'); // not used on clientside
|
|
356
|
+
log.debug(
|
|
357
|
+
`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `
|
|
358
|
+
);
|
|
359
|
+
include.push(...svelteImportsToInclude.filter((x) => !isIncluded(x)));
|
|
360
|
+
} else {
|
|
361
|
+
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.');
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// If we prebundle svelte libraries, we can skip the whole prebundling dance below
|
|
365
|
+
if (options.experimental?.prebundleSvelteLibraries) {
|
|
366
|
+
return { include, exclude };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// only svelte component libraries needs to be processed for optimizeDeps, js libraries work fine
|
|
370
|
+
svelteDeps = svelteDeps.filter((dep) => dep.type === 'component-library');
|
|
371
|
+
|
|
372
|
+
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter(
|
|
373
|
+
(dep) => !isIncluded(dep)
|
|
374
|
+
);
|
|
375
|
+
log.debug(`automatically excluding found svelte dependencies: ${svelteDepsToExclude.join(', ')}`);
|
|
376
|
+
exclude.push(...svelteDepsToExclude.filter((x) => !isExcluded(x)));
|
|
377
|
+
|
|
378
|
+
if (options.disableDependencyReinclusion !== true) {
|
|
379
|
+
const disabledReinclusions = options.disableDependencyReinclusion || [];
|
|
380
|
+
if (disabledReinclusions.length > 0) {
|
|
381
|
+
log.debug(`not reincluding transitive dependencies of`, disabledReinclusions);
|
|
382
|
+
}
|
|
383
|
+
const transitiveDepsToInclude = svelteDeps
|
|
384
|
+
.filter((dep) => !disabledReinclusions.includes(dep.name) && isExcluded(dep.name))
|
|
385
|
+
.flatMap((dep) => {
|
|
386
|
+
const localRequire = createRequire(`${dep.dir}/package.json`);
|
|
387
|
+
return Object.keys(dep.pkg.dependencies || {})
|
|
388
|
+
.filter((depOfDep) => !isExcluded(depOfDep) && needsOptimization(depOfDep, localRequire))
|
|
389
|
+
.map((depOfDep) => dep.path.concat(dep.name, depOfDep).join(' > '));
|
|
390
|
+
});
|
|
391
|
+
log.debug(
|
|
392
|
+
`reincluding transitive dependencies of excluded svelte dependencies`,
|
|
393
|
+
transitiveDepsToInclude
|
|
394
|
+
);
|
|
395
|
+
include.push(...transitiveDepsToInclude);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return { include, exclude };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function buildSSROptionsForSvelte(
|
|
402
|
+
svelteDeps: SvelteDependency[],
|
|
403
|
+
options: ResolvedOptions,
|
|
404
|
+
config: UserConfig
|
|
405
|
+
): any {
|
|
406
|
+
const noExternal: (string | RegExp)[] = [];
|
|
407
|
+
|
|
408
|
+
// add svelte to ssr.noExternal unless it is present in ssr.external
|
|
409
|
+
// so we can resolve it with svelte/ssr
|
|
410
|
+
if (!config.ssr?.external?.includes('svelte')) {
|
|
411
|
+
noExternal.push('svelte', /^svelte\//);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// add svelte dependencies to ssr.noExternal unless present in ssr.external
|
|
415
|
+
noExternal.push(
|
|
416
|
+
...Array.from(new Set(svelteDeps.map((s) => s.name))).filter(
|
|
417
|
+
(x) => !config.ssr?.external?.includes(x)
|
|
418
|
+
)
|
|
419
|
+
);
|
|
420
|
+
const ssr = {
|
|
421
|
+
noExternal,
|
|
422
|
+
external: [] as string[]
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
if (options.isServe) {
|
|
426
|
+
// during dev, we have to externalize transitive dependencies, see https://github.com/sveltejs/vite-plugin-svelte/issues/281
|
|
427
|
+
ssr.external = Array.from(
|
|
428
|
+
new Set(svelteDeps.flatMap((dep) => Object.keys(dep.pkg.dependencies || {})))
|
|
429
|
+
).filter(
|
|
430
|
+
(dep) =>
|
|
431
|
+
!ssr.noExternal.includes(dep) &&
|
|
432
|
+
// TODO noExternal can be something different than a string array
|
|
433
|
+
//!config.ssr?.noExternal?.includes(dep) &&
|
|
434
|
+
!config.ssr?.external?.includes(dep)
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return ssr;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
export function patchResolvedViteConfig(viteConfig: ResolvedConfig, options: ResolvedOptions) {
|
|
442
|
+
const facadeEsbuildSveltePlugin = viteConfig.optimizeDeps.esbuildOptions?.plugins?.find(
|
|
443
|
+
(plugin) => plugin.name === facadeEsbuildSveltePluginName
|
|
444
|
+
);
|
|
445
|
+
if (facadeEsbuildSveltePlugin) {
|
|
446
|
+
Object.assign(facadeEsbuildSveltePlugin, esbuildSveltePlugin(options));
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export type Options = Omit<SvelteOptions, 'vitePlugin'> & PluginOptionsInline;
|
|
451
|
+
|
|
452
|
+
interface PluginOptionsInline extends PluginOptions {
|
|
453
|
+
/**
|
|
454
|
+
* Path to a svelte config file, either absolute or relative to Vite root
|
|
455
|
+
*
|
|
456
|
+
* set to `false` to ignore the svelte config file
|
|
457
|
+
*
|
|
458
|
+
* @see https://vitejs.dev/config/#root
|
|
459
|
+
*/
|
|
460
|
+
configFile?: string | false;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export interface PluginOptions {
|
|
464
|
+
/**
|
|
465
|
+
* A `picomatch` pattern, or array of patterns, which specifies the files the plugin should
|
|
466
|
+
* operate on. By default, all svelte files are included.
|
|
467
|
+
*
|
|
468
|
+
* @see https://github.com/micromatch/picomatch
|
|
469
|
+
*/
|
|
470
|
+
include?: Arrayable<string>;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* A `picomatch` pattern, or array of patterns, which specifies the files to be ignored by the
|
|
474
|
+
* plugin. By default, no files are ignored.
|
|
475
|
+
*
|
|
476
|
+
* @see https://github.com/micromatch/picomatch
|
|
477
|
+
*/
|
|
478
|
+
exclude?: Arrayable<string>;
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Emit Svelte styles as virtual CSS files for Vite and other plugins to process
|
|
482
|
+
*
|
|
483
|
+
* @default true
|
|
484
|
+
*/
|
|
485
|
+
emitCss?: boolean;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Enable or disable Hot Module Replacement.
|
|
489
|
+
*
|
|
490
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
491
|
+
*
|
|
492
|
+
* DO NOT CUSTOMIZE SVELTE-HMR OPTIONS UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING
|
|
493
|
+
*
|
|
494
|
+
* YOU HAVE BEEN WARNED
|
|
495
|
+
*
|
|
496
|
+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
497
|
+
*
|
|
498
|
+
* Set an object to pass custom options to svelte-hmr
|
|
499
|
+
*
|
|
500
|
+
* @see https://github.com/rixo/svelte-hmr#options
|
|
501
|
+
* @default true for development, always false for production
|
|
502
|
+
*/
|
|
503
|
+
hot?: boolean | { injectCss?: boolean; [key: string]: any };
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Some Vite plugins can contribute additional preprocessors by defining `api.sveltePreprocess`.
|
|
507
|
+
* If you don't want to use them, set this to true to ignore them all or use an array of strings
|
|
508
|
+
* with plugin names to specify which.
|
|
509
|
+
*
|
|
510
|
+
* @default false
|
|
511
|
+
*/
|
|
512
|
+
ignorePluginPreprocessors?: boolean | string[];
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* vite-plugin-svelte automatically handles excluding svelte libraries and reinclusion of their dependencies
|
|
516
|
+
* in vite.optimizeDeps.
|
|
517
|
+
*
|
|
518
|
+
* `disableDependencyReinclusion: true` disables all reinclusions
|
|
519
|
+
* `disableDependencyReinclusion: ['foo','bar']` disables reinclusions for dependencies of foo and bar
|
|
520
|
+
*
|
|
521
|
+
* This should be used for hybrid packages that contain both node and browser dependencies, eg Routify
|
|
522
|
+
*
|
|
523
|
+
* @default false
|
|
524
|
+
*/
|
|
525
|
+
disableDependencyReinclusion?: boolean | string[];
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* These options are considered experimental and breaking changes to them can occur in any release
|
|
529
|
+
*/
|
|
530
|
+
experimental?: ExperimentalOptions;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export interface SvelteOptions {
|
|
534
|
+
/**
|
|
535
|
+
* A list of file extensions to be compiled by Svelte
|
|
536
|
+
*
|
|
537
|
+
* @default ['.svelte']
|
|
538
|
+
*/
|
|
539
|
+
extensions?: string[];
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* An array of preprocessors to transform the Svelte source code before compilation
|
|
543
|
+
*
|
|
544
|
+
* @see https://svelte.dev/docs#svelte_preprocess
|
|
545
|
+
*/
|
|
546
|
+
preprocess?: Arrayable<PreprocessorGroup>;
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* The options to be passed to the Svelte compiler. A few options are set by default,
|
|
550
|
+
* including `dev` and `css`. However, some options are non-configurable, like
|
|
551
|
+
* `filename`, `format`, `generate`, and `cssHash` (in dev).
|
|
552
|
+
*
|
|
553
|
+
* @see https://svelte.dev/docs#svelte_compile
|
|
554
|
+
*/
|
|
555
|
+
compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Handles warning emitted from the Svelte compiler
|
|
559
|
+
*/
|
|
560
|
+
onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Options for vite-plugin-svelte
|
|
564
|
+
*/
|
|
565
|
+
vitePlugin?: PluginOptions;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* These options are considered experimental and breaking changes to them can occur in any release
|
|
570
|
+
*/
|
|
571
|
+
export interface ExperimentalOptions {
|
|
572
|
+
/**
|
|
573
|
+
* Use extra preprocessors that delegate style and TypeScript preprocessing to native Vite plugins
|
|
574
|
+
*
|
|
575
|
+
* Do not use together with `svelte-preprocess`!
|
|
576
|
+
*
|
|
577
|
+
* @default false
|
|
578
|
+
*/
|
|
579
|
+
useVitePreprocess?: boolean;
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* Force Vite to pre-bundle Svelte libraries
|
|
583
|
+
*
|
|
584
|
+
* @default false
|
|
585
|
+
*/
|
|
586
|
+
prebundleSvelteLibraries?: boolean;
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* If a preprocessor does not provide a sourcemap, a best-effort fallback sourcemap will be provided.
|
|
590
|
+
* This option requires `diff-match-patch` to be installed as a peer dependency.
|
|
591
|
+
*
|
|
592
|
+
* @see https://github.com/google/diff-match-patch
|
|
593
|
+
* @default false
|
|
594
|
+
*/
|
|
595
|
+
generateMissingPreprocessorSourcemaps?: boolean;
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* A function to update `compilerOptions` before compilation
|
|
599
|
+
*
|
|
600
|
+
* `data.filename` - The file to be compiled
|
|
601
|
+
* `data.code` - The preprocessed Svelte code
|
|
602
|
+
* `data.compileOptions` - The current compiler options
|
|
603
|
+
*
|
|
604
|
+
* To change part of the compiler options, return an object with the changes you need.
|
|
605
|
+
*
|
|
606
|
+
* @example
|
|
607
|
+
* ```
|
|
608
|
+
* ({ filename, compileOptions }) => {
|
|
609
|
+
* // Dynamically set hydration per Svelte file
|
|
610
|
+
* if (compileWithHydratable(filename) && !compileOptions.hydratable) {
|
|
611
|
+
* return { hydratable: true };
|
|
612
|
+
* }
|
|
613
|
+
* }
|
|
614
|
+
* ```
|
|
615
|
+
*/
|
|
616
|
+
dynamicCompileOptions?: (data: {
|
|
617
|
+
filename: string;
|
|
618
|
+
code: string;
|
|
619
|
+
compileOptions: Partial<CompileOptions>;
|
|
620
|
+
}) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* enable svelte inspector
|
|
624
|
+
*/
|
|
625
|
+
inspector?: InspectorOptions | boolean;
|
|
626
|
+
|
|
627
|
+
/**
|
|
628
|
+
* send a websocket message with svelte compiler warnings during dev
|
|
629
|
+
*
|
|
630
|
+
*/
|
|
631
|
+
sendWarningsToBrowser?: boolean;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
export interface InspectorOptions {
|
|
635
|
+
/**
|
|
636
|
+
* define a key combo to toggle inspector,
|
|
637
|
+
* @default 'control-shift' on windows, 'meta-shift' on other os
|
|
638
|
+
*
|
|
639
|
+
* any number of modifiers `control` `shift` `alt` `meta` followed by zero or one regular key, separated by -
|
|
640
|
+
* examples: control-shift, control-o, control-alt-s meta-x control-meta
|
|
641
|
+
* Some keys have native behavior (e.g. alt-s opens history menu on firefox).
|
|
642
|
+
* To avoid conflicts or accidentally typing into inputs, modifier only combinations are recommended.
|
|
643
|
+
*/
|
|
644
|
+
toggleKeyCombo?: string;
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
|
|
648
|
+
* @default false
|
|
649
|
+
*/
|
|
650
|
+
holdMode?: boolean;
|
|
651
|
+
/**
|
|
652
|
+
* when to show the toggle button
|
|
653
|
+
* @default 'active'
|
|
654
|
+
*/
|
|
655
|
+
showToggleButton?: 'always' | 'active' | 'never';
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* where to display the toggle button
|
|
659
|
+
* @default top-right
|
|
660
|
+
*/
|
|
661
|
+
toggleButtonPos?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* inject custom styles when inspector is active
|
|
665
|
+
*/
|
|
666
|
+
customStyles?: boolean;
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* append an import to the module id ending with `appendTo` instead of adding a script into body
|
|
670
|
+
* useful for frameworks that do not support trannsformIndexHtml hook
|
|
671
|
+
*
|
|
672
|
+
* WARNING: only set this if you know exactly what it does.
|
|
673
|
+
* Regular users of vite-plugin-svelte or SvelteKit do not need it
|
|
674
|
+
*/
|
|
675
|
+
appendTo?: string;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
export interface PreResolvedOptions extends Options {
|
|
679
|
+
// these options are non-nullable after resolve
|
|
680
|
+
compilerOptions: CompileOptions;
|
|
681
|
+
experimental?: ExperimentalOptions;
|
|
682
|
+
// extra options
|
|
683
|
+
root: string;
|
|
684
|
+
isBuild: boolean;
|
|
685
|
+
isServe: boolean;
|
|
686
|
+
isDebug: boolean;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
export interface ResolvedOptions extends PreResolvedOptions {
|
|
690
|
+
isProduction: boolean;
|
|
691
|
+
server?: ViteDevServer;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
export type {
|
|
695
|
+
CompileOptions,
|
|
696
|
+
Processed,
|
|
697
|
+
MarkupPreprocessor,
|
|
698
|
+
Preprocessor,
|
|
699
|
+
PreprocessorGroup,
|
|
700
|
+
Warning
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
export type ModuleFormat = NonNullable<CompileOptions['format']>;
|
|
704
|
+
|
|
705
|
+
export type CssHashGetter = NonNullable<CompileOptions['cssHash']>;
|
|
706
|
+
|
|
707
|
+
export type Arrayable<T> = T | T[];
|