Skip to content

Commit 941a075

Browse files
Fix nextjs plugin to work on all environments (#1612)
* fix(ui~cli): `nextjs` plugin * log file writes in `dev` * log file writes in `dev` * update changeset
1 parent 5fdb439 commit 941a075

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

.changeset/short-candies-warn.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
"flowbite-react": patch
3+
---
4+
5+
Fix `nextjs` plugin to work on all environments
6+
7+
## Changes
8+
9+
- fix(ui): `nextjs` plugin to run properly on `NODE_ENV` environments: `production`, `development` and `test`
10+
- log file writes in `dev`
11+
12+
## Breaking changes
13+
14+
`withFlowbiteReact` now always returns async configuration (see: [Async Configuration](https://nextjs.org/docs/app/api-reference/config/next-config-js#async-configuration)) so make sure to wrap any other HOC (higher-order-functions) such as `withContentlayer` because most of them do not forward pass async config arguments (eg: `phase, options`)
15+
16+
```tsx
17+
// ❌ not working
18+
export default withContentlayer(withFlowbiteReact(nextConfig));
19+
20+
// ✅ working
21+
export default withFlowbiteReact(withContentlayer(nextConfig));
22+
```

apps/web/next.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,4 @@ const nextConfig = {
158158
},
159159
};
160160

161-
export default withContentlayer(withFlowbiteReact(nextConfig));
161+
export default withFlowbiteReact(withContentlayer(nextConfig));

packages/ui/src/cli/commands/dev.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export async function dev() {
6363

6464
if (!isEqual(classList, newClassList)) {
6565
classList = newClassList;
66+
console.log(`Generating ${classListFilePath} file...`);
6667
await fs.writeFile(classListFilePath, JSON.stringify(classList, null, 2));
6768
}
6869

@@ -106,6 +107,7 @@ export async function dev() {
106107

107108
if (!isEqual(classList, newClassList)) {
108109
classList = newClassList;
110+
console.log(`Generating ${classListFilePath} file...`);
109111
await fs.writeFile(classListFilePath, JSON.stringify(classList, null, 2));
110112
}
111113
}

packages/ui/src/plugin/nextjs.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NextConfig } from "next";
2+
import { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } from "next/constants.js";
23
import { build } from "../cli/commands/build";
34
import { dev } from "../cli/commands/dev";
45

@@ -8,24 +9,24 @@ interface ConfigurationOptions {
89

910
type ConfigurationCallback = (phase: string, options: ConfigurationOptions) => NextConfig | Promise<NextConfig>;
1011

11-
export default function withFlowbiteReact(
12-
nextConfig: NextConfig | ConfigurationCallback = {},
13-
): NextConfig | ConfigurationCallback {
14-
if (process.env.NODE_ENV === "development") {
15-
dev();
16-
}
17-
if (process.env.NODE_ENV === "production") {
18-
build();
19-
}
20-
21-
if (typeof nextConfig === "function") {
22-
return async (phase: string, options: ConfigurationOptions) => {
23-
const originalConfig = await nextConfig(phase, options);
24-
return patchConfig(originalConfig);
25-
};
26-
}
27-
28-
return patchConfig(nextConfig);
12+
let devRan = false;
13+
let buildRan = false;
14+
15+
export default function withFlowbiteReact(nextConfig: NextConfig | ConfigurationCallback = {}): ConfigurationCallback {
16+
return async (phase: string, options: ConfigurationOptions) => {
17+
if (phase === PHASE_DEVELOPMENT_SERVER && !devRan) {
18+
devRan = true;
19+
dev();
20+
}
21+
if (phase === PHASE_PRODUCTION_BUILD && !buildRan) {
22+
buildRan = true;
23+
build();
24+
}
25+
26+
const userConfig = typeof nextConfig === "function" ? await nextConfig(phase, options) : nextConfig;
27+
28+
return patchConfig(userConfig);
29+
};
2930
}
3031

3132
function patchConfig(config: NextConfig): NextConfig {

0 commit comments

Comments
 (0)