Skip to content

Commit

Permalink
[feat] vitePreprocess (#8036)
Browse files Browse the repository at this point in the history
* [feat] vitePreprocess

* fix test

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
benmccann and Rich-Harris authored Dec 9, 2022
1 parent eddedc8 commit 712cb28
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 76 deletions.
6 changes: 6 additions & 0 deletions .changeset/sour-bears-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

[feat] vitePreprocess
12 changes: 8 additions & 4 deletions documentation/docs/60-appendix/05-integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ title: Integrations

## Preprocessors

Preprocessors transform your `.svelte` files before passing them to the compiler. For example, if your `.svelte` file uses TypeScript and PostCSS, it must first be transformed into JavaScript and CSS so that the Svelte compiler can handle it. There are many [available preprocessors](https://sveltesociety.dev/tools#preprocessors).
Preprocessors transform your `.svelte` files before passing them to the compiler. For example, if your `.svelte` file uses TypeScript and PostCSS, it must first be transformed into JavaScript and CSS so that the Svelte compiler can handle it. There are many [available preprocessors](https://sveltesociety.dev/tools#preprocessors). The Svelte team maintains two official ones discussed below.

[`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess) automatically transforms the code in your Svelte templates to provide support for TypeScript, PostCSS, scss/sass, Less, and many other technologies (except CoffeeScript which is [not supported](https://github.com/sveltejs/kit/issues/2920#issuecomment-996469815) by SvelteKit). The first step of setting it up is to add `svelte-preprocess` to your [`svelte.config.js`](/docs/configuration). It is provided by the template if you're using TypeScript whereas JavaScript users will need to add it. After that, you will often only need to install the corresponding library such as `npm install -D sass` or `npm install -D less`. See the [`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess) docs for more details.
### `vitePreprocess`

`vite-plugin-svelte` also offers a [`vitePreprocess`](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md) feature which will utilize Vite for preprocessing which may be faster and require less configuration. To use this option, first run `npm install --save-dev @sveltejs/vite-plugin-svelte` and then update your `svelte.config.js`:
`vite-plugin-svelte` offers a [`vitePreprocess`](https://github.com/sveltejs/vite-plugin-svelte/blob/main/docs/preprocess.md) feature which utilizes Vite for preprocessing. It is capable of handling the language flavors Vite handles: TypeScript, PostCSS, SCSS, Less, Stylus, and SugarSS. For convenience, it is re-exported from the `@sveltejs/kit/vite` package. If you set your project up with TypeScript it will be included by default:

```js
// svelte.config.js
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { vitePreprocess } from '@sveltejs/kit/vite';

export default {
preprocess: [vitePreprocess()]
};
```

### `svelte-preprocess`

[`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess) automatically transforms the code in your Svelte templates to provide support for TypeScript, PostCSS, scss/sass, Less, and many other technologies (except CoffeeScript which is [not supported](https://github.com/sveltejs/kit/issues/2920#issuecomment-996469815) by SvelteKit). `vitePreprocess` may be faster and require less configuration, so it is used by default. However, `svelte-preprocess` has some additional functionality not found in `vitePreprocess`. The first step of setting it up is to add `svelte-preprocess` to your [`svelte.config.js`](/docs/configuration). After that, you will often only need to install the corresponding library such as `npm install -D sass` or `npm install -D less`. See the [`svelte-preprocess`](https://github.com/sveltejs/svelte-preprocess) docs for more details.

## Adders

[Svelte Adders](https://sveltesociety.dev/templates#adders) allow you to setup many different complex integrations like Tailwind, PostCSS, Storybook, Firebase, GraphQL, mdsvex, and more with a single command. Please see [sveltesociety.dev](https://sveltesociety.dev/) for a full listing of templates, components, and tools available for use with Svelte and SvelteKit.
Expand Down
1 change: 0 additions & 1 deletion packages/create-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"prettier-plugin-svelte": "^2.8.1",
"sucrase": "^3.29.0",
"svelte": "^3.54.0",
"svelte-preprocess": "^4.10.7",
"tiny-glob": "^0.2.9",
"uvu": "^0.5.6"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import adapter from '@sveltejs/adapter-auto';
import preprocess from 'svelte-preprocess';
import { vitePreprocess } from '@sveltejs/kit/vite';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: preprocess(),
preprocess: vitePreprocess(),

kit: {
adapter: adapter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import adapter from '@sveltejs/adapter-auto';
import preprocess from 'svelte-preprocess';
import { vitePreprocess } from '@sveltejs/kit/vite';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: preprocess(),
preprocess: vitePreprocess(),

kit: {
adapter: adapter()
Expand Down
3 changes: 1 addition & 2 deletions packages/create-svelte/shared/+typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"devDependencies": {
"typescript": "^4.9.3",
"tslib": "^2.4.1",
"svelte-check": "^2.9.2",
"svelte-preprocess": "^4.10.7"
"svelte-check": "^2.9.2"
}
}
6 changes: 3 additions & 3 deletions packages/create-svelte/shared/+typescript/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import adapter from '@sveltejs/adapter-auto';
import preprocess from 'svelte-preprocess';
import { vitePreprocess } from '@sveltejs/kit/vite';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: preprocess(),
preprocess: vitePreprocess(),

kit: {
adapter: adapter()
Expand Down
1 change: 0 additions & 1 deletion packages/create-svelte/templates/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@sveltejs/adapter-auto": "workspace:*",
"@sveltejs/kit": "workspace:*",
"svelte": "^3.54.0",
"svelte-preprocess": "^4.10.7",
"typescript": "^4.9.3",
"vite": "^4.0.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/create-svelte/templates/default/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import adapter from '@sveltejs/adapter-auto';
import preprocess from 'svelte-preprocess';
import { vitePreprocess } from '@sveltejs/kit/vite';

// This config is ignored and replaced with one of the configs in the shared folder when a project is created.

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: preprocess(),
preprocess: vitePreprocess(),

kit: {
adapter: adapter()
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { create_static_module, create_dynamic_module } from '../../core/env.js';
import { is_illegal, module_guard, normalize_id } from './graph_analysis/index.js';
import { create_assets } from '../../core/sync/create_manifest_data/index.js';

export { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

const cwd = process.cwd();

/** @type {import('./types').EnforcedConfig} */
Expand Down
4 changes: 2 additions & 2 deletions packages/package/test/fixtures/resolve-alias/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import preprocess from 'svelte-preprocess';
import { vitePreprocess } from '../../../../kit/src/exports/vite/index.js';
import { fileURLToPath } from 'url';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.join(__filename, '..');

export default {
preprocess: preprocess(),
preprocess: vitePreprocess(),
kit: {
alias: {
$utils: path.resolve(__dirname, './src/lib/utils')
Expand Down
54 changes: 0 additions & 54 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 712cb28

Please sign in to comment.