Home Web Front-end JS Tutorial What needs to be done to package specified JS files with webpack

What needs to be done to package specified JS files with webpack

Mar 17, 2018 pm 03:25 PM
javascript web webpack

This time I will show you how to package the specified JS file with webpack. What are the precautions for packaging the specified JS file with webpack? The following is a practical case, let's take a look.

Background

Recently received a request, because the back-end IP accessed by the printed front-end package is not sure, and it is necessary to check the IP in the project

Configuration file Take it out separately to facilitate IP modification during operation and maintenance deployment. Therefore, you need to use webpack to package the specified files separately.

CommonsChunkPlugin

module.exports = {
 entry: {
 app: APP_FILE // 入口文件
 },
 output: {
 publicPath: './dist/', //输出目录,index.html寻找资源的地址
 path: BUILD_PATH, // 打包目录
 filename: '[name].[chunkhash].js', // 输出文件名
 chunkFilename: '[name].[chunkhash].js' // commonChunk 输出文件
 }
}
Copy after login

Digression

{

Let’s talk about publicPath first. There is a point to note here, that is, the path is written as ./dist relative path. If you write an absolute path like /dist/, one drawback is that when nginx does not place the front-end package in the root directory, index.html will fail to access the resource. Therefore, it is recommended to write a relative path, but when using a relative path, there will be a potential problem, that is, if the routing access of the project itself is in HTML5 mode instead of using hash (there is a # sign on the routing), then the project will still be accessed. Deployment failed. Angular and React will have such problems. I have never used Vue, so it should be similar. The solution at this time is to add the base tag in the head of index.html, that is:

<head>
 <meta charset="utf-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
 <base href="/" rel="external nofollow" >
</head>
Copy after login

}

webpack itself is based on

Modularization, so in most cases, we only need one entry file. For this requirement, an entry file needs to be added to the app in addition to this entry. That is:

 entry: {
 app: APP_FILE // 入口文件
 ip: IP_FILE
 },
Copy after login
After configuring webpack in this way, the dist file will successfully print app.xxx.js and ip.xxx.js, but the packaged project will still report an error. The solution is: maintain the IP entry The file is unchanged, but treated as a commonChunk. That is, add:

new webpack.optimize.CommonsChunkPlugin({name: 'ip', minChunks: Infinity}),
Copy after login
to plugins to ensure that ip.xxx.js is loaded first and avoid errors.

Disadvantages: This packaging has an obvious disadvantage, that is, the packaged files are compressed, making it inconvenient to modify the files twice. (No solution to compression was found)

CopyWebpackPlugin

The final solution is to separate the ip.js file from the modularization of the project, and then in index.html quoted separately. (This was the solution I thought of at the beginning, but it was not the solution I wanted, but I had no choice but to have limited knowledge and did not solve the previous problem).

Solution process:

First introduce CopyWebpackPlugin in webpack, configuration code:

new CopyWebpackPlugin([
  {from: './src/config/ip.js', to: 'ip.js'},
 ])
Copy after login
Introduce it separately in index.html script tag, be sure to configure a random suffix, that is:

<script>
 document.write("<s"+"cript type=&#39;text/javascript&#39; src=&#39;./dist/ip.js?"+Math.random().toString(36).substr(2)+"&#39;></scr"+"ipt>");
</script>
Copy after login
to prevent ip.js from causing problems due to caching.

The above solves the problem of webpack packaging specified js separately.

ps: I hope there is a better way to share it with me.

This webpack method of packaging specified JS files separately is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support Script Home.

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!

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:

JS implements default avatar filling

Detailed explanation of the use of babel

How to deal with invalid hot loading of Webpack dev server

The above is the detailed content of What needs to be done to package specified JS files with webpack. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

what does web mean what does web mean Jan 09, 2024 pm 04:50 PM

The web is a global wide area network, also known as the World Wide Web, which is an application form of the Internet. The Web is an information system based on hypertext and hypermedia, which allows users to browse and obtain information by jumping between different web pages through hyperlinks. The basis of the Web is the Internet, which uses unified and standardized protocols and languages ​​to enable data exchange and information sharing between different computers.

See all articles