Unverified Commit 10e0828f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Use JSONC for default server-external-packages (#86252)

So that we can then use comments to track a reason why we have added packages to this list.
parent e9a3ba93
......@@ -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
......@@ -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"),
)
......
......@@ -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;
......@@ -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"),
)
......
......@@ -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,
},
};
......@@ -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?;
......
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::{
......@@ -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"),
)
......@@ -288,24 +287,44 @@ 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 {}",
match content.parse_json_ref() {
FileJsonContent::Unparsable(e) => Err(anyhow!("File is not valid JSON: {}", e)),
FileJsonContent::NotFound => Err(anyhow!(
"File not found: {:?}",
file_path.value_to_string().await?
);
};
)),
FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?),
}
}
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)?;
let result: T = parse_json_rope_with_source_context(file.content())?;
let content = &*file_path.read().await?;
Ok(result)
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: {:?}",
file_path.value_to_string().await?
)),
FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?),
}
}
pub fn styles_rule_condition() -> RuleCondition {
......
......@@ -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`
......
......@@ -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`
......
......@@ -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'
......@@ -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[]
......
......@@ -71,5 +71,5 @@
"vscode-oniguruma",
"webpack",
"websocket",
"zeromq"
"zeromq",
]
......@@ -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
......
......@@ -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')
}
......
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(
......
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