Skip to content

Commit

Permalink
feat: 实现es6的class转换 基本版
Browse files Browse the repository at this point in the history
  • Loading branch information
okbug committed Nov 20, 2021
1 parent 8dad7dc commit 593e24d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
92 changes: 92 additions & 0 deletions learnWebpack/ast-demo/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const core = require('@babel/core');
// const { types, template } = core;
const types = require('babel-types')
const template = require('@babel/template')
let code = `
function a(x, y) {
console.log(x, y);
return x + y
}
`

function parseCodeStringToStatement(str) {
let arr = str.split('\n').filter(i => i !== '');
// console.log(arr)
return arr.map(s => template.statement(s)())
}


const error = 'error'

let customeCode = `
console.log(${error});
`

let parseResult = parseCodeStringToStatement(customeCode);
let plugin = {
visitor: {
FunctionDeclaration(nodePath) {
let {node} = nodePath;
let {id} = node.id;
let blockStatement = node.body;
if (blockStatement.body && types.isTryStatement(blockStatement.body[0])) return;

// let catchStatement = template.statement(`
// console.log(error, 1);
// `)();

// // [catchStatement, template.statement('console.log(\'hello world\')')()]
let catchClause = types.catchClause(types.identifier(error), types.blockStatement(parseResult));

let tryStatement = types.tryStatement(blockStatement, catchClause);
let func = types.functionExpression(id, node.params, types.blockStatement([tryStatement]), node.generator, node.async);
nodePath.replaceWith(func)
}
}
}


// let res = core.transform(code, {
// plugins: [plugin]
// });

// console.log(res.code)

function parse(code, errorName, errorBody) {
const parseResult = parseCodeStringToStatement(errorBody)
let plugin = {
visitor: {
FunctionDeclaration(nodePath) {
let {node} = nodePath;
let {id} = node.id;
let blockStatement = node.body;
if (blockStatement.body && types.isTryStatement(blockStatement.body[0])) return;

// let catchStatement = template.statement(`
// console.log(error, 1);
// `)();

// // [catchStatement, template.statement('console.log(\'hello world\')')()]
let catchClause = types.catchClause(types.identifier(errorName), types.blockStatement(parseResult));

let tryStatement = types.tryStatement(blockStatement, catchClause);
let func = types.functionExpression(id, node.params, types.blockStatement([tryStatement]), node.generator, node.async);
nodePath.replaceWith(func)
}
}
}
return core.transform(code, {
plugins: [plugin]
}).code;
}

// console.log(parse(`
// function add(x, y) {
// console.log(x, y);
// return x + y;
// }
// `, 'error', `console.log(error)`))

let rt = JSON.stringify(core.parse(`let a = 1`).program, null ,2)
console.log(rt);
module.exports = parse;
10 changes: 8 additions & 2 deletions learnWebpack/ast-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,14 @@ const myClassTransformPlugin = {
```

> babel-types 不用安装,core里面其实也有
还有template

```js
// const types = require('babel-types');
const {types} = core
```
// const template = require('@babel/template');
const {types, template} = core
```

以上实现代码查看 (./4.js)['./4.js']

## 实现tree-shaking

0 comments on commit 593e24d

Please sign in to comment.