Skip to content

Commit

Permalink
Add option for inlining typeof window
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed May 24, 2024
1 parent 46fe083 commit 7b52e63
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ swc_core = { workspace = true, features = [
"ecma_transforms",
"ecma_transforms_module",
"ecma_transforms_react",
"ecma_transforms_optimization",
"ecma_transforms_typescript",
"ecma_transforms_proposal",
"ecma_quote",
Expand Down
19 changes: 18 additions & 1 deletion crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use std::{fmt::Debug, hash::Hash, sync::Arc};
use anyhow::Result;
use async_trait::async_trait;
use swc_core::{
atoms::JsWord,
base::SwcComments,
common::{chain, comments::Comments, util::take::Take, Mark, SourceMap},
common::{chain, collections::AHashMap, comments::Comments, util::take::Take, Mark, SourceMap},
ecma::{
ast::{Module, ModuleItem, Program, Script},
preset_env::{self, Targets},
transforms::{
base::{feature::FeatureFlag, helpers::inject_helpers, Assumptions},
optimization::inline_globals2,
react::react,
},
visit::{FoldWith, VisitMutWith},
Expand Down Expand Up @@ -39,6 +41,9 @@ pub enum EcmascriptInputTransform {
// swc.jsc.transform.react.runtime,
runtime: Vc<Option<String>>,
},
GlobalTypeofs {
window_value: String,
},
// These options are subset of swc_core::ecma::transforms::typescript::Config, but
// it doesn't derive `Copy` so repeating values in here
TypeScript {
Expand Down Expand Up @@ -134,6 +139,18 @@ impl EcmascriptInputTransform {
..
} = ctx;
match self {
EcmascriptInputTransform::GlobalTypeofs { window_value } => {
let mut typeofs: AHashMap<JsWord, JsWord> = Default::default();
let val = window_value.to_owned();
typeofs.insert("window".into(), JsWord::from(val));

program.visit_mut_with(&mut inline_globals2(
Default::default(),
Default::default(),
Default::default(),
Arc::new(typeofs),
));
}
EcmascriptInputTransform::React {
development,
refresh,
Expand Down
10 changes: 10 additions & 0 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl ModuleOptions {
import_externals,
ignore_dynamic_requests,
use_swc_css,
ref enable_typeof_window_inlining,
..
} = *module_options_context.await?;
if !rules.is_empty() {
Expand Down Expand Up @@ -130,6 +131,15 @@ impl ModuleOptions {
transforms.push(EcmascriptInputTransform::PresetEnv(env));
}

if let Some(enable_typeof_window_inlining) = enable_typeof_window_inlining {
transforms.push(EcmascriptInputTransform::GlobalTypeofs {
window_value: match enable_typeof_window_inlining {
TypeofWindow::Object => "object".to_string(),
TypeofWindow::Undefined => "undefined".to_string(),
},
});
}

let ts_transform = if let Some(options) = enable_typescript_transform {
let options = options.await?;
Some(EcmascriptInputTransform::TypeScript {
Expand Down
8 changes: 8 additions & 0 deletions crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ pub enum DecoratorsKind {
Ecma,
}

/// The types when replacing `typeof window` with a constant.
#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize)]
pub enum TypeofWindow {
Object,
Undefined,
}

/// Configuration options for the decorators transform.
/// This is not part of Typescript transform: while there are typescript
/// specific transforms (legay decorators), there is an ecma decorator transform
Expand Down Expand Up @@ -105,6 +112,7 @@ pub struct JsxTransformOptions {
#[derive(Default, Clone)]
#[serde(default)]
pub struct ModuleOptionsContext {
pub enable_typeof_window_inlining: Option<TypeofWindow>,
pub enable_jsx: Option<Vc<JsxTransformOptions>>,
pub enable_postcss_transform: Option<Vc<PostCssTransformOptions>>,
pub enable_webpack_loaders: Option<Vc<WebpackLoadersOptions>>,
Expand Down

0 comments on commit 7b52e63

Please sign in to comment.