Webpack: จะรวมฝั่งไคลเอ็นต์และเซิร์ฟเวอร์เข้าไว้ในชุดสุดท้ายได้อย่างไร

ฉันมีอาการเวียนศีรษะจากการกำหนดค่าและกรณีการใช้งาน webpack มากมาย และเข้าสู่ภาวะชะงักงัน ในที่สุดฉันก็สร้างแอปของฉันแล้ว: react.js + express.js

ขณะนี้ฝั่งเซิร์ฟเวอร์เพียงประมวลผลข้อมูลแบบฟอร์มที่ส่งและใช้ nodemailer เพื่อส่งอีเมล ในอนาคตอันใกล้นี้จะมีการแทรกข้อมูลลงในเทมเพลต html ขึ้นอยู่กับเส้นทาง

ดังนั้นทุกอย่างในเครื่องทำงานได้ดี แต่ฉันไม่รู้ว่าจะรวมไคลเอนต์และฝั่งเซิร์ฟเวอร์สำหรับชุดการผลิตขั้นสุดท้ายด้วย webpack ได้อย่างไร

นี่คือส่วนหนึ่งจาก packages.json ของฉัน:

  "main": "src/index.js",
  "scripts": {
    "start": "start npm run start:client && start npm run start:server",
    "start:client": "webpack-dev-server",
    "start:server": "node server.js"
  },
  "proxy": "http://localhost:3000",

ส่วน webpack:

module.exports = {

    devtool: 'source-map',
    devServer: {
        historyApiFallback: true,
        contentBase: './src',
        proxy : {
            '/contact': 'http://localhost:3000'
          }     
    },

    entry: {
        vendor:  ['./src/js/plugins.js', 
            './src/js/classList.min.js'].map(function(link){ 
                return path.resolve(__dirname, link); 
            }),



        base: ['./src/css/animate.css', './src/css/outdatedbrowser.css'].map(function(link){ 
                return path.resolve(__dirname, link); 
            }),
        entry: ["babel-polyfill", './src/index.js']
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].[chunkhash].js',
        publicPath: '/'
    },
    resolve: {
        extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
        modules: [
             path.join(__dirname, 'node_modules')
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('development')
        }),
        new webpack.optimize.ModuleConcatenationPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            filename: 'vendor.[chunkhash].js',
            minChunks: Infinity
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                screw_ie8: true,
                conditionals: true,
                unused: true,
                comparisons: true,
                sequences: true,
                dead_code: true,
                evaluate: true,
                if_return: true,
                join_vars: true
            },
            output: {
                comments: false
            }
        }),
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: [
                    autoprefixer({
                        browsers: [
                            'last 3 version',
                            'ie >= 10'
                        ]
                    })
                ],
                context: staticSourcePath
            }
        }),
        new webpack.HashedModuleIdsPlugin(),
        new HtmlWebpackPlugin({
            template: path.join(__dirname, './public/index.html'),
            path: buildPath,
            excludeChunks: ['base'],
            filename: 'index.html',
            minify: {
                collapseWhitespace: true,
                collapseInlineTagWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true
            }
        }),
        new PreloadWebpackPlugin({
            rel: 'preload',
            as: 'script',
            include: 'all',
            fileBlacklist: [/\.(css|map)$/, /base?.+/]
        }),
        new ExtractTextPlugin({
            filename: '[name].[contenthash].css',
            allChunks: true
        }), 
        new StyleExtHtmlWebpackPlugin({
            minify: true
        }),
        new webpack.NoEmitOnErrorsPlugin(),
        new CompressionPlugin({
            asset: '[path].gz[query]',
            algorithm: 'gzip',
            test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
            threshold: 10240,
            minRatio: 0.8
        })
    ],
    module: {

        rules: [{
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'react', 'stage-1'],
                    }
                },
                include: sourcePath
            }

        ]
    }

};

และ โครงสร้าง ไฟล์:

    dist <folder>
    node_modules <folder> ----   
    public <folder> ---- 
        index.html
    src <folder> ----   
        components <folder>
        ---css < folder > 
        ---fonts < folder >
        ---icons < folder >
        ---img < folder >
        ---js < folder > - third party JS
        ---.env    
        ---i18n.js    
        ---index.js
        ---myDetector.js
        ---sitemap.xml    
    server.js   
    package.json     
    package-lock.json 
    webpack.config.js

ใครสามารถช่วยกำหนดค่าเพื่อให้มันทำงานได้?


person Kuzma    schedule 07.02.2018    source แหล่งที่มา


คำตอบ (2)


ฉันไม่รู้ว่าทำไมไม่มีใครตอบ แต่คำตอบนั้นง่าย: ไม่สามารถรวมเข้าด้วยกันได้ ฝั่งเซิร์ฟเวอร์ควรทำงานบน VPS

person Kuzma    schedule 08.02.2018

ฉันกำลังแบ่งปันโครงสร้างโครงการ reactjs ของฉันด้วยการกำหนดค่า ..

ติดตั้ง nodemon เพื่อดูเซิร์ฟเวอร์ทุกครั้งที่ต้องการเปลี่ยนแปลงอะไรในโค้ดฝั่งเซิร์ฟเวอร์และ babel สำหรับการแปลง es6 หรือ es7 เป็น es5

แพ็คเกจ json

"scripts": {
"server": "nodemon --watch server --exec babel-node -- server/index.js",
"start": "nodemon --watch server --exec babel-node -- server/index.js",
"test": "echo \"Error: no test specified\" && exit 1"

},

webpack.config.dev.js

let outputPath = path.join(__dirname);


export default {
// devtools: 'eval-source-map',
devtool: 'source-map',
entry: [
    'webpack-hot-middleware/client',
    path.join(__dirname, 'client/index.js'),
],
output: {
    path: outputPath,
    publicPath: "/",
    filename: 'bundle.js'
},
module:{        
    loaders:[
        {
            test: /\.js$/,                
            include: path.join(__dirname, 'client'),
            exclude: /(node_modules|bower_components)/,
            loaders: ['react-hot-loader','babel-loader'],                
        }
    ]
},
node: {
    fs: 'empty'
},
plugins: [
    new webpack.ProvidePlugin({
        "React": "react",
    }),
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
],
resolve: ['','.js']

}

โครงสร้างโครงการ

node_modules <folder> ----   
public <folder> ----   
    components <folder>
    ---css < folder > 
    ---fonts < folder >
    ---icons < folder >
    ---img < folder >
    ---js < folder > - third party JS
    ---.env    
    ---i18n.js    
    ---index.js
    ---myDetector.js
    ---sitemap.xml    
server
    ---index.html
    ---index.js
package.json     
package-lock.json 
webpack.config.dev.js
person radhey shyam    schedule 07.02.2018
comment
ดูเหมือนว่าจะไม่เกี่ยวข้องกับคำถามของฉัน: ฉันต้องรวมเป็นชุดเดียวที่ทำงานส่วนหน้าตอบสนองแอป + ฝั่งเซิร์ฟเวอร์ - express.js - person Kuzma; 08.02.2018
comment
Bundle.js เป็นไฟล์ที่โค้ดส่วนหน้าทั้งหมดจะผูก: เอาต์พุต: { เส้นทาง: outputPath, publicPath: /, ชื่อไฟล์: 'bundle.js' }, - person radhey shyam; 08.02.2018
comment
ฉันไม่คิดว่าจะเป็นไปได้สำหรับไฟล์ฝั่งเซิร์ฟเวอร์ - person radhey shyam; 08.02.2018