Skip to content

Commit 646b4a8

Browse files
Merge pull request #22 from remarkablemark/webpack
Use webpack to build UMD bundle
2 parents 1774d2c + 22453d0 commit 646b4a8

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
coverage
3+
dist
34
*.swp
45
*.log
56
.DS_Store

index.js

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,31 @@
1-
/* eslint-disable strict */
2-
// UMD template: https://github.com/ForbesLindesay/umd/blob/master/template.js
3-
;(function(factory) { // eslint-disable-line no-extra-semi
4-
5-
// CommonJS
6-
if (typeof exports === 'object' && typeof module !== 'undefined') {
7-
module.exports = factory();
8-
9-
// RequireJS (AMD)
10-
} else if (typeof define === 'function' && define.amd) { // eslint-disable-line no-undef
11-
define([], factory()); // eslint-disable-line no-undef
12-
13-
// Browser (script tag)
14-
} else {
15-
var root;
16-
if (typeof window !== 'undefined') {
17-
root = window;
18-
} else if (typeof global !== 'undefined') {
19-
root = global;
20-
} else if (typeof self !== 'undefined') {
21-
root = self;
22-
} else {
23-
// works provided we're not in strict mode
24-
root = this;
25-
}
26-
27-
// define namespace
28-
root.HTMLReactParser = factory();
29-
}
30-
31-
})(function() {
32-
33-
var domToReact = require('./lib/dom-to-react');
34-
var htmlToDOM;
35-
36-
// client (browser)
37-
if (typeof window !== 'undefined' && this === window) {
38-
htmlToDOM = require('./lib/html-to-dom-client');
39-
40-
// server (node)
41-
} else {
42-
htmlToDOM = require('./lib/html-to-dom-server');
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var domToReact = require('./lib/dom-to-react');
7+
var htmlToDOM = (
8+
process.browser && process.title === 'browser' ?
9+
require('./lib/html-to-dom-client') :
10+
require('./lib/html-to-dom-server')
11+
);
12+
13+
/**
14+
* Convert HTML string to React elements.
15+
*
16+
* @param {String} html - The HTML.
17+
* @param {Object} [options] - The additional options.
18+
* @param {Function} [options.replace] - The replace method.
19+
* @return {ReactElement|Array}
20+
*/
21+
function HTMLReactParser(html, options) {
22+
if (typeof html !== 'string') {
23+
throw new Error('`HTMLReactParser`: The first argument must be a string.');
4324
}
25+
return domToReact(htmlToDOM(html), options);
26+
}
4427

45-
/**
46-
* Convert HTML string to React elements.
47-
*
48-
* @param {String} html - The HTML.
49-
* @param {Object} [options] - The additional options.
50-
* @param {Function} [options.replace] - The replace method.
51-
* @return {ReactElement|Array}
52-
*/
53-
function HTMLReactParser(html, options) {
54-
if (typeof html !== 'string') {
55-
throw new Error('`HTMLReactParser`: The first argument must be a string.');
56-
}
57-
return domToReact(htmlToDOM(html), options);
58-
}
59-
60-
// source
61-
return HTMLReactParser;
62-
63-
});
28+
/**
29+
* Export HTML to React parser.
30+
*/
31+
module.exports = HTMLReactParser;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"author": "Mark <mark@remarkablemark.org>",
66
"main": "index.js",
77
"scripts": {
8+
"build": "NODE_ENV=development webpack index.js dist/html-to-react.js",
9+
"build-min": "NODE_ENV=production webpack -p index.js dist/html-to-react.min.js",
10+
"prepublish": "npm run build && npm run build-min",
811
"test": "mocha",
912
"lint": "eslint index.js \"lib/**\" \"test/**\"",
1013
"cover": "istanbul cover _mocha -- -R spec \"test/**/*\"",
@@ -35,7 +38,8 @@
3538
"jsdomify": "^2.1.0",
3639
"mocha": "^3.0.2",
3740
"react": "*",
38-
"react-dom": "*"
41+
"react-dom": "*",
42+
"webpack": "^1.13.2"
3943
},
4044
"peerDependencies": {
4145
"react": ">=0.14"

webpack.config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
var webpack = require('webpack');
7+
8+
/**
9+
* Export webpack configuration.
10+
*/
11+
module.exports = {
12+
output: {
13+
library: 'HTMLReactParser',
14+
libraryTarget: 'umd'
15+
},
16+
externals: {
17+
'react': {
18+
root: 'React',
19+
commonjs2: 'react',
20+
commonjs: 'react',
21+
amd: 'react'
22+
}
23+
},
24+
plugins: [
25+
new webpack.optimize.OccurrenceOrderPlugin(),
26+
new webpack.DefinePlugin({
27+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
28+
})
29+
]
30+
};

0 commit comments

Comments
 (0)