Skip to content

Commit 079a7b8

Browse files
authored
feat: log compile stats (#503)
* wip: cleaned up version of perf logging with added logging for ssr * feat: compile stats * fix: node14 add import for performance * perf: use readFileSync in esbuild onLoad * fix: format comment * fix: sequential find package name in aggregate * fix: fix fix, first occurence needs package too * feat: add config option to disable stats logging * chore: remove nested .npmrc * fix: move disableCompileStats option to experimental section * fix: remove disableCompileStats from list of allowed top level options
1 parent d920ea5 commit 079a7b8

37 files changed

+888
-79
lines changed

.changeset/thick-papayas-stare.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+
add compile time stats logging

docs/config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,10 @@ export default {
413413
rawWarnings: Warning[]; // raw compiler output
414414
};
415415
```
416+
417+
### disableCompileStats
418+
419+
- **Type** `boolean | 'dev' | 'build'`
420+
- **Default:** `false`
421+
422+
disable svelte compile statistics.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"checkJs": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"sourceMap": true,
11+
"strict": true
12+
}
13+
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias and https://kit.svelte.dev/docs/configuration#files
14+
//
15+
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
16+
// from the referenced tsconfig.json - TypeScript does not merge them in
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "playground-big-component-library-kit",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite dev",
7+
"build": "vite build",
8+
"preview": "vite preview",
9+
"check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
10+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch"
11+
},
12+
"devDependencies": {
13+
"@sveltejs/adapter-auto": "1.0.0-next.88",
14+
"@sveltejs/kit": "^1.0.0-next.551",
15+
"carbon-components-svelte": "^0.70.12",
16+
"carbon-icons-svelte": "^11.4.0",
17+
"carbon-preprocess-svelte": "^0.9.1",
18+
"lodash-es": "^4.17.21",
19+
"svelte": "^3.44.0",
20+
"svelte-check": "^2.7.1",
21+
"svelte-preprocess": "^4.10.7",
22+
"typescript": "^4.7.4",
23+
"vite": "^3.1.0"
24+
},
25+
"type": "module"
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// See https://kit.svelte.dev/docs/types#app
2+
// for information about these interfaces
3+
// and what to do when importing types
4+
declare namespace App {
5+
// interface Locals {}
6+
// interface PageData {}
7+
// interface Error {}
8+
// interface Platform {}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
6+
<meta name="viewport" content="width=device-width" />
7+
%sveltekit.head%
8+
</head>
9+
<body>
10+
<div style="display: contents">%sveltekit.body%</div>
11+
</body>
12+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import 'carbon-components-svelte/css/white.css';
3+
</script>
4+
5+
<slot />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<script>
2+
import {
3+
Accordion,
4+
AccordionItem,
5+
Button,
6+
DatePickerInput,
7+
Link,
8+
DatePicker
9+
} from 'carbon-components-svelte';
10+
import { Save } from 'carbon-icons-svelte';
11+
/*
12+
// deep imports
13+
import Accordion from 'carbon-components-svelte/src/Accordion/Accordion.svelte';
14+
import AccordionItem from 'carbon-components-svelte/src/Accordion/AccordionItem.svelte';
15+
import Button from 'carbon-components-svelte/src/Button/Button.svelte';
16+
import DatePickerInput from 'carbon-components-svelte/src/DatePicker/DatePickerInput.svelte';
17+
import Link from 'carbon-components-svelte/src/Link/Link.svelte';
18+
import DatePicker from 'carbon-components-svelte/src/DatePicker/DatePicker.svelte';
19+
import Save from 'carbon-icons-svelte/lib/Save.svelte';
20+
21+
import { assign } from 'lodash-es';
22+
import toNumber from 'lodash-es/toNumber.js';
23+
import toPath from 'lodash-es/toPath.js';
24+
*/
25+
</script>
26+
27+
<Link href="/sub">go to sub page</Link>
28+
29+
<Button>Hello</Button>
30+
<DatePicker datePickerType="single" flatpickrProps={{ static: true }}>
31+
<DatePickerInput labelText="Meeting date" placeholder="mm/dd/yyyy" />
32+
</DatePicker>
33+
34+
<Accordion>
35+
<AccordionItem title="Section 1" open>Content 1</AccordionItem>
36+
<AccordionItem title="Section 2">Content 2</AccordionItem>
37+
<AccordionItem title="Section 3">Content 3</AccordionItem>
38+
</Accordion>
39+
<Save />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
import { NumberInput, Link } from 'carbon-components-svelte';
3+
import { Close } from 'carbon-icons-svelte';
4+
5+
/*
6+
// deep
7+
import NumberInput from 'carbon-components-svelte/src/NumberInput/NumberInput.svelte';
8+
import Link from 'carbon-components-svelte/src/Link/Link.svelte';
9+
import Close from 'carbon-icons-svelte/lib/Close.svelte';
10+
11+
*/
12+
</script>
13+
14+
<h1>sub page</h1>
15+
<Link href="/">back home</Link>
16+
<NumberInput />
17+
<Close />
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import adapter from '@sveltejs/adapter-auto';
2+
import { optimizeImports } from 'carbon-preprocess-svelte';
3+
import preprocess from 'svelte-preprocess';
4+
5+
/** @type {import('@sveltejs/kit').Config} */
6+
const config = {
7+
preprocess: [preprocess(), process.env.DEEP_IMPORTS ? optimizeImports() : null].filter(Boolean),
8+
kit: {
9+
adapter: adapter()
10+
}
11+
};
12+
13+
export default config;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//eslint-disable-next-line node/no-missing-import
2+
import { sveltekit } from '@sveltejs/kit/vite';
3+
4+
/** @type {import('vite').UserConfig} */
5+
const config = {
6+
plugins: [sveltekit()],
7+
optimizeDeps: {
8+
include: ['carbon-components-svelte', 'carbon-icons-svelte']
9+
}
10+
};
11+
12+
export default config;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Svelte + Vite
2+
3+
This template should help get you started developing with Svelte in Vite.
4+
5+
## Recommended IDE Setup
6+
7+
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
8+
9+
## Need an official Svelte framework?
10+
11+
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
12+
13+
## Technical considerations
14+
15+
**Why use this over SvelteKit?**
16+
17+
- It brings its own routing solution which might not be preferable for some users.
18+
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
19+
20+
This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
21+
22+
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
23+
24+
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
25+
26+
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
27+
28+
**Why include `.vscode/extensions.json`?**
29+
30+
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
31+
32+
**Why enable `checkJs` in the JS template?**
33+
34+
It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.
35+
36+
**Why is HMR not preserving my local component state?**
37+
38+
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
39+
40+
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
41+
42+
```js
43+
// store.js
44+
// An extremely simple external store
45+
import { writable } from 'svelte/store';
46+
export default writable(0);
47+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + Svelte</title>
8+
<!--app-head-->
9+
</head>
10+
<body>
11+
<div id="app"><!--app-html--></div>
12+
<script type="module" src="/src/entry-client.js"></script>
13+
</body>
14+
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "Node",
4+
"target": "ESNext",
5+
"module": "ESNext",
6+
/**
7+
* svelte-preprocess cannot figure out whether you have
8+
* a value or a type, so tell TypeScript to enforce using
9+
* `import type` instead of `import` for Types.
10+
*/
11+
"importsNotUsedAsValues": "error",
12+
"isolatedModules": true,
13+
"resolveJsonModule": true,
14+
/**
15+
* To have warnings / errors of the Svelte compiler at the
16+
* correct position, enable source maps by default.
17+
*/
18+
"sourceMap": true,
19+
"esModuleInterop": true,
20+
"skipLibCheck": true,
21+
"forceConsistentCasingInFileNames": true,
22+
/**
23+
* Typecheck JS in `.svelte` and `.js` files by default.
24+
* Disable this if you'd like to use dynamic types.
25+
*/
26+
"checkJs": true
27+
},
28+
/**
29+
* Use global.d.ts instead of compilerOptions.types
30+
* to avoid limiting type declarations.
31+
*/
32+
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "playground-big-component-library-vite-ssr",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "node server",
8+
"build": "npm run build:client && npm run build:server",
9+
"build:client": "vite build --ssrManifest --outDir dist/client",
10+
"build:server": "vite build --ssr src/entry-server.js --outDir dist/server",
11+
"preview": "cross-env NODE_ENV=production node server"
12+
},
13+
"dependencies": {
14+
"compression": "^1.7.4",
15+
"express": "^4.18.2",
16+
"sirv": "^2.0.2"
17+
},
18+
"devDependencies": {
19+
"@sveltejs/vite-plugin-svelte": "workspace:*",
20+
"carbon-components-svelte": "^0.70.12",
21+
"carbon-icons-svelte": "^11.4.0",
22+
"carbon-preprocess-svelte": "^0.9.1",
23+
"cross-env": "^7.0.3",
24+
"lodash-es": "^4.17.21",
25+
"svelte": "^3.53.1",
26+
"svelte-preprocess": "^4.10.7",
27+
"vite": "^3.2.3"
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import fs from 'node:fs/promises';
2+
import express from 'express';
3+
4+
// Constants
5+
const isProduction = process.env.NODE_ENV === 'production';
6+
const port = process.env.PORT || 5173;
7+
const base = process.env.BASE || '/';
8+
9+
// Cached production assets
10+
const templateHtml = isProduction ? await fs.readFile('./dist/client/index.html', 'utf-8') : '';
11+
const ssrManifest = isProduction
12+
? await fs.readFile('./dist/client/ssr-manifest.json', 'utf-8')
13+
: undefined;
14+
15+
// Create http server
16+
const app = express();
17+
18+
// Add Vite or respective production middlewares
19+
let vite;
20+
if (!isProduction) {
21+
const { createServer } = await import('vite');
22+
vite = await createServer({
23+
server: { middlewareMode: true },
24+
appType: 'custom',
25+
base
26+
});
27+
app.use(vite.middlewares);
28+
} else {
29+
const compression = (await import('compression')).default;
30+
const sirv = (await import('sirv')).default;
31+
app.use(compression());
32+
app.use(base, sirv('./dist/client', { extensions: [] }));
33+
}
34+
35+
// Serve HTML
36+
app.use('*', async (req, res) => {
37+
try {
38+
const url = req.originalUrl.replace(base, '');
39+
40+
let template;
41+
let render;
42+
if (!isProduction) {
43+
// Always read fresh template in development
44+
template = await fs.readFile('./index.html', 'utf-8');
45+
template = await vite.transformIndexHtml(url, template);
46+
render = (await vite.ssrLoadModule('/src/entry-server.js')).render;
47+
} else {
48+
template = templateHtml;
49+
//eslint-disable-next-line node/no-missing-import
50+
render = (await import('./dist/server/entry-server.js')).render;
51+
}
52+
53+
const rendered = await render(url, ssrManifest);
54+
55+
const html = template
56+
.replace(`<!--app-head-->`, rendered.head ?? '')
57+
.replace(`<!--app-html-->`, rendered.html ?? '');
58+
59+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
60+
} catch (e) {
61+
vite?.ssrFixStacktrace(e);
62+
console.log(e.stack);
63+
res.status(500).end(e.stack);
64+
}
65+
});
66+
67+
// Start http server
68+
app.listen(port, () => {
69+
console.log(`Server started at http://localhost:${port}`);
70+
});

0 commit comments

Comments
 (0)