onForged
is a lifecycle function used for file operations in generator plugin.
The onForged
parameter is a callback function with api
and input
as parameters, which are used to provide the APIs and current input information provided by the lifecycle function.
The generator plugin classifies files into four categories:
Files with pure text content that can be processed using Handlebars or EJS templates.
Files such as images, audio, and video.
Files in JSON format.
Files composed of lines of text, such as .gitignore
, .editorconfig
, and .npmrc
.
The type definitions for the four types of files are:
The APIs provided by the api
parameter will be introduced below.
Adds a single file.
Parameter types:
type
: File type.file
: Target file path, relative to the target project directory.template
: Template content, the value of this field can be directly used to render the file. Lower priority than templateFile
.templateFile
: Template file path, relative to the templates directory.force
: Whether to force overwrite when the target file exists, default is false.data
: Template rendering data.Text files are processed using Handlebars by default. If the template file ends with .ejs
, EJS will be used for template rendering.
For example, add the template file App.tsx.hanlebars
to src/App.tsx
:
Adds multiple files, usually used to add multiple files to the same target directory.
Parameter types:
type
: File type.destination
: Target folder, relative path to the target project directory.templateFiles
: Template file list, supporting regular expressions from globby.templateBase
: Common path of template files. When using this parameter, the target file will automatically delete this path.fileNameFunc
: Function to rename files. During the file addition process, the file name will be passed to this function in turn, and the renaming can be performed as needed.data
: Template rendering data.For example, render all files in the src-ts
directory of the template file to the src
directory:
Update fields in a JSON file.
Parameter types:
fileName
: JSON file path, relative to the target project path.updateInfo
: Update information. Nested field updates need to be flattened, otherwise the entire update will cause content loss.For example, update the name
field of package.json
:
For example, update the version of react-dom
in dependencies
:
Update the contents of a text list file.
Parameter types:
fileName
: Text list file path, relative to the target project path.update
: Update function. The parameter is an array divided by \n
of the current file content, and the return value is also the modified array after modification. @modern-js/create
will automatically merge it with \n
and write it to the source file.For example, add the dist
directory to the .gitinore
file:
In addition to configuring in modern.config.[tj]s
, Modern.js configuration also supports configuring modernConfig
in package.json
. This function is used to update this field.
Parameter types:
updateInfo
: Update content information. updateModernConfig
is a package based on updateJSONFile
, which will automatically update under the modernConfig
field. Only the configuration fields under modernConfig
need to be filled in updateInfo
.For example, enable SSR:
Delete files.
Parameter types:
fileName
: The path of the file to be deleted, relative to the target project path.Delete a folder.
Parameter types:
dirName
: The path of the folder to be deleted, relative to the target project path.Add a custom helper rendered by handlebrs.
Parameter types:
name
: Helper function name.fn
: Helper function implementation.Add a Partial rendered by Handlebars.
Parameter types:
name
: Partial name.str
: Template string of the Partial.Automatically run the new
command to create project elements.
Parameter types:
element
: Type of project element, new entry or new custom Web Server source directory.params
: Other parameters corresponding to creating project elements.Automatically run the new
command to enable optional features.
Parameter types:
func
: Name of the feature to enable.params
: Other parameters corresponding to enabling the feature.Refer to Configuration Parameters for creating project elements and enabling feature configurations.