This time I would like to show you how to extendPWA Studiowith new features. After reading you will know:
What is thePWA Studio’s extensibility frameworkand what tools/techniques it offers
How to extend PWA Studio storefront
How to create a new PWA Studio module
How to customize PWA Studio storefront’s styles
PWA Studio extensibility framework
PWA Studio extensibility framework is a set of tools that helps developers to extend storefronts in a smart way.
What does that mean? For me extension is smarty if it adds something to the app without core code overwriting, for example:
adding a new payment method without overwriting any checkout core files - this is smart
adding a short description to a product page without overwriting a bunch of components including the product root component - this is also cute
overwrite JS and CSS file of one of the components to change the look of it - this is totally thoughtless
Overwriting core files causing unexpected errors and complicates a project. Each overwrites complicating everything more and more and whatever you want to do in the project with a huge amount of overwrites is hard to do.
if you had overwritten too much in your project you would have looked like this guy:
Thanks to the PWA Studio extensibility framework you can minimize the amount of overwriting to a minimum.
Note:We are talking about PWA Studio 9 that introduced targetables. Thanks to this extending PWA Studio with the new features is possible without overwriting core code. In previous versions of PWA Studio, it was not possible in general, and keep in mind that many of my articles and tutorials are deprecated now and I need to update them.
Targets
One of the options to extend PWA Studio is to use Targets.
How it works:
Using interceptor pattern to modify core behavior by modifying core code output during build time.
the point that you can intercept the normal logic is Targets
targets are variants of a JavaScript pattern called Tappable hook
targets share some functionality with NodeJS’s EventEmitter class
TargetProviders
The TargetProvider is an object that provided public API to creating targets and intercepting targets from other extensions
For example, you can use routes Targets to add a new route. In this case, you create an intercept.js file in use "routes" target to add a new route to your storefront.
On the other hand, if you create your own PWA Studio extension you can use Targetqables to add specific targets for other modules.
How to customize PWA Studio
Typically if you deal with PWA Studio you create a new Storefront or a new extension.
Creating a new storefront
The first option to extend PWA Studio with new features is by creating a new storefront.Venia conceptcan be your starting point, but it’s not obligatory. Typically you will start from the Venia concept because it has many cool features already implemented. In this case, you start from scaffold your project using theyarn create @magento/pwacommand. Then you have the opportunity to add customizations.
Take a look at an example scenario when you want to add a new section to the product page. Let’s add lorem ipsum text beforeAdd to cartbutton.
First, scaffold the PWA Studio project:
1$ yarn create @magento/pwa
2$ cd <project_dir>
3$ yarn run buildpack create-custom-origin
4$ yarn run watch
Second, add this code to thelocal-intercept.jsfile:
This code inserting a span withHello World!before submit button of the add to cart form.
Targetables public API
In the example above I usedinsertBeforeJSXmethod of@magento / pwa-buildpack / lib / WebpackTools / targetables / TargetableReactComponent.js
There are a few more public methods available that help you with modifying the PWA Studio storefront:
insertAfterJSX- Inserting a JSX element _after_ `element`.
addJSXClassName- Adding a CSS classname to a JSX element.
addReactLazyImport- Add a new named dynamic import of another React component
appendJSX- Appending a JSX element to the children of `element`
prependJSX- Prepending a JSX element to the children of `element`.
removeJSX- Removing the JSX node matched by 'element'.
removeJSXProps- Removing JSX props from the element if they match one of the lists of names.
replaceJSX- Replacing a JSX element with a different code.
setJSXProps- Setting JSX props on a JSX element.
surroundJSX- wrapping a JSX element in an outer element.
As you can see those JSX manipulations are powerful and let you do whatever you want.
Creating a new extension
Another way to customize PWA Studio is by creating a new extension. For example, if you had wanted to add integration with any headless CMS systems, you would have to create a new extension.
Everyone who wants to use your extension, can install it and use it in his project. This also means that any extension can be extended in storefronts where it’s used.
As an extension creator, you can use Targetables in your intercept file to add specific Targets that are available to other extensions.
Then you need to link your extension in the package.json file of the PWA Studio project and you are able to work on it.
If you want to read more about creating extensions please checkthis article.
Styling PWA Studio
The common thing that frontend developers doing is styling. In terms, if you want to customize styles of the Storefront you need to add your custom styles somewhere.
Thanks to Targetables styling PWA Studio is much more simplified than before. Chris Brabender wrote a really interesting article about this.
Since PWA Studio 9.0.0 developer experience is much better. There is a public API called Targetables that gives developers an easy way to customize PWA Studio Storefronts.
Thanks to that you can manipulate JSX output, and develop high-quality storefronts/extensions.
Besides that, there is the Targets API that allows for example to create new storefront routes or new content renderers in 1 minute without any overwrites.
All of these goods make PWA Studio a great starting point for any new headless projects connected with the Magento backend.
Each share supports and motivates me to create new content. Thank you!
About the author
Marcin Kwiatkowski
Frontend developer, Certified Magento Full Stack Developer, writer, based in Poland. He has eight years of professional Software engineering experience. Privately, husband and father. He likes reading books, drawing, and walking through mountains.