Home Web Front-end JS Tutorial Detailed explanation of the difference between webpack require.ensure and require AMD_javascript skills

Detailed explanation of the difference between webpack require.ensure and require AMD_javascript skills

Dec 15, 2017 am 10:49 AM
require webpack

This article mainly introduces the detailed explanationwebpack The difference between require.ensure and require AMD. The editor thinks it is quite good and will also be used as a reference for everyone. If you are interested in webpack, you can follow the editor to take a look.

Introduction

The difference between require-ensure and require-amd:

require-amd

Description: Same as the require function of AMD specification. When used, a module array and callback function are passed. The callback function is executed only after the modules have been downloaded and executed.

Syntax: require(dependencies: String[], [callback: function(...)])

Parameters

  1. dependencies: module dependency array

  2. ##callback: callback function

require-ensure

Note: require.ensure only downloads dependent modules when needed. When all the modules specified by the parameters are downloaded (the downloaded modules have not been executed yet), the callback function specified by the

parameters will be executed. . require.ensure will create a chunk, and you can specify the name of the chunk. If the chunk name already exists, the dependent modules will be merged into the existing chunk. Finally, this chunk will be generated separately when webpack is built. a file.


Syntax: require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])

  1. dependencies: Array of dependent modules

  2. callback: callback function, a require parameter will be passed when the function is called

  3. chunkName: module name, used for building When generating files, use

. Note: the requi.ensure module will only be downloaded and will not be executed. Only after the callback function uses require (module name), this The module will be executed.


Example

require-amd

Source code

webpack.config.amd .js


var path = require("path");
module.exports = {
  entry: "./example.amd.js",
  output: {
    path: path.join(__dirname, "amd"),
    filename: "[name].bundle.js",
    chunkFilename: "[id].chunk.js"
  }
};
Copy after login


example.amd.js


require(["./module1"], function(module1) {
  console.log("aaa");
  var module2 = require("./module2");
  console.log("bbb");
});
Copy after login


module1.js


console.log("module1");
module.exports = 1;
Copy after login


module2.js


console.log("module2");
module.exports = 2;
Copy after login


Build result

Run webpack in the command line --config webpack.config.amd.js

- main.bundle. js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js

Run result

Run amd/index.html in the browser, the console output:


module1
aaa
module2
bbb
Copy after login


require-ensure

Source code

webpack.config.ensure.js


var path = require("path");
module.exports = {
  entry: "./example.ensure.js",
  output: {
    path: path.join(__dirname, "ensure"),
    filename: "[name].bundle.js",
    chunkFilename: "[name].chunk.js"
  }
};
Copy after login


example.ensure .js


require.ensure(["./module1"], function(require) {
  console.log("aaa");
  var module2 = require("./module2");
  console.log("bbb");
  require("./module1");
}, 'test');
Copy after login


##module1.js

Same as above


module2.js

Same as above



Build results

Run webpack in the command line --config webpack.config.ensure.js

- main.bundle.js

- example. amd.js
- 1.chunk.js
- module1.js
- module2.js

Run result

Run in browser ensure/index.html, console output:

aaa

module2

bbb
module1


##require-ensure-chunk

Source code

webpack.config.ensure.chunk.js

##

var path = require("path");
module.exports = {
  entry: "./example.ensur.chunk.js",
  output: {
    path: path.join(__dirname, "ensure-chunk"),
    filename: "[name].bundle.js",
    chunkFilename: "[name].chunk.js"
  }
};
Copy after login


example.ensur .chunk.js


require.ensure(["./module1"], function(require) {
  console.log("aaa");
  require("./module1");
  console.log("bbb");
}, 'common');

require.ensure(["./module2"], function(require) {
  console.log("ccc");
  require("./module2");
  console.log("ddd");
}, 'common');
Copy after login


module1.js

Same as above

module2.js

Same as above


Build results

Run webpack in the command line --config webpack.config.ensure.js - main.bundle.js - example.amd.js

- 1.chunk.js

- module1.js
- module2.js


Running result

Browser Run ensure/index.html in, the console output: aaa

module1

bbb

ccc

1module2
ddd


The above is I hope that the entire content of this article will be helpful to everyone's learning, and I also hope that everyone will support the PHP Chinese website.

Related recommendations:

Detailed explanation of vue loading components on demand webpack require.ensure

Detailed explanation of the difference between webpack require.ensure and require AMD

Webpack learning tutorial front-end performance optimization summary

The above is the detailed content of Detailed explanation of the difference between webpack require.ensure and require AMD_javascript skills. 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+vite: How to solve the error when using require to dynamically import images in src vue3+vite: How to solve the error when using require to dynamically import images in src May 21, 2023 pm 03:16 PM

vue3+vite:src uses require to dynamically import images and error reports and solutions. vue3+vite dynamically imports multiple images. If vue3 is using typescript development, require will introduce image errors. requireisnotdefined cannot be used like vue2 such as imgUrl:require(' .../assets/test.png') is imported because typescript does not support require, so import is used. Here is how to solve it: use awaitimport

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.

What are the uses of require? What are the uses of require? Nov 27, 2023 am 10:03 AM

Usage of require: 1. Introduce modules: In many programming languages, require is used to introduce external modules or libraries so that the functions they provide can be used in the program. For example, in Ruby, you can use require to load third-party libraries or modules; 2. Import classes or methods: In some programming languages, require is used to import specific classes or methods so that they can be used in the current file; 3. Perform specific tasks: In some programming languages ​​or frameworks, require is used to perform specific tasks or functions.

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 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.

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

What files can vue webpack package? What files can vue webpack package? Dec 20, 2022 pm 07:44 PM

In vue, webpack can package js, css, pictures, json and other files into appropriate formats for browser use; in webpack, js, css, pictures, json and other file types can be used as modules. Various module resources in webpack can be packaged and merged into one or more packages, and during the packaging process, the resources can be processed, such as compressing images, converting scss to css, converting ES6 syntax to ES5, etc., which can be recognized by HTML. file type.

See all articles