Laradock + Webpack +热重载

时间:2017-02-17 16:54:48

标签: laravel docker webpack sass laradock

您的工作场所是什么?

  

您好,我想问一下您同时开发前端和后端的方法是什么?

     

我正在进入Webpack,但是,当我想编辑框架时该怎么办?

     

我是否需要在开箱即用的情况下运行webpack?

     

是否可以将webpack --watch或其他模块作为代理引用到服务器?如果是这样,如何在更改时将* .php文件设置为强制刷新页面。

到目前为止,我已经分别在框架上工作,特别是在前端。现在我真的不知道如何组合在一起,特别是当webpack2的许多模块已经过时时。

Windows X, Docker(laradock), Webpack, SASS, JS, PHP

感谢您未来的建议。

1 个答案:

答案 0 :(得分:1)

您应该使用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()
],

};