Modern.js provides JSX syntax and HTML(EJS) syntax to customize the HTML template.
According to Modern.js conventions, you can create a Document.[jt]sx
file under src/
or the entry directory and default export a component". The rendering result of this component can be used as the HTML template of the entry.
For example, consider the following directory structure:
entry-a
will use the Docoument.[jt]sx
file under the current entry as the template. If there is no Document.[jt]sx
file under the current entry, like entry-b
, it will look for the Document.[jt]sx
file under the root directory.
If not found, default template will be used.
Modern.js provides some components for rendering pages to help developers generate templates. These components can be used from @modern-js/runtime/document
:
Html
: provide the ability of native HTML Element and and render necessary components that the developer did not add by default. <Head>
and <Body>
must exist, and other components can be assembled as needed.
Body
: provide the ability of native Body Element and needs to contain the <Root>
component internally. It also supports other elements as child elements at the same time, such as adding footers.
Root
: the root node <div id='root'></div>
to be rendered. The default id of the root node is id = 'root'
. You can set props.rootId
to change the id attribute. Child components can be added and will be rendered in the HTML template. After React rendering is complete, it will be overwritten and is generally used to implement global Loading.
Head
: provide the ability of native Head Element and automatically fills in <meta>
and <Scripts>
components.
Scripts
: Used to control the placement of the <script>
tags generated by the build. By default, they are placed within the <Head>
component.
Comment
: retain user-written comments like <!-- gateway -->
and outputs them to the rendered HTML.
Because it is in JSX format, various variables can be used freely in the component to assign values to various custom components in Document.[jt]sx
.
Modern.js also provides DocumentContext
to provide some configuration and environment parameters for easy access. The main parameters are:
processEnv
: provides the process.env
during the build.config
: the configuration of the Modern.js project. Currently, only the output configuration is exposed.entryName
: the current entry name.templateParams
: parameters of HTML template (compatible with traditional templates, so it's not recommended for use).The above JSX components will generate the following HTML template:
You can use the <Scripts>
component to insert the <script>
tags generated by the build inside the <body>
tag:
Modern.js also supports generating HTML files using HTML (EJS) syntax.
By default, Modern.js projects come with a built-in HTML template for generating HTML code. If you need to customize the HTML template, you can use two methods: Custom HTML Fragments and Fully Custom HTML Templates.
Under the application root directory, create the config/html/
directory, which supports the creation of four types of HTML fragments:
top.html
head.html
body.html
bottom.html
These fragments will be injected into the default HTML template according to their positions.
HTML fragments support the use of Lodash template.
For example, to insert an external script in body.html
:
The implementation of custom HTML fragments is to merge the fragments with the built-in template. Since the <title>
already exists in the default template, the title tag in the custom HTML template will not take effect. Please use html.title to modify the page title.
In some cases, HTML fragments may be is not better meet the customization requirements. Modern.js provides a fully customized way.
It is generally not recommended to directly override the default HTML template, as some functional options may be lost. If it is truly necessary to customize the entire HTML template, it is recommended to modify based on the built-in template as needed.
Under the config/html/
directory, create an index.html
file that will replace the default HTML template.
The default HTML template can be viewed in node_modules/.modern-js/${entryName}/index.html
.
The parameters used in the template can be defined by the html.templateParameters configuration.
The HTML fragments in the config/html/
directory take effect for all entries in the application. If you want to customize HTML fragments by entry, you can create a directory named after the entry name under the config/html/
directory, and then customize the HTML fragments in this directory.
For example, the following HTML fragments are only effective for the entry1
entry: