Unverified Commit addafa51 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: JSON5 and package.json for postcss config files (#68679)

Use JSON5 for parsing the PostCSS config files

Webpack also uses JSON5:
https://github.com/vercel/next.js/blob/17f2cb65c2aacc6b52ed6d34e98a839ee465c35d/packages/next/src/lib/find-config.ts#L96

Not sure if a new `Source` is the best way to do this

Closes PACK-3103
Closes https://github.com/vercel/next.js/issues/67218
Closes #67221
parent 6cfa7661
import 'tailwindcss/tailwind.css'
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
export default function Home() {
return (
<div className="flex flex-col items-center justify-center min-h-screen py-2">
<main className="flex flex-col items-center justify-center w-full flex-1 px-20 text-center">
<h1 className="text-blue-600 text-6xl font-bold">
Welcome to{' '}
<a className="text-blue-600" href="https://nextjs.org" id="test-link">
Next.js!
</a>
</h1>
<p className="mt-3 text-2xl">
Get started by editing{' '}
<code className="p-3 font-mono text-lg bg-gray-100 rounded-md">
pages/index.js
</code>
</p>
<div className="flex flex-wrap items-center justify-around max-w-4xl mt-6 sm:w-full">
<a
href="https://nextjs.org/docs"
className="p-6 mt-6 text-left border w-96 rounded-xl hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Documentation &rarr;</h3>
<p className="mt-4 text-xl">
Find in-depth information about Next.js features and API.
</p>
</a>
<a
href="https://nextjs.org/learn"
className="p-6 mt-6 text-left border w-96 rounded-xl hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Learn &rarr;</h3>
<p className="mt-4 text-xl">
Learn about Next.js in an interactive course with quizzes!
</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className="p-6 mt-6 text-left border w-96 rounded-xl hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Examples &rarr;</h3>
<p className="mt-4 text-xl">
Discover and deploy boilerplate example Next.js projects.
</p>
</a>
<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className="p-6 mt-6 text-left border w-96 rounded-xl hover:text-blue-600 focus:text-blue-600"
>
<h3 className="text-2xl font-bold">Deploy &rarr;</h3>
<p className="mt-4 text-xl">
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
</div>
)
}
module.exports = {
content: ['./pages/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {},
},
plugins: [],
}
import { FileRef, nextTestSetup } from 'e2e-utils'
import { join } from 'path'
describe('postcss-config-json', () => {
const { next } = nextTestSetup({
files: new FileRef(join(__dirname, 'app')),
dependencies: {
autoprefixer: '10.4.19',
postcss: '8.4.38',
tailwindcss: '3.4.4',
},
packageJson: {
postcss: {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
},
},
})
it('works with postcss config specified in package.json', async () => {
let browser = await next.browser('/')
try {
const text = await browser.elementByCss('.text-6xl').text()
expect(text).toMatch(/Welcome to/)
const cssBlue = await browser
.elementByCss('#test-link')
.getComputedCss('color')
expect(cssBlue).toBe('rgb(37, 99, 235)')
} finally {
await browser.close()
}
})
})
......@@ -1112,6 +1112,10 @@ impl FileSystemPath {
self.fs().read(self).parse_json()
}
pub fn read_json5(self: Vc<Self>) -> Vc<FileJsonContent> {
self.fs().read(self).parse_json5()
}
/// Reads content of a directory.
///
/// DETERMINISM: Result is in random order. Either sort result or do not
......@@ -1686,6 +1690,33 @@ impl FileContent {
}
}
pub fn parse_json5_ref(&self) -> FileJsonContent {
match self {
FileContent::Content(file) => match file.content.to_str() {
Ok(string) => match parse_to_serde_value(
&string,
&ParseOptions {
allow_comments: true,
allow_trailing_commas: true,
allow_loose_object_property_names: true,
},
) {
Ok(data) => match data {
Some(value) => FileJsonContent::Content(value),
None => FileJsonContent::unparseable(
"text content doesn't contain any json data",
),
},
Err(e) => FileJsonContent::Unparseable(Box::new(
UnparseableJson::from_jsonc_error(e, string.as_ref()),
)),
},
Err(_) => FileJsonContent::unparseable("binary is not valid utf-8 text"),
},
FileContent::NotFound => FileJsonContent::NotFound,
}
}
pub fn lines_ref(&self) -> FileLinesContent {
match self {
FileContent::Content(file) => match file.content.to_str() {
......@@ -1726,6 +1757,12 @@ impl FileContent {
Ok(this.parse_json_with_comments_ref().into())
}
#[turbo_tasks::function]
pub async fn parse_json5(self: Vc<Self>) -> Result<Vc<FileJsonContent>> {
let this = self.await?;
Ok(this.parse_json5_ref().into())
}
#[turbo_tasks::function]
pub async fn lines(self: Vc<Self>) -> Result<Vc<FileLinesContent>> {
let this = self.await?;
......@@ -1762,6 +1799,17 @@ impl ValueToString for FileJsonContent {
}
}
#[turbo_tasks::value_impl]
impl FileJsonContent {
#[turbo_tasks::function]
pub async fn content(self: Vc<Self>) -> Result<Vc<Value>> {
match &*self.await? {
FileJsonContent::Content(json) => Ok(Vc::cell(json.clone())),
FileJsonContent::Unparseable(e) => Err(anyhow!("File is not valid JSON: {}", e)),
FileJsonContent::NotFound => Err(anyhow!("File not found")),
}
}
}
impl FileJsonContent {
pub fn unparseable(message: &'static str) -> Self {
FileJsonContent::Unparseable(Box::new(UnparseableJson {
......
......@@ -1146,14 +1146,60 @@ pub async fn find_context_file(
names: Vc<Vec<RcStr>>,
) -> Result<Vc<FindContextFileResult>> {
let mut refs = Vec::new();
let context_value = lookup_path.await?;
for name in &*names.await? {
let fs_path = lookup_path.join(name.clone());
if let Some(fs_path) = exists(fs_path, &mut refs).await? {
return Ok(FindContextFileResult::Found(fs_path, refs).into());
}
}
if context_value.is_root() {
if lookup_path.await?.is_root() {
return Ok(FindContextFileResult::NotFound(refs).into());
}
if refs.is_empty() {
// Tailcall
Ok(find_context_file(
lookup_path.parent().resolve().await?,
names,
))
} else {
let parent_result = find_context_file(lookup_path.parent().resolve().await?, names).await?;
Ok(match &*parent_result {
FindContextFileResult::Found(p, r) => {
refs.extend(r.iter().copied());
FindContextFileResult::Found(*p, refs)
}
FindContextFileResult::NotFound(r) => {
refs.extend(r.iter().copied());
FindContextFileResult::NotFound(refs)
}
}
.into())
}
}
// Same as find_context_file, but also stop for package.json with the specified key
#[turbo_tasks::function]
pub async fn find_context_file_or_package_key(
lookup_path: Vc<FileSystemPath>,
names: Vc<Vec<RcStr>>,
package_key: Value<RcStr>,
) -> Result<Vc<FindContextFileResult>> {
let mut refs = Vec::new();
let package_json_path = lookup_path.join("package.json".into());
if let Some(package_json_path) = exists(package_json_path, &mut refs).await? {
if let Some(json) = &*read_package_json(package_json_path).await? {
if json.get(&**package_key).is_some() {
return Ok(FindContextFileResult::Found(package_json_path, refs).into());
}
}
}
for name in &*names.await? {
let fs_path = lookup_path.join(name.clone());
if let Some(fs_path) = exists(fs_path, &mut refs).await? {
return Ok(FindContextFileResult::Found(fs_path, refs).into());
}
}
if lookup_path.await?.is_root() {
return Ok(FindContextFileResult::NotFound(refs).into());
}
if refs.is_empty() {
......
......@@ -19,7 +19,7 @@ use turbopack_core::{
Issue, IssueDescriptionExt, IssueSeverity, IssueStage, OptionStyledString, StyledString,
},
reference_type::{EntryReferenceSubType, InnerAssets, ReferenceType},
resolve::{find_context_file, options::ImportMapping, FindContextFileResult},
resolve::{find_context_file_or_package_key, options::ImportMapping, FindContextFileResult},
source::Source,
source_transform::SourceTransform,
virtual_source::VirtualSource,
......@@ -225,12 +225,97 @@ async fn extra_configs_changed(
Ok(Vc::<Completions>::cell(configs).completed())
}
#[turbo_tasks::value]
pub struct JsonSource {
pub path: Vc<FileSystemPath>,
pub key: Vc<Option<RcStr>>,
pub allow_json5: bool,
}
#[turbo_tasks::value_impl]
impl JsonSource {
#[turbo_tasks::function]
pub fn new(path: Vc<FileSystemPath>, key: Vc<Option<RcStr>>, allow_json5: bool) -> Vc<Self> {
Self::cell(JsonSource {
path,
key,
allow_json5,
})
}
}
#[turbo_tasks::value_impl]
impl Source for JsonSource {
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
match &*self.key.await? {
Some(key) => Ok(AssetIdent::from_path(
self.path
.append(".".into())
.append(key.clone())
.append(".json".into()),
)),
None => Ok(AssetIdent::from_path(self.path.append(".json".into()))),
}
}
}
#[turbo_tasks::value_impl]
impl Asset for JsonSource {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<AssetContent>> {
let file_type = &*self.path.get_type().await?;
match file_type {
FileSystemEntryType::File => {
let json = if self.allow_json5 {
self.path.read_json5().content().await?
} else {
self.path.read_json().content().await?
};
let value = match &*self.key.await? {
Some(key) => {
let Some(value) = json.get(&**key) else {
return Err(anyhow::anyhow!("Invalid file type {:?}", file_type));
};
value
}
None => &*json,
};
Ok(AssetContent::file(File::from(value.to_string()).into()))
}
FileSystemEntryType::NotFound => {
Ok(AssetContent::File(FileContent::NotFound.cell()).cell())
}
_ => Err(anyhow::anyhow!("Invalid file type {:?}", file_type)),
}
}
}
#[turbo_tasks::function]
pub(crate) async fn config_loader_source(
project_path: Vc<FileSystemPath>,
postcss_config_path: Vc<FileSystemPath>,
) -> Result<Vc<Box<dyn Source>>> {
let postcss_config_path_value = &*postcss_config_path.await?;
let postcss_config_path_filename = postcss_config_path_value.file_name();
if postcss_config_path_filename == "package.json" {
return Ok(Vc::upcast(JsonSource::new(
postcss_config_path,
Vc::cell(Some("postcss".into())),
false,
)));
}
if postcss_config_path_value.path.ends_with(".json")
|| postcss_config_path_filename == ".postcssrc"
{
return Ok(Vc::upcast(JsonSource::new(
postcss_config_path,
Vc::cell(None),
true,
)));
}
// We can only load js files with `import()`.
if !postcss_config_path_value.path.ends_with(".js") {
......@@ -298,15 +383,23 @@ async fn find_config_in_location(
location: PostCssConfigLocation,
source: Vc<Box<dyn Source>>,
) -> Result<Option<Vc<FileSystemPath>>> {
if let FindContextFileResult::Found(config_path, _) =
*find_context_file(project_path, postcss_configs()).await?
if let FindContextFileResult::Found(config_path, _) = *find_context_file_or_package_key(
project_path,
postcss_configs(),
Value::new("postcss".into()),
)
.await?
{
return Ok(Some(config_path));
}
if matches!(location, PostCssConfigLocation::ProjectPathOrLocalPath) {
if let FindContextFileResult::Found(config_path, _) =
*find_context_file(source.ident().path().parent(), postcss_configs()).await?
if let FindContextFileResult::Found(config_path, _) = *find_context_file_or_package_key(
source.ident().path().parent(),
postcss_configs(),
Value::new("postcss".into()),
)
.await?
{
return Ok(Some(config_path));
}
......
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