Skip to content

Commit

Permalink
feat(ToyWebpack): ✨ add _parseModule function
Browse files Browse the repository at this point in the history
  • Loading branch information
classmatewu committed Sep 27, 2021
1 parent d448ebc commit 77c8530
Show file tree
Hide file tree
Showing 310 changed files with 74,106 additions and 24 deletions.
52 changes: 52 additions & 0 deletions ToyWebpack.js
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
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ToyWebpack = require('./src/ToyWebpack')
const ToyWebpack = require('./ToyWebpack')
const options = require('./config')

const compiler = new ToyWebpack(options)
Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
/**
* 入口文件
*/
entry : './test/index.js',
entry : './modules/index.js',

/**
* 打包出口
Expand Down
2 changes: 1 addition & 1 deletion test/a.js → modules/a.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'
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions modules/d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* this is a es6 module
*/
export const d = 'Test'
6 changes: 6 additions & 0 deletions modules/index.js
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}`);
1 change: 1 addition & 0 deletions node_modules/.bin/jsesc

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

1 change: 1 addition & 0 deletions node_modules/.bin/parser

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

22 changes: 22 additions & 0 deletions node_modules/@babel/code-frame/LICENSE

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

19 changes: 19 additions & 0 deletions node_modules/@babel/code-frame/README.md

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

163 changes: 163 additions & 0 deletions node_modules/@babel/code-frame/lib/index.js

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

61 changes: 61 additions & 0 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.

22 changes: 22 additions & 0 deletions node_modules/@babel/generator/LICENSE

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

Loading

0 comments on commit 77c8530

Please sign in to comment.