0

How we can set Proxy URL from a config.json external file like in .net applications we do configuration through web.config. I am using the vue-cli boilerplate (Vue2 + webpack) + vuex + axios.

I did some work for it but it's not helpful, I import the config.json and use the configuration data from it but after build webpack including that config file in output.min.js

Currently, I am manually changing the server URL in the code before running a build for a production server or a build for a development server but the requirement is needed to use config.json file separately so I will be able to change proxy URL from the config file on the production.

6
  • 1
    Sorry, it's not clear to me what you're trying to do. If you are using vue cli 3, you can use cli.vuejs.org/config/#devserver-proxy is that not sufficient? Commented Feb 25, 2019 at 16:43
  • I want to set a proxy URL from an external JSON file as that file will be used for app configuration and want to exclude from bundling when you build the project, webpack do bundling and minify the file on the build. After build changing the proxy URL from the minified file is difficult for DevOps user. Commented Feb 25, 2019 at 16:57
  • vue cli 3 uses the vue.config.js, so since it is not json, you can include logic in the configuration file, so that should allow you to call the data from an external file. Commented Feb 25, 2019 at 17:11
  • I think you didn't get the requirement. Did you know about bundling and minification? or do you have knowledge of webpack? Commented Feb 26, 2019 at 13:38
  • If you want to be able to change it easily after it's been bundled, put it into a separate js file (so you don't need ajax to load it), and use vue/webpack code splitting to generate a separate resource. Commented Feb 26, 2019 at 15:54

1 Answer 1

1

You can utilize webpack code splitting with the webpackChunkName comment. This will create a separate chunk, which can then be altered easier after build.

Here is the code to have it available inside a vue component

config.json:

{
  "abc": 123,
  "def": 123
}

vue component:

export default {
  name: 'app',
  data: function () {
    return {
      config: null
    }
  },
  created() {
    import (/* webpackChunkName: "config", webpackMode: "lazy-once" */ "./config.json").then(r => this.config = r.default);
  }
}

outside of component

let config;
import(/* webpackChunkName: "config" */ './config.js')
  .then(r => {config : r.default})
  .then(/* do stuff with config */);

Some things to note: - because it is no longer part of the same bundle, the config bundle will need to come from a network request, which means that the response is asynchronous, so you cannot do a simple assignment. This may require less-than-trivial change to your code, as you won't be able to use the value before it is loaded, so any other code that relies on it will need to wait. - whether you use json, or js file in source, the resulting webpack bundle will be a js file and will be almost the same, though the json version is slightly smaller

the config bundle will look something like this

(window["webpackJsonp"]=window["webpackJsonp"]||[]).push([["config"],{"7dc5":function(n){n.exports={abc:123,def:123}}}]);
//# sourceMappingURL=config.26910bd8.js.map

where {abc:123,def:123} is the data part that should be easy to modify now

resources:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Daniel it's helpful for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.