Comments (10)
进阶(具备手撸一个loader、plugins的能力):
from fe-interview.
13.手写 webpack loader
https://zhuanlan.zhihu.com/p/104205895
本质上来说,loader 就是一个导出为一个函数的 node 模块。
loader 的调用顺序:从下往上或者从右往左。
// a-loader.js
import { getOptions } from 'loader-utils';
import { validateOptions } from 'schema-utils';
const schema = {
type: object,
properties: {
test: {
type: string
}
}
}
export default function(source) {
const options = getOptions(this);
validateOptions(schema, options, 'Example Loader');
// 在这里写转换 source 的逻辑 ...
return `export default ${ JSON.stringify(source) }`;
};
// webpack.config.js
let webpackConfig = {
//...
module: {
rules: [{
test: /\.js$/,
use: [{
//这里写 loader 的路径
loader: path.resolve(__dirname, 'loaders/a-loader.js'),
options: {/* ... */}
}]
}]
}
}
14.手写 webpack plugin
本质上来说,plugin 就是一个JavaScript类函数,在该函数原型 (prototype)中定义了一个注入compiler
对象的apply
方法。
class MyPlugin{
constructor() {
}
apply(conpiler){
conpiler.hooks.break.tap("WarningLampPlugin", () => console.log('WarningLampPlugin'));
conpiler.hooks.accelerate.tap("LoggerPlugin", newSpeed => console.log(`Accelerating to ${newSpeed}`));
conpiler.hooks.calculateRoutes.tapAsync("calculateRoutes tapAsync", (source, target, routesList, callback) => {
setTimeout(() => {
console.log(`tapAsync to ${source}${target}${routesList}`)
callback();
}, 2000)
});
}
}
from fe-interview.
webpack5.0源码整体流程 https://juejin.cn/post/6919883483913388040
from fe-interview.
https://github.com/webpack/changelog-v5/blob/master/guides/persistent-caching.md
from fe-interview.
webpack5 启动流程部分源码分析 https://www.jianshu.com/p/a38cdd547f04
from fe-interview.
从构建进程间缓存设计 谈 Webpack5 优化和工作原理 https://segmentfault.com/a/1190000021918701
from fe-interview.
sourcemap原理与作用
编译源码生成编译后的代码和SourceMap,SourceMap有内联形式或者单独的.map文件,SourceMap的内容是一个json对象,记录了压缩处理后的代码与源码之间的映射关系
在webpack中选择合适的SourceMap能达到打包速度和生成包体积、方便调试之间的平衡。
from fe-interview.
from fe-interview.
from fe-interview.
from fe-interview.
Related Issues (20)
- 09. js原型及原型链 HOT 1
- 10. js中的基本类型和引用类型
- 12.闭包,new,箭头函数 HOT 1
- 13.获取对象属性的几种方法
- 14.解释下Promise与Promise.all HOT 1
- 15.JS中的面向对象,class,prototype和__proto__ HOT 1
- 16. mvc与mvvm的区别
- 17.前端性能优化的常用手段
- 18. http网络请求基本知识 HOT 2
- 19. Vue常见面试题 HOT 1
- 20.跨域及解决方案 HOT 2
- 21. Symbol的应用场景?
- 22.散装知识点 HOT 7
- 23.前端排序算法汇总 HOT 1
- 24. js实现二叉树的前序、中序、后续、层序遍历
- 25.前端常见算法 HOT 2
- 26. Set,Map,Symbol
- 27.html5语义化标签 HOT 1
- 28.常见css基础面试题
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-interview.