fix registerBeforeEachMatchPlugin and refactor more code