@sveltejs/vite-plugin-svelte
Advanced tools
Comparing version 1.0.0-next.20 to 1.0.0-next.21
@@ -85,2 +85,14 @@ import { Plugin } from 'vite'; | ||
/** | ||
* vite-plugin-svelte automatically handles excluding svelte libraries and reinclusion of their dependencies | ||
* in vite.optimizeDeps. | ||
* | ||
* `disableDependencyReinclusion: true` disables all reinclusions | ||
* `disableDependencyReinclusion: ['foo','bar']` disables reinclusions for dependencies of foo and bar | ||
* | ||
* This should be used for hybrid packages that contain both node and browser dependencies, eg Routify | ||
* | ||
* @default false | ||
*/ | ||
disableDependencyReinclusion?: boolean | string[]; | ||
/** | ||
* These options are considered experimental and breaking changes to them can occur in any release | ||
@@ -87,0 +99,0 @@ */ |
@@ -687,2 +687,3 @@ var __create = Object.create; | ||
"ignorePluginPreprocessors", | ||
"disableDependencyReinclusion", | ||
"experimental" | ||
@@ -789,3 +790,3 @@ ]); | ||
const extraViteConfig = { | ||
optimizeDeps: buildOptimizeDepsForSvelte(svelteDeps, config.optimizeDeps), | ||
optimizeDeps: buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps), | ||
resolve: { | ||
@@ -808,29 +809,32 @@ mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS], | ||
} | ||
function buildOptimizeDepsForSvelte(svelteDeps, optimizeDeps) { | ||
var _a; | ||
function buildOptimizeDepsForSvelte(svelteDeps, options, optimizeDeps) { | ||
const include = []; | ||
const exclude = ["svelte-hmr"]; | ||
const isSvelteExcluded = (_a = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a.includes("svelte"); | ||
if (!isSvelteExcluded) { | ||
const isIncluded = (dep) => { | ||
var _a; | ||
return include.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a.includes(dep)); | ||
}; | ||
const isExcluded = (dep) => { | ||
var _a; | ||
return exclude.includes(dep) || ((_a = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a.some((id) => dep === id || id.startsWith(`${dep}/`))); | ||
}; | ||
if (!isExcluded("svelte")) { | ||
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== "svelte/ssr"); | ||
log.debug(`adding bare svelte packages to optimizeDeps.include: ${svelteImportsToInclude.join(", ")} `); | ||
include.push(...svelteImportsToInclude.filter((x) => { | ||
var _a2; | ||
return !((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a2.includes(x)); | ||
})); | ||
include.push(...svelteImportsToInclude.filter((x) => !isIncluded(x))); | ||
} else { | ||
log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.'); | ||
} | ||
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter((dep) => { | ||
var _a2; | ||
return !((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.include) == null ? void 0 : _a2.includes(dep)); | ||
}); | ||
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter((dep) => !isIncluded(dep)); | ||
log.debug(`automatically excluding found svelte dependencies: ${svelteDepsToExclude.join(", ")}`); | ||
exclude.push(...svelteDepsToExclude.filter((x) => { | ||
var _a2; | ||
return !((_a2 = optimizeDeps == null ? void 0 : optimizeDeps.exclude) == null ? void 0 : _a2.includes(x)); | ||
})); | ||
const transitiveDepsToInclude = svelteDeps.filter((dep) => svelteDepsToExclude.includes(dep.name)).flatMap((dep) => Object.keys(dep.pkg.dependencies || {}).filter((depOfDep) => !svelteDepsToExclude.includes(depOfDep)).map((depOfDep) => dep.path.concat(dep.name, depOfDep).join(" > "))); | ||
log.debug(`reincluding transitive dependencies of excluded svelte dependencies`, transitiveDepsToInclude); | ||
include.push(...transitiveDepsToInclude); | ||
exclude.push(...svelteDepsToExclude.filter((x) => !isExcluded(x))); | ||
if (options.disableDependencyReinclusion !== true) { | ||
const disabledReinclusions = options.disableDependencyReinclusion || []; | ||
if (disabledReinclusions.length > 0) { | ||
log.debug(`not reincluding transitive dependencies of`, disabledReinclusions); | ||
} | ||
const transitiveDepsToInclude = svelteDeps.filter((dep) => !disabledReinclusions.includes(dep.name) && isExcluded(dep.name)).flatMap((dep) => Object.keys(dep.pkg.dependencies || {}).filter((depOfDep) => !isExcluded(depOfDep)).map((depOfDep) => dep.path.concat(dep.name, depOfDep).join(" > "))); | ||
log.debug(`reincluding transitive dependencies of excluded svelte dependencies`, transitiveDepsToInclude); | ||
include.push(...transitiveDepsToInclude); | ||
} | ||
return { include, exclude }; | ||
@@ -837,0 +841,0 @@ } |
{ | ||
"name": "@sveltejs/vite-plugin-svelte", | ||
"version": "1.0.0-next.20", | ||
"version": "1.0.0-next.21", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "author": "dominikg", |
@@ -16,8 +16,9 @@ import { findRootSvelteDependencies } from '../dependencies'; | ||
); | ||
expect(deps).toHaveLength(2); | ||
expect(deps).toHaveLength(3); | ||
expect(deps[0].name).toBe('e2e-test-dep-svelte-nested'); | ||
expect(deps[1].name).toBe('e2e-test-dep-svelte-simple'); | ||
expect(deps[1].path).toEqual(['e2e-test-dep-svelte-nested']); | ||
expect(deps[2].name).toBe('e2e-test-dep-svelte-hybrid'); | ||
}); | ||
}); | ||
}); |
@@ -30,2 +30,3 @@ /* eslint-disable no-unused-vars */ | ||
'ignorePluginPreprocessors', | ||
'disableDependencyReinclusion', | ||
'experimental' | ||
@@ -188,3 +189,3 @@ ]); | ||
const extraViteConfig: Partial<UserConfig> = { | ||
optimizeDeps: buildOptimizeDepsForSvelte(svelteDeps, config.optimizeDeps), | ||
optimizeDeps: buildOptimizeDepsForSvelte(svelteDeps, options, config.optimizeDeps), | ||
resolve: { | ||
@@ -219,2 +220,3 @@ mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS], | ||
svelteDeps: SvelteDependency[], | ||
options: ResolvedOptions, | ||
optimizeDeps?: DepOptimizationOptions | ||
@@ -225,4 +227,12 @@ ): DepOptimizationOptions { | ||
const exclude: string[] = ['svelte-hmr']; | ||
const isSvelteExcluded = optimizeDeps?.exclude?.includes('svelte'); | ||
if (!isSvelteExcluded) { | ||
const isIncluded = (dep: string) => include.includes(dep) || optimizeDeps?.include?.includes(dep); | ||
const isExcluded = (dep: string) => { | ||
return ( | ||
exclude.includes(dep) || | ||
// vite optimizeDeps.exclude works for subpackages too | ||
// see https://github.com/vitejs/vite/blob/c87763c1418d1ba876eae13d139eba83ac6f28b2/packages/vite/src/node/optimizer/scan.ts#L293 | ||
optimizeDeps?.exclude?.some((id) => dep === id || id.startsWith(`${dep}/`)) | ||
); | ||
}; | ||
if (!isExcluded('svelte')) { | ||
const svelteImportsToInclude = SVELTE_IMPORTS.filter((x) => x !== 'svelte/ssr'); // not used on clientside | ||
@@ -232,3 +242,3 @@ log.debug( | ||
); | ||
include.push(...svelteImportsToInclude.filter((x) => !optimizeDeps?.include?.includes(x))); | ||
include.push(...svelteImportsToInclude.filter((x) => !isIncluded(x))); | ||
} else { | ||
@@ -239,19 +249,25 @@ log.debug('"svelte" is excluded in optimizeDeps.exclude, skipped adding it to include.'); | ||
const svelteDepsToExclude = Array.from(new Set(svelteDeps.map((dep) => dep.name))).filter( | ||
(dep) => !optimizeDeps?.include?.includes(dep) | ||
(dep) => !isIncluded(dep) | ||
); | ||
log.debug(`automatically excluding found svelte dependencies: ${svelteDepsToExclude.join(', ')}`); | ||
exclude.push(...svelteDepsToExclude.filter((x) => !optimizeDeps?.exclude?.includes(x))); | ||
exclude.push(...svelteDepsToExclude.filter((x) => !isExcluded(x))); | ||
const transitiveDepsToInclude = svelteDeps | ||
.filter((dep) => svelteDepsToExclude.includes(dep.name)) | ||
.flatMap((dep) => | ||
Object.keys(dep.pkg.dependencies || {}) | ||
.filter((depOfDep) => !svelteDepsToExclude.includes(depOfDep)) | ||
.map((depOfDep) => dep.path.concat(dep.name, depOfDep).join(' > ')) | ||
if (options.disableDependencyReinclusion !== true) { | ||
const disabledReinclusions = options.disableDependencyReinclusion || []; | ||
if (disabledReinclusions.length > 0) { | ||
log.debug(`not reincluding transitive dependencies of`, disabledReinclusions); | ||
} | ||
const transitiveDepsToInclude = svelteDeps | ||
.filter((dep) => !disabledReinclusions.includes(dep.name) && isExcluded(dep.name)) | ||
.flatMap((dep) => | ||
Object.keys(dep.pkg.dependencies || {}) | ||
.filter((depOfDep) => !isExcluded(depOfDep)) | ||
.map((depOfDep) => dep.path.concat(dep.name, depOfDep).join(' > ')) | ||
); | ||
log.debug( | ||
`reincluding transitive dependencies of excluded svelte dependencies`, | ||
transitiveDepsToInclude | ||
); | ||
log.debug( | ||
`reincluding transitive dependencies of excluded svelte dependencies`, | ||
transitiveDepsToInclude | ||
); | ||
include.push(...transitiveDepsToInclude); | ||
include.push(...transitiveDepsToInclude); | ||
} | ||
@@ -374,2 +390,15 @@ return { include, exclude }; | ||
/** | ||
* vite-plugin-svelte automatically handles excluding svelte libraries and reinclusion of their dependencies | ||
* in vite.optimizeDeps. | ||
* | ||
* `disableDependencyReinclusion: true` disables all reinclusions | ||
* `disableDependencyReinclusion: ['foo','bar']` disables reinclusions for dependencies of foo and bar | ||
* | ||
* This should be used for hybrid packages that contain both node and browser dependencies, eg Routify | ||
* | ||
* @default false | ||
*/ | ||
disableDependencyReinclusion?: boolean | string[]; | ||
/** | ||
* These options are considered experimental and breaking changes to them can occur in any release | ||
@@ -376,0 +405,0 @@ */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
361125
4848