Plugins for HTML and PHP
The @scandipwa/scandipwa-template-plugin-system package provides the template plugin system used by Create Scandipwa App.
Abstract
This template plugin system processes the template (the main markup file) file of the application.
It provides an opportunity to modify the template similarly to the process which is provided by CSA to modify the build configuration.
Use cases
Your extension needs some additional HTML/PHP nodes in the template
Your extension is incompatible with some existing HTML/PHP nodes in the template and needs to remove them
Your extension needs to change attributes of the existing nodes to some other values
Limitations
This tool is not meant for PHP code modifications. It offers an opportunity to interact with template as with text, but changing PHP or JS code such way would be considered a misuse, due to possible incompatibilities with a plugin written such a way. To change the PHP logic - change abstractions, not delta-modify the code.
Dependencies
xmldom
is used to parse the markup to DOM and parse the DOM to markup. The DOM provided in the API is documented in this package's documentation.
lodash
is used to process the initial template the same way as the loader of the HtmlWebpackPlugin
does that.
How to use
Define the paths to the template plugins in your package.json
Use one of the provided APIs to modify the template
See the modified template in the compiled version of your project
API reference
The processing happens during the build time. Don't let the API (which is really similar to what browsers have for DOM) trick you!
overrideDOM({ dom, parser, serializer }): DOM
Interact with the DOM representation of the template. This is a recommended way to modify a template using this plugin system.
The DOM is parsed by the xmldom
package, see its documentation for some precise information on the deeper API.
This API covers all of the regular needs: handling the DOM including additional interactions with PHP nodes.
A valid DOM should be returned from this method.
overrideText({ markup }): string
Interact with the text representation of the template. It is not recommended to use this if you have an opportunity to use the first API.
This API is meant strictly for interaction with template types that are not supported by the first API, e.g. cshtml
, Razor syntax etc.
Such plugins will be executed after the DOM plugins. A valid markup should be returned from this method.
Usage examples
Adding an additional tag to the document
Adding additional non-supported by DOM syntax to the document
Last updated