Unverified Commit 719116ac authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Allow export const in font loader (#40836)

Allow `export const font = Font()` syntax

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 2e02204d
...@@ -68,7 +68,7 @@ impl<'a> FontImportsGenerator<'a> { ...@@ -68,7 +68,7 @@ impl<'a> FontImportsGenerator<'a> {
None None
} }
fn check_var_decl(&mut self, var_decl: &VarDecl) { fn check_var_decl(&mut self, var_decl: &VarDecl) -> Option<Ident> {
if let Some(decl) = var_decl.decls.get(0) { if let Some(decl) = var_decl.decls.get(0) {
let ident = match &decl.name { let ident = match &decl.name {
Pat::Ident(ident) => Ok(ident.id.clone()), Pat::Ident(ident) => Ok(ident.id.clone()),
...@@ -79,8 +79,6 @@ impl<'a> FontImportsGenerator<'a> { ...@@ -79,8 +79,6 @@ impl<'a> FontImportsGenerator<'a> {
let import_decl = self.check_call_expr(call_expr); let import_decl = self.check_call_expr(call_expr);
if let Some(mut import_decl) = import_decl { if let Some(mut import_decl) = import_decl {
self.state.removeable_module_items.insert(var_decl.span.lo);
match var_decl.kind { match var_decl.kind {
VarDeclKind::Const => {} VarDeclKind::Const => {}
_ => { _ => {
...@@ -100,12 +98,14 @@ impl<'a> FontImportsGenerator<'a> { ...@@ -100,12 +98,14 @@ impl<'a> FontImportsGenerator<'a> {
import_decl.specifiers = import_decl.specifiers =
vec![ImportSpecifier::Default(ImportDefaultSpecifier { vec![ImportSpecifier::Default(ImportDefaultSpecifier {
span: DUMMY_SP, span: DUMMY_SP,
local: ident, local: ident.clone(),
})]; })];
self.state self.state
.font_imports .font_imports
.push(ModuleItem::ModuleDecl(ModuleDecl::Import(import_decl))); .push(ModuleItem::ModuleDecl(ModuleDecl::Import(import_decl)));
return Some(ident);
} }
Err(pattern) => { Err(pattern) => {
HANDLER.with(|handler| { HANDLER.with(|handler| {
...@@ -122,6 +122,7 @@ impl<'a> FontImportsGenerator<'a> { ...@@ -122,6 +122,7 @@ impl<'a> FontImportsGenerator<'a> {
} }
} }
} }
None
} }
} }
...@@ -129,8 +130,37 @@ impl<'a> Visit for FontImportsGenerator<'a> { ...@@ -129,8 +130,37 @@ impl<'a> Visit for FontImportsGenerator<'a> {
noop_visit_type!(); noop_visit_type!();
fn visit_module_item(&mut self, item: &ModuleItem) { fn visit_module_item(&mut self, item: &ModuleItem) {
if let ModuleItem::Stmt(Stmt::Decl(Decl::Var(var_decl))) = item { match item {
self.check_var_decl(var_decl); ModuleItem::Stmt(Stmt::Decl(Decl::Var(var_decl))) => {
if self.check_var_decl(var_decl).is_some() {
self.state.removeable_module_items.insert(var_decl.span.lo);
}
}
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(export_decl)) => {
if let Decl::Var(var_decl) = &export_decl.decl {
if let Some(ident) = self.check_var_decl(var_decl) {
self.state
.removeable_module_items
.insert(export_decl.span.lo);
self.state.font_exports.push(ModuleItem::ModuleDecl(
ModuleDecl::ExportNamed(NamedExport {
span: DUMMY_SP,
specifiers: vec![ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(ident),
span: DUMMY_SP,
exported: None,
is_type_only: false,
})],
src: None,
type_only: false,
asserts: None,
}),
));
}
}
}
_ => {}
} }
} }
} }
......
...@@ -39,6 +39,7 @@ pub struct State { ...@@ -39,6 +39,7 @@ pub struct State {
font_functions: AHashMap<Id, FontFunction>, font_functions: AHashMap<Id, FontFunction>,
removeable_module_items: FxHashSet<BytePos>, removeable_module_items: FxHashSet<BytePos>,
font_imports: Vec<ModuleItem>, font_imports: Vec<ModuleItem>,
font_exports: Vec<ModuleItem>,
font_functions_in_allowed_scope: FxHashSet<BytePos>, font_functions_in_allowed_scope: FxHashSet<BytePos>,
} }
...@@ -76,10 +77,11 @@ impl VisitMut for NextFontLoaders { ...@@ -76,10 +77,11 @@ impl VisitMut for NextFontLoaders {
// Remove marked module items // Remove marked module items
items.retain(|item| !self.state.removeable_module_items.contains(&item.span_lo())); items.retain(|item| !self.state.removeable_module_items.contains(&item.span_lo()));
// Add font imports // Add font imports and exports
let mut new_items = Vec::new(); let mut new_items = Vec::new();
new_items.append(&mut self.state.font_imports); new_items.append(&mut self.state.font_imports);
new_items.append(items); new_items.append(items);
new_items.append(&mut self.state.font_exports);
*items = new_items; *items = new_items;
} }
} }
......
import React from 'react'
import { Abel, Inter } from '@next/font/google'
export let firaCode = Abel()
export var inter = Inter()
import firaCode from "@next/font/google?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter";
import React from 'react';
export { firaCode };
export { inter };
x Font loader calls must be assigned to a const
,-[input.js:4:1]
4 | export let firaCode = Abel()
: ^^^^^^^^^^^^^^^^^^^^^
`----
x Font loader calls must be assigned to a const
,-[input.js:5:1]
5 | export var inter = Inter()
: ^^^^^^^^^^^^^^^^^^^
`----
const { a } = Inter({
variant: '400'
});
const [b] = Inter({
variant: '400'
});
const { e } = {}; const { e } = {};
import React from 'react'
import { Abel, Inter } from '@next/font/google'
export const firaCode = Abel()
export const inter = Inter()
import firaCode from "@next/font/google?pages/test.tsx;Abel";
import inter from "@next/font/google?pages/test.tsx;Inter";
import React from 'react';
export { firaCode };
export { inter };
...@@ -11,11 +11,11 @@ const openSans = Open_Sans() ...@@ -11,11 +11,11 @@ const openSans = Open_Sans()
const sourceCodePro = Source_Code_Pro({ display: 'swap', preload: false }) const sourceCodePro = Source_Code_Pro({ display: 'swap', preload: false })
const abel = Abel({ variant: '400', display: 'optional', preload: false }) const abel = Abel({ variant: '400', display: 'optional', preload: false })
const inter = Inter({ display: 'block', preload: true }) export const inter = Inter({ display: 'block', preload: true })
const roboto = Roboto({ variant: '400' }) export const roboto = Roboto({ variant: '400' })
const myLocalFont = localFont({ export const myLocalFont = localFont({
src: './my-font.woff2', src: './my-font.woff2',
}) })
export { openSans, sourceCodePro, abel, inter, roboto, myLocalFont } export { openSans, sourceCodePro, abel }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment