Skip to content

Commit

Permalink
add escodegen & estraverse
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryOnlyZRJ committed Feb 12, 2019
1 parent 37cc8b7 commit a24ee3e
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 31 deletions.
4 changes: 2 additions & 2 deletions dist/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

eval("{};var s = __webpack_require__(/*! ./module.js */ \"./src/module.js\");\n\nconsole.log(s.s);\n\n//# sourceURL=webpack:///./src/index.js?");
eval("{}\n;\n\nvar s = __webpack_require__(/*! ./module.js */ \"./src/module.js\");\n\nconsole.log(s.s);\n\n//# sourceURL=webpack:///./src/index.js?");

/***/ }),

Expand All @@ -18,7 +18,7 @@ eval("{};var s = __webpack_require__(/*! ./module.js */ \"./src/module.js\");\n\
/*! no static exports found */
/***/ (function(module, exports) {

eval("{};var s = 123;\nconsole.log(s);\nmodule.exports = {\n s: s\n};\n\n//# sourceURL=webpack:///./src/module.js?");
eval("{}\n;\nvar s = 123;\nconsole.log(s);\nmodule.exports = {\n s: s\n};\n\n//# sourceURL=webpack:///./src/module.js?");

/***/ })

Expand Down
117 changes: 117 additions & 0 deletions loaders/babel/ast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
module.exports = {
"type": "Program",
"start": 0,
"end": 57,
"body": [{
"type": "VariableDeclaration",
"start": 0,
"end": 12,
"declarations": [{
"type": "VariableDeclarator",
"start": 4,
"end": 11,
"id": {
"type": "Identifier",
"start": 4,
"end": 5,
"name": "s"
},
"init": {
"type": "Literal",
"start": 8,
"end": 11,
"value": 123,
"raw": "123"
}
}],
"kind": "var"
}, {
"type": "ExpressionStatement",
"start": 13,
"end": 28,
"expression": {
"type": "CallExpression",
"start": 13,
"end": 27,
"callee": {
"type": "MemberExpression",
"start": 13,
"end": 24,
"object": {
"type": "Identifier",
"start": 13,
"end": 20,
"name": "console"
},
"property": {
"type": "Identifier",
"start": 21,
"end": 24,
"name": "log"
},
"computed": false
},
"arguments": [{
"type": "Identifier",
"start": 25,
"end": 26,
"name": "s"
}]
}
}, {
"type": "ExpressionStatement",
"start": 29,
"end": 57,
"expression": {
"type": "AssignmentExpression",
"start": 29,
"end": 56,
"operator": "=",
"left": {
"type": "MemberExpression",
"start": 29,
"end": 43,
"object": {
"type": "Identifier",
"start": 29,
"end": 35,
"name": "module"
},
"property": {
"type": "Identifier",
"start": 36,
"end": 43,
"name": "exports"
},
"computed": false
},
"right": {
"type": "ObjectExpression",
"start": 46,
"end": 56,
"properties": [{
"type": "Property",
"start": 50,
"end": 54,
"method": false,
"shorthand": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 50,
"end": 51,
"name": "s"
},
"value": {
"type": "Identifier",
"start": 53,
"end": 54,
"name": "s"
},
"kind": "init"
}]
}
}
}],
"sourceType": "script"
}
24 changes: 24 additions & 0 deletions loaders/babel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// code -> AST
const acorn = require('acorn')
// 遍历AST
const estraverse = require('estraverse')
// AST -> code
const escodegen = require('escodegen')

module.exports = function (content) {
// AST解析过程模拟
const AST_Object = acorn.parse(content)
// 结果见 ./ast.js
console.log("AST静态语法树:", AST_Object)
// 遍历AST
estraverse.traverse(AST_Object, {
enter: function (node, parent) {
if (node.type == 'VariableDeclaration') {
console.log("遍历AST拿到const:", node.kind)
node.kind = 'var'
}
}
});
// AST -> code
console.log('AST替换之后生成的代码:', escodegen.generate(AST_Object))
}
12 changes: 6 additions & 6 deletions loaders/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// loader是链式调用的,上一个loader会将处理结果传给下一个loader
//content是loader匹配到的单个文件内容 【String】
// content是loader匹配到的单个文件内容 【String】
// 如果use是个loaders数组,则从后往前执行,pitch会被挂载在数组末端,最先执行

const loaderUtils = require("loader-utils");
const acorn = require("acorn")
const loaderUtils = require('loader-utils');
// 模拟babel功能
const babel = require('./babel')

/**
* loader Function
* @param {String} content 文件内容
*/
module.exports = async function (content) {
// AST解析过程模拟
const AST_Object = acorn.parse(content)
console.log("AST静态语法树", AST_Object)
babel(content)
// webpack-loader实战
// 获取用户配置的options
const options = loaderUtils.getOptions(this);
/**
Expand Down
Loading

0 comments on commit a24ee3e

Please sign in to comment.