简体   繁体   English

如何使用webpack.config.js自定义配置?

[英]How to use webpack.config.js custom configuration?

I have a react project with webpack with a webpack.config.js file like this 我有一个带有webpack的react项目,其中包含一个webpack.config.js文件

const webpack = require('webpack');

const config = {
  serverBaseUrl: 'http://localhost:3000',
  devtool: "inline-source-map",
  entry:  __dirname + "/app/App.js",
  output: {
    path: "./public/js/",
    publicPath: "/js/",
    filename: "bundle.js"
  },
  module: {
    loaders: [{
      test: /.jsx?$/,
      exclude: /node_modules/,
      loader: "babel",
      query: {
        presets: ["es2015","react","stage-0"]
      }
    }]
  },
  devServer: {
    port: 8008,
    contentBase: "./public",
    colors: true,
    historyApiFallback: true,
    inline: true
  },
}

if (process.env.NODE_ENV === 'production') {
  config.serverBaseUrl = 'http://api.domain.com';
  config.devtool = false;
  config.plugins = [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({comments: false}),
    new webpack.DefinePlugin({
      'process.env': {NODE_ENV: JSON.stringify('production')}
    })
  ];
};

module.exports = config;

I would like to use config.serverBaseUrl in another js file? 我想在另一个js文件中使用config.serverBaseUrl吗? What should I do? 我该怎么办?

Can I do 我可不可以做

const config = require('./webpack.config.js')

and use the config like this 并使用这样的配置

config.serverBaseUrl

UPDATE : 更新

if I use const= require('config') in another file I got this error 如果我在另一个文件中使用const= require('config') ,则会收到此错误

ERROR in ./app/components/ComponentBase.js
Module not found: Error: Cannot resolve module 'config' in /Users/username/data/projects/bwe_web/app/components
 @ ./app/components/ComponentBase.js 33:13-30

ICN, To go along with J. Titus statement, you probably should just create another JavaScript class called say Environment.js and export it and use the settings from there. ICN,要与J. Titus语句一起使用,您可能应该只创建另一个JavaScript类(例如,称为Environment.js)并将其导出并从那里使用设置。 At the top of your JS class you can require Environment.js and then just pull in the values. 在JS类的顶部,您可以要求Environment.js,然后只需输入值即可。 Keep your webpack config your web.pack config. 保持您的webpack配置为web.pack配置。

'use strict'
var EnvironmentSettings = function(){
   var serverBaseUrl = 'http://api.domain.com';
};

module.exports.EnvironmentSettings = EnvironmentSettings;

Then for any implementation that you need or say want to implement/initialize, you'd just require it at the top of the JavaScript Class. 然后,对于需要或要实现/初始化的任何实现,只需在JavaScript类的顶部进行即可。

const envSettings = require('EnvironmentSettings');
var api = envSettings.serverBaseUrl

You should familiarize yourself with some kind of JS Patterns as well. 您还应该熟悉某种JS模式。 Prototype Pattern, Revealing Prototype Pattern and this will help with your education of including external classes. 原型模式,揭示原型模式,这将有助于您进行包括外部课程在内的教育。

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

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