directory
- Introduce the Plop
- Specific use of Plop
- Specific steps
- conclusion
Introduce the Plop
A gadget that is primarily used to create specific file types in a project, similar to the Sub Generator in Yeoman, and is not typically used in isolation. Plop is typically integrated into projects to automate the creation of project files of the same type.
- plop-npm
- plop-github
Specific use of Plop
Specific steps
- Create a directory and initialize it
npm init -y
To installPlop
npm install -g plop
Copy the code
- Create it in a directory
plop-templates
Folder, which creates three template files
- component.css.hbs
.{{name}} {
}
Copy the code
- component.hbs
import React from 'react';
export default() = > (<div className="{{ name }}">
<h1>{{name}} Component</h1>
</div>
)
Copy the code
- component.test.hbs
import React from 'react';
import ReactDOM from 'react-dom';
import {{name}} from './{{name}}';
it('renders widthout crashing'.() = > {
const div = document.createElement('div');
ReactDOM.render(<{{name}} />, div);
ReactDOM.unmountComponentAtNode(div);
});
Copy the code
- Create one in the root directory
plopfile.js
This file isPlop
To export a function that receives aplop
Object for creating generator tasks.
plop.setGenerator
: Sets a generator, the first argument being the project name and the second function being the object, corresponding to the Settings option- Configuration items:
description
Description:prompts
: values are arrays, command line interaction problems, one problem for one objectactions
The: values are arrays of actions that are completed after the command line interaction, one action per object
module.exports = plop= > {
// Set a generator, the first parameter is the project name, the second function is the object, corresponding to the Settings option
plop.setGenerator('compontent', {
/ / description
description: 'create a component'.// Command line interaction problems
prompts: [
// One problem corresponds to one object. For configuration, refer to the custom Generator
{
type: 'input'.name: 'name'.message: 'component name'.default: 'MyComponent'}].// Complete some actions after the command line interaction
actions: [
// Each object is an action
{
type: 'add'.// adds the file
// Where is the output path of the added file? The double curly braces interpolation can get the data generated by the interaction
path: 'src/components/{{name}}/{{name}}.js'.// What is the template file
templateFile: 'plop-templates/component.hbs'
},
{
type: 'add'.path: 'src/components/{{name}}/{{name}}.css'.templateFile: 'plop-templates/component.css.hbs'
},
{
type: 'add'.path: 'src/components/{{name}}/{{name}}.test.js'.templateFile: 'plop-templates/component.test.hbs'})}}]Copy the code
- in
package.json
add
"scripts": {
"plop": "plop"
}
Copy the code
- run
npm run plop
Enter the module name
? component name Header
+ + \ SRC \ #) components, the Header, the Header. The js
+ + \ SRC \ #) components, the Header, the Header. The CSS
+ + \ SRC \ #) components, the Header, the Header. Test. Js
Copy the code
Now you have three files under SRC \ Components in the root directory
- Header.js
import React from 'react';
export default() = > (<div className="Header">
<h1>Header Component</h1>
</div>
)
Copy the code
- Header.css
.Header{}Copy the code
- Header.test.js
import React from 'react';
import ReactDOM from 'react-dom';
import Header from './Header';
it('renders widthout crashing'.() = > {
const div = document.createElement('div');
ReactDOM.render(<Header />, div);
ReactDOM.unmountComponentAtNode(div);
});
Copy the code
This allows you to generate a component directory from the template with one click.
conclusion
- will
plop
Modules are installed as project development dependencies - Write templates to generate files of a particular type
- Create one in the project root directory
plopfile.js
file - in
plopfile.js
Scaffolding tasks are defined in the file - through
Plop
To provide theCLI
Running scaffolding tasks