-
Notifications
You must be signed in to change notification settings - Fork 1
/
_webpack.js
138 lines (115 loc) · 3.05 KB
/
_webpack.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* 引:
* 作者:null仔
* 链接:https://juejin.im/post/5e116fce6fb9a047ea7472a6
* 来源:掘金
* 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
const path = require('path')
const fse = require('fs-extra')
const { parse } = require('@babel/parser')
const { transformFromAstSync } = require('@babel/core')
const traverse = require('@babel/traverse').default
const webpackOptions = {
entry: path.resolve(__dirname, './test.js'),
output: {
path: path.resolve(__dirname, './dist'),
filename: 'test.bundle.js',
},
}
class Parser {
getAst(path) {
const file = fse.readFileSync(path, 'utf-8')
const ast = parse(file, {
sourceType: 'module',
})
return ast
}
getDependecies(ast, filename) {
const dirname = path.dirname(filename)
const dependenciesMap = new Map()
traverse(ast, {
ImportDeclaration({ node }) {
const importPath = node.source.value
const filePath = path.resolve(dirname, importPath)
dependenciesMap.set(importPath, filePath)
},
})
return dependenciesMap
}
getCode(ast) {
const { code } = transformFromAstSync(ast, null, {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
modules: false,
corejs: 3,
},
],
],
plugins: [['@babel/plugin-transform-runtime']],
})
// fse.writeFileSync('./test3.js', code)
// console.log('code', code)
return code
}
}
class Compiler {
constructor(options) {
const { entry, output } = options
this.entry = entry
this.output = output
this.modules = []
this.parser = new Parser()
}
run() {
const entryInfo = this.build(this.entry)
this.modules.push(entryInfo)
entryInfo.dependeciesMap.forEach(path => {
this.modules.push(this.build(path))
})
const dependecyGraph = this.modules.reduce(
(graph, curr) => ({
...graph,
[curr.filepath]: {
code: curr.code,
dependeciesMap: curr.dependeciesMap,
},
}),
{},
)
// console.log(dependecyGraph)
this.generate(dependecyGraph)
}
build(filepath) {
const { getAst, getCode, getDependecies } = this.parser
const ast = getAst(filepath)
const dependeciesMap = getDependecies(ast, filepath)
const code = getCode(ast)
return {
code,
filepath,
dependeciesMap,
}
}
generate(graph) {
const outputFilePath = path.join(this.output.path, this.output.filename)
const bundle = `(function(graph){
function require(module){
function localRequire(relativePath){
return require(graph[module].dependecies[relativePath])
}
var exports = {};
(function(require,exports,code){
eval(code)
})(localRequire,exports,graph[module].code);
return exports;
}
require('${this.entry}')
})(${JSON.stringify(graph)})`
fse.writeFileSync(outputFilePath, bundle)
}
}
new Compiler(webpackOptions).run()