From 836e0e02a1bb262053d176a8a4d03a12f48d1452 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Feb 2024 16:30:55 -0500 Subject: [PATCH 1/4] feat: support jsx precompile --- js/mod.ts | 2 +- .../jsx/jsx_type_react/bundle.js | 4 +--- .../transpile/local/jsx/main.tsx | 4 +--- tests/jsx_test.ts | 12 +++++++++++ wasm/src/lib.rs | 21 ++++++++++++------- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/js/mod.ts b/js/mod.ts index cd2e7ae..ea399b7 100644 --- a/js/mod.ts +++ b/js/mod.ts @@ -129,7 +129,7 @@ export interface CompilerOptions { inlineSources?: boolean; /** Controls how JSX constructs are emitted in JavaScript files. This only * affects output of JS files that started in `.jsx` or `.tsx` files. */ - jsx?: "preserve" | "react-jsx" | "react-jsxdev" | "react-native" | "react"; + jsx?: "precompile" | "preserve" | "react-jsx" | "react-jsxdev" | "react-native" | "react"; /** Changes the function called in `.js` files when compiling JSX Elements * using the classic JSX runtime. The most common change is to use `"h"` or * `"preact.h"`. */ diff --git a/tests/__snapshots__/jsx/jsx_type_react/bundle.js b/tests/__snapshots__/jsx/jsx_type_react/bundle.js index f76473a..acc19f5 100644 --- a/tests/__snapshots__/jsx/jsx_type_react/bundle.js +++ b/tests/__snapshots__/jsx/jsx_type_react/bundle.js @@ -1,3 +1 @@ -React.createElement("div", { - id: "helloWorld" -}); +
; diff --git a/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx b/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx index 0a062cb..5c970c5 100644 --- a/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx +++ b/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx @@ -1,3 +1 @@ -const helloWorld = /*#__PURE__*/ React.createElement("div", { - id: "helloWorld" -}); +const helloWorld =
; diff --git a/tests/jsx_test.ts b/tests/jsx_test.ts index 7870814..af5c08d 100644 --- a/tests/jsx_test.ts +++ b/tests/jsx_test.ts @@ -35,6 +35,18 @@ Deno.test({ ), }); +Deno.test({ + name: "jsx type react", + fn: testTranspileAndBundle( + resolveFixture("jsx/main.tsx"), + { + compilerOptions: { + jsx: "precompile", + }, + }, + ), +}); + Deno.test({ name: "jsx type preserve", fn: testTranspileAndBundle( diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index f96b867..d9955d5 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -61,6 +61,16 @@ impl From for EmitOptions { _ => ImportsNotUsedAsValues::Remove, }; + // copied from the CLI + let (transform_jsx, jsx_automatic, jsx_development, precompile_jsx) = + match options.jsx.as_str() { + "react" => (true, false, false, false), + "react-jsx" => (true, true, false, false), + "react-jsxdev" => (true, true, true, false), + "precompile" => (false, false, false, true), + _ => (false, false, false, false), + }; + Self { use_decorators_proposal: !options.experimental_decorators, use_ts_decorators: options.experimental_decorators, @@ -70,16 +80,13 @@ impl From for EmitOptions { inline_sources: options.inline_sources, jsx_factory: options.jsx_factory, jsx_fragment_factory: options.jsx_fragment_factory, - transform_jsx: options.jsx == "react" - || options.jsx == "react-jsx" - || options.jsx == "react-jsxdev", + transform_jsx, var_decl_imports: false, source_map: options.source_map, - jsx_automatic: options.jsx == "react-jsx" - || options.jsx == "react-jsxdev", - jsx_development: options.jsx == "react-jsxdev", + jsx_automatic, + jsx_development, jsx_import_source: options.jsx_import_source, - precompile_jsx: false, + precompile_jsx, } } } From 0d0026b698dddd44c314dcaa80a620c3ee64a2af Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Feb 2024 16:33:22 -0500 Subject: [PATCH 2/4] Update --- js/mod.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/js/mod.ts b/js/mod.ts index ea399b7..c3d1b44 100644 --- a/js/mod.ts +++ b/js/mod.ts @@ -129,7 +129,13 @@ export interface CompilerOptions { inlineSources?: boolean; /** Controls how JSX constructs are emitted in JavaScript files. This only * affects output of JS files that started in `.jsx` or `.tsx` files. */ - jsx?: "precompile" | "preserve" | "react-jsx" | "react-jsxdev" | "react-native" | "react"; + jsx?: + | "precompile" + | "preserve" + | "react-jsx" + | "react-jsxdev" + | "react-native" + | "react"; /** Changes the function called in `.js` files when compiling JSX Elements * using the classic JSX runtime. The most common change is to use `"h"` or * `"preact.h"`. */ From 2c3261abdccc38bf30d80997bfcaec90f5ffba48 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Feb 2024 16:49:45 -0500 Subject: [PATCH 3/4] Fix --- rs-lib/src/emit.rs | 2 +- .../jsx/jsx_type_precompile/deno.json | 3 +++ .../jsx_type_precompile/local/jsx/main.tsx | 5 ++++ .../jsx/jsx_type_precompile/modules.json | 3 +++ .../jsx_type_precompile/transpile/deno.json | 3 +++ .../transpile/local/jsx/main.tsx | 5 ++++ .../transpile/modules.json | 3 +++ .../jsx/jsx_type_preserve/deno.json | 3 +++ .../jsx/jsx_type_preserve/local/jsx/main.tsx | 1 + .../jsx/jsx_type_preserve/modules.json | 3 +++ .../jsx/jsx_type_react/bundle.js | 4 +++- .../transpile/local/jsx/main.tsx | 4 +++- tests/jsx_test.ts | 5 ++-- wasm/src/lib.rs | 24 +++++++++---------- 14 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 tests/__snapshots__/jsx/jsx_type_precompile/deno.json create mode 100644 tests/__snapshots__/jsx/jsx_type_precompile/local/jsx/main.tsx create mode 100644 tests/__snapshots__/jsx/jsx_type_precompile/modules.json create mode 100644 tests/__snapshots__/jsx/jsx_type_precompile/transpile/deno.json create mode 100644 tests/__snapshots__/jsx/jsx_type_precompile/transpile/local/jsx/main.tsx create mode 100644 tests/__snapshots__/jsx/jsx_type_precompile/transpile/modules.json create mode 100644 tests/__snapshots__/jsx/jsx_type_preserve/deno.json create mode 100644 tests/__snapshots__/jsx/jsx_type_preserve/local/jsx/main.tsx create mode 100644 tests/__snapshots__/jsx/jsx_type_preserve/modules.json diff --git a/rs-lib/src/emit.rs b/rs-lib/src/emit.rs index 3868c0c..379c3ae 100644 --- a/rs-lib/src/emit.rs +++ b/rs-lib/src/emit.rs @@ -203,7 +203,7 @@ pub fn bundle_graph( ); let output = bundler .bundle(entries) - .context("Unable to output during bundling.")?; + .context("Unable to output during bundling")?; let mut buf = Vec::new(); let mut srcmap = Vec::new(); { diff --git a/tests/__snapshots__/jsx/jsx_type_precompile/deno.json b/tests/__snapshots__/jsx/jsx_type_precompile/deno.json new file mode 100644 index 0000000..f6ca845 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_precompile/deno.json @@ -0,0 +1,3 @@ +{ + "imports": {} +} diff --git a/tests/__snapshots__/jsx/jsx_type_precompile/local/jsx/main.tsx b/tests/__snapshots__/jsx/jsx_type_precompile/local/jsx/main.tsx new file mode 100644 index 0000000..df396c6 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_precompile/local/jsx/main.tsx @@ -0,0 +1,5 @@ +import { jsxTemplate as _jsxTemplate } from "jsx-precompile/jsx-runtime"; +const $$_tpl_1 = [ + '
' +]; +const helloWorld = _jsxTemplate($$_tpl_1); diff --git a/tests/__snapshots__/jsx/jsx_type_precompile/modules.json b/tests/__snapshots__/jsx/jsx_type_precompile/modules.json new file mode 100644 index 0000000..f7b4a69 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_precompile/modules.json @@ -0,0 +1,3 @@ +{ + "file:///jsx/main.tsx": "local/jsx/main.tsx" +} diff --git a/tests/__snapshots__/jsx/jsx_type_precompile/transpile/deno.json b/tests/__snapshots__/jsx/jsx_type_precompile/transpile/deno.json new file mode 100644 index 0000000..f6ca845 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_precompile/transpile/deno.json @@ -0,0 +1,3 @@ +{ + "imports": {} +} diff --git a/tests/__snapshots__/jsx/jsx_type_precompile/transpile/local/jsx/main.tsx b/tests/__snapshots__/jsx/jsx_type_precompile/transpile/local/jsx/main.tsx new file mode 100644 index 0000000..df396c6 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_precompile/transpile/local/jsx/main.tsx @@ -0,0 +1,5 @@ +import { jsxTemplate as _jsxTemplate } from "jsx-precompile/jsx-runtime"; +const $$_tpl_1 = [ + '
' +]; +const helloWorld = _jsxTemplate($$_tpl_1); diff --git a/tests/__snapshots__/jsx/jsx_type_precompile/transpile/modules.json b/tests/__snapshots__/jsx/jsx_type_precompile/transpile/modules.json new file mode 100644 index 0000000..f7b4a69 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_precompile/transpile/modules.json @@ -0,0 +1,3 @@ +{ + "file:///jsx/main.tsx": "local/jsx/main.tsx" +} diff --git a/tests/__snapshots__/jsx/jsx_type_preserve/deno.json b/tests/__snapshots__/jsx/jsx_type_preserve/deno.json new file mode 100644 index 0000000..f6ca845 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_preserve/deno.json @@ -0,0 +1,3 @@ +{ + "imports": {} +} diff --git a/tests/__snapshots__/jsx/jsx_type_preserve/local/jsx/main.tsx b/tests/__snapshots__/jsx/jsx_type_preserve/local/jsx/main.tsx new file mode 100644 index 0000000..5c970c5 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_preserve/local/jsx/main.tsx @@ -0,0 +1 @@ +const helloWorld =
; diff --git a/tests/__snapshots__/jsx/jsx_type_preserve/modules.json b/tests/__snapshots__/jsx/jsx_type_preserve/modules.json new file mode 100644 index 0000000..f7b4a69 --- /dev/null +++ b/tests/__snapshots__/jsx/jsx_type_preserve/modules.json @@ -0,0 +1,3 @@ +{ + "file:///jsx/main.tsx": "local/jsx/main.tsx" +} diff --git a/tests/__snapshots__/jsx/jsx_type_react/bundle.js b/tests/__snapshots__/jsx/jsx_type_react/bundle.js index acc19f5..f76473a 100644 --- a/tests/__snapshots__/jsx/jsx_type_react/bundle.js +++ b/tests/__snapshots__/jsx/jsx_type_react/bundle.js @@ -1 +1,3 @@ -
; +React.createElement("div", { + id: "helloWorld" +}); diff --git a/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx b/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx index 5c970c5..0a062cb 100644 --- a/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx +++ b/tests/__snapshots__/jsx/jsx_type_react/transpile/local/jsx/main.tsx @@ -1 +1,3 @@ -const helloWorld =
; +const helloWorld = /*#__PURE__*/ React.createElement("div", { + id: "helloWorld" +}); diff --git a/tests/jsx_test.ts b/tests/jsx_test.ts index af5c08d..688ab54 100644 --- a/tests/jsx_test.ts +++ b/tests/jsx_test.ts @@ -36,12 +36,13 @@ Deno.test({ }); Deno.test({ - name: "jsx type react", - fn: testTranspileAndBundle( + name: "jsx type precompile", + fn: testTranspile( resolveFixture("jsx/main.tsx"), { compilerOptions: { jsx: "precompile", + jsxImportSource: "jsx-precompile" }, }, ), diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index d9955d5..30061c0 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -191,10 +191,10 @@ pub async fn bundle( let compiler_options: CompilerOptions = serde_wasm_bindgen::from_value::< Option, >(maybe_compiler_options) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))? + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))? .unwrap_or_default(); let root = ModuleSpecifier::parse(&root) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?; + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))?; let mut loader = JsLoader::new(load); let emit_options: EmitOptions = compiler_options.into(); let bundle_type = match maybe_bundle_type.as_deref() { @@ -209,13 +209,13 @@ pub async fn bundle( let maybe_import_map = serde_wasm_bindgen::from_value::< Option, >(maybe_import_map) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))? + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))? .map(|js_input| { let result: anyhow::Result = js_input.try_into(); result }) .transpose() - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?; + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))?; let result = deno_emit::bundle( root, @@ -229,13 +229,13 @@ pub async fn bundle( }, ) .await - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?; + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))?; serde_wasm_bindgen::to_value(&SerializableBundleEmit { code: result.code, maybe_map: result.maybe_map, }) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string()))) + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err)))) } #[wasm_bindgen] @@ -249,23 +249,23 @@ pub async fn transpile( let compiler_options: CompilerOptions = serde_wasm_bindgen::from_value::< Option, >(maybe_compiler_options) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))? + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))? .unwrap_or_default(); let root = ModuleSpecifier::parse(&root) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?; + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))?; let mut loader = JsLoader::new(load); let emit_options: EmitOptions = compiler_options.into(); let maybe_import_map = serde_wasm_bindgen::from_value::< Option, >(maybe_import_map) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))? + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))? .map(|js_input| { let result: anyhow::Result = js_input.try_into(); result }) .transpose() - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?; + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))?; let map = deno_emit::transpile( root, @@ -274,8 +274,8 @@ pub async fn transpile( TranspileOptions { emit_options }, ) .await - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string())))?; + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err))))?; serde_wasm_bindgen::to_value(&map) - .map_err(|err| JsValue::from(js_sys::Error::new(&err.to_string()))) + .map_err(|err| JsValue::from(js_sys::Error::new(&format!("{:#}", err)))) } From 6462d30acebbf5c3bc402c0f63b181aa64db7633 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Feb 2024 16:57:26 -0500 Subject: [PATCH 4/4] sigh --- tests/jsx_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/jsx_test.ts b/tests/jsx_test.ts index 688ab54..95086ec 100644 --- a/tests/jsx_test.ts +++ b/tests/jsx_test.ts @@ -42,7 +42,7 @@ Deno.test({ { compilerOptions: { jsx: "precompile", - jsxImportSource: "jsx-precompile" + jsxImportSource: "jsx-precompile", }, }, ),