Schematics
Note: Spartacus 2.x is no longer maintained. Please upgrade to the latest version.
Spartacus schematics allow you to install Spartacus libraries in your project. The following sections describe what the Spartacus schematics do, and also provide information about the various options and commands you can use with the schematics. If you are a developer and are looking for more technical information, see the README in the Spartacus schematics project.
Prerequisites
Before using Spartacus schematics, ensure that you have all of the necessary prerequisites.
Your Angular development environment should include the following:
- Angular CLI: Version 9.1 or later, < 10.0.
- Node.js: The most recent 12.x version is recommended, < 13.
- Yarn: Version 1.15 or later.
Adding Spartacus Libraries to Your Angular Project
You can add Spartacus libraries to your Angular project by running the following command from your project root:
ng add @spartacus/schematics
The following is a description of the various options you can use with the ng add @spartacus/schematics command:
baseUrlsets the base URL of your CX OCC back end.baseSiteis a comma-separated list of base site(s) to use with Spartacus.currencyis a comma-separated list of currencies to use in Spartacus.languageis a comma-separated list of languages to use in Spartacus.occPrefixsets the OCC API prefix, such as/occ/v2/, for example.useMetaTagsdetermines whether or not to configure thebaseUrlandmediaUrlin the meta tags fromindex.html.featureLevelsets the application feature level. The default value is2.0.overwriteAppComponentoverwrites the content ofapp.component.html. The default value istrue.pwaincludes progressive web application (PWA) features when building the application.ssrincludes the server-side rendering (SSR) configuration.
The following is an example that generates an application that is ready to be used with the electronics storefront, that sets the baseUrl and the baseSite, and that also enables server-side rendering:
ng add @spartacus/schematics --baseUrl https://spartacus-demo.eastus.cloudapp.azure.com:8443/ --baseSite=electronics-spa --ssr
Another example is the following, which generates an application that is ready to be used with both an apparel storefront and an electronics storefront, that sets the baseUrl, baseSite, currency, and language, and also enables server-side rendering:
ng add @spartacus/schematics --baseUrl https://spartacus-demo.eastus.cloudapp.azure.com:8443/ --baseSite=apparel-uk-spa,electronics-spa --currency=gbp,usd --language=uk,en --ssr
Additional Commands
By default, the ng add @spartacus/schematics command adds only a basic configuration of Spartacus. The following is a description of the commands you can use to extend your application:
ng g @spartacus/schematics:add-pwaadds a Spartacus-specific PWA module.ng g @spartacus/schematics:add-ssradds the SSR configuration.ng g @spartacus/schematics:add-cms-componentgenerates a CMS component, and adds the CMS component mapping to the specified module, or to a newly-generated module, if no module is specified. For more information, see CMS Component Schematic, below.
How Spartacus Schematics Work
When you run ng add @spartacus/schematics, the command does the following:
- Adds the required dependencies.
- Imports the Spartacus modules in the
app.moduleand sets up the default configuration. - Imports Spartacus styles to
main.scss. - Adds the
cx-storefrontcomponent to yourapp.component. - Optionally updates
index.htmlwith the Spartacus URL endpoints in meta tags. - If the
--pwaflag is included, it adds PWA service worker support for your project. - If the
--ssrflag is included, the command does the following:- Adds server-side rendering dependencies.
- Provides additional files that are required for SSR.
CMS Component Schematic
The following is a description of the available options for the CMS component schematic:
--declareCmsModulespecifies which module the newly-generated CMS component is added to. If no module is specified, a new module is generated.--cmsComponentData, alias--cms, injects theCmsComponentDatainto the new component. By default, this option is set totrue.--cmsComponentDataModel, alias--cms-model, specifies the model class for theCmsComponentData, such asMyModel, for example. This argument is required if--cmsComponentDatais set totrue.--cmsComponentDataModelPath, alias--cms-model-path, specifies the import path for theCmsComponentData. The default is@spartacus/core.
Aside from these custom options, the add-cms-component supports almost all options that are available for the Angular component and module schematics. The full list can be seen in this schema.json file.
The following Angular options are not supported:
- Deprecated options.
- The
--moduleoption for components. If you want to specify an existing module for a component, use--declareCmsModule. Themoduleoption is only applied to the Angularmoduleschematic. - The
--skipImportoption.
Using the ‘add-cms-component’ Schematic
The following are some examples of how the add-cms-component schematic can be used:
ng g @spartacus/schematics:add-cms-component myAwesomeCms --cms-model=MyModelgenerates themy-awesome-cms.component.tscomponent and themy-awesome-cms.module.tsmodule.ng g @spartacus/schematics:add-cms-component myAwesomeCms --cms-model=MyModel --declareCmsModule=my-cms-path/my-cmsgenerates themy-awesome-cms.component.tscomponent and adds it to the specified CMS mapping formy-cms-path/my-cms.module.ts.ng g @spartacus/schematics:add-cms-component myAwesomeCms --cms-model=MyModel --module=appgenerates themy-awesome-cms.component.tscomponent and themy-awesome-cms.module.tsmodule, and imports them to the specifiedapp.module.ts.ng g @spartacus/schematics:add-cms-component myAwesomeCms --cms-model=MyModel --module=app --declareCmsModule=my-cms-path/my-cmsgenerates themy-awesome-cms.component.tscomponent and adds it to the specifiedmy-cms-path/my-cms.module.tsmodule. It also importsmy-cms.module.tsto the specifiedapp.module.ts.