Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions crates/next-core/src/next_font/google/font_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
issue::NextFontIssue,
util::{FontFamilyType, get_scoped_font_family},
},
util::load_next_js_templateon,
util::load_next_js_json_file,
};

/// An entry in the Google fonts metrics map
Expand Down Expand Up @@ -45,7 +45,7 @@ struct Fallback {
// This JSON file is large, so we cache it in turbotasks
#[turbo_tasks::function]
async fn load_font_metrics(project_root: FileSystemPath) -> Result<Vc<FontMetricsMap>> {
let data: FontMetricsMap = load_next_js_templateon(
let data: FontMetricsMap = load_next_js_json_file(
project_root,
rcstr!("dist/server/capsize-font-metrics.json"),
)
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/next_font/google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use super::{
};
use crate::{
embed_js::next_js_file_path, mode::NextMode, next_app::metadata::split_extension,
next_font::issue::NextFontIssue, util::load_next_js_templateon,
next_font::issue::NextFontIssue, util::load_next_js_json_file,
};

pub mod font_fallback;
Expand Down Expand Up @@ -466,7 +466,7 @@ impl ImportMappingReplacement for NextFontGoogleFontFileReplacer {

#[turbo_tasks::function]
async fn load_font_data(project_root: FileSystemPath) -> Result<Vc<FontData>> {
let data: FontData = load_next_js_templateon(
let data: FontData = load_next_js_json_file(
project_root,
rcstr!("dist/compiled/@next/font/dist/google/font-data.json"),
)
Expand Down
6 changes: 3 additions & 3 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use crate::{
},
util::{
NextRuntime, OptionEnvMap, defines, foreign_code_context_condition,
get_transpiled_packages, internal_assets_conditions, load_next_js_templateon,
get_transpiled_packages, internal_assets_conditions, load_next_js_jsonc_file,
module_styles_rule_condition,
},
};
Expand Down Expand Up @@ -162,9 +162,9 @@ pub async fn get_server_resolve_options_context(
.await?;

// Always load these predefined packages as external.
let mut external_packages: Vec<RcStr> = load_next_js_templateon(
let mut external_packages: Vec<RcStr> = load_next_js_jsonc_file(
project_path.clone(),
rcstr!("dist/lib/server-external-packages.json"),
rcstr!("dist/lib/server-external-packages.jsonc"),
)
.await?;

Expand Down
43 changes: 28 additions & 15 deletions crates/next-core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{fmt::Display, str::FromStr};

use anyhow::{Result, bail};
use anyhow::{Result, anyhow, bail};
use next_taskless::expand_next_js_template;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, NonLocalValue, TaskInput, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{
self, File, FileContent, FileSystem, FileSystemPath, json::parse_json_rope_with_source_context,
rope::Rope,
self, File, FileContent, FileJsonContent, FileSystem, FileSystemPath, rope::Rope,
};
use turbopack::module_options::RuleCondition;
use turbopack_core::{
Expand Down Expand Up @@ -118,7 +117,7 @@ pub async fn get_transpiled_packages(
) -> Result<Vc<Vec<RcStr>>> {
let mut transpile_packages: Vec<RcStr> = next_config.transpile_packages().owned().await?;

let default_transpiled_packages: Vec<RcStr> = load_next_js_templateon(
let default_transpiled_packages: Vec<RcStr> = load_next_js_json_file(
project_path,
rcstr!("dist/lib/default-transpiled-packages.json"),
)
Expand Down Expand Up @@ -288,24 +287,38 @@ async fn virtual_next_js_template_path(
.join(&format!("{NEXT_TEMPLATE_PATH}/{file}"))
}

pub async fn load_next_js_templateon<T: DeserializeOwned>(
pub async fn load_next_js_json_file<T: DeserializeOwned>(
project_path: FileSystemPath,
path: RcStr,
sub_path: RcStr,
) -> Result<T> {
let file_path = get_next_package(project_path.clone()).await?.join(&path)?;
let file_path = get_next_package(project_path.clone())
.await?
.join(&sub_path)?;

let content = &*file_path.read().await?;

let FileContent::Content(file) = content else {
bail!(
"Expected file content at {}",
file_path.value_to_string().await?
);
};
match content.parse_json_ref() {
FileJsonContent::Unparsable(e) => Err(anyhow!("File is not valid JSON: {}", e)),
FileJsonContent::NotFound => Err(anyhow!("File not found")),
FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?),
}
}

let result: T = parse_json_rope_with_source_context(file.content())?;
pub async fn load_next_js_jsonc_file<T: DeserializeOwned>(
project_path: FileSystemPath,
sub_path: RcStr,
) -> Result<T> {
let file_path = get_next_package(project_path.clone())
.await?
.join(&sub_path)?;

Ok(result)
let content = &*file_path.read().await?;

match content.parse_json_with_comments_ref() {
FileJsonContent::Unparsable(e) => Err(anyhow!("File is not valid JSON: {}", e)),
FileJsonContent::NotFound => Err(anyhow!("File not found")),
FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?),
}
}

pub fn styles_rule_condition() -> RuleCondition {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const nextConfig = {
module.exports = nextConfig
```

Next.js includes a [short list of popular packages](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json) that currently are working on compatibility and automatically opt-ed out:
Next.js includes a [short list of popular packages](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.jsonc) that currently are working on compatibility and automatically opt-ed out:

- `@appsignal/nodejs`
- `@aws-sdk/client-s3`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const nextConfig = {
module.exports = nextConfig
```

Next.js includes a [short list of popular packages](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json) that currently are working on compatibility and automatically opt-ed out:
Next.js includes a [short list of popular packages](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.jsonc) that currently are working on compatibility and automatically opt-ed out:

- `@appsignal/nodejs`
- `@aws-sdk/client-s3`
Expand Down
10 changes: 8 additions & 2 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { yellow, bold } from '../lib/picocolors'
import crypto from 'crypto'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import path from 'path'
import fs from 'fs'

import { getDefineEnv } from './define-env'
import { escapeStringRegexp } from '../shared/lib/escape-regexp'
Expand Down Expand Up @@ -95,14 +96,19 @@ import type { NextBuildContext } from './build-context'
import type { RootParamsLoaderOpts } from './webpack/loaders/next-root-params-loader'
import type { InvalidImportLoaderOpts } from './webpack/loaders/next-invalid-import-error-loader'
import { defaultOverrides } from '../server/require-hook'
import JSON5 from 'next/dist/compiled/json5'

type ExcludesFalse = <T>(x: T | false) => x is T
type ClientEntries = {
[key: string]: string | string[]
}

const EXTERNAL_PACKAGES =
require('../lib/server-external-packages.json') as string[]
const EXTERNAL_PACKAGES = JSON5.parse(
fs.readFileSync(
path.join(__dirname, '../lib/server-external-packages.jsonc'),
'utf8'
)
) as string[]

const DEFAULT_TRANSPILED_PACKAGES =
require('../lib/default-transpiled-packages.json') as string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@
"vscode-oniguruma",
"webpack",
"websocket",
"zeromq"
"zeromq",
]
1 change: 1 addition & 0 deletions packages/next/taskfile-swc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = function (task) {
if (
file.base.endsWith('.d.ts') ||
file.base.endsWith('.json') ||
file.base.endsWith('.jsonc') ||
file.base.endsWith('.woff2')
)
return
Expand Down
4 changes: 2 additions & 2 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2415,14 +2415,14 @@ export async function cli(task, opts) {

export async function lib(task, opts) {
await task
.source('src/lib/**/!(*.test).+(js|ts|tsx|json)')
.source('src/lib/**/!(*.test).+(js|ts|tsx|json|jsonc)')
.swc('server', { dev: opts.dev })
.target('dist/lib')
}

export async function lib_esm(task, opts) {
await task
.source('src/lib/**/!(*.test).+(js|ts|tsx|json)')
.source('src/lib/**/!(*.test).+(js|ts|tsx|json|jsonc)')
.swc('server', { dev: opts.dev, esm: true })
.target('dist/esm/lib')
}
Expand Down
11 changes: 10 additions & 1 deletion scripts/validate-externals-doc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const fs = require('fs')
const path = require('path')
const JSON5 = require('next/dist/compiled/json5')

const serverExternals = require('../packages/next/src/lib/server-external-packages.json')
const serverExternals = JSON5.parse(
fs.readFileSync(
path.join(
__dirname,
'../packages/next/src/lib/server-external-packages.jsonc'
),
'utf8'
)
)

function validate(docPath) {
const docContent = fs.readFileSync(
Expand Down
Loading