From de9872717b0cc404583bf7662a5bfef1dcde2c0c Mon Sep 17 00:00:00 2001 From: Benjamin Gwynn Date: Thu, 22 Jun 2023 17:33:46 +0100 Subject: [PATCH 1/4] feat: add a way to compile from an AST Allows the developer to compile directly from an AST, returned previously from `svelte.parse`. There is currently no way to compile a modified AST to source code, this adds a function, `compileAST`, that provides that functionality. Notes: - The second argument to `new Component` is currently set to null, this might break something, but for our purposes seems to be working. - `@param {object}` could be typed better. - `compile` could call `compileAST` underneath so we don't have to copy/paste code. Hopefully someone has useful feedback! --- packages/svelte/src/compiler/compile/index.js | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index 4271ee6c27..d1192849e7 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -119,6 +119,42 @@ function validate_options(options, warnings) { } } +/** + * `compileAST` takes your component AST, and turns it into a JavaScript module that exports a class. + * + * @param {object} 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('parse'); + stats.stop('parse'); + 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. * From baaad28b5a0614ce7015fabb9b9776d5430c4d93 Mon Sep 17 00:00:00 2001 From: Benjamin Gwynn Date: Fri, 23 Jun 2023 13:54:07 +0100 Subject: [PATCH 2/4] fix: use correct type for AST on compileAST Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --- packages/svelte/src/compiler/compile/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index d1192849e7..8f7470e9a1 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -122,7 +122,7 @@ function validate_options(options, warnings) { /** * `compileAST` takes your component AST, and turns it into a JavaScript module that exports a class. * - * @param {object} ast + * @param {import('../interfaces.js').Ast} ast * @param {import('../interfaces.js').CompileOptions} options */ export function compileAST(ast, options = {}) { From a33e502380cd92d6dec081393a59682f880a7ee8 Mon Sep 17 00:00:00 2001 From: Benjamin Gwynn Date: Fri, 23 Jun 2023 13:55:03 +0100 Subject: [PATCH 3/4] fix: remove stats calls for "parse" in compileAST --- packages/svelte/src/compiler/compile/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index 8f7470e9a1..9862481a83 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -133,8 +133,6 @@ export function compileAST(ast, options = {}) { const stats = new Stats(); const warnings = []; validate_options(options, warnings); - stats.start('parse'); - stats.stop('parse'); stats.start('create component'); const component = new Component( ast, From aa5442630a4fd67243a495b15b9d5e617f5a441e Mon Sep 17 00:00:00 2001 From: Benjamin Gwynn Date: Fri, 23 Jun 2023 14:18:55 +0100 Subject: [PATCH 4/4] chore: prettier --- packages/svelte/src/compiler/compile/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/svelte/src/compiler/compile/index.js b/packages/svelte/src/compiler/compile/index.js index 9862481a83..1613f22561 100644 --- a/packages/svelte/src/compiler/compile/index.js +++ b/packages/svelte/src/compiler/compile/index.js @@ -152,7 +152,6 @@ export function compileAST(ast, options = {}) { return component.generate(result); } - /** * `compile` takes your component source code, and turns it into a JavaScript module that exports a class. *