webpack 替换模块算法
Opened this issue · 1 comments
noneven commented
let source = `require('a');console.log(123);require('b');`
// source => AST => replaces
let replaces = [{
from: 8,
to: 11,
name: 'a',
value: 1
}, {
from: 38,
to: 41,
name: 'b',
value: 2
}];
// 期望输出
'require(/* a */1);console.log(123);require(/* b */2);'
noneven commented
let result = [source];
replaces.sort((a, b) => b.from - a.from).forEach(replace => {
let remainSource = result.shift();
result.unshift(
remainSource.substr(0, replace.from),
`/* ${replace.name} */${replace.value}`,
remainSource.substr(replace.to)
)
});
console.log(result.join(''));