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
7 changes: 7 additions & 0 deletions .changeset/next-webpack-optimize-package-imports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/cli': patch
---

[docs] `astryx docs styling/tokens-and-setup` has a Next.js with webpack section. It shows `experimental.optimizePackageImports: ['@astryxdesign/core']` in `next.config.mjs` and recommends a component entry, such as `@astryxdesign/core/Button`, in Server Components. Without either, a webpack build ships every Astryx client component to a page whose Server Component imports from the package root. The theme preview starter app that `astryx experimental theme preview app` copies has the same setting. No API changes.

@cixzhang
8 changes: 7 additions & 1 deletion apps/example-nextjs-stylex/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
// With webpack, a Server Component that imports from the package root
// otherwise ships every client component.
experimental: {
optimizePackageImports: ['@astryxdesign/core'],
},
};

export default nextConfig;
8 changes: 7 additions & 1 deletion apps/example-nextjs-tailwind/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
// With webpack, a Server Component that imports from the package root
// otherwise ships every client component.
experimental: {
optimizePackageImports: ['@astryxdesign/core'],
},
};

export default nextConfig;
15 changes: 15 additions & 0 deletions apps/example-nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ export function Providers({children}) {

`LinkProvider` wires up Next.js client-side navigation for all Astryx link-based components (Link, Button with href, TopNav, SideNav, Breadcrumbs, TabList).

### 4. Next.js config (webpack builds)

```js
// next.config.mjs
const nextConfig = {
experimental: {
optimizePackageImports: ['@astryxdesign/core'],
},
};

export default nextConfig;
```

With webpack (`next build --webpack`), a Server Component that imports from the package root would otherwise ship every client component; this keeps first load to what the page uses. In Server Components, also import from the component's own entry, such as `@astryxdesign/core/Button`. Turbopack needs no change.

## Gotchas

| Issue | Symptom | Fix |
Expand Down
8 changes: 7 additions & 1 deletion apps/example-nextjs/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
// With webpack, a Server Component that imports from the package root
// otherwise ships every client component.
experimental: {
optimizePackageImports: ['@astryxdesign/core'],
},
};

export default nextConfig;
42 changes: 39 additions & 3 deletions packages/cli/assets/docs/tree/tokens-and-setup.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

/**
* @file `astryx docs styling/tokens-and-setup`: using design tokens in custom
* styles and configuring a StyleX compiler for swizzled components.
* styles, configuring a StyleX compiler for swizzled components, and keeping
* Next.js webpack builds small.
*/

/** @type {import('@astryxdesign/cli/authoring').ReferenceDoc} */
Expand All @@ -14,8 +15,16 @@ export const docs = {
placement: {parent: 'namespace:styling', slot: 'guides', order: 30},
category: 'guide',
description:
'Using design tokens in custom styles and configuring a StyleX compiler for swizzled components.',
keywords: ['StyleX setup', 'swizzle', 'webpack', 'Vite', 'Next.js'],
'Using design tokens in custom styles, configuring a StyleX compiler for swizzled components, and keeping Next.js webpack builds small.',
keywords: [
'StyleX setup',
'swizzle',
'webpack',
'Vite',
'Next.js',
'optimizePackageImports',
'Server Components',
],

sections: [
{
Expand Down Expand Up @@ -129,5 +138,32 @@ export default stylexPlugin({
},
],
},
{
id: 'nextjs-webpack',
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
title: 'Next.js with webpack',
content: [
{
type: 'prose',
text: 'If your Next.js app builds with webpack (`next build --webpack`), add `@astryxdesign/core` to `experimental.optimizePackageImports`. With webpack, a Server Component that imports from the package root would otherwise ship every client component; this keeps first load to what the page uses. Turbopack, the default bundler in Next.js 16, needs no change, and the setting is safe to keep there.',
},
{
type: 'code',
lang: 'js',
label: 'next.config.mjs',
code: `/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
optimizePackageImports: ['@astryxdesign/core'],
},
};

export default nextConfig;`,
},
{
type: 'prose',
text: "In Server Components, also import each component from its own entry, for example `import {Button} from '@astryxdesign/core/Button'`. `{@command component <Name>}` prints that entry. Files that start with `'use client'` are not affected.",
},
],
},
],
};
8 changes: 7 additions & 1 deletion packages/cli/assets/preview-app/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
// With webpack, a Server Component that imports from the package root
// otherwise ships every client component.
experimental: {
optimizePackageImports: ['@astryxdesign/core'],
},
};

export default nextConfig;
8 changes: 6 additions & 2 deletions packages/cli/test/__golden__/no-icon-set/docs-tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -1240,9 +1240,13 @@
"type": "docs.detail",
"sections": [
"design-tokens",
"stylex-setup"
"stylex-setup",
"nextjs-webpack"
],
"mentions": []
"mentions": [
"/** @type {import('next').NextConfig} */\nconst nextConfig = {\n experimental: {\n optimizePackageImports: ['@astryxdesign/core'],\n },\n};\n\nexport default nextConfig;",
"If your Next.js app builds with webpack (`next build --webpack`), add `@astryxdesign/core` to `experimental.optimizePackageImports`."
]
},
"theme": {
"type": "docs.detail",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "search",
"data": {
"query": "experimental",
"matchCount": 3,
"matchCount": 4,
"results": [
{
"domain": "doc",
Expand Down Expand Up @@ -50,6 +50,22 @@
"mentions": [
"docs mention \"experimental\""
]
},
{
"domain": "doc",
"name": "styling/tokens-and-setup",
"package": "@astryxdesign/cli",
"score": "<text>",
"reason": "<text>",
"description": "<text>",
"section": "<text>",
"title": "<text>",
"command": "astryx docs styling/tokens-and-setup nextjs-webpack",
"parent": "astryx docs styling/tokens-and-setup --index",
"mentions": [
"If your Next.js app builds with webpack (`next build --webpack`), add `@astryxdesign/core` to `experimental.optimizePackageImports`.",
"description mentions \"experimental\""
]
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Results for "experimental" (3)
Results for "experimental" (4)
name: authoring/field-reference
section: config
domain: doc
Expand All @@ -16,4 +16,10 @@ section: template-doc
domain: doc
package: @astryxdesign/cli
command: pnpm exec astryx docs authoring/field-reference template-doc
parent: pnpm exec astryx docs authoring/field-reference --index
parent: pnpm exec astryx docs authoring/field-reference --index
name: styling/tokens-and-setup
section: nextjs-webpack
domain: doc
package: @astryxdesign/cli
command: pnpm exec astryx docs styling/tokens-and-setup nextjs-webpack
parent: pnpm exec astryx docs styling/tokens-and-setup --index
Loading