Skip to content

Commit

Permalink
upd(ToyWebpack): ✨ update _parseModule function
Browse files Browse the repository at this point in the history
  • Loading branch information
classmatewu committed Sep 28, 2021
1 parent 77c8530 commit b2cd772
Show file tree
Hide file tree
Showing 31 changed files with 1,695 additions and 301 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_module
node_modules
25 changes: 16 additions & 9 deletions ToyWebpack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const parse = require('@babel/parser')
const { default: traverse } = require('@babel/traverse')
const { transformFromAst } = require('@babel/core')

class ToyWebpack {
constructor(options) {
Expand All @@ -15,7 +16,7 @@ class ToyWebpack {
*/
run() {
console.log('run ToyWebpack')
this._parseModule(this.entry)
const {dep, code} = this._parseModule(this.entry)
}

/**
Expand All @@ -24,27 +25,33 @@ class ToyWebpack {
_parseModule(modulePath) {
// 读取module文件,并输出为utf-8编码格式的字符串
const moduleStr = fs.readFileSync(modulePath, 'utf-8')

// 利用babel/parse将源码转换为ast
const ast = parse.parse(moduleStr, {
sourceType: "module",
})
// 利用

// 利用traverse遍历ast节点,而不用去类似`ast.program.body`这样去遍历
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);
// TODO 添加 cjs、amd 模块的打包的方式
// VariableDeclaration({node}) {
// console.log(node);
// }
})

// ast转换为源码,并将原先的es6+语法转换为es5语法
const { code } = transformFromAst(ast, null, {
presets: ['@babel/preset-env']
})

return {
dep,
// code,
code,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import a from './a.js';
// const c = require('./c')
import d from './d.js';
const b = require('./b')
// import d from './d.js';
// const b = require('./b')

console.log(`Hello ${a} ${b} ${d}`);
24 changes: 14 additions & 10 deletions node_modules/@babel/code-frame/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions node_modules/@babel/generator/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions node_modules/@babel/helper-function-name/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions node_modules/@babel/helper-get-function-arity/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions node_modules/@babel/helper-hoist-variables/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b2cd772

Please sign in to comment.