Environments
When working with Nx-Shopify themes, we can think of two kind of environemnts: Build environments and Shopify Themekit environments.
This guide will explain how to configure and work with each of these environments.
Build environments#
You can define different named build configurations for your theme's build target in the workspace.json file, such as stage and production, with different default options.
Among the options available for the @trafilea/nx-shopify:build executor, the fileReplacements option is used to work with multiple environments.
The default generated build target contains a production configuration that replaces the environment.ts content with the one inside environment.prod.ts.
And the project files look like this:
That way, you should only import the environment.ts file and let the build process select the respective environemnt values.
You can use this file to place any content specific to the environment the theme is being executed in. For example an API url, an analytics service identifier, implement a simple feature flagging system, etc.
Shopify Themekit environments#
The theme's serve and deploy targets both receive a themekitEnv option with the name of the environment where files are going to be deployed to.
These themekit environments are configured in the theme's config.yml file (Learn more about how to configure this file here).
important
If no value is passed to the themekitEnv option, the default development environment will be used.
By default, the config.example.yml file (reference configuration) has an additional production environment.
That production environment is used by the production configurations of the theme's serve and deploy targets.
You can run these configurations using the nx cli:
Or you can override the themekitEnv by passing it as a cli option:
Example#
Let's configure our theme project for a given situation where we have three different environments and we communicate to a different API data source for each environment.
The environments are:
development(default)stagingproduction
This is our default environment.ts file (used for the default development environment).
We will need to create a copy of the environment.ts for the staging and production environments.
Here is how our environments directory would look like:
Now we just need to import the environment.ts file in our code, for example:
important
Remember to always import the deafult environment.ts file, contents from other environments will be automatically replaced.
Then, we will need to add each environment to the config.yml file:
note
You could configure all themekit envs using the same store but different theme IDs. This depends on your specific case.
Finally, we need to configure our workspace.json to use the environments.
To make use of our environments we can simple run:
You are ready to make the most out of environments! ๐