Introduction
Hello everyone, this is part 2 of my series. For the first one, you can check out SPFX - Learning and Findings - Part One.
Environment Update During Builds
In this article, we will learn how to make environment-based changes dynamically. I was using CDN in my application and as you know the URL changes for each environment so before creating a build I must make changes everywhere.
This was boring, so I thought of automating it. I started Googling it and found plugins that can do these changes on Json files but what happens if there is something in code (i.e. typescript files.)?
For JSON changes I got inspired by Wictor Wilen Article SharePoint Framework bundles for multiple tenants. Thanks to him, I was able to write JSON modification logic.
Before we go in detail, you should know SPFx uses “webpack” as a module bundler and “gulp” for tasks. For more details about these, you can Google it. In the latest version of SPFx 1.10.0, they use webpack 4+. Webpack 4+ has few inbuilt plugins like define plugin. This define plugin will help us in defining global constants. For modifications in the JSON file, we will create a task which is like gulp task and add in the SPFx pipeline.
This code has to be added in gulp file before “build.Initialize(gulp);”.
- //Define this variables at the top of the file
- var cdnurl = "";
- var tinymcecdnurl=""
- var gulpenvsetting="local";
- //Following function initialise the environment based on parameter specified in terminal
- const getCDNUrl = build.subTask('getcdnurl', (gulp, buildConfig, done) => {
- //Getting the environment parameter from terminal
- const targetval = buildConfig.args['target-env'];
- //checking if environment parameter is present otherwise it will take default
- if (targetval) {
- gulpenvsetting = targetval;
- }
- //based on env cdn url is set.You can do other operations also.
- if (gulpenvsetting == "local") {
- cdnurl = "https://publiccdn.sharepointonline.com/<<url>>";
- }
- else if (gulpenvsetting == "qa") {
- cdnurl = "https://publiccdn.sharepointonline.com/<<url>>";
- }
- else if (gulpenvsetting == "prod") {
- cdnurl = "https://publiccdn.sharepointonline.com/<<url>>";
- }
- tinymcecdnurl = cdnurl + '/tinymce/tinymce.min.js';
- done();
- });
- //Following function replaces url in config.json
- const replaceCDNUrlsInConfig = build.subTask('replacecdnurl', (gulp, buildConfig, done) => {
- //Other operations can be done here based on your requirements.
- //parsing the config.json externals
- for (const key in buildConfig.properties.externals) {
- var external = buildConfig.properties.externals[key];
- //replacing the dummystring mentioned in the config.json with our actual value
- if (external.path) {
- external.path = external.path.replace(dummyurl, cdnurl);
- }
- else {
- buildConfig.properties.externals[key] = external.replace(dummyurl, cdnurl);
- }
- }
- done();
- });
- //Following function merge webpackconfig with our configuration.
- //In this function we define global constants which will be used in TS file.
- build.configureWebpack.mergeConfig({
- additionalConfiguration: (generatedConfiguration) => {
- for (var i = 0; i < generatedConfiguration.plugins.length; i++) {
- var plugin = generatedConfiguration.plugins[i];
- if (plugin instanceof webpack.DefinePlugin) {
- //defining global constant "tinymcecdnurl"
- plugin.definitions.tinymcecdnurl = JSON.stringify(tinymcecdnurl);
- break;
- }
- }
- return generatedConfiguration;
- }
- });
- //Task create above has to be added in SPFx pipeline.Following is the way to add in pipeline.
- build.rig.addPreBuildTask(getCDNUrl);
- build.rig.addPostBuildTask(replaceCDNUrlsInConfig);
Join the conversation! Your thoughts help the community grow.