Skip to content

Commit 434c58e

Browse files
authored
Merge pull request #67 from jviide/remove-html-option
Cleanups to jsx-to-htm: remove html option & dependency to babel-plugin-jsx-pragmatic, update README.md
2 parents 468d091 + e3ab363 commit 434c58e

File tree

4 files changed

+15
-59
lines changed

4 files changed

+15
-59
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
"@babel/core": "^7.2.2",
6565
"@babel/preset-env": "^7.1.6",
6666
"babel-jest": "^24.1.0",
67-
"babel-plugin-jsx-pragmatic": "^1.0.2",
6867
"babel-preset-env": "^1.7.0",
6968
"eslint": "^5.2.0",
7069
"eslint-config-developit": "^1.1.1",

packages/babel-plugin-transform-jsx-to-htm/README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# babel-plugin-transform-jsx-to-htm
22

3-
This plugin converts JSX into Tagged Templates that work with things like [htm] and [lit-html].
3+
This plugin converts JSX into Tagged Templates that work with [htm].
44

55
```js
66
// INPUT:
@@ -32,45 +32,42 @@ The following options are available:
3232

3333
| Option | Type | Default | Description
3434
|--------|---------|----------|------------
35-
| `tag` | String | `"html"` | The "tag" function to prefix [Tagged Templates] with.<br> _Useful when [Auto-importing a tag function](#auto-importing-the-tag)._
36-
| `html` | Boolean | `false` | `true` outputs HTML-like templates for use with [lit-html].<br> _The is default XML-like, with self-closing tags._
35+
| `tag` | String | `"html"` | The "tag" function to prefix [Tagged Templates] with.
36+
| `import` | `false`\|String\|Object | `false` | Auto-import a tag function, off by default.<br>_See [Auto-importing a tag function](#auto-importing-the-tag) for an example._
3737

3838
Options are passed to a Babel plugin using a nested Array:
3939

4040
```js
4141
"plugins": [
4242
["babel-plugin-transform-jsx-to-htm", {
43-
"tag": "$$html",
44-
"html": true
43+
"tag": "$$html"
4544
}]
4645
]
4746
```
4847

4948
## Auto-importing the tag
5049

51-
Want to automatically import `html` into any file that uses JSX? It works the same as with JSX!
52-
Just use [babel-plugin-jsx-pragmatic]:
50+
Want to automatically import `html` into any file that uses JSX?
51+
Just use the `import` option:
5352

5453
```js
5554
"plugins": [
56-
["babel-plugin-jsx-pragmatic", {
57-
// the module to import:
58-
"module": "lit-html",
59-
// a named export to use from that module:
60-
"export": "html",
61-
// what to call it locally: (should match your "tag" option)
62-
"import": "$$html"
63-
}],
6455
["babel-plugin-transform-jsx-to-htm", {
65-
"tag": "$$html"
56+
"tag": "$$html",
57+
"import": {
58+
// the module to import:
59+
"module": "htm/preact",
60+
// a named export to use from that module:
61+
"export": "html"
62+
}
6663
}]
6764
]
6865
```
6966

7067
The above will produce files that look like:
7168

7269
```js
73-
import { html as $$html } from 'lit-html';
70+
import { html as $$html } from 'htm/preact';
7471

7572
export default $$html`<h1>hello</h1>`
7673
```
@@ -80,5 +77,3 @@ export default $$html`<h1>hello</h1>`
8077
Apache 2
8178

8279
[htm]: https://github.com/developit/htm
83-
[lit-html]: https://github.com/polymer/lit-html
84-
[babel-plugin-jsx-pragmatic]: https://github.com/jmm/babel-plugin-jsx-pragmatic

packages/babel-plugin-transform-jsx-to-htm/index.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import jsx from '@babel/plugin-syntax-jsx';
55
* @param {object} [options]
66
* @param {string} [options.tag='html'] The tagged template "tag" function name to produce.
77
* @param {string | boolean | object} [options.import=false] Import the tag automatically
8-
* @param {string} [options.html=false] If `true`, output HTML-like instead of XML-like (no self-closing tags, etc).
98
*/
109
export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
1110
const tagString = options.tag || 'html';
1211
const tag = dottedIdentifier(tagString);
13-
const htmlOutput = !!options.html;
1412
const importDeclaration = tagImport(options.import || false);
1513

1614
function tagImport(imp) {
@@ -130,7 +128,7 @@ export default function jsxToHtmBabelPlugin({ types: t }, options = {}) {
130128
}
131129

132130
const children = t.react.buildChildren(node);
133-
if (htmlOutput || children && children.length !== 0) {
131+
if (children && children.length !== 0) {
134132
raw('>');
135133
for (let i = 0; i < children.length; i++) {
136134
let child = children[i];

test/babel-transform-jsx.test.mjs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,6 @@ describe('babel-plugin-transform-jsx-to-htm', () => {
101101
});
102102
});
103103

104-
describe('options.html = true', () => {
105-
test('use explicit end tags instead of self-closing', () => {
106-
expect(
107-
compile('(<div />);', { html: true })
108-
).toBe('html`<div></div>`;');
109-
110-
expect(
111-
compile('(<div a />);', { html: true })
112-
).toBe('html`<div a></div>`;');
113-
114-
expect(
115-
compile('(<a>b</a>);', { html: true })
116-
).toBe('html`<a>b</a>`;');
117-
});
118-
});
119-
120104
describe('props', () => {
121105
test('static values', () => {
122106
expect(
@@ -203,24 +187,4 @@ describe('babel-plugin-transform-jsx-to-htm', () => {
203187
).toBe('html`<div/>`;');
204188
});
205189
});
206-
207-
describe('integration with babel-plugin-jsx-pragmatic', () => {
208-
test('JSX is still identified and import added', () => {
209-
expect(
210-
compile('const Foo = props => <div>hello</div>;', {
211-
tag: '$$html',
212-
plugins: [
213-
['babel-plugin-jsx-pragmatic', {
214-
// module to import:
215-
module: 'lit-html',
216-
// the name of the export to use:
217-
export: 'html',
218-
// whatever you specified for the "tag" option:
219-
import: '$$html'
220-
}]
221-
]
222-
})
223-
).toBe('import { html as $$html } from "lit-html";\n\nconst Foo = props => $$html`<div>hello</div>`;');
224-
});
225-
});
226190
});

0 commit comments

Comments
 (0)