Skip to content

Commit 4b0ec01

Browse files
authored
fix(core): improve JS ESM detection (#2139)
1 parent 51a5cfe commit 4b0ec01

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

.changes/improve-esm-detection.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-codegen": patch
3+
---
4+
5+
Improve ESM detection with regexes.

core/tauri-codegen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ thiserror = "1"
2222
walkdir = "2"
2323
zstd = "0.9"
2424
kuchiki = "0.8"
25+
regex = "1"

core/tauri-codegen/src/embedded_assets.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use kuchiki::traits::*;
66
use proc_macro2::TokenStream;
77
use quote::{quote, ToTokens, TokenStreamExt};
8+
use regex::RegexSet;
89
use std::{
910
collections::HashMap,
1011
ffi::OsStr,
@@ -187,11 +188,24 @@ impl EmbeddedAssets {
187188
.any(|e| path.extension() == Some(OsStr::new(e)));
188189
if is_javascript {
189190
let js = String::from_utf8_lossy(&input).into_owned();
190-
input = if [
191-
"import{", "import*", "import ", "export{", "export*", "export ",
192-
]
193-
.iter()
194-
.any(|t| js.contains(t))
191+
input = if RegexSet::new(&[
192+
// import keywords
193+
"import\\{",
194+
"import \\{",
195+
"import\\*",
196+
"import \\*",
197+
"import (\"|');?$",
198+
"import\\(",
199+
"import (.|\n)+ from (\"|')([A-Za-z\\-]+)(\"|')",
200+
// export keywords
201+
"export\\{",
202+
"export \\{",
203+
"export\\*",
204+
"export \\*",
205+
"export (default|class|let|const|function)",
206+
])
207+
.unwrap()
208+
.is_match(&js)
195209
{
196210
format!(
197211
r#"

0 commit comments

Comments
 (0)