Strapi Plugin
Relevant source files
The following files were used as context for generating this wiki page:
The Strapi Plugin (@nx-extend/strapi) provides a set of tools to generate, build, and serve Strapi applications within an Nx workspace. It integrates Strapi's headless CMS capabilities with Nx's task running and project management features, supporting TypeScript by default and providing automated configuration for development and production environments.
Sources: packages/strapi/README.md:1-15, packages/strapi/src/generators/init/init.impl.ts:40-42
Core Features
The plugin automates the lifecycle of a Strapi project through specific Nx generators and executors.
| Feature | Description |
|---|---|
| Project Generation | Creates a new Strapi application with pre-configured Nx targets. |
| Development Server | Runs Strapi with auto-reload and optional admin panel watch mode. |
| Production Build | Compiles the Strapi project to a specified output directory. |
| TypeScript Support | Configures projects to use TypeScript by default. |
| Lock File Support | Supports generating lock files for deployment consistency. |
Sources: packages/strapi/README.md:7-15, packages/strapi/src/generators/init/init.impl.ts:40-42
Initialization and Generation
The init generator sets up the foundational structure of a Strapi application. It uses @strapi/generate-new to bootstrap the Strapi core and then applies Nx-specific configurations.
Generation Workflow
The generator performs the following actions:
- Normalizes Options: Processes the project name, directory, and tags.
- Configures Nx Project: Adds a project configuration with
serveandbuildtargets. - Bootstraps Strapi: Invokes
generateNewwith a fixed Strapi version (4.26.1) and TypeScript enabled. - Injects Files: Generates boilerplate files from templates.
- Installs Dependencies: Adds required React and Styled Components libraries to
package.json.
Sources: packages/strapi/src/generators/init/init.impl.ts:55-108, packages/strapi/package.json:20-24
The diagram shows the sequence of operations during project initialization.
Sources: packages/strapi/src/generators/init/init.impl.ts:55-108
Default Configuration
The generator configures the following targets in the project's project.json:
- serve: Uses
@nx-extend/strapi:serve. - build: Uses
@nx-extend/strapi:buildwith anoutputPathpointing to the workspacedistfolder.
Sources: packages/strapi/src/generators/init/init.impl.ts:60-84
Build Executor
The build executor compiles the Strapi application for deployment. It requires an outputPath and supports environment variable injection.
Build Options
| Option | Type | Default | Description |
|---|---|---|---|
production | boolean | false | Enables production mode optimizations. |
outputPath | string | - | Required. Destination for build artifacts. |
tsConfig | string | - | Path to the TypeScript configuration file. |
generateLockFile | boolean | false | Generates a lockfile for the output. |
envVars | object | - | Environment variables to set before execution. |
Sources: packages/strapi/README.md:33-43, packages/strapi/src/generators/init/init.impl.ts:68-75
Serve Executor
The serve executor manages the development lifecycle, providing hot-reloading for both the backend and the administration panel.
Execution Flow
The sequence diagram illustrates how the serve executor handles the Strapi development process.
Sources: packages/strapi/README.md:52-65
Serve Options
| Option | Type | Default | Description |
|---|---|---|---|
build | boolean | true | Enables autoReload and skips initial admin build. |
watchAdmin | boolean | false | Starts the front-end development server for admin customization. |
root | string | Project Root | Specifies the directory of the Strapi project. |
Sources: packages/strapi/README.md:58-65
Technical Implementation Details
Strapi Versioning
The plugin enforces a specific version of Strapi (currently 4.26.1) during generation to ensure compatibility with the provided templates and executors. This version is hardcoded in the generator implementation.
Sources: packages/strapi/src/generators/init/init.impl.ts:24, packages/strapi/package.json:23
Dependency Management
Upon initialization, the plugin automatically adds the following peer dependencies to the workspace to support the Strapi Admin UI:
react(^18.0.0)react-dom(^18.0.0)react-router-dom(5.3.4)styled-components(5.3.3)
Sources: packages/strapi/src/generators/init/init.impl.ts:98-103
Project Structure
A generated Strapi project follows the standard Strapi v4 layout but is adapted for Nx:
- Source Root:
<projectRoot>/src - Build Output:
dist/<projectRoot> - Entry Point:
src/index.ts(TypeScript)
Sources: packages/strapi/src/generators/init/init.impl.ts:63, e2e/strapi-e2e/tests/strapi.spec.ts:18-32
Summary
The Strapi Plugin for Nx streamlines the integration of Strapi into a monorepo by automating project creation and providing standardized executors for development and production. By enforcing TypeScript and specific versioning, it ensures a consistent development experience across the workspace.