Skip to content

Commit

Permalink
Fixes #124: add es5 build
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Alford committed Jun 7, 2020
1 parent d0ff9d3 commit 24a9baf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,10 @@ JsxParser.defaultProps = {
renderUnrecognized: tagName => null, // unrecognized tags are rendered via this method
}
```

## Older Browser Support

If your application needs to support older browsers, like `IE11`, import from `react-jsx-parser/lib/es5/react-jsx-parser.min.js`,
which transpiles the `acorn-jsx` dependency down to ES5, and also adds additional polyfill support for code used in this package.

**Note**: <u>not</u> recommended for implementations which only support modern browsers, as the ES5 version is roughly 30% larger.
2 changes: 2 additions & 0 deletions lib/es5/react-jsx-parser.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/es5/react-jsx-parser.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"version": "1.25.0",
"dependencies": {
"acorn": "^7.2.0",
"acorn-jsx": "^5.2.0"
"acorn-jsx": "^5.2.0",
"core-js": "^3.6.5"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
14 changes: 10 additions & 4 deletions source/lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ jest.unmock('../lib/cjs/react-jsx-parser.min')
jest.unmock('../lib/umd/react-jsx-parser.min')

describe('JSXParser', () => {
describe('cjs', () => {
it('should work without window object', () => {
describe('cjs build', () => {
it('should load and parse', () => {
const fn = () => require('../lib/cjs/react-jsx-parser.min')
expect(fn).not.toThrow()
})
})
describe('umd', () => {
it('should work without window object', () => {
describe('es5 build', () => {
it('should load and parse', () => {
const fn = () => require('../lib/es5/react-jsx-parser.min')
expect(fn).not.toThrow()
})
})
describe('umd build', () => {
it('should load and parse', () => {
const fn = () => require('../lib/umd/react-jsx-parser.min')
expect(fn).not.toThrow()
})
Expand Down
38 changes: 31 additions & 7 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ const buildTarget = {
'react-dom': 'react-dom',
},
mode: ENVIRONMENT,
module: {
rules: [{
test: /\.tsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
}],
},
optimization: {
minimize: PRODUCTION,
minimizer: [new TerserPlugin()],
Expand All @@ -51,10 +44,41 @@ const buildTarget = {

const TYPES = {
cjs: 'commonjs2',
es5: 'commonjs2',
umd: 'umd',
}

const babelEnvEs5 = ['@babel/preset-env', {
corejs: '3.6.5',
modules: 'commonjs',
targets: { chrome: '58', ie: '11' },
useBuiltIns: 'usage',
}]

module.exports = Object.entries(TYPES).map(([typeName, libraryTarget]) => ({
...buildTarget,
module: {
rules: [{
exclude: (
typeName === 'es5'
? /node_modules\/(?!(acorn-jsx)\/).*/
: /node_modules/
),
test: /\.[jt]sx?$/,
use: {
loader: 'babel-loader',
options: {
plugins: ['@babel/plugin-proposal-class-properties'],
presets: [
typeName === 'es5' ? babelEnvEs5 : '@babel/preset-env',
'@babel/preset-react',
'@babel/preset-typescript',
],
highlightCode: true,
},
},
}],
},
output: {
...buildTarget.output,
libraryTarget,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,11 @@ core-js-pure@^3.0.0:
resolved "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.6.5.tgz#c79e75f5e38dbc85a662d91eea52b8256d53b813"
integrity sha512-lacdXOimsiD0QyNf9BC/mxivNJ/ybBGJXQFKzRekp1WTHoVUWsUHEn+2T8GJAzzIhyOuXA+gOxCVN3l+5PLPUA==

core-js@^3.6.5:
version "3.6.5"
resolved "https://registry.npmjs.org/core-js/-/core-js-3.6.5.tgz#7395dc273af37fb2e50e9bd3d9fe841285231d1a"
integrity sha512-vZVEEwZoIsI+vPEuoF9Iqf5H7/M3eeQqWlQnYa8FSKKePuYTf5MWnxb5SDAzCa60b3JBRS5g9b+Dq7b1y/RCrA==

[email protected], core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
Expand Down

0 comments on commit 24a9baf

Please sign in to comment.