True ENV Values in Laravel Mix

后端 未结 3 1398
面向向阳花
面向向阳花 2021-02-15 18:33

So in Laravel Mix it says in the docs we can add stuff to our .env file prefixed with MIX_ and we can then access it in our JS file when compiled.

I think I may be missi

3条回答
  •  隐瞒了意图╮
    2021-02-15 19:01

    Here is the way I ended up doing this:

    Anyone landing here from google here is how I did this:

    env.js

    let globalEnv = {
      development: {
        embedScriptUrl: 'http://localjspath.js',
        embedCssUrl: 'http://localcsspath.css',
      },
     production: {
        embedScriptUrl: 'https://s3.us-east-2.amazonaws.com/pathtojs.js',
        embedCssUrl: 'https://s3.us-east-2.amazonaws.com/pathtocss.css',
      }
    };
    
    export default function env(property){
      return globalEnv[process.env.NODE_ENV][property];
    };
    

    You then just import and call env('embedScriptUrl') in your front end JS like you would in a laravel PHP file

提交回复
热议问题