Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace random import alias with constant #94

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
use swc_ecma_transforms::resolver;
use swc_ecma_utils::private_ident;
use swc_ecma_visit::{as_folder, VisitMutWith, VisitWith};
use uuid::Uuid;

mod bindings;
mod locate;
Expand All @@ -41,6 +40,8 @@ pub struct CodeMapPair {
pub map: String,
}

pub const IMPORT_ALIAS: &str = "template_fd9b2463e5f141cfb5666b64daa1f11a";

struct SourceMapConfig;
impl SourceMapGenConfig for SourceMapConfig {
fn file_name_to_source(&self, f: &swc_common::FileName) -> String {
Expand Down Expand Up @@ -120,11 +121,7 @@ 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!(IMPORT_ALIAS);
let mut needs_import = false;
parsed_module.visit_mut_with(&mut as_folder(transform::TransformVisitor::new(
&id,
Expand Down
5 changes: 2 additions & 3 deletions src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use difference::Changeset;
use regex::Regex;
use swc_common::comments::SingleThreadedComments;
use swc_common::{self, sync::Lrc, FileName, SourceMap};
use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};

use crate::Preprocessor;
use crate::IMPORT_ALIAS;

pub fn testcase(input: &str, expected: &str) -> Result<(), swc_ecma_parser::error::Error> {
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.code, "template_UUID");
let actual_santized = actual.code.replace(IMPORT_ALIAS, "template_UUID");
let normalized_expected = normalize(expected);
if actual_santized != normalized_expected {
panic!(
Expand Down
Loading