From 14ca443daabc4a705aa2f4e054d0c64b39275da3 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Wed, 6 Nov 2024 17:50:51 -0500 Subject: [PATCH 1/7] Change return type of process from string to { code, map } --- src/bindings.rs | 7 ++++--- src/lib.rs | 49 +++++++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index afd7669..68b9c71 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1,5 +1,6 @@ -use crate::{Options, Preprocessor as CorePreprocessor}; +use crate::{CodeMapPair, Options, Preprocessor as CorePreprocessor}; use js_sys::Reflect; +use serde_wasm_bindgen::to_value; use std::{fmt, path::PathBuf, str}; use swc_common::{ errors::Handler, @@ -123,13 +124,13 @@ impl Preprocessor { Self {} } - pub fn process(&self, src: String, options: JsValue) -> Result { + pub fn process(&self, src: String, options: JsValue) -> Result { let options = Options::new(options); let preprocessor = CorePreprocessor::new(); let result = preprocessor.process(&src, options); match result { - Ok(output) => Ok(output), + Ok(output) => Ok(to_value(&output).unwrap()), Err(err) => Err(as_javascript_error(err, preprocessor.source_map()).into()), } } diff --git a/src/lib.rs b/src/lib.rs index 96701a6..15dc4ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,11 @@ use base64::{engine::general_purpose, Engine as _}; use std::path::PathBuf; use swc_common::comments::SingleThreadedComments; use swc_common::source_map::SourceMapGenConfig; -use swc_common::{self, sync::Lrc, FileName, SourceMap, Mark}; +use swc_common::{self, sync::Lrc, FileName, Mark, SourceMap}; use swc_core::common::GLOBALS; use swc_ecma_ast::{ - Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl, - ModuleExportName, ModuleItem, + Ident, ImportDecl, ImportNamedSpecifier, ImportSpecifier, Module, ModuleDecl, ModuleExportName, + ModuleItem, }; use swc_ecma_codegen::Emitter; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig}; @@ -21,9 +21,9 @@ use swc_ecma_visit::{as_folder, VisitMutWith, VisitWith}; use uuid::Uuid; mod bindings; +mod locate; mod snippets; mod transform; -mod locate; #[derive(Default)] pub struct Options { @@ -36,6 +36,11 @@ pub struct Preprocessor { comments: SingleThreadedComments, } +pub struct CodeMapPair { + pub code: String, + pub map: String, +} + struct SourceMapConfig; impl SourceMapGenConfig for SourceMapConfig { fn file_name_to_source(&self, f: &swc_common::FileName) -> String { @@ -47,7 +52,6 @@ impl SourceMapGenConfig for SourceMapConfig { } } - impl Preprocessor { pub fn new() -> Self { Self { @@ -93,7 +97,7 @@ impl Preprocessor { &self, src: &str, options: Options, - ) -> Result { + ) -> Result { let target_specifier = "template"; let target_module = "@ember/template-compiler"; let filename = match options.filename { @@ -116,7 +120,11 @@ impl Preprocessor { GLOBALS.set(&Default::default(), || { let mut parsed_module = parser.parse_module()?; - let id = private_ident!(format!("{}_{}", target_specifier, Uuid::new_v4().to_string().replace("-", ""))); + let id = private_ident!(format!( + "{}_{}", + target_specifier, + Uuid::new_v4().to_string().replace("-", "") + )); let mut needs_import = false; parsed_module.visit_mut_with(&mut as_folder(transform::TransformVisitor::new( &id, @@ -132,13 +140,16 @@ impl Preprocessor { parsed_module.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); - Ok(self.print(&parsed_module, options.inline_source_map)) + let codemap = self.print(&parsed_module, options.inline_source_map); + + Ok(codemap) }) } - fn print(&self, module: &Module, inline_source_map: bool) -> String { + fn print(&self, module: &Module, inline_source_map: bool) -> CodeMapPair { let mut buf = vec![]; let mut srcmap = vec![]; + let mut source_map_buffer = vec![]; let mut emitter = Emitter { cfg: Default::default(), cm: self.source_map.clone(), @@ -152,27 +163,30 @@ impl Preprocessor { }; emitter.emit_module(module).unwrap(); - if inline_source_map { - let mut source_map_buffer = vec![]; - self.source_map() - .build_source_map_with_config(&srcmap, None, SourceMapConfig {}) - .to_writer(&mut source_map_buffer) - .unwrap(); + self.source_map() + .build_source_map_with_config(&srcmap, None, SourceMapConfig {}) + .to_writer(&mut source_map_buffer) + .unwrap(); + if inline_source_map { let mut comment = "//# sourceMappingURL=data:application/json;base64," .to_owned() .into_bytes(); buf.append(&mut comment); let mut encoded = general_purpose::URL_SAFE_NO_PAD - .encode(source_map_buffer) + .encode(source_map_buffer.clone()) .into_bytes(); buf.append(&mut encoded); } let s = String::from_utf8_lossy(&buf); - s.to_string() + + CodeMapPair { + code: s.to_string(), + map: String::from_utf8(source_map_buffer.clone()).unwrap(), + } } pub fn source_map(&self) -> Lrc { @@ -209,7 +223,6 @@ fn insert_import( #[cfg(test)] mod test_helpers; - macro_rules! testcase { ($test_name:ident, $input:expr, $expected:expr) => { #[test] From 1c3725b5d1fe5c35d256dd046682544774ec8727 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 7 Nov 2024 16:57:08 -0500 Subject: [PATCH 2/7] progress? --- src/bindings.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index 68b9c71..20011d1 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1,4 +1,4 @@ -use crate::{CodeMapPair, Options, Preprocessor as CorePreprocessor}; +use crate::{Options, Preprocessor as CorePreprocessor}; use js_sys::Reflect; use serde_wasm_bindgen::to_value; use std::{fmt, path::PathBuf, str}; @@ -55,6 +55,20 @@ impl Options { } } +#[wasm_bindgen] +pub struct CodeMapPair { + pub code: String, + pub map: String, +} + +#[wasm_bindgen] +impl CodeMapPair { + #[wasm_bindgen(constructor)] + pub fn new(code: String, map: String) -> Self { + Self { code, map } + } +} + #[wasm_bindgen] pub struct Preprocessor { // TODO: reusing this between calls result in incorrect spans; there may @@ -130,7 +144,7 @@ impl Preprocessor { let result = preprocessor.process(&src, options); match result { - Ok(output) => Ok(to_value(&output).unwrap()), + Ok(output) => Ok(CodeMapPair::new(output.code, output.map)), Err(err) => Err(as_javascript_error(err, preprocessor.source_map()).into()), } } From 1dd0aa4aab0a083e21dc5b0394fa81555306aa82 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 7 Nov 2024 17:03:59 -0500 Subject: [PATCH 3/7] omg it compiled --- src/bindings.rs | 3 +-- src/main.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index 20011d1..321fe2e 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -1,6 +1,5 @@ use crate::{Options, Preprocessor as CorePreprocessor}; use js_sys::Reflect; -use serde_wasm_bindgen::to_value; use std::{fmt, path::PathBuf, str}; use swc_common::{ errors::Handler, @@ -55,7 +54,7 @@ impl Options { } } -#[wasm_bindgen] +#[wasm_bindgen(getter_with_clone)] pub struct CodeMapPair { pub code: String, pub map: String, diff --git a/src/main.rs b/src/main.rs index 475bf03..7e43294 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn main() { ); match result { - Ok(output) => println!("{}", output), + Ok(output) => println!("{}", output.code), Err(err) => { let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(p.source_map())); From 2dc4dcc3e2eec4b47840fc06aae3f87fbf7a1093 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:17:18 -0500 Subject: [PATCH 4/7] Update test-helpers to know about .code --- src/test_helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test_helpers.rs b/src/test_helpers.rs index 0c85c97..65f61d0 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -11,7 +11,7 @@ pub fn testcase(input: &str, expected: &str) -> Result<(), swc_ecma_parser::erro let re = Regex::new(r"template_[0-9a-f]{32}").unwrap(); let p = Preprocessor::new(); let actual = p.process(input, Default::default())?; - let actual_santized = re.replace_all(&actual, "template_UUID"); + let actual_santized = re.replace_all(&actual.code, "template_UUID"); let normalized_expected = normalize(expected); if actual_santized != normalized_expected { panic!( From e5146d0f05f86360bdb40d2fc6f253026ed9212d Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:56:27 -0500 Subject: [PATCH 5/7] Node tests pass --- package-lock.json | 4 ++-- test/parse.test.js | 2 +- test/process.test.js | 6 +++--- test/require.test.cjs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0adcea7..51c43f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "content-tag", - "version": "2.0.2", + "version": "2.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "content-tag", - "version": "2.0.2", + "version": "2.0.3", "license": "MIT", "devDependencies": { "@arethetypeswrong/cli": "^0.13.2", diff --git a/test/parse.test.js b/test/parse.test.js index ac4b25a..2734c3e 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -173,7 +173,7 @@ describe(`parse`, function () { p.process( `const thing = "face";