Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack 替换模块算法 #20

Open
noneven opened this issue Mar 26, 2018 · 1 comment
Open

webpack 替换模块算法 #20

noneven opened this issue Mar 26, 2018 · 1 comment

Comments

@noneven
Copy link
Owner

noneven commented Mar 26, 2018

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
Copy link
Owner Author

noneven commented Mar 26, 2018

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(''));

@noneven noneven changed the title webpack 中替换模块算法 webpack 替换模块算法 Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant