Skip to content

Commit 38352d9

Browse files
Make package UMD (Univeral Module Definition) compatible
Wrap main logic so that it can be loaded by CommonJS (node and bundlers like webpack), AMD (RequireJS), and browser script. UMD template: https://github.com/ForbesLindesay/umd/blob/master/template.js
1 parent 3195109 commit 38352d9

File tree

1 file changed

+58
-32
lines changed

1 file changed

+58
-32
lines changed

index.js

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
1-
'use strict';
2-
3-
/**
4-
* Module dependencies.
5-
*/
6-
var domToReact = require('./lib/dom-to-react');
7-
var htmlToDOM;
8-
9-
/**
10-
* Detect environment.
11-
*/
12-
13-
/** Client (Browser). */
14-
if (typeof window !== 'undefined' && this === window) {
15-
htmlToDOM = require('./lib/html-to-dom-client');
16-
17-
/** Server (Node). */
18-
} else {
19-
htmlToDOM = require('./lib/html-to-dom-server');
20-
}
21-
22-
/**
23-
* Convert HTML to React.
24-
*
25-
* @param {String} html - The HTML.
26-
* @param {Object} [options] - The additional options.
27-
* @param {Function} [options.replace] - The replace method.
28-
* @return {ReactElement|Array}
29-
*/
30-
module.exports = function(html, options) {
31-
return domToReact(htmlToDOM(html), options);
32-
};
1+
;(function(factory) {
2+
3+
// CommonJS
4+
if (typeof exports === 'object' && typeof module !== 'undefined') {
5+
module.exports = factory();
6+
7+
// RequireJS (AMD)
8+
} else if (typeof define === 'function' && define.amd) {
9+
define([], factory());
10+
11+
// Browser (script tag)
12+
} else {
13+
var root;
14+
if (typeof window !== 'undefined') {
15+
root = window;
16+
} else if (typeof global !== 'undefined') {
17+
root = global;
18+
} else if (typeof self !== 'undefined') {
19+
root = self;
20+
} else {
21+
// works provided we're not in strict mode
22+
root = this;
23+
}
24+
25+
// define namespace
26+
root.HTMLReactParser = factory();
27+
}
28+
29+
})(function() {
30+
31+
var domToReact = require('./lib/dom-to-react');
32+
var htmlToDOM;
33+
34+
// client (browser)
35+
if (typeof window !== 'undefined' && this === window) {
36+
htmlToDOM = require('./lib/html-to-dom-client');
37+
38+
// server (node)
39+
} else {
40+
htmlToDOM = require('./lib/html-to-dom-server');
41+
}
42+
43+
/**
44+
* Convert HTML string to React elements.
45+
*
46+
* @param {String} html - The HTML.
47+
* @param {Object} [options] - The additional options.
48+
* @param {Function} [options.replace] - The replace method.
49+
* @return {ReactElement|Array}
50+
*/
51+
function HTMLReactParser(html, options) {
52+
return domToReact(htmlToDOM(html), options);
53+
}
54+
55+
// source
56+
return HTMLReactParser;
57+
58+
});

0 commit comments

Comments
 (0)