Home > Web Front-end > JS Tutorial > body text

How to package node.js with webpack

php中世界最好的语言
Release: 2018-03-17 09:37:42
Original
2592 people have browsed it

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
Copy after login

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;
}
Copy after login

Project Directory

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!