create-wp-component
v1.0.2
Published
CLI tool to scaffold WordPress components compatible with vite-plugin-wp-component
Maintainers
Readme
create-wp-component
A CLI scaffolding tool for quickly setting up a WordPress component project powered by vite-plugin-wp-component.
This tool generates the project boilerplate with: - Pre-configured Vite and PostCSS settings.
- A ready-to-use index.html with the correct
rootIDinjected. - An empty
.envfor FTP credentials. - Built-in npm scripts that map to
vite-plugin-wp-componentcommands.
Installation
You can create a new project using npm or npx:
npx create-wp-component
cd my-component
npm installAvailable Scripts
The CLI automatically registers the following scripts in package.json:
npm run config→ Runswp-component config(edit.envorcomponent.config.json).npm run build→ Runsvite buildandwp-component build(bundles your project and generates the plugin PHP file in one step).npm run deploy→ Runswp-component deploy(deploys via FTP to your WordPress site).
Project Structure
A newly scaffolded project has the following structure:
Project/
├──index.html // Preconfigured with __COMPONENT_CONFIG__.rootID
└── src/
├── assets/ // Directory for external/static files
│ └── javascript.webp
├── generator.js // Core component logic
├── info.json // Data consumed by generator (can be local or fetched remotely)
├── main.js // Entry point, mounts the component into the rootID
├── styles.module.css // CSS Modules stylesheet
└── template.js // Example of importing assets & stylesKey Files
main.js
Accesses__COMPONENT_CONFIG__.rootIDand renders your component inside the root element.styles.module.css
Uses CSS Modules to ensure class encapsulation and isolation.
Class names follow this structure:[componentName]__[local]___[hash:base64:5]They are automatically transformed from
class-csstoclassCss, so you can import them in JavaScript as:import styles from "./styles.module.css"; element.className = styles.classCss;generator.js
Contains the core logic of the component, which is invoked frommain.js.info.json
Provides configuration or data forgenerator.js.
This can be a static file or dynamically fetched from an external server.template.js
Demonstrates how to import assets from/assetsand styles fromstyles.module.css.
Typical Workflow
Configure FTP credentials and metadata:
npm run configBuild the bundle and generate the WordPress plugin PHP:
npm run buildDeploy the plugin directly to WordPress:
npm run deploy
Notes
- The scaffold ensures that the same rootID is used across development and production (in sync with
vite-plugin-wp-component). - CSS Modules are essential for encapsulation — no class name collisions with WordPress themes or other plugins.
