0

I have some js file which is generated by aws cognito, and its content is different based on environment.
As I should not modify it manually, so vue's environment variables may not work for me.

How should I dynamically load the js file based on the environment?
I use following command to run my server on local

vue-cli-service serve --mode=dev

I located the file under /environment/dev/aws-exports.js

const awsmobile = {
    "aws_project_region": "ap-northeast-1",
    ... some more json...
};
export default awsmobile;

and load in the main.js

import config from "./environment/dev/aws-exports"
Amplify.configure(config);

How should I modify my code so that it can load it based on --mode?

brian661
  • 538
  • 3
  • 11
  • 31

1 Answers1

0

I am not sure if this is exactly what you are looking for but I'll try to help:

To dynamically load the js file based on the environment you can create a function that returns an import:

function getConfig(mode) {
  return import(`./environment/${mode}/aws-exports`)  // import returns a Promise
}

Note: remember that import is a Promise so you might need to await it.

Adam Orłowski
  • 4,268
  • 24
  • 33