fix(core): improve JS ESM detection (#2139)

This commit is contained in:
Lucas Fernandes Nogueira
2021-07-02 00:00:40 -03:00
committed by GitHub
parent 51a5cfe4b5
commit 4b0ec01880
3 changed files with 25 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"tauri-codegen": patch
---
Improve ESM detection with regexes.

View File

@@ -22,3 +22,4 @@ thiserror = "1"
walkdir = "2"
zstd = "0.9"
kuchiki = "0.8"
regex = "1"

View File

@@ -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,
@@ -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#"