Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate list of svelte exports to optimize and dedupe #877

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-pianos-sit.md
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

fix: dynamically generate list of svelte exports to optimize and dedupe
1 change: 0 additions & 1 deletion packages/e2e-tests/kit-node/__tests__/kit.spec.ts
Expand Up @@ -328,7 +328,6 @@ describe('kit-node', () => {
'svelte/easing',
'svelte/internal',
'svelte/motion',
'svelte/ssr',
'svelte/store',
'svelte/transition',
'svelte',
Expand Down
43 changes: 21 additions & 22 deletions packages/vite-plugin-svelte/src/utils/constants.js
@@ -1,31 +1,30 @@
import { isSvelte5 } from './svelte-version.js';
import { isSvelte4 } from './svelte-version.js';
import { createRequire } from 'node:module';

export const VITE_RESOLVE_MAIN_FIELDS = ['browser', 'module', 'jsnext:main', 'jsnext'];

export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte'];

export const SVELTE_IMPORTS = [
'svelte/animate',
'svelte/easing',
'svelte/internal',
'svelte/internal/disclose-version',
'svelte/motion',
'svelte/ssr',
'svelte/store',
'svelte/transition',
'svelte'
];
export const SVELTE_IMPORTS = Object.entries(
createRequire(import.meta.url)('svelte/package.json').exports
)
.map(([name, config]) => {
// ignore type only
if (typeof config === 'object' && Object.keys(config).length === 1 && config.types) {
return '';
}
// ignore names
if (name === './package.json' || name === './compiler') {
return '';
}
return name.replace(/^\./, 'svelte');
})
.filter((s) => s.length > 0);

export const SVELTE_HMR_IMPORTS = [
'svelte-hmr/runtime/hot-api-esm.js',
'svelte-hmr/runtime/proxy-adapter-dom.js',
'svelte-hmr'
];

if (isSvelte5) {
SVELTE_IMPORTS.push('svelte/server', 'svelte/internal/server', 'svelte/legacy');
SVELTE_HMR_IMPORTS.length = 0; // truncate, svelte-hmr isn't used with svelte5
}
// svelte-hmr isn't used with svelte5
export const SVELTE_HMR_IMPORTS = isSvelte4
? ['svelte-hmr/runtime/hot-api-esm.js', 'svelte-hmr/runtime/proxy-adapter-dom.js', 'svelte-hmr']
: [];

export const SVELTE_EXPORT_CONDITIONS = ['svelte'];

Expand Down
7 changes: 4 additions & 3 deletions packages/vite-plugin-svelte/src/utils/options.js
Expand Up @@ -26,7 +26,7 @@ import {
import { isCommonDepWithoutSvelteField } from './dependencies.js';
import { VitePluginSvelteStats } from './vite-plugin-svelte-stats.js';
import { VitePluginSvelteCache } from './vite-plugin-svelte-cache.js';
import { isSvelte5 } from './svelte-version.js';
import { isSvelte4, isSvelte5 } from './svelte-version.js';

const allowedPluginOptions = new Set([
'include',
Expand Down Expand Up @@ -581,7 +581,9 @@ function buildExtraConfigForSvelte(config) {
const include = [];
const exclude = ['svelte-hmr'];
if (!isDepExcluded('svelte', config.optimizeDeps?.exclude ?? [])) {
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== 'svelte/ssr'); // not used on clientside
const svelteImportsToInclude = SVELTE_IMPORTS.filter(
(x) => isSvelte4 || !x.endsWith('/server')
); // not used on clientside
log.debug(
`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(', ')} `,
undefined,
Expand All @@ -600,7 +602,6 @@ function buildExtraConfigForSvelte(config) {
/** @type {string[]} */
const external = [];
// add svelte to ssr.noExternal unless it is present in ssr.external
// so we can resolve it with svelte/ssr
if (!isDepExternaled('svelte', config.ssr?.external ?? [])) {
noExternal.push('svelte', /^svelte\//);
}
Expand Down