nodejs service, web pack packaged koa service

Keywords: Javascript Webpack npm JQuery JSON

Preface

At present, the iteration of front-end update is faster and faster, and the corresponding supporting services are also developing rapidly. node service koa uses web pack packaging scheme.

install

npm install
npm run build
  1. New project package.json
{
  "name": "koa2-blog",
  "version": "1.0.0",
  "description": "blog",
  "main": "main.js",
  "directories": {
    "lib": "./src/main.js"
  },
  "scripts": {
    "dev": "supervisor -w src src/main.js",
    "build": "webpack --progress --hide-modules --config webpack.config.js",
    "test": "./node_modules/mocha/bin/mocha --harmony test"
  },
  "author": "Jiang Xiao Er",
  "license": "MIT",
  "dependencies": {
    "config-lite": "^2.0.0",
  },
  "devDependencies": {
    "babel-core": "^6.22.1",
    "babel-loader": "^6.2.10",
    "babel-minify-webpack-plugin": "^0.3.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-3": "^6.24.1",
    "clean-webpack-plugin": "^0.1.17",
    "crypto": "^1.0.1",
    "express": "^4.17.1",
    "fs": "^0.0.1-security",
    "mocha": "^4.0.1",
    "path": "^0.12.7",
    "webpack": "^3.10.0",
    "webpack-node-externals": "^1.7.2"
  }
}
  1. webpack.config.js
/**
 * Created by Jiang Xiao Er 2019/7/10.
 * 
 * 
 */
'use strict';
const webpack = require('webpack');
const path = require('path');
const cleanWebpackPlugin = require('clean-webpack-plugin');
var nodeExternals = require('webpack-node-externals');
const MinifyPlugin = require('babel-minify-webpack-plugin');
module.exports = {
    entry: path.resolve(__dirname, 'service.js'), //Entry file
    output: {
        path: path.resolve(__dirname), //Output path
        filename: 'app.js'             // Output project root directory
    },
    module: {
        loaders: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: [{
                loader: 'babel-loader',
                options: {
                    presets: ['es2015', 'stage-3'] //Compatible with es6 and add. babelrc
                }
            }]

        }]
    },
    target: 'node', // Server Packing
    externals: [nodeExternals()], //node packaging removes some warnings
    resolve: {
        modules: [
            'node_modules',
            path.resolve(__dirname, 'src')
        ]
    },
    plugins: [
        new cleanWebpackPlugin(['app.js'], {
            root: path.resolve(__dirname, './')
        }),
        new MinifyPlugin() //Compress js
    ]
};
  1. ES6 grammar support

Modify the. babelrc file in the project

{
 "presets": [
    "es2015",
    "stage-3"
 ],
 "plugins" : ["transform-runtime"]
}
  1. service.js
const $ = require('jquery')
console.log( "Hello Word,hi jquery")
console.log( "hi jquery",$ )

$ npm run build
$ node app.js

Project address https://github.com/shanyanwt/...

problem

Because it is based on packaging in the node environment, in fact, webpack does not enter node_modules into app.js. To execute app.js, it needs to be recorded in the project. Webpack packaging only integrates multiple files into one file, without project dependency. It also needs npm install in the service. If one day, it can also depend on it as a web project. Input files and java generate compiled jar execution files, so that the production environment also eliminates some unnecessary links.

If you have good suggestions and ideas, you can leave a message in the comments section, or contact your email address: shanyanwt@163.com.
May you remain independent thinking, not humble, not overly aggressive, and try to grow into your own likes.

I am a lonely wolf... Welcome star

Posted by kavitam on Sat, 05 Oct 2019 01:52:50 -0700