Skip to content

Commit 1c780ce

Browse files
authored
feat: support additional queries (#513)
* feat: support direct and raw query parameters for svelte requests * wip: add option to pass compileOptions for raw scripts, fix issues with output as default export, add tests * wip: move handling of raw and direct to load hook, change tests to use external snapshot and add more tests * wip: add sourcemap query and inline sourcemaps to direct requests. improve error handling * wip: clean up implementation; add documentation * fix: make sure to return all affected modules from handleHotUpdate instead of just looking at js and css. This updates ?raw imports correctly during dev * feat: add new type 'all' that returns source and complete result with preprocessed, js, css, dependencies and ast * fix: heal pnpm-lock * fix: improve snapshot normalization, sourcemap output included absolute path to testDir, which differs between serve and build tests * fix: avoid absolute paths in sourcemap sources * fix: windows * fix: pass normalizedFilename to svelte compiler so windows compile does not add full absolute path to generated by comment * fix: update snapshots * docs: mark raw&svelte and direct&svelte as experimental * fix: vite ?raw is string by default, so export code string as default and others as named exports * fix: sort exports and don't run mixed tests during build
1 parent 0971449 commit 1c780ce

23 files changed

+1861
-79
lines changed

.changeset/fresh-papayas-change.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': patch
3+
---
4+
5+
ensure sources paths in sourcemaps are not absolute file paths

.changeset/giant-tools-hunt.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+
support `&direct` and `&raw` query parameters for svelte requests

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

docs/advanced-usage.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Advanced usage
2+
3+
> **HERE BE DRAGONS**
4+
>
5+
> The features described here are not meant to be used in regular libraries or end-user applications.
6+
> They can be useful for frameworks, documentation sites or in special situations, but you are responsible for applying them correctly.
7+
>
8+
> **Proceed with caution!**
9+
10+
## custom queries
11+
12+
Vite supports using query parameters to request different outcomes for the same file.
13+
14+
The following schemes are supported by vite-plugin-svelte:
15+
16+
### raw
17+
18+
```js
19+
//get .svelte file content as a string
20+
import content from 'File.svelte?raw';
21+
```
22+
23+
### experimental
24+
25+
In addition to the plain .svelte source content, you can use special svelte queries.
26+
27+
> These svelte subqueries are experimental, availability, syntax and output format may change
28+
29+
#### raw&svelte
30+
31+
```js
32+
//get output of svelte.preprocess code as string
33+
import preprocessed from 'File.svelte?raw&svelte&type=preprocessed';
34+
```
35+
36+
```js
37+
//get output of svelte.compile js.code as string
38+
import script from 'File.svelte?raw&svelte&type=script';
39+
```
40+
41+
```js
42+
//get output of svelte.compile css.code as string
43+
import style from 'File.svelte?raw&svelte&type=style';
44+
```
45+
46+
##### detail exports
47+
48+
raw&svelte exports code string as default export, but also offers named exports if you need details
49+
50+
```js
51+
//get output of svelte.preprocess
52+
import { code, map, dependencies } from 'File.svelte?raw&svelte&type=preprocessed';
53+
```
54+
55+
```js
56+
//get output of svelte.compile js
57+
import { code, map, dependencies } from 'File.svelte?raw&svelte&type=script';
58+
```
59+
60+
```js
61+
//get output of svelte.compile css
62+
import { code, map, dependencies } from 'File.svelte?raw&svelte&type=style';
63+
```
64+
65+
```js
66+
//get everything in one go
67+
import * as all from 'File.svelte?raw&svelte&type=all';
68+
import {
69+
source,
70+
preprocessed,
71+
dependencies,
72+
js,
73+
css,
74+
ast,
75+
normalizedFilename,
76+
ssr,
77+
lang,
78+
warnings,
79+
stats
80+
} from 'File.svelte?raw&svelte&type=all';
81+
```
82+
83+
#### direct&svelte
84+
85+
```html
86+
<!-- load and execute component script -->
87+
<script type="application/javascript" src="File.svelte?direct&svelte&type=script&lang.js" />
88+
<!-- embed component style as css -->
89+
<link rel="stylesheet" type="text/css" href="File.svelte?direct&svelte&type=style&lang.css" />
90+
```
91+
92+
#### sourcemap
93+
94+
add `&sourcemap` to `?(raw|direct)&svelte&type=(script|style|all)` queries to include sourcemaps (inline for direct)
95+
96+
#### compilerOptions
97+
98+
?raw and ?direct use default compilerOptions, even if you have different values in your svelte.config.js:
99+
100+
```js
101+
const compilerOptions = {
102+
dev: false,
103+
generate: 'dom',
104+
css: false,
105+
hydratable: false,
106+
enableSourcemap: false // or {js: true} or {css:true} if sourcemap query is set
107+
};
108+
```
109+
110+
to get output with different compilerOptions, append them as json like this:
111+
112+
```js
113+
//get ssr output of svelte.compile js as {code, map, dependencies}
114+
import script from 'File.svelte?raw&svelte&type=script&compilerOptions={generate:"ssr"}';
115+
```
116+
117+
only a subset of compilerOptions is supported
118+
119+
- generate
120+
- dev
121+
- css
122+
- hydratable
123+
- customElement
124+
- immutable
125+
- enableSourcemap

0 commit comments

Comments
 (0)