我一直在研究Rollupjs,特别是因为它具有消除死代码的功能。
在我的示例中,我创建了以下人为的示例:
来源:
const opts = {
method: 'GET',
url: 'https://www.google.com',
secure: true
}
const fs = require('fs')
let token
if (opts.secure) {
token = 'secret_token'
}
if (opts.method === 'GET') {
if (token) {
fs.readFileSync(['will get', opts.url, 'with token', token])
} else {
fs.readFileSync(['will get', opts.url])
}
} else if (opts.method === 'POST') {
console.log('will post', opts.url)
} else if (opts.method === 'PUT') {
console.log('will put', opts.url)
} else if (opts.method === 'DELETE') {
console.log('will delete', opts.url)
}
结果:
'use strict';
const opts = {
method: 'GET',
url: 'https://www.google.com',
secure: true
};
const fs = require('fs');
let token;
{
token = 'secret_token';
}
{
if (token) {
fs.readFileSync(['will get', opts.url, 'with token', token]);
} else {
fs.readFileSync(['will get', opts.url]);
}
}
如您所见,结果是,token = 'secret_token'
周围有一个方块。
这也不能消除if (token) {
块周围的无效代码。
但是,如果删除if语句周围的条件并仅设置令牌,例如let token = 'secret_token
,则会得到更接近的结果。尽管您仍然会嵌套不必要的块。
{
{
fs.readFileSync(['will get', opts.url, 'with token', token]);
}
}
我相信我可以使用esprima进行一些黑客操作,以删除不必要的块。但是我有两个问题。
-
汇总中是否有一种方法可以删除这些不必要的代码块。大括号。
-
是否有一种方法可以使定义的变量达到let。
IE更改:
let a = 1
收件人:
let a
{
a = 1
}
演示:
https://rollupjs.org/repl?version=0.66.6&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmNvbnN0JTIwb3B0cyUyMCUzRCUyMCU3QiU1Q24lMjAlMjBtZXRob2QlM0ElMjAnR0VUJyUyQyU1Q24lMjAlMjB1cmwlM0ElMjAnaHR0cHMlM0ElMkYlMkZ3d3cuZ29vZ2xlLmNvbSclMkMlNUNuJTIwJTIwc2VjdXJlJTNBJTIwdHJ1ZSU1Q24lN0QlNUNuJTVDbmNvbnN0JT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