|
| 1 | +# typescript-loadable-components-plugin |
| 2 | + |
| 3 | +A custom transformer of typescript that is used to add some necessary properties to loadable-components. |
| 4 | + |
| 5 | +This transformer helps you to transform code like: |
| 6 | + |
| 7 | +```typescript jsx |
| 8 | +import loadable from '@loadable/component'; |
| 9 | + |
| 10 | +export const LazyFoo = loadable(() => import('./input/AsyncDefaultComponent')); |
| 11 | +``` |
| 12 | + |
| 13 | +to the following format: |
| 14 | + |
| 15 | +```typescript jsx |
| 16 | +import loadable from 'loadable-components'; |
| 17 | + |
| 18 | +export const LazyFoo = loadable({ |
| 19 | + chunkName() { |
| 20 | + return 'input-AsyncDefaultComponent'; |
| 21 | + }, |
| 22 | + isReady(props) { |
| 23 | + return ( |
| 24 | + typeof __webpack_modules__ !== 'undefined' && |
| 25 | + Boolean(__webpack_modules__[this.resolve(props)]) |
| 26 | + ); |
| 27 | + }, |
| 28 | + requireAsync: () => |
| 29 | + import( |
| 30 | + /* "webpackChunkName":"input-AsyncDefaultComponent" */ './input/AsyncDefaultComponent' |
| 31 | + ), |
| 32 | + requireSync(props) { |
| 33 | + return typeof '__webpack_require__' !== 'undefined' |
| 34 | + ? __webpack_require__(this.resolve(props)) |
| 35 | + : eval('module.require')(this.resolve(props)); |
| 36 | + }, |
| 37 | + resolve() { |
| 38 | + if (require.resolveWeak) |
| 39 | + return require.resolveWeak( |
| 40 | + /* "webpackChunkName":"input-AsyncDefaultComponent" */ './input/AsyncDefaultComponent', |
| 41 | + ); |
| 42 | + else |
| 43 | + return eval('require.resolve')( |
| 44 | + /* "webpackChunkName":"input-AsyncDefaultComponent" */ './input/AsyncDefaultComponent', |
| 45 | + ); |
| 46 | + }, |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +## Install |
| 51 | + |
| 52 | +```bash |
| 53 | +yarn add typescript-loadable-components-plugin -D |
| 54 | + |
| 55 | +# or npm |
| 56 | +npm install typescript-loadable-components-plugin -D |
| 57 | +``` |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +### with `ttypescript` |
| 62 | + |
| 63 | +you just need add `typescript--plugin` to your `tsconfig.json`: |
| 64 | + |
| 65 | +```json |
| 66 | +{ |
| 67 | + "compilerOptions": { |
| 68 | + "plugins": [ |
| 69 | + { |
| 70 | + "transform": "typescript-loadable-components-plugin" |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### with `webpack` and `ts-loader` |
| 78 | + |
| 79 | +you need to add the following options to your loader: |
| 80 | + |
| 81 | +```js |
| 82 | +import { createLoadableComponentsTransformer } from 'typescript-loadable-components-plugin'; |
| 83 | +export default { |
| 84 | + // ... |
| 85 | + module: { |
| 86 | + rules: [ |
| 87 | + { |
| 88 | + test: /\.tsx?$/, |
| 89 | + loader: 'ts-loader', // or awesome-typescript-loader |
| 90 | + options: { |
| 91 | + getCustomTransformers: (program) => ({ |
| 92 | + before: [createLoadableComponentsTransformer(program, {})], |
| 93 | + }), |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | +}; |
| 99 | +``` |
| 100 | + |
| 101 | +## Options |
| 102 | + |
| 103 | +_No options needed_ |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +**Supported syntax**: |
| 108 | + |
| 109 | +1. loadable default component: `loadable(() => import(...), [options])` |
| 110 | +2. loadable lib component: `loadable.lib(() => import(...), [options])` |
| 111 | + |
| 112 | +## LICENSE |
| 113 | + |
| 114 | +MIT |
| 115 | + |
| 116 | + The MIT License (MIT) |
| 117 | + |
| 118 | + Copyright (c) 2019 acrazing |
| 119 | + |
| 120 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 121 | + of this software and associated documentation files (the "Software"), to deal |
| 122 | + in the Software without restriction, including without limitation the rights |
| 123 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 124 | + copies of the Software, and to permit persons to whom the Software is |
| 125 | + furnished to do so, subject to the following conditions: |
| 126 | + |
| 127 | + The above copyright notice and this permission notice shall be included in all |
| 128 | + copies or substantial portions of the Software. |
| 129 | + |
| 130 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 131 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 132 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 133 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 134 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 135 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 136 | + SOFTWARE. |
0 commit comments