File size: 7,247 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
use chrono::Utc;
use swc_core::{
common::{errors::HANDLER, Span, DUMMY_SP},
ecma::{
ast::*,
visit::{fold_pass, Fold, FoldWith},
},
};
pub fn page_config(is_development: bool, is_page_file: bool) -> impl Pass {
fold_pass(PageConfig {
is_development,
is_page_file,
..Default::default()
})
}
pub fn page_config_test() -> impl Pass {
fold_pass(PageConfig {
in_test: true,
is_page_file: true,
..Default::default()
})
}
#[derive(Debug, Default)]
struct PageConfig {
drop_bundle: bool,
in_test: bool,
is_development: bool,
is_page_file: bool,
}
const STRING_LITERAL_DROP_BUNDLE: &str = "__NEXT_DROP_CLIENT_FILE__";
const CONFIG_KEY: &str = "config";
/// TODO: Implement this as a [Pass] instead of a full visitor ([Fold])
impl Fold for PageConfig {
fn fold_module_items(&mut self, items: Vec<ModuleItem>) -> Vec<ModuleItem> {
let mut new_items = vec![];
for item in items {
new_items.push(item.fold_with(self));
if !self.is_development && self.drop_bundle {
let timestamp = match self.in_test {
true => String::from("mock_timestamp"),
false => Utc::now().timestamp().to_string(),
};
return vec![ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
decls: vec![VarDeclarator {
name: Pat::Ident(BindingIdent {
id: Ident {
sym: STRING_LITERAL_DROP_BUNDLE.into(),
..Default::default()
},
type_ann: None,
}),
init: Some(Box::new(Expr::Lit(Lit::Str(Str {
value: format!("{STRING_LITERAL_DROP_BUNDLE} {timestamp}").into(),
span: DUMMY_SP,
raw: None,
})))),
span: DUMMY_SP,
definite: false,
}],
span: DUMMY_SP,
kind: VarDeclKind::Const,
..Default::default()
}))))];
}
}
new_items
}
fn fold_export_decl(&mut self, export: ExportDecl) -> ExportDecl {
if let Decl::Var(var_decl) = &export.decl {
for decl in &var_decl.decls {
let mut is_config = false;
if let Pat::Ident(ident) = &decl.name {
if ident.id.sym == CONFIG_KEY {
is_config = true;
}
}
if is_config {
if let Some(expr) = &decl.init {
if let Expr::Object(obj) = &**expr {
for prop in &obj.props {
if let PropOrSpread::Prop(prop) = prop {
if let Prop::KeyValue(kv) = &**prop {
match &kv.key {
PropName::Ident(ident) => {
if &ident.sym == "amp" {
if let Expr::Lit(Lit::Bool(Bool {
value,
..
})) = &*kv.value
{
if *value && self.is_page_file {
self.drop_bundle = true;
}
} else if let Expr::Lit(Lit::Str(_)) =
&*kv.value
{
// Do not replace
// bundle
} else {
self.handle_error(
"Invalid value found.",
export.span,
);
}
}
}
_ => {
self.handle_error(
"Invalid property found.",
export.span,
);
}
}
} else {
self.handle_error(
"Invalid property or value.",
export.span,
);
}
} else {
self.handle_error(
"Property spread is not allowed.",
export.span,
);
}
}
} else {
self.handle_error("Expected config to be an object.", export.span);
}
} else {
self.handle_error("Expected config to be an object.", export.span);
}
}
}
}
export
}
fn fold_export_named_specifier(
&mut self,
specifier: ExportNamedSpecifier,
) -> ExportNamedSpecifier {
match &specifier.exported {
Some(ident) => {
if let ModuleExportName::Ident(ident) = ident {
if ident.sym == CONFIG_KEY {
self.handle_error("Config cannot be re-exported.", specifier.span)
}
}
}
None => {
if let ModuleExportName::Ident(ident) = &specifier.orig {
if ident.sym == CONFIG_KEY {
self.handle_error("Config cannot be re-exported.", specifier.span)
}
}
}
}
specifier
}
}
impl PageConfig {
fn handle_error(&mut self, details: &str, span: Span) {
if self.is_page_file {
let message = format!("Invalid page config export found. {details} \
See: https://nextjs.org/docs/messages/invalid-page-config");
HANDLER.with(|handler| handler.struct_span_err(span, &message).emit());
}
}
}
|