Skip to content

Commit

Permalink
fix: add svelte lib without components to noExternal (#166)
Browse files Browse the repository at this point in the history
* chore: add test

* fix: add svelte lib without components to noExternal

* chore: update test

* chore: add changeset

* perf(dependencies): better svelte lib check
  • Loading branch information
bluwy committed Sep 11, 2021
1 parent abc0233 commit adccb23
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-horses-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

Svelte libraries without any Svelte components are also added to ssr.noExternal
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "index.js",
"files": [
"package.json",
"index.cjs"
"index.js"
],
"exports":{
".": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { setContext } from 'svelte';

export function setSomeContext() {
setContext('svelte-api-only', true);
}
18 changes: 18 additions & 0 deletions packages/e2e-tests/_test_dependencies/svelte-api-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "1.0.0",
"private": true,
"name": "e2e-test-dep-svelte-api-only",
"main": "index.js",
"module": "index.js",
"files": [
"package.json",
"index.js"
],
"exports":{
".": {
"import": "./index.js"
},
"./package.json": "./package.json"
},
"type": "module"
}
1 change: 1 addition & 0 deletions packages/e2e-tests/kit-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"devDependencies": {
"@sveltejs/adapter-node": "^1.0.0-next.46",
"@sveltejs/kit": "^1.0.0-next.165",
"e2e-test-dep-svelte-api-only": "workspace:*",
"svelte": "^3.42.4"
},
"type": "module",
Expand Down
2 changes: 2 additions & 0 deletions packages/e2e-tests/kit-node/src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import Counter from '$lib/Counter.svelte';
// eslint-disable-next-line node/no-missing-import
import Child from '$lib/Child.svelte';
import { setSomeContext } from 'e2e-test-dep-svelte-api-only';
export let load_status = 'NOT_LOADED';
let mount_status = 'BEFORE_MOUNT';
onMount(async () => {
const isSSR = (await import('../client-only-module.js')).default;
console.log(`onMount dynamic imported isSSR: ${isSSR}`);
mount_status = 'AFTER_MOUNT';
});
setSomeContext();
</script>

<main>
Expand Down
32 changes: 27 additions & 5 deletions packages/vite-plugin-svelte/src/utils/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ function getSvelteDependencies(
const localRequire = createRequire(`${pkgDir}/package.json`);
const resolvedDeps = deps
.map((dep) => resolveDependencyData(dep, localRequire))
.filter((depData) => !!depData && isSvelte(depData.pkg)) as DependencyData[];
// @ts-ignore
.filter(Boolean) as DependencyData[];
for (const { pkg, dir } of resolvedDeps) {
result.push({ name: pkg.name, pkg, dir, path });
if (pkg.dependencies) {
const type = getSvelteDependencyType(pkg);
if (!type) continue;
result.push({ name: pkg.name, type, pkg, dir, path });
// continue crawling for component libraries so we can optimize them, js libraries are fine
if (type === 'component-library' && pkg.dependencies) {
let dependencyNames = Object.keys(pkg.dependencies);
const circular = dependencyNames.filter((name) => path.includes(name));
if (circular.length > 0) {
Expand Down Expand Up @@ -102,10 +104,24 @@ function parsePkg(dir: string, silent = false): Pkg | void {
}
}

function isSvelte(pkg: Pkg) {
function getSvelteDependencyType(pkg: Pkg): SvelteDependencyType | undefined {
if (isSvelteComponentLib(pkg)) {
return 'component-library';
} else if (isSvelteLib(pkg)) {
return 'js-library';
} else {
return undefined;
}
}

function isSvelteComponentLib(pkg: Pkg) {
return !!pkg.svelte;
}

function isSvelteLib(pkg: Pkg) {
return !!pkg.dependencies?.svelte || !!pkg.peerDependencies?.svelte;
}

const COMMON_DEPENDENCIES_WITHOUT_SVELTE_FIELD = [
'@lukeed/uuid',
'@sveltejs/vite-plugin-svelte',
Expand Down Expand Up @@ -178,16 +194,22 @@ interface DependencyData {

export interface SvelteDependency {
name: string;
type: SvelteDependencyType;
dir: string;
pkg: Pkg;
path: string[];
}

// component-library => exports svelte components
// js-library => only uses svelte api, no components
export type SvelteDependencyType = 'component-library' | 'js-library';

export interface Pkg {
name: string;
svelte?: string;
dependencies?: DependencyList;
devDependencies?: DependencyList;
peerDependencies?: DependencyList;
[key: string]: any;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export function buildExtraViteConfig(

if (configEnv.command === 'serve') {
extraViteConfig.optimizeDeps = buildOptimizeDepsForSvelte(
svelteDeps,
svelteDeps.filter((dep) => dep.type === 'component-library'),
options,
config.optimizeDeps
);
Expand Down
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit adccb23

Please sign in to comment.