项目作者: smithc

项目描述 :
A webpack library that enables module decoration
高级语言: JavaScript
项目地址: git://github.com/smithc/webpack-decorators.git
创建时间: 2020-05-13T16:42:41Z
项目社区:https://github.com/smithc/webpack-decorators

开源协议:MIT License

下载


Webpack Module Decorator

This library allows consumers to decorate the behavior of JavaScript modules via module proxies that are aliased through Webpack configuration.

This library has behavior similar to native JavaScript decorator support, except that instead
of requiring decorator annotations to be used within user-code, this library can inject your decorators dynamically into a proxied ES module.

This provides a level of metaprogramming support for scenarios where you may need to inject cross-cutting functionality
into your codebase without requiring you to sprinkle annotations throughout the codebase (or even for situations where
you may not own the code in question to begin with).

This repo currently supports two module proxies:

  • react (in package webpack-decorators-react)
  • react-dom (in package webpack-decorators-react-dom)

Setup

  1. Install the package npm install webpack-decorators-react or npm install webpack-decorators-react-dom
  2. Add the desired react or react-dom configs (examples in reactDecoratorConfig) to your webpack.config.js

For react:

  1. resolve: {
  2. alias: {
  3. react$: require.resolve(`webpack-decorators-react`),
  4. "___react-original___$": require.resolve(`react`),
  5. },
  6. }

For react-dom:

  1. resolve: {
  2. alias: {
  3. "react-dom$": require.resolve(`webpack-decorators-react-dom`),
  4. "___react-dom-original___$": require.resolve(`react-dom`),
  5. },
  6. }

For react-dom/server:

  1. resolve: {
  2. alias: {
  3. "react-dom/server$": require.resolve(`webpack-decorators-react-dom-server`),
  4. "___react-dom-server-original___$": require.resolve(`react-dom/server`),
  5. },
  6. }
  1. Register your custom decorators
  1. const { registerDecorator } = require('webpack-decorators');
  2. const decorator = {
  3. createElement: function(originalFunc, ...args) {
  4. console.log('[decorator]: Decorated React.createElement...');
  5. return originalFunc(...args);
  6. }
  7. }
  8. const decorator2 = {
  9. createElement: function(originalFunc, ...args) {
  10. console.log('[decorator2]: Decorated React.createElement...');
  11. return originalFunc(...args);
  12. },
  13. Component: function(originalFunc, ...args) {
  14. console.log('[decorator2]: Decorated React.Component...');
  15. return originalFunc(...args);
  16. }
  17. }
  18. registerDecorator('react', decorator, 'createElement');
  19. registerDecorator('react', decorator2, 'createElement', 'Component');

NOTE: decorators are pipelined and executed in the order in which they are defined.

Given the above order of registrations, when calling something like React.createElement in your code, the following call chain will result:

  1. decorator2.createElement
  2. decorator.createElement
  3. React.createElement (original module)