Skip to content

Commit

Permalink
force Vite to bundle component libraries in SSR - fixes #904
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 20, 2021
1 parent 7f0004f commit d6b6edc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-points-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Force Vite to bundle Svelte component libraries in SSR
8 changes: 2 additions & 6 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import path from 'path';
import { rimraf } from '../filesystem/index.js';
import create_manifest_data from '../../core/create_manifest_data/index.js';
import { copy_assets, posixify, resolve_entry } from '../utils.js';
import { copy_assets, get_no_external, posixify, resolve_entry } from '../utils.js';
import { create_app } from '../../core/create_app/index.js';
import vite from 'vite';
import svelte from '@sveltejs/vite-plugin-svelte';
Expand Down Expand Up @@ -438,11 +438,7 @@ async function build_server(
// @ts-ignore
ssr: {
...user_config.ssr,
noExternal: [
'svelte',
'@sveltejs/kit',
...((user_config.ssr && user_config.ssr.noExternal) || [])
]
noExternal: get_no_external(cwd, user_config.ssr && user_config.ssr.noExternal)
},
optimizeDeps: {
entries: []
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { create_app } from '../../core/create_app/index.js';
import { rimraf } from '../filesystem/index.js';
import { ssr } from '../../runtime/server/index.js';
import { getRawBody } from '../http/index.js';
import { copy_assets } from '../utils.js';
import { copy_assets, get_no_external } from '../utils.js';
import svelte from '@sveltejs/vite-plugin-svelte';
import { get_server } from '../server/index.js';
import '../../install-fetch.js';
Expand Down Expand Up @@ -107,6 +107,10 @@ class Watcher extends EventEmitter {
optimizeDeps: {
...user_config.optimizeDeps,
entries: []
},
ssr: {
...user_config.ssr,
noExternal: get_no_external(this.cwd, user_config.ssr && user_config.ssr.noExternal)
}
});

Expand Down
32 changes: 32 additions & 0 deletions packages/kit/src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,35 @@ export function resolve_entry(entry) {
export function posixify(str) {
return str.replace(/\\/g, '/');
}

/**
* Get a list of packages that use pkg.svelte, so they can be added
* to ssr.noExternal. This is done on a best-effort basis to reduce
* the frequency of 'Must use import to load ES Module' and similar
* @param {string} cwd
* @returns {string[]}
*/
function find_svelte_packages(cwd) {
const pkg_file = path.join(cwd, 'package.json');
if (!fs.existsSync(pkg_file)) return [];

const pkg = JSON.parse(fs.readFileSync(pkg_file, 'utf8'));

const deps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.devDependencies || {})];

return deps.filter((dep) => {
const dep_pkg_file = path.join(cwd, 'node_modules', dep, 'package.json');
if (!fs.existsSync(dep_pkg_file)) return false;

const dep_pkg = JSON.parse(fs.readFileSync(dep_pkg_file, 'utf-8'));
return !!dep_pkg.svelte;
});
}

/**
* @param {string} cwd
* @param {string[]} [user_specified_deps]
*/
export function get_no_external(cwd, user_specified_deps = []) {
return ['svelte', '@sveltejs/kit', ...user_specified_deps, ...find_svelte_packages(cwd)];
}

0 comments on commit d6b6edc

Please sign in to comment.