简体   繁体   English

Vue.js将设置文件排除在捆绑之外

[英]Vue.js exclude settings file from being bundled

I am using the vue-webpack template and I have created a settings.json file to store environment variables that should be changed when installing the script. 我使用的是vue-webpack模板,我创建了一个settings.json文件来存储安装脚本时应该更改的环境变量。

My settings.json (just store the absolute path to the API server): 我的settings.json (只存储API服务器的绝对路径):

{
  "apiURL": "//localhost/app/server/API"
}

How can I keep the file from being minified/bundled in the production version such that I can change it and the updated file will be used next time the app is accessed (without having to build it again) ? 如何防止文件在生产版本中被缩小/捆绑,以便我可以更改它,下次访问应用程序时将使用更新的文件(无需再次构建)?

In my app I use this file via require : 在我的应用程序中,我通过require使用此文件:

const SETTINGS = require('../settings.json');

I understand that by require ing it webpack will bundle it as a module, but how can I include it in my app such that the settings file will still be a separated file in the production build that I can edit. 据我所知,通过require它将webpack捆绑为一个模块,但我如何将其包含在我的应用程序中,以便设置文件仍然是我可以编辑的生产版本中的单独文件

Is there a better format/way to store those settings (so that they can be edited in production without re-building) ? 是否有更好的格式/方式来存储这些设置(以便可以在生产中编辑它们而无需重新构建)?

You can define those settings in an object that can be referenced in the externals configuration in webpack.config.js. 您可以在对象中定义这些设置,这些设置可以在webpack.config.js的externals配置中引用。

The externals configuration option provides a way of excluding dependencies from the output bundles. externals配置选项提供了一种从输出包中排除依赖关系的方法。 Instead, the created bundle relies on that dependency to be present in the consumer's environment. 相反,创建的捆绑包依赖于该依赖关系在消费者环境中的存在。

Example: 例:

externals: {
  appSettings: "appSettings",
  "window.appSettings": "appSettings"
}

Where appSettings is a global variable containing the environment variables you want to manipulate. appSettings是一个包含您想要操作的环境变量的全局变量。


Alternatively, if you do not like that method that exposes the settings in the global object, you can do the following: 或者,如果您不喜欢公开全局对象中的设置的方法,则可以执行以下操作:

Export a variable with the default settings, which will be included in webpack bundle. 使用默认设置导出变量,该变量将包含在webpack包中。

export var appSettings = {
  currentSettings: "",
  settings: {},
  getString: function(strName) {
    var sett = this.currentSettings ?
      this.settings[this.currentSettings] :
      appDefaultStrings;
    if (!sett || !sett[strName]) sett = appDefaultStrings;
    return sett[strName];
  },
  getSettings: function() { //Gets all available settings
    var res = [];
    res.push("");
    for (var key in this.settings) {
      res.push(key);
    }
    res.sort();
    return res;
  }
};

export var appDefaultStrings = {
  apiURL: "//localhost/app/server/API"
    //...
}
appSettings.settings["default"] = appDefaultStrings;

You can then require or import this variable and use it like so: 然后,您可以要求或导入此变量并使用它,如下所示:

import appSettings from "../path/to/appSettings";

appSettings.getString("apiURL"); //"//localhost/app/server/API"

Now that you have your default settings up and running, we will create another file containing the custom settings. 现在您已启动并运行默认设置,我们将创建包含自定义设置的另一个文件。

import appSettings from "../path/to/appSettings";

export var appProductionSettings = {
  apiUrl: "http://example.com"
    //...
}

appSettings.settings["production"] = appProductionSettings;

The last thing you need to do is handle which settings you want to use. 您需要做的最后一件事是处理您要使用的设置。 I have not used vue.js yet, but hopefully this will lead you in the right direction: 我还没有使用过vue.js,但希望这会引导你朝着正确的方向前进:

import appSettings from "../path/to/appSettings";

export class MyApp {
  constructor() {
    this.settingsValue = "";
  }
  get settings() {
    return this.settingsValue;
  }
  set settings(value) {
    this.settingsValue = value;
    appSettings.currentSettings = value;
  }
}

Change the settings: 更改设置:

import "../path/to/productionSettings";

var app = new MyApp();

app.settings = "production";

With this method you can create and use as many settings files as you want. 使用此方法,您可以根据需要创建和使用任意数量的设置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM