Skip to content

Commit

Permalink
feat(next): #120 add turbopack support
Browse files Browse the repository at this point in the history
Use process parameters to differentiate between build and dev instead of using webpack

Closes: #120
  • Loading branch information
sdorra committed Jun 1, 2024
1 parent 66e32c6 commit 370caac
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 319 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-colts-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@content-collections/next": minor
---

Support for turbopack
4 changes: 0 additions & 4 deletions docs/adapter/01-next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ title: Next.js

The `@content-collections/next` package allows a seamless integration of Content Collections into your Next.js app. This package provides a Next.js plugin that will automatically generate the content collections and add them to the build process.

<Notification type="warning">
The package currently does not work with TurboPack. We are working on a solution. In the meantime, you can use the [CLI adapter](/docs/adapter/cli) package instead of the Next.js adapter if you want to use TurboPack.
</Notification>

## Installation

1. First, install the required packages:
Expand Down
3 changes: 1 addition & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
"@content-collections/integrations": "workspace:*"
},
"peerDependencies": {
"next": "^12 || ^13 || ^14",
"next": "^12 || ^13 || ^14 || ^15",
"@content-collections/core": "0.x"
},
"devDependencies": {
"next": "^14.2.3",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"webpack": "^5.89.0",
"@content-collections/core": "workspace:*"
}
}
68 changes: 21 additions & 47 deletions packages/next/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { configureLogging } from "@content-collections/integrations";
import type { NextConfig } from "next";
import type webpack from "webpack";

let initialized = false;

type Options = {
configPath: string;
Expand All @@ -12,52 +9,29 @@ const defaultOptions: Options = {
configPath: "content-collections.ts",
};

// use dynamic import if the next package is used in a commonjs environment
const coreImport = import("@content-collections/core");

class ContentCollectionWebpackPlugin {
constructor(readonly options: Options) {}

apply(compiler: webpack.Compiler) {
compiler.hooks.beforeCompile.tapPromise(
"contentCollectionWebpackPlugin",
async () => {
if (initialized) {
return;
}
initialized = true;

const { createBuilder } = await coreImport;

console.log("Starting content-collections", this.options.configPath);
const builder = await createBuilder(this.options.configPath);
configureLogging(builder);
await builder.build();

if (compiler.watchMode) {
console.log("start watching ...");
await builder.watch();
}
}
);
}
}

export function createContentCollectionPlugin(pluginOptions: Options) {
const plugin = new ContentCollectionWebpackPlugin(pluginOptions);
return (nextConfig: Partial<NextConfig> = {}): Partial<NextConfig> => {
return {
...nextConfig,
webpack(config, options) {
config.plugins!.push(plugin);

if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options);
}
return async (
nextConfig: Partial<NextConfig> = {}
): Promise<Partial<NextConfig>> => {
const [command] = process.argv
.slice(2)
.filter((arg) => !arg.startsWith("-"));
if (command === "build" || command === "dev") {
const { createBuilder } = await import("@content-collections/core");

console.log("Starting content-collections", pluginOptions.configPath);
const builder = await createBuilder(pluginOptions.configPath);
configureLogging(builder);

await builder.build();

if (command === "dev") {
console.log("start watching ...");
await builder.watch();
}
}

return config;
},
};
return nextConfig;
};
}

Expand Down
Loading

0 comments on commit 370caac

Please sign in to comment.