Home >Web Front-end >JS Tutorial >How to package node.js with webpack

How to package node.js with webpack

php中世界最好的语言
php中世界最好的语言Original
2018-03-17 09:37:422658browse

This time I will show you how to package webpacknode.js, what are the precautions for webpack packaging node.js, the following is a practical case, let's take a look.

Installation dependencies

The code is as follows:

npm install --save-dev webpack babel-loader babel-preset-es2015 babel-preset-stage-0

webpack configuration

webpack.config.js

'use strict';
const webpack = require('webpack');
let externals = _externals();
module.exports = {
  entry: {
    app: './app.js',
  },
  target: 'node',
  output: {
    path: './build',
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js']
  },
  externals: externals,
  node: {
    console: true,
    global: true,
    process: true,
    Buffer: true,
    filename: true,
    dirname: true,
    setImmediate: true
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        query: {
          presets: ['es2015','stage-0']
        },
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin()
  ]
};
function _externals() {
  let manifest = require('./package.json');
  let dependencies = manifest.dependencies;
  let externals = {};
  for (let p in dependencies) {
    externals[p] = 'commonjs ' + p;
  }
  return externals;
}

Project Directory

+controller
+models
+routes
+service
+test
+util
-app.js
-config.json
-gulpfile.js
-models.js
-package.json
-pm2.json
-webpack.config.js

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to make a small ball animation with js

How to convert JS data types

How to use the $http service in angular

What are the three attributes of the javascript object

The above is the detailed content of How to package node.js with webpack. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn