Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-beds-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

feat: resolve `svelte` in `exports`. prefer `exports` to `svelte` field
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"package.json"
],
"exports": {
"./package.json": "./package.json"
"./package.json": "./package.json",
".": {
"svelte": "./src/index.js"
}
},
"dependencies": {
"e2e-test-dep-svelte-simple": "workspace:*",
Expand Down
18 changes: 1 addition & 17 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';

import { ensureWatchedFile, setupWatchers } from './utils/watch';
import { resolveViaPackageJsonSvelte } from './utils/resolve';
import { PartialResolvedId } from 'rollup';
import { toRollupError } from './utils/error';
import { saveSvelteMetadata } from './utils/optimizer';
Expand Down Expand Up @@ -67,6 +66,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
}
// @ts-expect-error temporarily lend the options variable until fixed in configResolved
options = await preResolveOptions(inlineOptions, config, configEnv);

// extra vite config
const extraViteConfig = buildExtraViteConfig(options, config);
log.debug('additional vite config', extraViteConfig);
Expand Down Expand Up @@ -154,22 +154,6 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin[] {
}
return resolvedSvelteSSR;
}
try {
const resolved = resolveViaPackageJsonSvelte(importee, importer, cache);
if (resolved) {
log.debug(
`resolveId resolved ${resolved} via package.json svelte field of ${importee}`
);
return resolved;
}
} catch (e) {
log.debug.once(
`error trying to resolve ${importee} from ${importer} via package.json svelte field `,
e
);
// this error most likely happens due to non-svelte related importee/importers so swallow it here
// in case it really way a svelte library, users will notice anyway. (lib not working due to failed resolve)
}
},

async transform(code, id, opts) {
Expand Down
2 changes: 2 additions & 0 deletions packages/vite-plugin-svelte/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const VITE_RESOLVE_MAIN_FIELDS = ['module', 'jsnext:main', 'jsnext'];

export const SVELTE_RESOLVE_CONDITIONS = ['svelte'];

export const SVELTE_RESOLVE_MAIN_FIELDS = ['svelte', ...VITE_RESOLVE_MAIN_FIELDS];

export const SVELTE_IMPORTS = [
Expand Down
7 changes: 2 additions & 5 deletions packages/vite-plugin-svelte/src/utils/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ function getSvelteDependencies(
return result;
}

export function resolveDependencyData(
dep: string,
localRequire: NodeRequire
): DependencyData | void {
function resolveDependencyData(dep: string, localRequire: NodeRequire): DependencyData | void {
try {
const pkgJson = `${dep}/package.json`;
const pkg = localRequire(pkgJson);
Expand Down Expand Up @@ -173,7 +170,7 @@ const COMMON_PREFIXES_WITHOUT_SVELTE_FIELD = [
* @param dependency {string}
* @returns {boolean} true if it is a dependency without a svelte field
*/
export function is_common_without_svelte_field(dependency: string): boolean {
function is_common_without_svelte_field(dependency: string): boolean {
return (
COMMON_DEPENDENCIES_WITHOUT_SVELTE_FIELD.includes(dependency) ||
COMMON_PREFIXES_WITHOUT_SVELTE_FIELD.some(
Expand Down
10 changes: 8 additions & 2 deletions packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
} from 'vite';
import { log } from './log';
import { loadSvelteConfig } from './load-svelte-config';
import { SVELTE_HMR_IMPORTS, SVELTE_IMPORTS, SVELTE_RESOLVE_MAIN_FIELDS } from './constants';
import {
SVELTE_HMR_IMPORTS,
SVELTE_IMPORTS,
SVELTE_RESOLVE_CONDITIONS,
SVELTE_RESOLVE_MAIN_FIELDS
} from './constants';
// eslint-disable-next-line node/no-missing-import
import type { CompileOptions, Warning } from 'svelte/types/compiler/interfaces';
import type {
Expand Down Expand Up @@ -303,7 +308,8 @@ export function buildExtraViteConfig(
const svelteDeps = findRootSvelteDependencies(options.root);
const extraViteConfig: Partial<UserConfig> = {
resolve: {
mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
conditions: SVELTE_RESOLVE_CONDITIONS,
mainFields: SVELTE_RESOLVE_MAIN_FIELDS,
dedupe: [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS]
}
// this option is still awaiting a PR in vite to be supported
Expand Down
57 changes: 0 additions & 57 deletions packages/vite-plugin-svelte/src/utils/resolve.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/vite-plugin-svelte/src/utils/vite-plugin-svelte-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,4 @@ export class VitePluginSvelteCache {
const dependants = this._dependants.get(path);
return dependants ? [...dependants] : [];
}

public getResolvedSvelteField(name: string, importer?: string): string | void {
return this._resolvedSvelteFields.get(this._getResolvedSvelteFieldKey(name, importer));
}

public setResolvedSvelteField(
importee: string,
importer: string | undefined = undefined,
resolvedSvelte: string
) {
this._resolvedSvelteFields.set(
this._getResolvedSvelteFieldKey(importee, importer),
resolvedSvelte
);
}

private _getResolvedSvelteFieldKey(importee: string, importer?: string): string {
return importer ? `${importer} > ${importee}` : importee;
}
}