GrapesJS CLI
raw JSON →GrapesJS CLI is a command-line interface tool designed to streamline the development of GrapesJS plugins. It handles the complexities of setting up modern JavaScript build tools such as Webpack and Babel, providing a simplified and accelerated workflow for plugin authors. The current stable version, 4.1.3, focuses on dependency updates and build enhancements like the `dts` option. Historically, the CLI has undergone significant updates, including a transition to Webpack 5 in v3.0.0 and a move away from the `esm` package for internal bundling in v4.1.0, which can impact custom configurations. Its release cadence generally aligns with major GrapesJS ecosystem changes or critical dependency updates. Key differentiators for `grapesjs-cli` include its opinionated, zero-configuration approach for common plugin development scenarios, direct integration with the GrapesJS plugin structure, and features like fast project scaffolding and a built-in development server. It aims to reduce the boilerplate and configuration overhead for developers, allowing them to focus on the core plugin logic.
Common errors
error Webpack options object has an unknown property '...' (e.g., 'resolve.modules'). These options were deprecated in webpack 4 and removed in webpack 5. ↓
webpack.config.js to use current options and syntax. error Error: Cannot find module 'your-plugin-name' ↓
package.json main field to dist/index.js. error Error: No valid `src/index.js` (or `src/index.ts`) entry file found for your plugin. ↓
index.js or index.ts file located directly under the src/ directory of your plugin project. error Node.js version 12.x.x is not supported. Minimum supported Node.js version is 14.15.0. ↓
Warnings
breaking GrapesJS CLI v3.0.0 upgraded to Webpack 5. Custom `webpack.config.js` files may need updates to avoid deprecated options or configurations from Webpack 4. ↓
breaking With v3.0.0, the output filenames changed from `dist/my-plugin-name.min.js` to `dist/index.js`. This affects how your plugin is referenced. ↓
breaking Node.js 14.15.0 or newer is required since v4.0.0. Older versions of Node.js are no longer supported and will cause the CLI to fail. ↓
breaking Version 4.1.0 moved away from the `esm` package for bundling the CLI internally. While this primarily affects the CLI's internal workings, custom setups that relied on the previous bundling approach might be impacted. ↓
gotcha When customizing Webpack through `webpack.config.js`, you must export a function that receives the default configuration object and returns a new one. ↓
gotcha Passing options to `webpack-dev-server` via the `serve` command requires a specific JSON string format. ↓
Install
npm install grapesjs-cli yarn add grapesjs-cli pnpm add grapesjs-cli Imports
- init wrong
import { init } from 'grapesjs-cli'correctnpx grapesjs-cli init - serve wrong
const serve = require('grapesjs-cli').servecorrectnpx grapesjs-cli serve - build wrong
require('grapesjs-cli/dist/build')correctnpx grapesjs-cli build
Quickstart
/**
* This quickstart demonstrates the GrapesJS plugin development workflow using grapesjs-cli.
* The commands below are intended to be run in a shell environment.
*/
// 1. Create a new directory for your plugin and initialize it
// mkdir grapesjs-my-plugin
// cd grapesjs-my-plugin
// npm init -y
// git init
// 2. Install the GrapesJS CLI as a development dependency
// npm i -D grapesjs-cli
// 3. Initialize your plugin project. Use '-y' to skip interactive questions.
// npx grapesjs-cli init -y --user=your-github-username
// 4. Start the development server. Access your plugin demo at http://localhost:8080.
// npx grapesjs-cli serve
// You can also pass custom webpack-dev-server options:
// npx grapesjs-cli serve --devServer='{"https": true}'
// 5. Build your plugin for production. This generates output in the 'dist' directory.
// npx grapesjs-cli build
// Example: Custom webpack.config.js for advanced configurations (placed in your plugin's root directory)
// This JavaScript code block is runnable as a conceptual example of the config file structure.
/** @type {import('webpack').Configuration} */
const customWebpackConfig = ({ config }) => {
// `config` is the default webpack configuration provided by grapesjs-cli
const isBuild = config.mode === 'production'; // Differentiate build from serve command
return {
...config,
// Example: Add a new rule for handling SVG files
module: {
rules: [
{
test: /\.svg$/,
use: ['@svgr/webpack'],
},
...config.module.rules,
],
},
// Example: Customize an existing plugin or add a new one
plugins: [
...(config.plugins || []),
// new MyCustomWebpackPlugin(),
],
};
};
// In a real scenario, this would be exported from a webpack.config.js file:
// export default customWebpackConfig;
console.log('GrapesJS CLI workflow and custom webpack config demonstrated.');