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

Add option for inlining typeof window #8211

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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();
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
typeofs.insert("window".into(), JsWord::from(val));
timneutkens marked this conversation as resolved.
Show resolved Hide resolved

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
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
Loading