993b1601fa4150d25d9ac63a7568af68a34ce3f6.svn-base
2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# import-jsx [![Build Status](https://travis-ci.org/vadimdemedes/import-jsx.svg?branch=master)](https://travis-ci.org/vadimdemedes/import-jsx)
> Require and transpile JSX on the fly
- Doesn't install any `require()` hooks
- Auto-detects React pragma (`React.createElement`) and falls back to `h` pragma supported by Preact and others
- Caches transpiled sources by default
- Bundles in [object rest spread](https://babeljs.io/docs/plugins/transform-object-rest-spread/) transform
## Install
```
$ npm install --save import-jsx
```
## Usage
```js
const importJsx = require('import-jsx');
const reactComponent = importJsx('./react');
const preactComponent = importJsx('./preact');
const customComponent = importJsx('./custom', {pragma: 'x'});
```
**react.js**
```jsx
const React = require('react');
module.exports = <div/>;
```
**preact.js**
```jsx
const {h} = require('preact');
module.exports = <div/>;
```
**custom.js**
```jsx
const x = (tagName, attrs, ...children) => {};
module.exports = <div/>;
```
## API
### importJsx(moduleId, [options])
#### moduleId
Type: `string`
Module id.
#### options
##### pragma
Type: `string`<br>
Default: `h`
Override [JSX pragma](https://jasonformat.com/wtf-is-jsx/).
##### pragmaFrag
Type: `string`<br>
Default: `Fragment`
Override pragma for [JSX fragments](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx#pragmafrag).
##### cache
Type: `boolean`<br>
Default: `true`
Enable or disable caching of transpiled sources.
### importJsx.create([options])
Factory method to create a version of `importJsx()` with pre-defined options.
Useful when you need a custom pragma, but don't want to pass it along with each `importJsx()` call.
#### options
Type: `object`
Options to pass to `importJsx()`.
```js
// Before
const importJsx = require('import-jsx');
importJsx('./a', {pragma: 'x'});
importJsx('./b', {pragma: 'x'});
// After
const importJsx = require('import-jsx').create({pragma: 'x'});
importJsx('./a');
importJsx('./b');
```
## License
MIT © [Vadim Demedes](https://github.com/vadimdemedes)