Skip to content

Commit

Permalink
fix(core): improve JS ESM detection (#2139)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jul 2, 2021
1 parent 51a5cfe commit 4b0ec01
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/improve-esm-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-codegen": patch
---

Improve ESM detection with regexes.
1 change: 1 addition & 0 deletions core/tauri-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ thiserror = "1"
walkdir = "2"
zstd = "0.9"
kuchiki = "0.8"
regex = "1"
24 changes: 19 additions & 5 deletions core/tauri-codegen/src/embedded_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use kuchiki::traits::*;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens, TokenStreamExt};
use regex::RegexSet;
use std::{
collections::HashMap,
ffi::OsStr,
Expand Down Expand Up @@ -187,11 +188,24 @@ impl EmbeddedAssets {
.any(|e| path.extension() == Some(OsStr::new(e)));
if is_javascript {
let js = String::from_utf8_lossy(&input).into_owned();
input = if [
"import{", "import*", "import ", "export{", "export*", "export ",
]
.iter()
.any(|t| js.contains(t))
input = if RegexSet::new(&[
// import keywords
"import\\{",
"import \\{",
"import\\*",
"import \\*",
"import (\"|');?$",
"import\\(",
"import (.|\n)+ from (\"|')([A-Za-z\\-]+)(\"|')",
// export keywords
"export\\{",
"export \\{",
"export\\*",
"export \\*",
"export (default|class|let|const|function)",
])
.unwrap()
.is_match(&js)
{
format!(
r#"
Expand Down

0 comments on commit 4b0ec01

Please sign in to comment.