-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const fs = require('fs') | ||
const parse = require('@babel/parser') | ||
const { default: traverse } = require('@babel/traverse') | ||
|
||
class ToyWebpack { | ||
constructor(options) { | ||
console.log('new ToyWebpack'); | ||
const {entry, output} = options | ||
this.entry = entry | ||
this.output = output | ||
} | ||
|
||
/** | ||
* @description 入口方法 | ||
*/ | ||
run() { | ||
console.log('run ToyWebpack') | ||
this._parseModule(this.entry) | ||
} | ||
|
||
/** | ||
* @description 解析模块,做两件事情,一是获取依赖,二是获取模块源码 | ||
*/ | ||
_parseModule(modulePath) { | ||
// 读取module文件,并输出为utf-8编码格式的字符串 | ||
const moduleStr = fs.readFileSync(modulePath, 'utf-8') | ||
// 利用babel/parse将源码转换为ast | ||
const ast = parse.parse(moduleStr, { | ||
sourceType: "module", | ||
}) | ||
// 利用 | ||
const dep = [] | ||
traverse(ast, { | ||
ImportDeclaration({node}) { | ||
const depPath = node?.source?.value | ||
dep.push(depPath) | ||
}, | ||
VariableDeclaration({node}) { | ||
console.log(node); | ||
} | ||
}); | ||
// console.log(ast.program.body); | ||
console.log(dep); | ||
|
||
return { | ||
dep, | ||
// code, | ||
} | ||
} | ||
} | ||
|
||
module.exports = ToyWebpack |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ module.exports = { | |
/** | ||
* 入口文件 | ||
*/ | ||
entry : './test/index.js', | ||
entry : './modules/index.js', | ||
|
||
/** | ||
* 打包出口 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/** | ||
* this is a es module | ||
* this is a es6 module | ||
*/ | ||
export const a = 'World' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* this is a es6 module | ||
*/ | ||
export const d = 'Test' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import a from './a.js'; | ||
// const c = require('./c') | ||
import d from './d.js'; | ||
const b = require('./b') | ||
|
||
console.log(`Hello ${a} ${b} ${d}`); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.