|
| 1 | +import DocPage from '@docs/doc-page'; |
| 2 | + |
| 3 | +<DocPage> |
| 4 | + |
| 5 | +# Optimization |
| 6 | + |
| 7 | +Data Driven Forms is built in three module formats: CJS, ESM and UMD. |
| 8 | + |
| 9 | +## CJS |
| 10 | + |
| 11 | +```jsx |
| 12 | +import FormRenderer from '@data-driven-forms/react-form-renderer/dist/cjs/form-renderer'; |
| 13 | +import useField from '@data-driven-forms/react-form-renderer/dist/cjs/use-field'; |
| 14 | +``` |
| 15 | + |
| 16 | +## ESM |
| 17 | + |
| 18 | +```jsx |
| 19 | +import FormRenderer from '@data-driven-forms/react-form-renderer/dist/esm/form-renderer'; |
| 20 | +import useField from '@data-driven-forms/react-form-renderer/dist/esm/use-field'; |
| 21 | +``` |
| 22 | + |
| 23 | +In both of CJS and ESM, exported files follow the kebab case. All components are exported in default exports. |
| 24 | + |
| 25 | +## UMD |
| 26 | + |
| 27 | +**Recommendation: UMD is just a fallback option. We strongly recommend to use CJS or ESM. These two modules allow to better optimization via treeshaking and more modern language syntax.** |
| 28 | + |
| 29 | +```jsx |
| 30 | +import FormRenderer, { useField } from '@data-driven-forms/react-form-renderer'; |
| 31 | +``` |
| 32 | + |
| 33 | +Only the `FormRenderer` component is exported as a default. In all other packages, use only named exports. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Mixing imports |
| 38 | + |
| 39 | +**Do not mix** different module formats (for example `Renderer` imported from UMD and `componentMapper` from CJS), otherwise you will encounter this error: |
| 40 | + |
| 41 | +```jsx |
| 42 | +useField must be used inside of a <Form> component |
| 43 | +``` |
| 44 | + |
| 45 | +## Transforming imports in Babel |
| 46 | + |
| 47 | +You can automate tranforming imports to specific module formats by using [transform-imports](https://www.npmjs.com/package/babel-plugin-transform-imports) plugin in your Babel configuration. With that, you don't need to write explicit imports manually and you can still import everything from projects' roots. |
| 48 | + |
| 49 | +Example: |
| 50 | + |
| 51 | +```jsx |
| 52 | +// babel.config.js |
| 53 | + |
| 54 | +const transformDDFImportsToCJS = [ |
| 55 | + 'transform-imports', |
| 56 | + { |
| 57 | + '@data-driven-forms/react-form-renderer': { |
| 58 | + transform: (importName) => |
| 59 | + `@data-driven-forms/react-form-renderer/dist/cjs/${importName |
| 60 | + .split(/(?=[A-Z])/) |
| 61 | + .join('-') |
| 62 | + .toLowerCase()}.js`, |
| 63 | + } |
| 64 | + }, |
| 65 | + 'react-form-renderer-cjs' |
| 66 | +] |
| 67 | + |
| 68 | +... |
| 69 | + |
| 70 | +module.exports = { |
| 71 | + ..., |
| 72 | + plugins: [ |
| 73 | + ..., |
| 74 | + transformDDFImportsToCJS |
| 75 | + ] |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Result: |
| 80 | + |
| 81 | +```jsx |
| 82 | +import { useField } from '@data-driven-forms/react-form-renderer'; |
| 83 | +// ^^ this will be converted to >> |
| 84 | +import useField from '@data-driven-forms/react-form-renderer/dist/cjs/use-field'; |
| 85 | +``` |
| 86 | + |
| 87 | +</DocPage> |
0 commit comments