webpack打包懒加载
lazyload
https://webpack.js.org/guides/lazy-loading/
懒加载 -- 按需加载。
Lazy, or "on demand", loading is a great way to optimize your site or application. This practice essentially involves splitting your code at logical breakpoints, and then loading it once the user has done something that requires, or will require, a new block of code. This speeds up the initial load of the application and lightens its overall weight as some blocks may never even be loaded.
webpack solution
https://webpack.js.org/migrate/3/#code-splitting-with-es2015
Code Splitting with ES2015
In webpack 1, you could use
require.ensure()as a method to lazily-load chunks for your application:require.ensure([], function(require) {
var foo = require('./module');
});The ES2015 Loader spec defines
import()as method to load ES2015 Modules dynamically on runtime. webpack treatsimport()as a split-point and puts the requested module in a separate chunk.import()takes the module name as argument and returns a Promise.function onClick() {
import('./module').then(module => {
return module.default;
}).catch(err => {
console.log('Chunk loading failed');
});
}Good news: Failure to load a chunk can now be handled because they are
Promise
require.ensure
https://webpack.js.org/api/module-methods/#requireensure
require.ensure()is specific to webpack and superseded byimport().require.ensure(
dependencies: String[],
callback: function(require),
errorCallback: function(error),
chunkName: String
)Split out the given
dependenciesto a separate bundle that that will be loaded asynchronously. When using CommonJS module syntax, this is the only way to dynamically load dependencies. Meaning, this code can be run within execution, only loading thedependenciesif certain conditions are met.This feature relies on
Promiseinternally. If you userequire.ensurewith older browsers, remember to shimPromiseusing a polyfill such as es6-promise or promise-polyfill.var a = require('normal-dep'); if ( module.hot ) {
require.ensure(['b'], function(require) {
var c = require('c'); // Do something special...
});
}The following parameters are supported in the order specified above:
dependencies: An array of strings declaring all modules required for the code in thecallbackto execute.callback: A function that webpack will execute once the dependencies are loaded. An implementation of therequirefunction is sent as a parameter to this function. The function body can use this to furtherrequire()modules it needs for execution.errorCallback: A function that is executed when webpack fails to load the dependencies.chunkName: A name given to the chunk created by this particularrequire.ensure(). By passing the samechunkNameto variousrequire.ensure()calls, we can combine their code into a single chunk, resulting in only one bundle that the browser must load.Although the implementation of
requireis passed as an argument to thecallbackfunction, using an arbitrary name e.g.require.ensure([], function(request) { request('someModule'); })isn't handled by webpack's static parser. Userequireinstead, e.g.require.ensure([], function(require) { require('someModule'); }).
import()
import('path/to/module') -> PromiseDynamically load modules. Calls to
import()are treated as split points, meaning the requested module and it's children are split out into a separate chunk.The ES2015 Loader spec defines
import()as method to load ES2015 modules dynamically on runtime.if ( module.hot ) {
import('lodash').then(_ => {
// Do something with lodash (a.k.a '_')...
});
}This feature relies on
Promiseinternally. If you useimport()with older browsers, remember to shimPromiseusing a polyfill
require (amd-version)
https://webpack.js.org/api/module-methods/#require-amd-version
require(dependencies: String[], [callback: function(...)])Similar to
require.ensure, this will split the givendependenciesinto a separate bundle that will be loaded asynchronously. Thecallbackwill be called with the exports of each dependency in thedependenciesarray.This feature relies on
Promiseinternally. If you use AMD with older browsers (e.g. Internet Explorer 11), remember to shimPromiseusing a polyfill such as es6-promise or promise-polyfill.require(['b'], function(b) {
var c = require('c');
});There is no option to provide a chunk name.
external NPM module -- bundle-loader
https://www.npmjs.com/package/bundle-loader
https://github.com/ruanyf/webpack-demos#demo08-html-webpack-plugin-and-open-browser-webpack-plugin-source
Another way of code splitting is using bundle-loader.
// main.js // Now a.js is requested, it will be bundled into another file
var load = require('bundle-loader!./a.js'); // To wait until a.js is available (and get the exports)
// you need to async wait for it.
load(function(file) {
document.open();
document.write('<h1>' + file + '</h1>');
document.close();
});
require('bundle-loader!./a.js')tells Webpack to loada.jsfrom another chunk.Now Webpack will build
main.jsintobundle.js, anda.jsinto0.bundle.js.
others lazy load
https://webpack.js.org/guides/lazy-loading/
Frameworks
Many frameworks and libraries have their own recommendations on how this should be accomplished within their methodologies. Here are a few examples:
reference:
https://github.com/amdjs/amdjs-api
https://github.com/yongningfu/webpa_ensure
https://github.com/yongningfu/webpack_package
webpack打包懒加载的更多相关文章
- 「Vue.js」Vue-Router + Webpack 路由懒加载实现
一.前言 当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了.结合 Vue ...
- vue+webpack 实现懒加载的三种方式
第一种: 引入方式 就是正常的路由引入方式 const router = new Router({ routes: [ { path: '/hyh', component: hyh, name: 'h ...
- vue-cli webpack打包后加载资源的路径问题
vue项目,访问打包后的项目,输入路径后,页面加载空白.这时会有两类问题,都是路径问题. 1.一个是css,js,ico等文件加载不到,是目录里少了dist 打开页面时一片空白 解决办法: confi ...
- 在webpack中使用Code Splitting--代码分割来实现vue中的懒加载
当Vue应用程序越来越大,使用Webpack的代码分割来懒加载组件,路由或者Vuex模块, 只有在需要时候才加载代码. 我们可以在Vue应用程序中在三个不同层级应用懒加载和代码分割: 组件,也称为异步 ...
- webpack多页面开发与懒加载hash解决方案
之前讨论了webpack的hash与chunkhash的区别以及各自的应用场景,如果是常规单页面应用的话,上篇文章提供的方案是没有问题的.但是前端项目复杂多变,应对复杂多页面项目时,我们不得不继续踩w ...
- vue 组件按需引用,vue-router懒加载,vue打包优化,加载动画
当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了. 结合 Vue 的 异步 ...
- webpack学习笔记—优化缓存、合并、懒加载等
除了前面的webpack基本配置,还可以进一步添加配置,优化合并文件,加快编译速度.下面是生产环境配置文件webpack.production.js,与wenbpack.config.js相比其不需要 ...
- vue2.x 路由懒加载 优化打包体积
当打包构建应用时,Javascript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了. 结合 Vue 的异步组 ...
- Webpack探索【16】--- 懒加载构建原理详解(模块如何被组建&如何加载)&源码解读
本文主要说明Webpack懒加载构建和加载的原理,对构建后的源码进行分析. 一 说明 本文以一个简单的示例,通过对构建好的bundle.js源码进行分析,说明Webpack懒加载构建原理. 本文使用的 ...
随机推荐
- 【HANA系列】SAP HANA XS使用JavaScript编程详解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS使用Jav ...
- Linux学习历程——Centos 7 diff命令
一.命令介绍 diff命令用于比较文本差异. diff以逐行的方式,比较文本文件的异同处.如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录. ------------- ...
- WPF软件开发系统之四——医疗病人信息管理系统
仿360悬浮窗的方式,始终有个工具栏浮在桌面的最顶层,方便任何时候操作. 主要功能包括:病人信息的添加.修改.查询.包括别人基本信息.诊断结果.接待医生.手术多张图片等. 系统特点:简洁.易操作.美观 ...
- Flink监控:Monitoring Apache Flink Applications
This post originally appeared on the Apache Flink blog. It was reproduced here under the Apache Lice ...
- Synchronized的基本知识、实现原理以及其与ReentrantLock的区别
一.synchronized知识 在谈论synchronized之前,我们需要了解线程安全问题的主要诱因.线程安全问题的主要诱因如下: 存在共享数据(也称为临界资源) 存在多条线程共同操作这些共享数据 ...
- css display和vertical-align 属性
display 定义和用法 display 属性规定元素应该生成的框的类型. 实例 <html> <head> <style type="text/css&qu ...
- 作业2:分布式版本控制系统Git的安装与使用
1.下载安装配置用户名和邮箱. 2. 创建工作目录并通过git init命令把这个目录变成Git可以管理的仓库. 3. 在工作目录下准备文本文件,建议下载Notepad++代替记事本. 4. 组合用g ...
- 如何在FineUIMvc(ASP.NET MVC)视图中绑定多个模型?
起因 这是知识星球内的一个网友提出的,按理说ASP.NET MVC中一个视图只能绑定一个模型(Model),在视图顶部标识如下: @model IEnumerable<FineUICore.Ex ...
- nginx 的各种配置
负载均衡 以上是ip的负载均衡,主要是保证 固定ip地址访问到固定服务,如果不做ip的匹配,那么每次请求的机器都不相同,就会出现问题,sessionid 之类的问题 //修改 路由负载均衡不能写has ...
- ABP之启动配置
ASP.NET Boilerplate提供了在StartUp中配置其模块的基础设施和模型. 配置ASP.NET Boilerplate 配置ABP是在模块的PreInitialize 方法中做的,如下 ...