index.js 583 B

1234567891011121314151617181920
  1. module.exports = options => {
  2. const aliases = Object.keys(options);
  3. const re = new RegExp(`^(${aliases.map(x => escapeRegExp(x)).join('|')})$`);
  4. return {
  5. name: 'alias',
  6. setup(build) {
  7. // we do not register 'file' namespace here, because the root file won't be processed
  8. // https://github.com/evanw/esbuild/issues/791
  9. build.onResolve({ filter: re }, args => ({
  10. path: options[args.path],
  11. }));
  12. },
  13. };
  14. };
  15. function escapeRegExp(string) {
  16. // $& means the whole matched string
  17. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  18. }