Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log compile stats #503

Merged
merged 12 commits into from
Nov 22, 2022
Merged
5 changes: 5 additions & 0 deletions .changeset/thick-papayas-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

add compile time stats logging
1 change: 1 addition & 0 deletions packages/playground/big-component-library-kit/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
dominikg marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions packages/playground/big-component-library-kit/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias and https://kit.svelte.dev/docs/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}
26 changes: 26 additions & 0 deletions packages/playground/big-component-library-kit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "playground-big-component-library-kit",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch"
},
"devDependencies": {
"@sveltejs/adapter-auto": "1.0.0-next.88",
"@sveltejs/kit": "^1.0.0-next.551",
"carbon-components-svelte": "^0.70.12",
"carbon-icons-svelte": "^11.4.0",
"carbon-preprocess-svelte": "^0.9.1",
"lodash-es": "^4.17.21",
"svelte": "^3.44.0",
"svelte-check": "^2.7.1",
"svelte-preprocess": "^4.10.7",
"typescript": "^4.7.4",
"vite": "^3.1.0"
},
"type": "module"
}
9 changes: 9 additions & 0 deletions packages/playground/big-component-library-kit/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
declare namespace App {
// interface Locals {}
// interface PageData {}
// interface Error {}
// interface Platform {}
}
12 changes: 12 additions & 0 deletions packages/playground/big-component-library-kit/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body>
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import 'carbon-components-svelte/css/white.css';
</script>

<slot />
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<script>
import {
Accordion,
AccordionItem,
Button,
DatePickerInput,
Link,
DatePicker
} from 'carbon-components-svelte';
import { Save } from 'carbon-icons-svelte';
/*
// deep imports
import Accordion from 'carbon-components-svelte/src/Accordion/Accordion.svelte';
import AccordionItem from 'carbon-components-svelte/src/Accordion/AccordionItem.svelte';
import Button from 'carbon-components-svelte/src/Button/Button.svelte';
import DatePickerInput from 'carbon-components-svelte/src/DatePicker/DatePickerInput.svelte';
import Link from 'carbon-components-svelte/src/Link/Link.svelte';
import DatePicker from 'carbon-components-svelte/src/DatePicker/DatePicker.svelte';
import Save from 'carbon-icons-svelte/lib/Save.svelte';

import { assign } from 'lodash-es';
import toNumber from 'lodash-es/toNumber.js';
import toPath from 'lodash-es/toPath.js';
*/
</script>

<Link href="/sub">go to sub page</Link>

<Button>Hello</Button>
<DatePicker datePickerType="single" flatpickrProps={{ static: true }}>
<DatePickerInput labelText="Meeting date" placeholder="mm/dd/yyyy" />
</DatePicker>

<Accordion>
<AccordionItem title="Section 1" open>Content 1</AccordionItem>
<AccordionItem title="Section 2">Content 2</AccordionItem>
<AccordionItem title="Section 3">Content 3</AccordionItem>
</Accordion>
<Save />
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import { NumberInput, Link } from 'carbon-components-svelte';
import { Close } from 'carbon-icons-svelte';

/*
// deep
import NumberInput from 'carbon-components-svelte/src/NumberInput/NumberInput.svelte';
import Link from 'carbon-components-svelte/src/Link/Link.svelte';
import Close from 'carbon-icons-svelte/lib/Close.svelte';

*/
</script>

<h1>sub page</h1>
<Link href="/">back home</Link>
<NumberInput />
<Close />
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions packages/playground/big-component-library-kit/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import adapter from '@sveltejs/adapter-auto';
import { optimizeImports } from 'carbon-preprocess-svelte';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: [preprocess(), process.env.DEEP_IMPORTS ? optimizeImports() : null].filter(Boolean),
kit: {
adapter: adapter()
}
};

export default config;
12 changes: 12 additions & 0 deletions packages/playground/big-component-library-kit/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//eslint-disable-next-line node/no-missing-import
import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
const config = {
plugins: [sveltekit()],
optimizeDeps: {
include: ['carbon-components-svelte', 'carbon-icons-svelte']
}
};

export default config;
47 changes: 47 additions & 0 deletions packages/playground/big-component-library-vite-ssr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Svelte + Vite

This template should help get you started developing with Svelte in Vite.

## Recommended IDE Setup

[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).

## Need an official Svelte framework?

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.

## Technical considerations

**Why use this over SvelteKit?**

- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.

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.

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.

**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**

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.

**Why include `.vscode/extensions.json`?**

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.

**Why enable `checkJs` in the JS template?**

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.

**Why is HMR not preserving my local component state?**

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).

If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.

```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store';
export default writable(0);
```
14 changes: 14 additions & 0 deletions packages/playground/big-component-library-vite-ssr/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte</title>
<!--app-head-->
</head>
<body>
<div id="app"><!--app-html--></div>
<script type="module" src="/src/entry-client.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions packages/playground/big-component-library-vite-ssr/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"moduleResolution": "Node",
"target": "ESNext",
"module": "ESNext",
/**
* svelte-preprocess cannot figure out whether you have
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
* To have warnings / errors of the Svelte compiler at the
* correct position, enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable this if you'd like to use dynamic types.
*/
"checkJs": true
},
/**
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
29 changes: 29 additions & 0 deletions packages/playground/big-component-library-vite-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "playground-big-component-library-vite-ssr",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "node server",
"build": "npm run build:client && npm run build:server",
"build:client": "vite build --ssrManifest --outDir dist/client",
"build:server": "vite build --ssr src/entry-server.js --outDir dist/server",
"preview": "cross-env NODE_ENV=production node server"
},
"dependencies": {
"compression": "^1.7.4",
"express": "^4.18.2",
"sirv": "^2.0.2"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"carbon-components-svelte": "^0.70.12",
"carbon-icons-svelte": "^11.4.0",
"carbon-preprocess-svelte": "^0.9.1",
"cross-env": "^7.0.3",
"lodash-es": "^4.17.21",
"svelte": "^3.53.1",
"svelte-preprocess": "^4.10.7",
"vite": "^3.2.3"
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions packages/playground/big-component-library-vite-ssr/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import fs from 'node:fs/promises';
import express from 'express';

// Constants
const isProduction = process.env.NODE_ENV === 'production';
const port = process.env.PORT || 5173;
const base = process.env.BASE || '/';

// Cached production assets
const templateHtml = isProduction ? await fs.readFile('./dist/client/index.html', 'utf-8') : '';
const ssrManifest = isProduction
? await fs.readFile('./dist/client/ssr-manifest.json', 'utf-8')
: undefined;

// Create http server
const app = express();

// Add Vite or respective production middlewares
let vite;
if (!isProduction) {
const { createServer } = await import('vite');
vite = await createServer({
server: { middlewareMode: true },
appType: 'custom',
base
});
app.use(vite.middlewares);
} else {
const compression = (await import('compression')).default;
const sirv = (await import('sirv')).default;
app.use(compression());
app.use(base, sirv('./dist/client', { extensions: [] }));
}

// Serve HTML
app.use('*', async (req, res) => {
try {
const url = req.originalUrl.replace(base, '');

let template;
let render;
if (!isProduction) {
// Always read fresh template in development
template = await fs.readFile('./index.html', 'utf-8');
template = await vite.transformIndexHtml(url, template);
render = (await vite.ssrLoadModule('/src/entry-server.js')).render;
} else {
template = templateHtml;
//eslint-disable-next-line node/no-missing-import
render = (await import('./dist/server/entry-server.js')).render;
}

const rendered = await render(url, ssrManifest);

const html = template
.replace(`<!--app-head-->`, rendered.head ?? '')
.replace(`<!--app-html-->`, rendered.html ?? '');

res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
} catch (e) {
vite?.ssrFixStacktrace(e);
console.log(e.stack);
res.status(500).end(e.stack);
}
});

// Start http server
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});