Home Web Front-end JS Tutorial How to extract third-party libraries using webpack

How to extract third-party libraries using webpack

Jun 19, 2018 am 09:30 AM
webpack Third-party libraries

This article mainly introduces the correct posture of webpack to extract third-party libraries. There are two commonly used methods to extract third-party libraries. This article introduces these two methods in detail. Those who are interested can learn more

When we use webpack to package, we often want to extract the third-party library separately, use it as a stable version file, and use the browsing cache to reduce the number of requests. There are two commonly used methods to extract third-party libraries

  1. CommonsChunkPlugin

  2. DLLPlugin

The difference : The first method requires the third-party library to be run and packaged once every time it is packaged. The second method only packages the project files each time. We only need to quote the third-party compressed file that was packaged for the first time.

CommonsChunkPlugin method introduction

Let’s take vue as an example

const vue = require('vue')
{
 entry: {
 // bundle是我们要打包的项目文件的导出名字, app是入口js文件
 bundle: 'app',
 // vendor就是我们要打包的第三方库最终生成的文件名,数组里是要打包哪些第三方库, 如果不是在node——modules里面,可以填写库的具体地址
 vendor: ['vue']
 },
 output: {
  path: __dirname + '/bulid/',
 // 文件名称
 filename: '[name].js'
 },
 plugins: {
 // 这里实例化webpack.optimize.CommonsChunkPlugin构造函数
 // 打包之后就生成vendor.js文件
 new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js')
 }
}
Copy after login

Then package the generated file and introduce it into the html file

<script src="/build/vendor.js"></script>
 <script src="/build/bundle.js"></script>
Copy after login

DLLPlugin method Introduction

First prepare two files

  1. webpack.config.js

  2. ##webpack.dll.config. js

The webpack.dll.config.js file is configured as follows

const webpack = require(&#39;webpack&#39;)
const library = &#39;[name]_lib&#39;
const path = require(&#39;path&#39;)

module.exports = {
 entry: {
 vendors: [&#39;vue&#39;, &#39;vuex&#39;]
 },

 output: {
 filename: &#39;[name].dll.js&#39;,
 path: &#39;dist/&#39;,
 library
 },

 plugins: [
 new webpack.DllPlugin({
  path: path.join(__dirname, &#39;dist/[name]-manifest.json&#39;),
  // This must match the output.library option above
  name: library
 }),
 ],
}
Copy after login

Then the webpack.config.js file is configured as follows

const webpack = require(&#39;webpack&#39;)

module.exports = {
 entry: {
 app: &#39;./src/index&#39;
 },
 output: {
 filename: &#39;app.bundle.js&#39;,
 path: &#39;dist/&#39;,
 },
 plugins: [
 new webpack.DllReferencePlugin({
  context: __dirname,
  manifest: require(&#39;./dist/vendors-manifest.json&#39;)
 })
 ]
}
Copy after login

Then run

$ webpack --config webpack.dll.config.js
$ webpack --config webpack.config.js
Copy after login

htmlQuotation method

<script src="/dist/vendors.dll.js"></script>
<script src="/dist/app.bundle.js"></script>
Copy after login
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

JavaScript module optimization

How to use webpack express to achieve multi-page site development

Webpack framework (mastering core technology)

How to control multiple scroll bars to scroll synchronously using JS

Use vue-cli How webpack builds vue

The above is the detailed content of How to extract third-party libraries using 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)

VUE3 Getting Started Tutorial: Packaging and Building with Webpack VUE3 Getting Started Tutorial: Packaging and Building with Webpack Jun 15, 2023 pm 06:17 PM

Vue is an excellent JavaScript framework that can help us quickly build interactive and efficient web applications. Vue3 is the latest version of Vue, which introduces many new features and functionality. Webpack is currently one of the most popular JavaScript module packagers and build tools, which can help us manage various resources in our projects. This article will introduce how to use Webpack to package and build Vue3 applications. 1. Install Webpack

What is the difference between vite and webpack What is the difference between vite and webpack Jan 11, 2023 pm 02:55 PM

Differences: 1. The startup speed of the webpack server is slower than that of Vite; because Vite does not require packaging when starting, there is no need to analyze module dependencies and compile, so the startup speed is very fast. 2. Vite hot update is faster than webpack; in terms of HRM of Vite, when the content of a certain module changes, just let the browser re-request the module. 3. Vite uses esbuild to pre-build dependencies, while webpack is based on node. 4. The ecology of Vite is not as good as webpack, and the loaders and plug-ins are not rich enough.

How to use PHP and webpack for modular development How to use PHP and webpack for modular development May 11, 2023 pm 03:52 PM

With the continuous development of web development technology, front-end and back-end separation and modular development have become a widespread trend. PHP is a commonly used back-end language. When doing modular development, we need to use some tools to manage and package modules. Webpack is a very easy-to-use modular packaging tool. This article will introduce how to use PHP and webpack for modular development. 1. What is modular development? Modular development refers to decomposing a program into different independent modules. Each module has its own function.

How to use third-party libraries in Go? How to use third-party libraries in Go? May 11, 2023 pm 03:30 PM

In Go language, it is very convenient to use third-party libraries. Many excellent third-party libraries and frameworks can help us develop applications quickly, while also reducing the workload of writing code ourselves. But how to use third-party libraries correctly to ensure their stability and reliability is a problem we must understand. This article will introduce how to use third-party libraries from the following aspects, and explain them with specific examples. 1. Obtaining third-party libraries There are two ways to obtain third-party libraries in Go language: 1. Use the goget command first

How does webpack convert es6 to es5 module? How does webpack convert es6 to es5 module? Oct 18, 2022 pm 03:48 PM

Configuration method: 1. Use the import method to put the ES6 code into the packaged js code file; 2. Use the npm tool to install the babel-loader tool, the syntax is "npm install -D babel-loader @babel/core @babel/preset- env"; 3. Create the configuration file ".babelrc" of the babel tool and set the transcoding rules; 4. Configure the packaging rules in the webpack.config.js file.

How to install and use third-party libraries in Go language? How to install and use third-party libraries in Go language? Jun 10, 2023 am 08:15 AM

How to install and use third-party libraries in Go language? Go language has become one of the most popular modern programming languages ​​because it has many very useful features and benefits. It is a very easy-to-learn language that can be used to write a variety of programs. Similar to many other programming languages, Go also has a large number of third-party libraries that can help you write code more efficiently and provide a lot of functions and a modular component structure. This article will introduce how to use Go's third-party libraries. Find and select third parties

Use Spring Boot and Webpack to build front-end projects and plug-in systems Use Spring Boot and Webpack to build front-end projects and plug-in systems Jun 22, 2023 am 09:13 AM

As the complexity of modern web applications continues to increase, building excellent front-end engineering and plug-in systems has become increasingly important. With the popularity of Spring Boot and Webpack, they have become a perfect combination for building front-end projects and plug-in systems. SpringBoot is a Java framework that creates Java applications with minimal configuration requirements. It provides many useful features, such as automatic configuration, so that developers can build and deploy web applications faster and easier. W

Easily install third-party libraries using pip: an easy-to-follow guide Easily install third-party libraries using pip: an easy-to-follow guide Jan 27, 2024 am 09:07 AM

Simple and easy-to-understand tutorial: How to use pip to install third-party libraries, specific code examples are required Introduction: In Python development, we often need to use third-party libraries to implement various functions. Pip is Python's package management tool, which can help us install and manage third-party libraries quickly and easily. This article will introduce how to use pip to install third-party libraries and give specific code examples. Step 1: Check the installation of Python and pip Before starting, we need to check the Python installation

See all articles