Alert This post is over a year old, some of this information may be out of date.

Passing arguments with custom Gulp tasks for SharePoint Framework projects

If you ever created Gulp tasks which made use of arguments to specify certain configuration settings to be used during the execution. You probably made use of a module called yargs.

The yargs module makes it easy to check if arguments are provided and to retrieve their values.

If you are building your own custom Gulp tasks for your SharePoint Framework projects, you do not require this module. The Gulp build engine which is created for SharePoint Framework automatically processes all the provided arguments and provide them with the config object in your task.

By default, if you create a new Gulp task for SPFx, you start with the following code:

1
2
3
4
5
build.task('upload-to-sharepoint', {
  execute: (config) => {
    // Your task code
  }
});

The config object has a property args that contains all your provided arguments. So if you called the following command for example: gulp upload-to-sharepoint --username elio. You could retrieve the username argument like this in the Gulp task:

1
2
3
4
5
6
build.task('upload-to-sharepoint', {
  execute: (config) => {
    const username = config.args['username'];
    // The rest of your task code
  }
});

Hope this helps you extending your SharePoint Projects.

Comments

Back to top