Skip to content

Commit f6f56fe

Browse files
authored
feat: resolve svelte to svelte/ssr when building for ssr (fixes #74) (#75)
* feat: resolve svelte to svelte/ssr when building for ssr (fixes #74) * remove duplicate detection of ssr build, vite already handles this correctly * add 'svelte' to ssr.noExternal to be able to resolve it to svelte/ssr and add tests. reduce logging * add 'svelte' to ssr.noExternal to be able to resolve it to svelte/ssr and add tests. reduce logging * fix: sync pnpm lock * only try to resolve svelte/ssr once and cache result. disable test until svelte with svelte/ssr is released * honor svelte in ssr.external to allow user config to override plugin behavior
1 parent c6a7834 commit f6f56fe

34 files changed

+1070
-0
lines changed

.changeset/angry-laws-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
resolve svelte to svelte/ssr when building for ssr (see #74)

.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ module.exports = {
150150
settings: {
151151
'svelte3/ignore-styles': () => true
152152
}
153+
},
154+
{
155+
/* required because $app and $lib are not known */
156+
files: ['packages/playground/kit-demo-app/src/**'],
157+
rules: {
158+
'node/no-missing-import': 'off'
159+
}
153160
}
154161
]
155162
};

packages/e2e-tests/kit-node/__tests__/kit.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
readFileContent,
23
editFile,
34
editFileAndWaitForHmrComplete,
45
getColor,
@@ -9,6 +10,7 @@ import {
910
} from '../../testUtils';
1011

1112
import fetch from 'node-fetch';
13+
import path from 'path';
1214

1315
describe('kit-node', () => {
1416
describe('index route', () => {
@@ -47,6 +49,19 @@ describe('kit-node', () => {
4749
});
4850
});
4951

52+
it('should load dynamic import in onMount', async () => {
53+
// expect log to contain message with dynamic import value from onMount
54+
expect(browserLogs.some((x) => x === `onMount dynamic imported isSSR: false`)).toBe(true);
55+
});
56+
57+
if (isBuild) {
58+
// disabled until svelte releases svelte/ssr export
59+
it.skip('should not include dynamic import from onmount in ssr output', async () => {
60+
const app = readFileContent(path.join('.svelte-kit', 'output', 'server', 'app.js'));
61+
expect(app.includes('__SHOULD_NOT_BE_IN_SSR_APP_JS')).toBe(false);
62+
});
63+
}
64+
5065
if (!isBuild) {
5166
describe('hmr', () => {
5267
const updateIndexSvelte = editFileAndWaitForHmrComplete.bind(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const __SHOULD_NOT_BE_IN_SSR_APP_JS = import.meta.env.SSR;
2+
export default __SHOULD_NOT_BE_IN_SSR_APP_JS;

packages/e2e-tests/kit-node/src/routes/index.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<script>
2+
import { onMount } from 'svelte';
23
// eslint-disable-next-line node/no-missing-import
34
import Counter from '$lib/Counter.svelte';
5+
6+
onMount(async () => {
7+
const isSSR = (await import('../client-only-module.js')).default;
8+
console.log(`onMount dynamic imported isSSR: ${isSSR}`);
9+
});
410
</script>
511

612
<main>

packages/e2e-tests/testUtils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ export async function getBg(el: string | ElementHandle) {
6262
return el.evaluate((el) => getComputedStyle(el as Element).backgroundImage);
6363
}
6464

65+
export function readFileContent(filename: string) {
66+
filename = path.resolve(testDir, filename);
67+
return fs.readFileSync(filename, 'utf-8');
68+
}
69+
6570
export function editFile(filename: string, replacer: (str: string) => string) {
6671
if (isBuild) return;
6772
filename = path.resolve(testDir, filename);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
node_modules
3+
/.svelte-kit
4+
/package
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# create-svelte
2+
3+
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte);
4+
5+
## Creating a project
6+
7+
If you're seeing this, you've probably already done this step. Congrats!
8+
9+
```bash
10+
# create a new project in the current directory
11+
npm init svelte@next
12+
13+
# create a new project in my-app
14+
npm init svelte@next my-app
15+
```
16+
17+
> Note: the `@next` is temporary
18+
19+
## Developing
20+
21+
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
22+
23+
```bash
24+
npm run dev
25+
26+
# or start the server and open the app in a new browser tab
27+
npm run dev -- --open
28+
```
29+
30+
## Building
31+
32+
Before creating a production version of your app, install an [adapter](https://kit.svelte.dev/docs#adapters) for your target environment. Then:
33+
34+
```bash
35+
npm run build
36+
```
37+
38+
> You can preview the built app with `npm run preview`, regardless of whether you installed an adapter. This should _not_ be used to serve your app in production.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"$lib/*": ["src/lib/*"]
6+
}
7+
},
8+
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
9+
}

0 commit comments

Comments
 (0)