current position:Home>What is the role of webpack cli in webpack packaging
What is the role of webpack cli in webpack packaging
2022-04-29 19:25:51【Billion cloud speed】
webpack-cli stay webpack What is the role of packaging
This article mainly explains “webpack-cli stay webpack What is the role of packaging ”, The explanation in the text is simple and clear , Easy to learn and understand , Next, please follow Xiaobian's ideas and go deeper slowly , Study and learn together “webpack-cli stay webpack What is the role of packaging ” Well !
webpack & webpack-cli
webpack Introduction
webpack It's a static modular packaging tool , For modern JavaScript Application services
pack bundler:webpack Can help us pack , So it's a packaging tool
Static static: Package the code into the final static resource ( Deploy to static The server )
modularization module:webpack It supports all kinds of modular development by default ,ES Module、CommonJS、AMD etc.
modern modern: Various problems of front-end development , It gave birth webpack Appearance
webpack install
npm install webapck webpack-cli --save-dev
webpack from 4.0 Version start , At the time of installation , You have to install these two things .
webpack It is the core content of packaging code , and webpack-cli Is a program used to run... On the command line webpack Tools for .
but ,webpack-cli It is not necessary to package files , Why is that ?
webpack-cli Detailed explanation
With npm run build
Command to parse webpack-cli Role in code packaging ,wk.config.js For customization webpack The configuration file
"scripts": { "build": "webpack --config wk.config.js" }
When executing... On the command line npm run build when , Will execute node_modules/.bin Under the webpack Executable file
Here are three executables , Corresponding to different platforms
# unix The system defaults to the executable file , You must enter the full file name webpack# windows cmd The default executable in webpack.cmd# windows PowerShell Executable file in , Can cross platform webpack.ps1
With webpack Take the content of executable file as an example :
#!/bin/shbasedir=$(dirname "$(echo "$0" | sed -e 's,\,/,g')")case `uname` in *CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;esacif [ -x "$basedir/node" ]; then "$basedir/node" "$basedir/../webpack/bin/webpack.js" "[email protected]" ret=$?else node "$basedir/../webpack/bin/webpack.js" "[email protected]" ret=$?fiexit $ret
You can see that in the code , Will execute node_modules/webpack/bin/ In the catalog webpack.js, The main code of this document is as follows :
// This function is used to execute the command , For example, for downloading the required package const runCommand = (command ,args) => {}// Determine whether the package is installed const isInstalled = packageName => {}// This function is used to execute webpack-cli In bag bin In the catalog cli.js file const runCli = cli => { const path = require("path"); const pkgPath = require.resolve(`${cli.package}/package.json`); // pkgPath: D:\webpack\node_modules\webpack-cli\package.json const pkg = require(pkgPath); // pkg: webpack-cli Of package.json Configuration in // path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]): D:\webpack\node_modules\webpack-cli\bin\cli.js require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));}if (!cli.installed) { // Judge webpack-cli Whether to install // If not installed , Ask if you want to install webpack-cli ... ...} else { runCli(cli);}
The most important function of this file is runCli
, This function can execute webpack-cli In bag bin In the catalog cli.js file , In other words, the previous steps are only to find cli.js file , After that ,webpack-cli To work .
meanwhile ,webpack.js The document also makes some auxiliary judgments , First check to see if you have installed webpack-cli, If not installed , You'll be asked if ( Or manual installation ) The package , If you choose not to install , Then the program stops running here .
Then open webpack-cli/bin/cli.js
#!/usr/bin/env node"use strict";const importLocal = require("import-local");const runCLI = require("../lib/bootstrap");if (!process.env.WEBPACK_CLI_SKIP_IMPORT_LOCAL) { // Prefer the local installation of `webpack-cli` if (importLocal(__filename)) { return; }}process.title = "webpack";runCLI(process.argv);
The main functions of this file are runCLI
, and runCLI
From bootstrap.js file , open bootstrap.js file
const WebpackCLI = require("./webpack-cli");const runCLI = async (args) => { // Create a new instance of the CLI object const cli = new WebpackCLI(); try { await cli.run(args); } catch (error) { cli.logger.error(error); process.exit(2); }};module.exports = runCLI;
Be careful , It's only here that we really use webpack-cli Exposed interfaces ,cli.run(args) Used to handle command-line parameters , here args Parameter is :
[ 'E:\nodejs\node.exe', 'D:\webpack\node_modules\webpack\bin\webpack.js', '--config', 'wk.config.js']
Final , From the whole process above , We can know webpack-cli Is used to handle command line parameters , And constructed by parameters compiler object , Then comes the process of packaging the code .
This also solves the problems raised above , Why? webpack-cli Packaging is not required for files . since webpack-cli
Just to handle command line arguments , Then we can also build our own cli To deal with parameters , such as lyx-cli. In a third-party framework ,React and Vue( not used Vite Version of ) Not used webpack-cli.
Reference
Three face interviewer : function npm run xxx What happened when ? - Nuggets (juejin.cn)
Command line interface (CLI) | webpack Chinese document (docschina.org)
Thank you for reading , That's all “webpack-cli stay webpack What is the role of packaging ” Content. , After learning this article , I'm sure you're right webpack-cli stay webpack We have a deeper understanding of the role of packaging , The specific use needs to be verified by practice . This is billion speed cloud , Xiaobian will push you articles with more relevant knowledge points , Welcome to your attention !
copyright notice
author[Billion cloud speed],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291744574973.html
The sidebar is recommended
- How to use HTML to display segmentation
- How to define and use HTML box model
- How to use the box pack attribute and box align attribute inside the box in HTML
- What are the differences and relationships among HTML, CSS and JS
- How to save home page as HTML
- How to solve the post request return 405 of nginx reverse proxy to HTML page
- How to upload pictures without refresh with HTML5 + PHP
- How to define HTML text tags, pictures, paths, hyperlinks and anchors
- How to upload static HTML files to the host or server
- How to use calculated and watch in Vue
guess what you like
How does Vue Preview PDF, word, xls, PPT and txt files
Can jQuery listen for events
Luxury cars "senseless price increase", the configuration of the new Porsche Macan remains unchanged, with a maximum increase of 19000 yuan
1-ch579m program upgrade OTA (self built Internet of things platform) - ch579m uses its own Ethernet to remotely download and upgrade MCU program through HTTP (MCU program rotation check and update)
The front-end downloads files, and the back-end sends gzip files. Is there a way to get the file size?
Why does Vue route jump refresh the page?
The conversion code of Babel in the project needs to include node_ Modules
[nginx] prefix removal in nginx proxy pass configuration
Vue packaging error module build failed: typeerror: this getOptions is not a function
Use of I18N in Vue
Random recommended
- Who is bunk, the new hero of endless dungeon? Introduction to new hero bunk skills
- Front end entry first bullet | development tools and configuration
- Nice HTML login page
- Vue error prone summary
- Summary of material resources website (icon | wallpaper | Adobe plug-in | jQuery plug-in | badge | online conversion | color splicing | continuous update)
- Step by step, configure nodesjs environment with hbuilder for Vue JS development
- Vue sets the global websocket and accepts message notification processing on the corresponding page
- Solve the problem that vuex data is reset during page refresh
- Java data structures and algorithms lesson 2 - generics
- Leetcode-82 - delete duplicate Element II in the sorting linked list
- [front end] quickly create HTML
- Vue router routing navigation
- Vue router redirection and alias
- Vue router named routes, (nested) named views
- Dynamic components & slots & custom instructions for getting started with Vue Foundation
- JavaScript algorithm -- heap sorting
- K8s deployment-43-take you to learn ingress nginx (Part 2)
- Detailed breakdown of actual combat of CMS background system project (VII)
- Explore the store | new architecture blessing, performance and safety. This style is super personality_ Integra_ Honda_ platform
- Sports kit blessing / replacement of four cylinder engine Chevrolet chuangku RS live shooting analysis_ Aspect_ Display screen_ Complete vehicle
- Talking about nodejs server
- Node. js< I & gt—— Encounter node and repl usage
- Vue basic API: calculation attribute + filter + listener
- 1-stm32 + mn316 (nb-iot) remote upgrade OTA (self built Internet of things platform) - STM32 uses HTTP to download program files and upgrade programs through mn316 (MCU program rotation check and update)
- Vue Axios response interception
- vue3 ref
- How does Vue transfer the data from the parent component to the child component intact?
- The back-end interface developed by springboot in idea and the Vue front-end developed by vscode. How to integrate Vue code into springboot?
- Fried cold rice series 4: scope and closure in JavaScript
- Typescript type compatibility learning
- Summary of bugs encountered in front-end development
- Chrome developer tool: performance analysis using web panel
- Collation of common semantic elements and global attributes in HTML
- Life cycle in Vue
- 5.1 fear of traffic jam? With a budget of less than 100000, these cars with adaptive cruise make it easy for you to travel
- Docker compose deploy nginx configure SSL
- The content of element type “mapper“ must match “(cache-ref|cache|resultMap*|parameterMap*|sql*|inse
- -CSS-
- Vue uses two-way binding to implement the user registration page
- Is Infiniti qx60 worth less than 400000 yuan? It depends on the discount