Webpack using nodejs

I am new to reaction. I'm just starting to study the reaction. I have a problem using webpack in nodejs. I want to create a node server that will run the webpack file. I have a webpack file:

const {resolve} = require('path');
const webpack = require('webpack');
const validate = require('webpack-validator');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
  const {ifProd, ifNotProd} = getIfUtils(env)

  return validate({
    entry: './index.js',
    context: __dirname,
    output: {
      path: resolve(__dirname, './build'),
      filename: 'bundle.js',
      publicPath: '/build/',
      pathinfo: ifNotProd(),
    },
    devtool: ifProd('source-map', 'eval'),
    devServer: {
      port: 8080,
      historyApiFallback: true
    },
    module: {
      loaders: [
        {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
        {test: /\.css$/, loader: 'style-loader!css-loader'},
        {test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
      ],
    },
    plugins: removeEmpty([
      ifProd(new webpack.optimize.DedupePlugin()),
      ifProd(new webpack.LoaderOptionsPlugin({
        minimize: true,
        debug: false,
        quiet: true,
      })),
      ifProd(new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: '"production"',
        },
      })),
      ifProd(new webpack.optimize.UglifyJsPlugin({
        sourceMap: true,
        compress: {
          screw_ie8: true, // eslint-disable-line
          warnings: false,
        },
      })),
    ])
  });
};

How can I use this configuration with nodejs. please, help

+4
source share
5 answers

, webpack- webpack 2+, webpack-validator , . npm install webpack-dev-server -g . , (webpack.config.js):

const path = require("path");
const webpack = require('webpack');
const {getIfUtils, removeEmpty} = require('webpack-config-utils');

module.exports = env => {
  const {ifProd, ifNotProd} = getIfUtils(env)

  return {
    entry: [
      "webpack-dev-server/client?http://localhost:3003",
      "webpack/hot/only-dev-server",
      "react-hot-loader/patch"
    ],
    context: __dirname,
    output: {
      path: path.join(__dirname, './build'),
      filename: 'bundle.js',
      publicPath: '/build/',
      pathinfo: ifNotProd(),
    },
    devtool: ifProd('source-map', 'eval'),
    devServer: {
        contentBase: path.join(__dirname, "src"),
        // enable HMR
        hot: true,
        // embed the webpack-dev-server runtime into the bundle
        inline: true,
        // serve index.html in place of 404 responses to allow HTML5 history
        historyApiFallback: true,
        port: 3003
    },
    module: {
      loaders: [
        {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
        {test: /\.css$/, loader: 'style-loader!css-loader'},
        {test: /(\.eot|\.woff2|\.woff|\.ttf|\.svg)/, loader: 'file-loader'},
      ],
    },
    plugins: removeEmpty([
    //...
    // same as before
    //...
    ])
  };
};

package.json:

{
  ...
  "dependencies": {},
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "react-hot-loader": "^3.1.1",
    "webpack": "^3.8.1",
    "webpack-config-utils": "^2.3.0",
    "webpack-dev-server": "^2.9.4",
  }
}

, webpack.config.js webpack.development.js, enviorment:

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

module.exports = config("development"); // can be "production" or "development"

:

root
    build
        bundle.js
    src
        index.html
    index.js
    package.json
    webpack.config.js
    webpack.development.js

: webpack-dev-server --config webpack.development.js --progress -p --hot -w

--hot - , -w -

+5

:

package.json ( webpack (-g -save-dev), nodemon (-g -save-dev) (-save-dev))

  "scripts": {
    "webpack-watch-client": "webpack -w",
    "server": "nodemon server",
    "dev": "concurrently --kill-others \"npm run webpack-watch-client\" \"npm run server\""
  }

webpack.config.js

'use strict'

const path = require('path');

const PATHS = {
  app: path.join(__dirname, 'src'),
  public: path.join(__dirname, 'public')
};


module.exports = {
  entry: {
    app: PATHS.app,
  },
  output: {
    path: PATHS.public,
    filename: 'js/bundle.js'
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['react', 'es2015']
            }
          }
        ]
      },
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader' }
        ]
      },
      {
        test: /\.(woff|woff2|eot|ttf)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'assets/fonts/'
            }
          }
        ]
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              outputPath: 'assets/img/'
            }
          }
        ]
      }
    ]
  },
 plugins: [
// plugins
  ]
};

Node ./server.js

./src/index.js

./public index.html :

  <div id="root"></div>
  <script type="text/javascript" src="/js/bundle.js" charset="utf-8"></script>

bundle.js - ./public/js

./public/assets/fonts

- ./public/assets/img

: npm dev ( server.js)

.

+2

, , , :

  • un Webpack.config(Webpack).
  • webpack lauch Node server (Express).
  • (React).

- post.

+1

gander github repo.

-, webpack webpack-dev-server. . webpack FE. . /.env . express .

ReactJS, webpack es2015 & &.jsx.

https://github.com/christian4423/express_blog

... . - , :) PM

, webpack . . , PM.

+1

. webpackConfig.js

var webpack = require('webpack')

var config = require('./your_config')

var compiler = webpack(config)

compiler.run(function(err, stats){
   console.log(err, stats)
})
Hide result

node webpackConfig.js "

0

Source: https://habr.com/ru/post/1683667/


All Articles