Custom arguments with aurelia CLI

Is there a way of putting a variable when using the cli to start an au project (eg: mode:4), and that variable be acessible inside the app?

example:

au run --mode:4

and have this acessible in app.ts

1 Like

Not arbitrary variable. What you can do is through environment file.

For webpack app, add property to config/environment.json and config/environment.production.json (if you want different value for production).

For cli-bundler app, add property to aurelia_project/environments/*.js (or ts file).

Then you can run

au run (implicit au run --env dev)
au run --env prod
au run --env stage (only available for cli-bundler app)

Note our webpack config by default only supports two environments (prod and dev). But it can be adjusted to support more environment files, this is done by app-settings-loader in you config file. I have talked with @Sayan751 before on some way to enhance our default webpack config to support arbitrary environment, but have not done it because of a small breaking change.

3 Likes

If you have a webpack project then I believe that au run finally ends up spawning npm start. Therefore, a workaround can simply be to use cross-env to define a process level variable and use that in your webpack config. If needed you can further use the DefinePlugin to propagate that value to your app.

But I am giving these suggestions w/o the context of your problem. As @huochunpeng pointed out, if your concern is only about different environments, then you can simply use the environment*.json files for that.

2 Likes

@sayan751 @huochunpeng

Context: I currently have an endpoint in my backend that tells me the current release of my SPA, using the Azure release number. The clients of my application usually keep it open in the browser for hours, with no refresh. I wanted, without relying on sessionStorage, to have the release version within my app code without an additional GET for the initial version .

1 Like

So, if I have understood it correctly, every release of the app has a number and it is shown to the user. This information naturally is static.

If this is true, then I think using the DefinePlugin + --env.custom_arg should do the trick. Here is what you can do.

If you use au run pass on any arbitrary argument with env. prefix like this: au run --env.answer 42.
In webpack.config.js modify the first parameter to the webpack config function like this:

module.exports = ({ production, answer } = {}, {...} = {}) => (console.log(`answer: ${answer}`),{...});

In the next step, you can further change your webpack config with the DefinePlugin. During build webpack will replace every occurance of the variable with the static value provided.

3 Likes