Write a mini webpack project construction tool

zidea 2021-08-08 18:41:02
write mini webpack project construction


This is my participation 8 The fourth of the yuegengwen challenge 7 God , Check out the activity details :8 Yuegengwen challenge

The article went on and on for hours , For bundler Make it clear , Mainly because bundle It's trivial and complicated , So far, I'm not sure if I've made it clear , In fact, technical documents are more rigorous because of their internal logic , Even so, it is not easy to express properly , It must be more difficult to tell a story clearly , Suddenly there was a sense of awe for the writers of literary works .

002.jpeg

First of all, we all know that today for any language , You can't do without a bag ( modular ) Management tools , Because for large applications , Need to be code Split by function to modularize , The benefits of this modularity are self-evident , Just look at the benefits of modularity in the development process , Modularity allows everyone to work together in modules to develop in parallel .

001.jpeg

javascript At first there was no concept of modules , In the early javascript The main work is to add dynamic effects and interactive effects to web pages toy , It may not be necessary to introduce the concept of modules , But today is different , With web Applications are becoming more and more complex , More things to be done at the front end , and nodejs The emergence of let javascript From the front end to the back end , thus , The introduction of package management into this language has become imminent . I came into contact with the first javascript Package management tools include require.js. Today javascript There are two main concepts of package management tools , Everyone is going to realize these two concepts .

005.png

  • ES6 module
import _ from 'lodash'
export default someValue;
 Copy code 

It should be noted that ES module Form needs to start the service , adopt http Request ways to value

  • Commonjs modules
const _ = require('lodash');
module.exports = someValue
 Copy code 

motivation

Webpack It's a very good javascript Package management tools , Many projects use webpack Build , Maybe recently it seems that vue Project abandonment webpack And use rollup.js Building tools . Exposed to early require.js Used it, too webpack To build the project , They just stay at the level of use , We still don't know what the internal mechanism is . Yesterday I saw a foreigner share about how to realize a javascript Build tool video , Ready to work with you to achieve an , Of course, it's not just copy, Some key contents will be shared , And expand locally .

006.jpeg

Ideas

One thing to do , You need to think about what to do first , For example, putting elephants in the refrigerator is divided into 3 Ministry , So let's do what project construction should do , Think about it , First, we need to js File read in , Then find the information containing the dependency from the file ,

  • Read js Parse the file to a AST
  • adopt AST Abstract the code and extract dependency related information
  • Use the relevant information to generate a dependency graph that can be traversed
  • Use dependency diagrams to effectively organize code together

007.png

entry.js - message.js - name.js
 Copy code 

Get resource object

We will javascript File as a resource , here createAsset Method is mainly responsible for reading each javascript file ( As a resource ), Then extract the required information from the resource .

Read javascript file

introduce fs modular , Use readFileSync Read file contents , Read files synchronously .

const fs = require("fs")
 Copy code 
function createAsset(filename){
const content = fs.readFileSync(filename,'utf-8');
console.log(content);
}
creatAsset('./example/entry.js');
 Copy code 

Parse the text into AST

here createAsset Access to resources , Read the file as a string and assign it to content object . Next, we need to read the string and parse it , Parse into a AST, So what is AST, The full name is the abstract syntax tree , That is to say javascript The code forms a structured syntax tree , Actually AST It's just plain Json file , Used to describe .

Here we introduce babylon This package , Help us parse the string into a AST Structural data . Next, we need to parse the file to get which files the file depends on ,

  • File Program body ImportDeclaration
  • Each node has a type type
const fs = require("fs");
const babylon = require('babylon');
function creatAsset(filename){
const content = fs.readFileSync(filename,'utf-8');
const ast = babylon.parse(content,{
sourceType:'module'
});
console.log(ast);
}
 Copy code 

Here's our parse output AST object , This is an ordinary json object , We can simply read . Each node has a type type , Let's take a brief look at what each node contains ,type Indicates the type of node ,start and end Indicates that the node is in code Middle position , Let's compare code Let's take a look and resolve AST object .

import message from './message.js';
console.log(message)
 Copy code 

There are two sets comments and tokens, stay tokens One by one

Node {
type: 'File',
start: 0,
end: 57,
loc: SourceLocation {
start: Position { line: 1, column: 0 },
end: Position { line: 3, column: 20 }
},
program: Node {
type: 'Program',
start: 0,
end: 57,
loc: SourceLocation { start: [Position], end: [Position] },
sourceType: 'module',
body: [ [Node], [Node] ],
directives: []
},
comments: [],
tokens: [
Token {
type: [KeywordTokenType],
value: 'import',
start: 0,
end: 6,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: 'message',
start: 7,
end: 14,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: 'from',
start: 15,
end: 19,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: './message.js',
start: 20,
end: 34,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: undefined,
start: 34,
end: 35,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: 'console',
start: 37,
end: 44,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: undefined,
start: 44,
end: 45,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: 'log',
start: 45,
end: 48,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: undefined,
start: 48,
end: 49,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: 'message',
start: 49,
end: 56,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: undefined,
start: 56,
end: 57,
loc: [SourceLocation]
},
Token {
type: [TokenType],
value: undefined,
start: 57,
end: 57,
loc: [SourceLocation]
}
]
}
 Copy code 

Next, let's improve the work in createAsset , We have parsed the text into AST after , Let's introduce babel-traverse , The purpose is to analyze good AST Get to ImportDeclaration, of import Related statements .

/** */
const fs = require("fs");
const babylon = require('babylon');
const traverse = require('babel-traverse').default;
function creatAsset(filename){
const content = fs.readFileSync(filename,'utf-8');
const ast = babylon.parse(content,{
sourceType:'module'
});
traverse(ast,{
ImportDeclaration:({node})=>{
console.log(node);
}
})
// console.log(ast);
}
creatAsset('./example/entry.js');
 Copy code 

adopt traverse It's extracted import Content , Through this node source.value Can be obtained ./message.js This is what we want , here ImportDeclaration The node can obtain the dependency of the file .

Node {
type: 'ImportDeclaration',
start: 0,
end: 35,
loc: SourceLocation {
start: Position { line: 1, column: 0 },
end: Position { line: 1, column: 35 }
},
specifiers: [
Node {
type: 'ImportDefaultSpecifier',
start: 7,
end: 14,
loc: [SourceLocation],
local: [Node]
}
],
source: Node {
type: 'StringLiteral',
start: 20,
end: 34,
loc: SourceLocation { start: [Position], end: [Position] },
extra: { rawValue: './message.js', raw: "'./message.js'" },
value: './message.js'
}
}
 Copy code 

Collected dependency information of the file , Is stored in a dependencies In the array ,

function creatAsset(filename){
const content = fs.readFileSync(filename,'utf-8');
const ast = babylon.parse(content,{
sourceType:'module'
});
const dependencies = []
traverse(ast,{
ImportDeclaration:({node})=>{
// console.log(node);
dependencies.push(node.source.value);
}
});
console.log(dependencies);
}
 Copy code 
[ './message.js' ]
 Copy code 

We createAsset Return a resource object , This resource object contains several properties ,id 、filename and dependencies Dependency relationship , among dependencies Save the file on which the file depends .

let ID = 0;
function createAsset(filename){
const content = fs.readFileSync(filename,'utf-8');
const ast = babylon.parse(content,{
sourceType:'module'
});
const dependencies = []
traverse(ast,{
ImportDeclaration:({node})=>{
// console.log(node);
dependencies.push(node.source.value);
}
});
const id = ID++;
return{
id,
filename,
dependencies,
};
// console.log(dependencies);
}
const mainAsset = creatAsset('./example/entry.js');
console.log(mainAsset)
 Copy code 
{
id: 0,
filename: './example/entry.js',
dependencies: [ './message.js' ]
}
 Copy code 

016.png

Create dependency graphs

Build file dependency graph , First, put the inbound resources into a queue (queue), Traversal dependency (dependencies) The path where the file depends is stored , But the dependent path here is a relative path , however createAsset You need to accept an absolute path as a parameter . We use the path relative to the library or file referenced by the file import message from './message.js';,

function createGraph(entry){
const mainAsset = createAsset(entry);
const queue = [mainAsset];
for(const asset of queue){
const dirname = path.dirname(asset.filename);
asset.mapping = {}
asset.dependencies.forEach(relativePath => {
const absolutePath = path.join(dirname,relativePath);
const child = createAsset(absolutePath);
asset.mapping[relativePath] = child.id
queue.push(child)
});
}
return queue;
}
 Copy code 

Create diagrams , The dependencies of each file of the project are represented in the form of a diagram , Here create a method createGraph(), Introduce the file path of the main entry as a parameter , And then call createAsset Get the package id,filename and dependencies The object of , Next define a queue, This array holds all , Let's call it resource object for now , Every time a resource object is obtained , We get the path of the dependent file according to the dependency array in the resource object , Because the dependent file path is relative to the path that references the file , and creatAsset When reading a file, you need an absolute path . So the introduction of path package , adopt const absolutePath = path.join(dirname,relativePath); Get the absolute path .

[
{
id: 0,
filename: './example/entry.js',
dependencies: [ './message.js' ],
mapping: { './message.js': 1 }
},
{
id: 1,
filename: 'example/message.js',
dependencies: [ './name.js' ],
mapping: { './name.js': 2 }
},
{ id: 2, filename: 'example/name.js', dependencies: [], mapping: {} }
]
 Copy code 

012.jpeg

Based on dependency graph bundle

So-called bundle Is to organize these module files together according to the dependency diagram ,

function bundle(graph){
let modules = '';
graph.forEach(mod => {
modules + `${mod.i}:[ ]`
})
const result = ` (function(){ })({${modules}}) }`
 Copy code 
const result = ` (function(){ })({${modules}}) }`
 Copy code 

Here's a return to result Is to execute the function immediately , Pass in an object as a parameter .

Because not all browsers are right EMSCscript module form , Most support commonjs In this way, the package references ,bebel It can be regarded as a code conversion tool , According to what you give presets:['env'] Convert code to other forms .

 const {code} = babel.transformFromAst(ast,null,{presets:['env']});
// install babel-core and npm install babel-preset-env --save
return{
id,
filename,
dependencies,
code
};
 Copy code 

Next, add a for each resource object code Properties and mapping attribute , Here first mapping Bury a foreshadowing ,

[
{
id: 0,
filename: './example/entry.js',
dependencies: [ './message.js' ],
code: '"use strict";\n' +
'\n' +
'var _message = require("./message.js");\n' +
'\n' +
'var _message2 = _interopRequireDefault(_message);\n' +
'\n' +
'function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n' +
'\n' +
'console.log(_message2.default);',
mapping: { './message.js': 1 }
},
{
id: 1,
filename: 'example/message.js',
dependencies: [ './name.js' ],
code: '"use strict";\n' +
'\n' +
'Object.defineProperty(exports, "__esModule", {\n' +
' value: true\n' +
'});\n' +
'\n' +
'var _name = require("./name.js");\n' +
'\n' +
'exports.default = "hello " + _name.name;',
mapping: { './name.js': 2 }
},
{
id: 2,
filename: 'example/name.js',
dependencies: [],
code: '"use strict";\n' +
'\n' +
'Object.defineProperty(exports, "__esModule", {\n' +
' value: true\n' +
'});\n' +
"var name = exports.name = 'machine learning';",
mapping: {}
}
]
 Copy code 

function bundle(graph){
let modules = '';
graph.forEach(mod => {
modules +=`${mod.id}:[ function(require, module, exports){ ${mod.code} } ]`
})
const result = ` (function(){ })({${modules}}) `
return result;
}
 Copy code 
const result = ` (function(){ })({${modules}}) `
 Copy code 

Here is a brief introduction result, {${moduels}} , After the above string splicing, we get module The value of is similar to "id:[function],id:[function]... This form , Then we put the parsed string into an object .

 (function(){
})({0:[
function(require, module, exports){
 "use strict";
var _message = require("./message.js");
var _message2 = _interopRequireDefault(_message);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
console.log(_message2.default);
}
],1:[
function(require, module, exports){
 "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _name = require("./name.js");
exports.default = "hello " + _name.name;
}
],2:[
function(require, module, exports){
 "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var name = exports.name = 'machine learning';
}
]})
 Copy code 

017.jpeg

Analyze build files

Recursion is used here , For recursion, it is a sign for beginners to improve ,

  • First module It's an object ,key Is the index from 0 , Value is an array , The array contains a function and object

  • stay modules Each function in accepts three require, modules and exports

    function localRequire(relativePath) {
    return require(mapping[relativePath])
    }
     Copy code 

localRequire Will be based on the input relative path , Corresponding to module in id, Here we use mapping , stay mapping It's an object , The object key value is the relative path of the module ./messge.js And this is the value corresponding to the module id.

Let's first define a require The function accepts a id Then call the function and import it 0 As a parameter , Let's take a look require What has been done inside , First of all, according to the id from modules Array get fn and mapping,mapping This is such a key value pair . stay require Internally define a localRequire Here, the file depends on the relative path id To recursively call require This is also the beginning of our definition mapping Why . Definition module, Call the function and pass in require,module and module.exports.

018.jpeg

(function (modules) {
function require(id) {
const [fn, mapping] = modules[id]
function localRequire(relativePath) {
return require(mapping[relativePath])
}
const module = {
exports: {}
};
fn(localRequire, module, module.exports)
return module.exports
}
require(0);
})({
0: [
function (require, module, exports) {
 "use strict";
var _message = require("./message.js");
var _message2 = _interopRequireDefault(_message);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
console.log(_message2.default);
},
{
"./message.js": 1
}
],
1: [
function (require, module, exports) {
 "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _name = require("./name.js");
exports.default = "hello " + _name.name;
},
{
"./name.js": 2
}
],
2: [
function (require, module, exports) {
 "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var name = exports.name = 'machine learning';
},
{}
],
})
 Copy code 
版权声明
本文为[zidea]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/08/20210808183744914O.html

  1. HTML + CSS + JavaScript to achieve cool Fireworks (cloud like particle text 3D opening)
  2. HTML + CSS + JavaScript realizes 520 advertising love tree (including music), which is necessary for programmers to express themselves
  3. Solve the problem of Web front-end deployment server (it can be deployed online without a server)
  4. HTML + CSS + JS make wedding countdown web page template (520 / Tanabata Valentine's Day / programmer advertisement)
  5. What else can driverless minibus do besides "Park connection"?
  6. Cloud native leads the era of all cloud development
  7. NRM mirror source management tool
  8. Bring it to you, flex Jiugong
  9. Lolstyle UI component development practice (II) -- button group component
  10. Deconstruction assignment in ES6
  11. Luo 2 peerless Tang clan was officially launched. The official gave a key point, and the broadcast time was implied
  12. 20初识前端HTML(1)
  13. 当新零售遇上 Serverless
  14. 20 initial knowledge of front-end HTML (1)
  15. When new retail meets serverless
  16. [golang] - go into go language lesson 5 type conversion
  17. [golang] - go into go language lesson 6 conditional expression
  18. HTML5(八)——SVG 之 path 详解
  19. HTML5 (8) -- detailed explanation of SVG path
  20. 需要开通VIP以后页面内容才能复制怎么办?控制台禁用javascript即可
  21. Web前端|CSS入门教程(超详细的CSS使用讲解,适合前端初学者)
  22. 实践积累 —— 用Vue3简单写一个单行横向滚动组件
  23. Serverless 全能选手,再下一城
  24. What if you need to open a VIP to copy the page content? Just disable JavaScript on the console
  25. Web front end | CSS introductory tutorial (super detailed CSS explanation, suitable for front-end beginners)
  26. Practice accumulation - write a single line horizontal scroll component simply with vue3
  27. Dili Reba is thin again. She looks elegant and high in a strapless hollow skirt, and her "palm waist" is beautiful to a new height
  28. Serverless all-round player, next city
  29. The difference between MySQL semi synchronous replication and lossless semi synchronous replication
  30. Vue表单设计器的终极解决方案
  31. The ultimate solution for Vue form designer
  32. Nginx从理论到实践超详细笔记
  33. Yu Shuxin's red backless swimsuit is split to the waist and tail, with a concave convex figure and excessive color matching, and his face is white to dazzling
  34. Nginx ultra detailed notes from theory to practice
  35. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画
  36. typecho全站启用https
  37. CCTV has another popular employee. The off-site interpretation is very professional, and the appearance ability is no less than that of Wang Bingbing
  38. [animation Xiaole | CSS] 086. Cool water wave loading transition animation
  39. Enable HTTPS in Typecho
  40. 50天用JavaScript完成50个web项目,我学到了什么?
  41. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  42. What have I learned from completing 50 web projects with JavaScript in 50 days?
  43. "My neighbor doesn't grow up" has hit the whole network. There are countless horse music circles, and actor Zhou Xiaochuan has successfully made a circle
  44. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  45. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  46. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  47. 30 + women still wear less T-shirts and jeans. If they wear them like stars, they will lose weight
  48. 数栈技术分享前端篇:TS,看你哪里逃~
  49. Several stack technology sharing front end: TS, see where you escape~
  50. 舍弃Kong和Nginx,Apache APISIX 在趣链科技 BaaS 平台的落地实践
  51. Abandon the landing practice of Kong and nginx, Apache apisik on the baas platform of fun chain technology
  52. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)
  53. 前端HTML两万字图文大总结,快来看看你会多少!【️熬夜整理&建议收藏️】
  54. Wandering around the world king teaches you to use elementui to make complex tables and process report data (merge header, merge table body rows and columns)
  55. 路由刷新数据丢失 - vuex数据读取的问题
  56. Front end HTML 20000 word graphic summary, come and see how much you can【 Stay up late to sort out & suggestions]
  57. Route refresh data loss - vuex data reading problem
  58. Systemctl系统启动Nginx服务脚本
  59. Systemctl system startup nginx service script
  60. sleepless