Skip to content

Commit 2e7e6b3

Browse files
committed
feat(core): initial implementation
0 parents  commit 2e7e6b3

25 files changed

+8222
-0
lines changed

.editorconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
tab_width = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
max_line_length = 80
12+
13+
[*.bom.*]
14+
charset = utf-8-bom
15+
16+
[*.md]
17+
trim_trailing_whitespace = false
18+
indent_size = 4
19+
20+
[*.go]
21+
indent_style = tab
22+
23+
[*.py]
24+
indent_style = tab

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
node_modules/
2+
.cache/
3+
build/
4+
temp/
5+
esm/
6+
cjs/
7+
lib/
8+
*.log
9+
local.*
10+
local-*
11+
*.local
12+
*.local.*
13+
/.env.development
14+
.DS_Store
15+
.DS_Store*
16+
*~
17+
.*~
18+
*.swp
19+
.*.swp
20+
*.tgz
21+
.idea/
22+
.vscode/
23+
dist/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 acrazing
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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.

compare/input/AsyncComponent.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*!
2+
* Copyright 2019 acrazing <joking.young@gmail.com>. All rights reserved.
3+
* @since 2019-11-01 20:58:14
4+
*/
5+
6+
import { PureComponent } from 'react';
7+
import * as React from 'react';
8+
9+
export class AsyncComponent extends PureComponent {
10+
render() {
11+
return <div>Async component!</div>;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*!
2+
* Copyright 2019 acrazing <joking.young@gmail.com>. All rights reserved.
3+
* @since 2019-11-01 20:56:41
4+
*/
5+
6+
import { PureComponent } from 'react';
7+
import * as React from 'react';
8+
9+
export default class AsyncDefaultComponent extends PureComponent {
10+
render() {
11+
return <div>Async default component!</div>;
12+
}
13+
}

compare/input/sample.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*!
2+
* Copyright 2019 acrazing <joking.young@gmail.com>. All rights reserved.
3+
* @since 2019-11-01 22:45:52
4+
*/
5+
6+
export function sample() {
7+
return 'this is sample file for dynamic import';
8+
}

0 commit comments

Comments
 (0)