M
M
mrFlyer2017-09-30 13:58:11
webpack
mrFlyer, 2017-09-30 13:58:11

How to pass webpack media variable to script?

Good afternoon.
How to pass a variable to the script itself? I do it through DefinePlugin, but as a result I get undefined. Thanks in advance for your reply!

const Elixir = require('laravel-elixir');
const webpack = require('webpack');
require('laravel-elixir-webpack-official');
require('laravel-elixir-vue-2');

Elixir.webpack.config.plugins   =   [
    new webpack.DefinePlugin({
        'process.env': {
            'API_URL': JSON.stringify("http://domain.com/api_url")
        }
    })
];

Elixir(
    mix => {
        mix.webpack('./src/app.js', publicPath),
            mix.copy(nodeModulesPath+"/font-awesome/fonts/",publicPath+"/fonts/")
    }
);

In script:
mounted(){
        console.log(process.env.API_URL); // undefined
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-10-02
@maxfarseer

To get the value of a variable defined via the define plugin, you just need to write its name. In your case it is: API_URL(not process.env.API_URL)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question