diff --git a/site/content/docs/03-compile-time.md b/site/content/docs/03-compile-time.md index 4f578332f8..20ad62e973 100644 --- a/site/content/docs/03-compile-time.md +++ b/site/content/docs/03-compile-time.md @@ -2,10 +2,215 @@ title: Compile time --- -### svelte.preprocess +Typically, you won't interact with the Svelte compiler directly, but will instead integrate it into your build system using a bundler plugin: + +* [rollup-plugin-svelte](https://github.com/rollup/rollup-plugin-svelte) for users of [Rollup](https://rollupjs.org) +* [svelte-loader](https://github.com/sveltejs/svelte-loader) for users of [webpack](https://webpack.js.org) +* [parcel-plugin-svelte](https://github.com/DeMoorJasper/parcel-plugin-svelte) for users of [Parcel](https://parceljs.org/) + +Nonetheless, it's useful to understand how to use the compiler, since bundler plugins generally expose compiler options to you. + -TODO ### svelte.compile -TODO +```js +compiled: { + // `map` is a v3 sourcemap with toString()/toUrl() methods + js: { code: string, map: {...} }, + css: { code: string, map: {...} }, + ast: {...}, // ESTree-like syntax tree for the component, including HTML, CSS and JS + warnings: Array<{ + code: string, + message: string, + filename: string, + pos: number, + start: { line: number, column: number }, + end: { line: number, column: number }, + frame: string, + toString: () => string + }>, + vars: Array<{ + name: string, + export_name: string, + injected: boolean, + module: boolean, + mutated: boolean, + reassigned: boolean, + referenced: boolean, + writable: boolean + }>, + stats: { + timings: { [label]: number } + } +} = svelte.compile(source: string, options: { + format?: 'esm' | 'cjs', + name?: string, + filename?: string, + generate?: 'dom' | 'ssr' | false, + dev?: boolean, + immutable?: boolean, + hydratable?: boolean, + legacy?: boolean, + customElement?: { tag: string } | true, + css?: boolean, + preserveComments?: boolean, + outputFilename?: string, + cssOutputFilename?: string, + sveltePath?: string +}) +``` + +--- + +This is where the magic happens. `svelte.compile` takes your component source code, and turns it into a JavaScript module that exports a class. + +Don't worry if the signature above looks a little overwhelming — none of the options are required. + +```js +const svelte = require('svelte/compiler'); + +const { js } = svelte.compile(source); +console.log(js.code); +``` + +* TODO document this mess of options + + + +### svelte.preprocess + +```js +result: { + code: string, + dependencies: Array +} = svelte.preprocess( + source: string, + preprocessors: Array<{ + markup?: (input: { source: string, filename: string }) => Promise<{ + code: string, + dependencies?: Array + }>, + script?: (input: { source: string, attributes: Record, filename: string }) => Promise<{ + code: string, + dependencies?: Array + }>, + style?: (input: { source: string, attributes: Record, filename: string }) => Promise<{ + code: string, + dependencies?: Array + }> + }>, + options?: { + filename?: string + } +) +``` + +--- + +The `preprocess` function provides convenient hooks for arbitrarily transforming component source code. For example, it can be used to convert a `