Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
nobita457
webpack_ast
提交
7d4b9754
W
webpack_ast
项目概览
nobita457
/
webpack_ast
与 Fork 源项目一致
Fork自
渔滒 / webpack_ast
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
webpack_ast
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7d4b9754
编写于
3月 11, 2022
作者:
5
592767809
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增一个案例
上级
74c42f90
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
873 addition
and
0 deletion
+873
-0
whatsapp/cmd.txt
whatsapp/cmd.txt
+1
-0
whatsapp/runtime.js
whatsapp/runtime.js
+693
-0
whatsapp/test.js
whatsapp/test.js
+5
-0
whatsapp/webpack_mixer.js
whatsapp/webpack_mixer.js
+174
-0
未找到文件。
whatsapp/cmd.txt
0 → 100644
浏览文件 @
7d4b9754
node webpack_mixer.js -l runtime.js -o webpack_out.js
\ No newline at end of file
whatsapp/runtime.js
0 → 100644
浏览文件 @
7d4b9754
此差异已折叠。
点击以展开。
whatsapp/test.js
0 → 100644
浏览文件 @
7d4b9754
let
n
=
require
(
'
./webpack_out
'
);
console
.
log
(
n
);
\ No newline at end of file
whatsapp/webpack_mixer.js
0 → 100644
浏览文件 @
7d4b9754
const
parser
=
require
(
"
@babel/parser
"
);
const
t
=
require
(
"
@babel/types
"
);
const
generator
=
require
(
"
@babel/generator
"
);
const
traverse
=
require
(
"
@babel/traverse
"
).
default
;
const
fs
=
require
(
"
fs
"
);
function
wtofile
(
path
,
flags
,
code
)
{
var
fd
=
fs
.
openSync
(
path
,
flags
);
fs
.
writeSync
(
fd
,
code
);
fs
.
closeSync
(
fd
);
}
function
run
(
loader_path
,
out_path
,
modular_path
)
{
let
js_code
=
fs
.
readFileSync
(
loader_path
,
{
encoding
:
"
utf-8
"
});
// 转换为AST语法树
let
loader_ast
=
parser
.
parse
(
js_code
);
// 获取加载器代码
let
loader_body
;
if
(
loader_ast
.
program
.
body
[
0
].
expression
.
type
===
'
UnaryExpression
'
){
loader_body
=
loader_ast
.
program
.
body
[
0
].
expression
.
argument
.
callee
.
body
.
body
;
}
else
{
loader_body
=
loader_ast
.
program
.
body
[
0
].
expression
.
callee
.
body
.
body
;
}
let
export_function
=
null
;
traverse
(
loader_ast
,
{
FunctionDeclaration
(
path
){
if
(
path
.
toString
().
includes
(
"
exports:
"
)
&&
export_function
==
null
){
export_function
=
path
.
node
.
id
.
name
}
}
});
if
(
export_function
==
null
){
throw
"
未找到导出函数错误
"
}
for
(
let
i
=
0
;
i
<
loader_body
.
length
;
i
++
){
let
item
=
loader_body
[
i
];
if
(
item
.
type
===
'
ExpressionStatement
'
){
if
(
item
.
expression
.
type
===
'
SequenceExpression
'
&&
item
.
expression
.
expressions
[
0
].
type
===
'
AssignmentExpression
'
&&
item
.
expression
.
expressions
[
0
].
left
.
type
===
'
MemberExpression
'
&&
item
.
expression
.
expressions
[
0
].
left
.
object
.
type
===
'
Identifier
'
&&
item
.
expression
.
expressions
[
0
].
left
.
object
.
name
===
export_function
){
let
j
=
item
.
expression
.
expressions
.
length
-
1
;
while
(
j
>=
0
){
let
item2
=
item
.
expression
.
expressions
[
j
];
if
(
item2
.
left
&&
item2
.
left
.
type
===
'
MemberExpression
'
&&
((
item2
.
left
.
object
.
type
===
'
Identifier
'
&&
item2
.
left
.
object
.
name
===
export_function
)
||
(
item2
.
left
.
object
.
type
===
'
MemberExpression
'
&&
item2
.
left
.
object
.
object
.
type
===
'
Identifier
'
&&
item2
.
left
.
object
.
object
.
name
===
export_function
))){
item
.
expression
.
expressions
.
splice
(
j
+
1
,
item
.
expression
.
expressions
.
length
-
j
-
1
);
break
}
else
{
j
--
;
}
}
loader_body
.
splice
(
i
+
1
,
loader_body
.
length
-
i
-
1
);
loader_body
.
push
(
t
.
expressionStatement
(
t
.
assignmentExpression
(
"
=
"
,
t
.
identifier
(
"
export_function
"
),
t
.
identifier
(
export_function
))));
}
}
else
if
(
item
.
type
===
'
ReturnStatement
'
){
loader_body
[
i
]
=
t
.
expressionStatement
(
item
.
argument
);
let
j
=
0
;
while
(
j
<
loader_body
[
i
].
expression
.
expressions
.
length
){
let
item2
=
loader_body
[
i
].
expression
.
expressions
[
j
];
if
(
item2
.
left
&&
item2
.
left
.
type
===
'
MemberExpression
'
&&
item2
.
left
.
object
.
type
===
'
Identifier
'
&&
item2
.
left
.
object
.
name
===
export_function
){
j
++
}
else
{
loader_body
[
i
].
expression
.
expressions
.
splice
(
j
,
loader_body
[
i
].
expression
.
expressions
.
length
-
j
);
break
}
}
loader_body
.
push
(
t
.
expressionStatement
(
t
.
assignmentExpression
(
"
=
"
,
t
.
identifier
(
"
export_function
"
),
t
.
identifier
(
export_function
))));
}
}
// 导入加载器中的函数体
// let loader_arguments;
// if (loader_ast.program.body[0].expression.type === 'UnaryExpression'){
// loader_arguments = loader_ast.program.body[0].expression.argument.arguments[0];
// }else{
// loader_arguments = loader_ast.program.body[0].expression.arguments[0];
// }
// if (loader_arguments.type === 'ArrayExpression'){
// let tempobjectexpression = t.objectExpression([]);
// loader_arguments.elements.forEach(function (item, index) {
// if (item && item.type === 'FunctionExpression'){
// tempobjectexpression.properties.push(t.objectProperty(
// t.numericLiteral(index),
// item,
// false,
// false
// ));
// }
// });
// loader_arguments = tempobjectexpression;
// }
// 加载外部函数体
modular_path
.
forEach
(
function
(
item
,
index
)
{
var
jscode
=
fs
.
readFileSync
(
item
,
{
encoding
:
"
utf-8
"
});
let
modular_ast
=
parser
.
parse
(
jscode
);
modular_ast
.
program
.
body
[
0
].
expression
.
arguments
[
0
].
elements
.
forEach
(
function
(
item2
,
index2
)
{
if
(
item2
.
type
===
'
ArrayExpression
'
){
item2
.
elements
.
forEach
(
function
(
item3
,
index3
)
{
if
(
item3
&&
item3
.
type
===
'
FunctionExpression
'
){
loader_arguments
.
properties
.
push
(
t
.
objectProperty
(
t
.
numericLiteral
(
index3
),
item3
,
false
,
false
));
}
});
}
else
{
item2
.
properties
.
forEach
(
function
(
item3
,
index3
)
{
loader_arguments
.
properties
.
push
(
item3
)
})
}
});
});
// if (loader_ast.program.body[0].expression.type === 'UnaryExpression'){
// loader_ast.program.body[0].expression.argument.arguments[0] = loader_arguments;
// }else{
// loader_ast.program.body[0].expression.arguments[0] = loader_arguments;
// }
// 申请全局导出函数
loader_ast
.
program
.
body
.
splice
(
0
,
0
,
t
.
variableDeclaration
(
"
var
"
,[
t
.
variableDeclarator
(
t
.
identifier
(
"
export_function
"
))]));
// 导出函数
loader_ast
.
program
.
body
.
push
(
t
.
expressionStatement
(
t
.
assignmentExpression
(
"
=
"
,
t
.
memberExpression
(
t
.
identifier
(
"
module
"
),
t
.
identifier
(
"
exports
"
),
false
),
t
.
identifier
(
"
export_function
"
))));
let
code
=
generator
.
default
(
loader_ast
,
{
compact
:
false
,
// 压缩格式
comments
:
false
,
// 注释
jsescOption
:
{
minimal
:
false
// 转义
}
}).
code
;
wtofile
(
out_path
,
'
w
'
,
code
);
}
process
.
argv
.
push
(
'
-l
'
);
process
.
argv
.
push
(
'
runtime.js
'
);
process
.
argv
.
push
(
'
-o
'
);
process
.
argv
.
push
(
'
webpack_out.js
'
);
console
.
log
(
process
.
argv
);
!
function
()
{
let
loader_path
,
out_path
;
let
modular_path
=
[];
for
(
let
i
=
0
;
i
<
process
.
argv
.
length
;
i
++
){
if
(
process
.
argv
[
i
]
===
'
-l
'
){
loader_path
=
process
.
argv
[
i
+
1
];
i
+=
1
;
}
else
if
(
process
.
argv
[
i
]
===
'
-m
'
){
modular_path
.
push
(
process
.
argv
[
i
+
1
]);
i
+=
1
;
}
else
if
(
process
.
argv
[
i
]
===
'
-o
'
){
out_path
=
process
.
argv
[
i
+
1
];
i
+=
1
;
}
}
if
(
loader_path
&&
out_path
){
run
(
loader_path
,
out_path
,
modular_path
)
}
}();
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录