


This article introduces to you the steps and final deployment of using webpack to package the koa2 framework. It is very practical. Friends in need can refer to it.
In the past, when I used koa to write servers, publishing was simply It's a nightmare. All files in src need to be overwritten, and the config configuration file must also be overwritten. If you are not careful, various problems will be reported online, and then you have to roll back and adjust them locally before publishing. I accidentally saw an article about how to use webpack to package koa app. I was shocked. It turns out that webpack can also package the backend. I had never thought of this before.
Key issues
1: All modules in node_modules are not packaged
The core function of webpack is to reference Each module is typed into a file, and various standardized modules are unified and modularized (webpack specification).
However, node contains a large number of fs and path operations. These fs and path operations will have no operation objects after the packaging is completed, and many various errors will be reported.
So the core of using webpack for packaging is to refuse to package all modules in node_modules, and only package the files referenced by relative paths into one file. It happened that we found that webapck provides the externals attribute to exclude modules that do not need to be packaged.
If we dig deeper, we can find that modules like webpack, nodemon, and babel-preset-env are packages that the app development environment depends on, and our program will not require these modules at all.
In summary, we can find that: we only need to exclude all required packages. This module corresponds to the module under dependencies in package.json. It is important to understand the difference between dependencies and devDependencies.
So we can use the externals-dependencies plug-in with the externals attribute to exclude dependencies.
Code:
const webpack = require('webpack'); const _externals = require('externals-dependencies') module.exports = { ... externals: _externals(), ... }
Two: target points to node
Official documentation: Compiled into a Node.js-like environment available (use Node.js require to load chunk)
Code:
target: 'node',
Three: Add node configuration
Official documentation: These options can configure whether to polyfill or mock certain Node.js Global variables and modules. This allows code originally written for the Node.js environment to run in other environments such as browsers.
Code:
node: { console: true, global: true, process: true, Buffer: true, __filename: true, __dirname: true, setImmediate: true, path: true },
Four: babel configuration
In order to be compatible with the problem that lower versions of node do not natively support async/await. Here I use babel-preset-env{"modules": false} configuration for babel. This configuration will convert es6 syntax to es5 syntax, such as let and const to var.
At the same time, all async/await functions are also converted into the _asyncToGenerator function defined in the polyfill.
In fact, promises are used to implement the functions of async functions.
Of course this function also requires the regeneratorRuntime function when running. So I introduced babel-polyfill globally to provide the regeneratorRuntime function.
Note: If your node version is very high and supports async/await natively, you can omit babel-preset-env and babel-polyfill
Code:
const path = require('path'); const webpack = require('webpack'); const _externals = require('externals-dependencies') module.exports = { entry: { app: [ // 如果polyfill放在这里,打包的时候将不会被external,必须在js里require才能有效external // 'babel-polyfill', './src/index.js' ] }, output: { path: path.resolve(__dirname), filename: '[name].js' }, resolve: { extensions: [".js"] }, target: 'node', externals: _externals(), context: __dirname, node: { console: true, global: true, process: true, Buffer: true, __filename: true, __dirname: true, setImmediate: true, path: true }, module: { rules: [ { test: /\.js/, use: ['babel-loader'] } ] }, plugins: [ new webpack.DefinePlugin({ 'process.env': { NODE_ENV: '"development"' } }), ] }
Deployment
After packaging, deployment is much more convenient. You only need to deploy package.json, app.js, and the html in the view online. Then execute
1. npm install
2. npm run for
on the server and then the server will run in the background. If you need to update and publish, you only need to re-package npm run dev or npm run build locally and drag it to the server to overwrite app.js.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to develop component libraries using React
##Using fullpage.js to implement scrolling
The problem of failure to install Electron using npm
The above is the detailed content of How to package the koa2 framework app through webpack? What should I do?. For more information, please follow other related articles on the PHP Chinese website!

Vue是一款优秀的JavaScript框架,它可以帮助我们快速构建交互性强、高效性好的Web应用程序。Vue3是Vue的最新版本,它引入了很多新的特性和功能。Webpack是目前最流行的JavaScript模块打包器和构建工具之一,它可以帮助我们管理项目中的各种资源。本文就为大家介绍如何使用Webpack打包和构建Vue3应用程序。1.安装Webpack

区别:1、webpack服务器启动速度比vite慢;由于vite启动的时候不需要打包,也就无需分析模块依赖、编译,所以启动速度非常快。2、vite热更新比webpack快;vite在HRM方面,当某个模块内容改变时,让浏览器去重新请求该模块即可。3、vite用esbuild预构建依赖,而webpack基于node。4、vite的生态不及webpack,加载器、插件不够丰富。

随着Web开发技术的不断发展,前后端分离、模块化开发已经成为了一个广泛的趋势。PHP作为一种常用的后端语言,在进行模块化开发时,我们需要借助一些工具来实现模块的管理和打包,其中webpack是一个非常好用的模块化打包工具。本文将介绍如何使用PHP和webpack进行模块化开发。一、什么是模块化开发模块化开发是指将程序分解成不同的独立模块,每个模块都有自己的作

Laravel是一个极受欢迎的PHP开发框架,它以其简洁、优雅和高效的特性得到了众多开发者的青睐。随着Laravel的不断发展,LaravelEnvoyer作为一种部署工具,可帮助开发者更容易地将应用程序部署在服务器上。本文将向您介绍如何使用LaravelEnvoyer快速、轻松地部署应用程序。LaravelEnvoyer是什么?LaravelEnv

在vue中,webpack可以将js、css、图片、json等文件打包为合适的格式,以供浏览器使用;在webpack中js、css、图片、json等文件类型都可以被当做模块来使用。webpack中各种模块资源可打包合并成一个或多个包,并且在打包的过程中,可以对资源进行处理,如压缩图片、将scss转成css、将ES6语法转成ES5等可以被html识别的文件类型。

Webpack是一款模块打包工具。它为不同的依赖创建模块,将其整体打包成可管理的输出文件。这一点对于单页面应用(如今Web应用的事实标准)来说特别有用。

配置方法:1、用导入的方法把ES6代码放到打包的js代码文件中;2、利用npm工具安装babel-loader工具,语法“npm install -D babel-loader @babel/core @babel/preset-env”;3、创建babel工具的配置文件“.babelrc”并设定转码规则;4、在webpack.config.js文件中配置打包规则即可。

随着现代Web应用程序的复杂性不断增加,构建优秀的前端工程和插件系统变得越来越重要。随着SpringBoot和Webpack的流行,它们成为了一个构建前端工程和插件系统的完美组合。SpringBoot是一个Java框架,它以最小的配置要求来创建Java应用程序。它提供了很多有用的功能,比如自动配置,使开发人员可以更快、更容易地搭建和部署Web应用程序。W


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
