你应该使用 webpack-dev-server 作为代理来支持多个主机。然后,您将能够从服务器加载“后端”并使用 webpack MHR 内联的所有好处在另一个环境中进行开发。
这是我的webpack.config.js
var path = require('path');
var htmlWebPack = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack')
var ProvidePlugin = require('webpack/lib/ProvidePlugin');
var BrowserSyncPlugin = require('browser-sync-webpack-plugin');
var buildPath = path.join(__dirname); // Local /public/
var serverURL = 'http://localhost:8080/' // Webpack-dev-server
var proxyURL = 'http://LaraDock.dev:85/' // External server (laradock)
var proxy = {
'*': proxyURL
};
module.exports = {
devtool: 'cheap-eval-source-map',
context: path.join(__dirname, 'resources'), // ABSOLUTE ROUTE ./
entry: {
app: [
'webpack-dev-server/client?' + serverURL,
'webpack/hot/only-dev-server',
path.join(__dirname, 'resources/assets/js/app.js')
]
},
output: {
publicPath: serverURL + buildPath,
path: path.resolve(__dirname, buildPath),
filename: "js/[name].bundle.js"
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.common.js'
},
},
module: {
loaders: [
// JS
{
test: /\.js$/, // ON WHAT TYPE USE THIS LOADER
loader: 'babel-loader',
include: path.join(__dirname, 'resources', 'assets', 'js'),
},
// VUE
{
test: /\.vue$/,
loader: 'vue-loader',
include: path.join(__dirname, 'resources', 'assets', 'js'),
},
// STYLE
{
test: /\.(sass|scss|css)$/,
loader: ['style-loader', 'css-loader', 'sass-loader'],
include: path.join(__dirname, 'resources', 'assets', 'scss'),
},
// FILES
{
test: /\.(jpe?g|png|gif|svg|ico)$/i,
loader: "file-loader?name=[path][name].[ext]&context=./resources/assets"
},
// FONTS
{
test: /\.(otf|eot|svg|ttf|woff)$/,
loader: 'url-loader?limit=10000'
},
{
test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url-loader?limit=10000"
},
{
test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
loader: 'file-loader'
},
// BOOTSTRAP
{
test: /bootstrap\/public\/js\/umd\//,
loader: 'imports?jQuery=jquery'
},
],
},
devServer: {
contentBase: serverURL + buildPath,
proxy: proxy,
historyApiFallback: true,
hot: true,
noInfo: true,
stats: {
errors: true,
colors: true,
errorDetails: true,
reasons: false,
hash: false,
version: false,
timings: false,
assets: false,
chunks: false,
modules: false,
children: false,
source: false,
warnings: false,
publicPath: false
}
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new BrowserSyncPlugin(
// BrowserSync options
{
// Webpack-dev-server endpoint
host: 'http://localhost',
port: 80,
// proxy the Webpack Dev Server endpoint
// (which should be serving on http://localhost:80/) through BrowserSync
proxy: serverURL,
// Files
files: ['public/*', 'app/**/*.php', 'config/**/*.php', 'resources/views/**/*.php'],
},
// plugin options
{
// prevent BrowserSync from reloading the page
// and let Webpack Dev Server take care of this
reload: false
}),
new ExtractTextPlugin({
filename: './css/[name].style.css',
disable: false,
allChunks: true
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
"Tether": 'tether' // Bootstrap v4 problem
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin()
],
};