Skip to content

Commit

Permalink
Merge 741c460 into 5f7a7ab
Browse files Browse the repository at this point in the history
  • Loading branch information
wbinnssmith committed Nov 17, 2022
2 parents 5f7a7ab + 741c460 commit bc3f650
Show file tree
Hide file tree
Showing 20 changed files with 2,607 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub async fn get_client_module_options_context(
// we try resolve it once at the root and pass down a context to all
// the modules.
enable_emotion: true,
enable_next_font: true,
enable_react_refresh,
enable_styled_components: true,
enable_styled_jsx: true,
Expand Down Expand Up @@ -127,7 +128,7 @@ pub async fn add_next_transforms_to_pages(
]),
]),
vec![ModuleRuleEffect::AddEcmascriptTransforms(
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::NextJs]),
EcmascriptInputTransformsVc::cell(vec![EcmascriptInputTransform::NextJsPageSsr]),
)],
));
Ok(module_options_context.cell())
Expand Down
15 changes: 15 additions & 0 deletions crates/next-font/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "next-font"
version = "0.1.0"
description = "TBD"
license = "MPL-2.0"
edition = "2021"

[lib]
bench = false

[dependencies]
fxhash = "0.2.1"
serde = "1.0.136"
serde_json = "1.0.85"
swc_core = { workspace = true }
34 changes: 34 additions & 0 deletions crates/next-font/src/find_functions_outside_module_scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use swc_core::{
common::errors::HANDLER,
ecma::{
ast::*,
visit::{noop_visit_type, Visit},
},
};

pub struct FindFunctionsOutsideModuleScope<'a> {
pub state: &'a super::State,
}

impl<'a> Visit for FindFunctionsOutsideModuleScope<'a> {
noop_visit_type!();

fn visit_ident(&mut self, ident: &Ident) {
if self.state.font_functions.get(&ident.to_id()).is_some()
&& self
.state
.font_functions_in_allowed_scope
.get(&ident.span.lo)
.is_none()
{
HANDLER.with(|handler| {
handler
.struct_span_err(
ident.span,
"Font loaders must be called and assigned to a const in the module scope",
)
.emit()
});
}
}
}
71 changes: 71 additions & 0 deletions crates/next-font/src/font_functions_collector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use swc_core::{
common::errors::HANDLER,
ecma::{
ast::*,
atoms::JsWord,
visit::{noop_visit_type, Visit},
},
};

pub struct FontFunctionsCollector<'a> {
pub font_loaders: &'a [JsWord],
pub state: &'a mut super::State,
}

impl<'a> Visit for FontFunctionsCollector<'a> {
noop_visit_type!();

fn visit_import_decl(&mut self, import_decl: &ImportDecl) {
if self.font_loaders.contains(&import_decl.src.value) {
self.state
.removeable_module_items
.insert(import_decl.span.lo);
for specifier in &import_decl.specifiers {
match specifier {
ImportSpecifier::Named(ImportNamedSpecifier {
local, imported, ..
}) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);

let function_name = if let Some(ModuleExportName::Ident(ident)) = imported {
ident.sym.clone()
} else {
local.sym.clone()
};
self.state.font_functions.insert(
local.to_id(),
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name: Some(function_name),
},
);
}
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. }) => {
self.state
.font_functions_in_allowed_scope
.insert(local.span.lo);
self.state.font_functions.insert(
local.to_id(),
super::FontFunction {
loader: import_decl.src.value.clone(),
function_name: None,
},
);
}
ImportSpecifier::Namespace(_) => {
HANDLER.with(|handler| {
handler
.struct_span_err(
import_decl.span,
"Font loaders can't have namespace imports",
)
.emit()
});
}
}
}
}
}
}

0 comments on commit bc3f650

Please sign in to comment.