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