2017-06-01 23 views
13

我已經使用了official Webpack template用於Vue.js.它對不同的environments使用不同的配置。他們提供測試,開發和生產。但是,由於我們有兩臺生產服務器(一臺生產和一臺分段),我需要另一臺服務器。Vue.js用不同的環境變量構建

針對不同的生產環境有不同配置的最佳實踐是什麼?我會想到像npm run build --URL:http://some-url.com --PORT:80 ...

歡迎任何建議!

回答

12

這更像是一個webpack問題,而不是Vue.js, 我想分享我們以前的設置來處理不同的構建文件和環境。首先,我們將我們的配置保存在分離的文件夾中。

配置/ index.js

// see http://vuejs-templates.github.io/webpack for documentation. 
var path = require('path') 

const CDN = 'https://cdnURL.com/' 

module.exports = { 
    build: { 
    env: require('./prod.env'), 
    assetsRoot: path.resolve(__dirname, '../dist'), 
    assetsSubDirectory: 'static', 
    assetsPublicPath: CDN, 
    productionSourceMap: true, 
    // Gzip off by default as many popular static hosts such as 
    // Surge or Netlify already gzip all static assets for you. 
    // Before setting to `true`, make sure to: 
    // npm install --save-dev compression-webpack-plugin 
    productionGzip: false, 
    productionGzipExtensions: ['js', 'css'], 
    productionBundleAnalyze: process.env.ANALYZE ? true : false 
    }, 
    dev: { 
    env: require('./dev.env'), 
    port: 8080, 
    assetsSubDirectory: 'static', 
    assetsPublicPath: '/', 
    proxyTable: { 
     '/api': { 
     target: process.env.npm_package_config_proxy, 
     logLevel: 'debug', 
     changeOrigin: true, 
     onProxyRes(proxyRes, req, res) { 
      // http-proxy-middleware 
      proxyRes.headers['Content-Type'] = proxyRes.headers['content-type'] 
      delete proxyRes.headers['content-type'] 
     } 
     } 
    }, 
    // CSS Sourcemaps off by default because relative paths are "buggy" 
    // with this option, according to the CSS-Loader README 
    // (https://github.com/webpack/css-loader#sourcemaps) 
    // In our experience, they generally work as expected, 
    // just be aware of this issue when enabling this option. 
    cssSourceMap: false 
    }, 
    projects: { 
    main: { 
     entry: './packages/home/index.js', 
     devPath: 'main.html', 
     target: 'web', 
     buildPath: path.resolve(__dirname, '../dist/index.html'), 
     testPath: '../packages/home/__test__/index.js' 
    }, 
    desktop: { 
     entry: './packages/desktop/index.js', 
     devPath: 'desktop.html', 
     target: 'electron-renderer', 
     buildPath: path.resolve(__dirname, '../../static/desktop.html'), 
     assetsRoot: path.resolve(__dirname, '../../'), 
     assetsSubDirectory: 'static', 
     assetsPublicPath: '../', 
     testPath: '../packages/desktop/__test__/index.js' 
    }, 
    login: { 
     entry: './packages/login/index.js', 
     devPath: 'login.html', 
     target: 'web', 
     buildPath: path.resolve(__dirname, '../dist/login.html'), 
     testPath: '../packages/login/__test__/index.js' 
    }, 
    setting: { 
     entry: './packages/setting/index.js', 
     devPath: 'setting.html', 
     target: 'web', 
     buildPath: path.resolve(__dirname, '../dist/setting.html'), 
     testPath: '../packages/setting/__test__/index.js' 
    }, 
    playground: { 
     entry: './packages/playground/index.js', 
     target: 'web' 
    } 
    } 
} 

配置/ dev.env.js

var merge = require('webpack-merge') 
var prodEnv = require('./prod.env') 

module.exports = merge(prodEnv, { 
    NODE_ENV: '"development"', 
    API_ROOT: '"/api"' 
}) 

配置/ prod.env

module.exports = { 
    NODE_ENV: '"production"', 
    API_ROOT: '"http://test.example.co/api"' //staging server 
    // API_ROOT: '"http://127.0.0.1:8787/api"' //mock-up server 
} 

櫃面的,我們想要的工作,我們改變API根在這裏。

和我們的webpack.base.conf.js看起來像這樣。 編譯/ webpack.base.conf.js

var path = require('path') 
var config = require('../config') 
var utils = require('./utils') 
var projectRoot = path.resolve(__dirname, '../') 

const isProduction = process.env.NODE_ENV === 'production' 

module.exports = { 
    entry: utils.entrys(), 
    output: { 
    path: config.build.assetsRoot, 
    publicPath: isProduction ? config.build.assetsPublicPath : config.dev.assetsPublicPath, 
    filename: '[name].js' 
    }, 
    resolve: { 
    extensions: ['.js', '.vue', '.json'], 
    alias: { 
     'src': path.resolve(__dirname, '../src'), 
     'assets': path.resolve(__dirname, '../src/assets'), 
     'components': path.resolve(__dirname, '../src/components') 
    }, 
    unsafeCache: true 
    }, 
    target: config.projects[process.env.npm_package_config_dev].target, 
    module: { 
    rules: [ 
     { 
     test: /\.vue$/, 
     loader: 'vue-loader', 
     options: { 
      postcss: [ 
      require('postcss-cssnext')(), 
      require('lost')() 
      ], 
      cssModules: { 
      localIdentName: isProduction ? '[path][name]---[local]---[hash:base64:5]' : '[path][name]--[local]', 
      camelCase: true 
      }, 
      loaders: Object.assign({}, utils.cssLoaders()), 
      preLoaders: { 
      html: 'inline-svg-loader' 
      } 
     } 
     }, 
     { 
     test: /\.js$/, 
     loader: 'babel-loader', 
     include: projectRoot, 
     exclude: /node_modules/, 
     query: { 
      cacheDirectory: true 
     } 
     }, 
     { 
     test: /\.json$/, 
     loader: 'json-loader' 
     }, 
     { 
     test: /\.html$/, 
     loader: 'vue-html-loader' 
     }, 
     { 
     test: /\.(png|jpe?g|gif)(\?.*)?$/, 
     loader: 'url-loader', 
     query: { 
      limit: 10000, 
      name: utils.assetsPath('img/[name].[hash:7].[ext]') 
     } 
     }, 
     { 
     test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, 
     loader: 'url-loader', 
     query: { 
      limit: 10000, 
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]') 
     } 
     } 
    ] 
    } 
} 

,最後我們的package.json是這個樣子

... 
... 
... 
    "scripts": { 
     "dev": "webpack-dashboard -- node build/dev-server.js", 
     "dev:login": "npm config set mesh:dev login && npm run dev", 
     "dev:setting": "npm config set mesh:dev setting && npm run dev", 
     "dev:main": "npm config set mesh:dev main && npm run dev", 
     "dev:desktop": "npm config set mesh:dev desktop && node build/dev-server.js", 
     "dev:playground": " npm config set mesh:dev playground && cross-env PORT=9000 npm run dev" 
    } 
... 
... 
... 
我們使用這種設置了捆綁我們的應用程序的電子,網絡

,和WebKit而使用共享組件。

但後來我們遇到了問題。如果你需要更多模塊化的東西,我們開始使用lerna。我建議你檢查一下。

此致敬禮。

+0

非常感謝你這麼整齊的解釋是可用! +1希望我可以給你更多的分數=) –

0

有一個簡單的方法。在config/prod.env.js追加的變量是這樣的:

module.exports = { 
    NODE_ENV: '"production"', 
    MY_URL: JSON.stringify(process.env.MY_URL || 'http://example.com') 
} 

然後像這樣運行構建:MY_URL=http://example.org npm run build

您的變量將在main.jsprocess.env.MY_URL