Skip to content

feat: add a way to compile from an AST #8804

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/svelte/src/compiler/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ function validate_options(options, warnings) {
}
}

/**
* `compileAST` takes your component AST, and turns it into a JavaScript module that exports a class.
*
* @param {import('../interfaces.js').Ast} ast
* @param {import('../interfaces.js').CompileOptions} options
*/
export function compileAST(ast, options = {}) {
options = Object.assign(
{ generate: 'dom', dev: false, enableSourcemap: true, css: 'injected' },
options
);
const stats = new Stats();
const warnings = [];
validate_options(options, warnings);
stats.start('create component');
const component = new Component(
ast,
null,
options.name || get_name_from_filename(options.filename) || 'Component',
options,
stats,
warnings
);
stats.stop('create component');
const result =
options.generate === false
? null
: options.generate === 'ssr'
? render_ssr(component, options)
: render_dom(component, options);
return component.generate(result);
}

/**
* `compile` takes your component source code, and turns it into a JavaScript module that exports a class.
*
Expand Down