-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (62 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fs from 'fs'
import parser from '@babel/parser'
import traverse from "@babel/traverse"
import path from 'path'
import ejs from 'ejs'
import { transformFromAst } from "babel-core";
function creatAsset(filePath){
// 读取文件内容
const source = fs.readFileSync(filePath,{
encoding:'utf-8'
})
// 获取依赖关系(@babel/parser)
const ast = parser.parse(source,{
sourceType:'module'
})
// console.log('ast: ', ast);
// 从ast树里取出依赖的文件 @babel/traverse
// 定义数组存储依赖的文件
const deps =[]
traverse.default(ast,{
ImportDeclaration({node}){
// console.log('node: ', node);
deps.push(node.source.value)
}
})
// 将import语法转换为require(babel-core)
const {code} = transformFromAst(ast,null,{
presets:['env'] //这里需要引入babel-preset-env 否则会报错
})
return {
filePath,
code,
deps
}
}
// 获取依赖关系图的函数
function createGraph(){
// 获取主文件依赖
const mainAsset = creatAsset('./example/main.js')
// 创建依赖关系图数组[{soure:'',deps:[]},{soure:'',deps:[]}]
const queue = [mainAsset]
for (const asset of queue) {
// 取出各依赖文件的路劲
asset.deps.forEach(relativePath=>{
// 再分析各依赖文件的依赖
const child = creatAsset(path.resolve('./example',relativePath))
// 添加到依赖关系图数组里
queue.push(child)
})
}
return queue
}
const graph = createGraph()
// 实现打包函数
function build(graph){
// 引入ejs 根据设定的模板转换代码
// 读取模板文件
const template= fs.readFileSync('./bundle.ejs',{encoding:'utf-8'})
const code = ejs.render(template,{graph})
fs.writeFileSync('./dist/bundle.js',code)
}
build(graph)