Skip to content

Commit 574d850

Browse files
armandabricvvo
authored andcommitted
feat(browser): build a dedicated version for the browser (#242)
* feat(browser): build a dedicated version for the browser BREAKING CHANGE: This PR change of the internal directory structure of the exported code. The previous code has move from the `dist/` into the `dist/esm` directory (but remender that we do not avice you to do use internals code 🤓) * fix(bunble): do not bundle peer dependencies * qa(ci): Avoid duplicate runs of checks on CI * qa(dependencies): Upgrade to latest rollup versions * qa(test): Allow to run the smoke tests aggaint all builded versions
1 parent 937a7e9 commit 574d850

File tree

8 files changed

+724
-20
lines changed

8 files changed

+724
-20
lines changed

.travis.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ script:
2020
- yarn run test
2121
- yarn run lint
2222
- yarn run flow
23-
- yarn run smoke 15.6.2
24-
- yarn run smoke 16.1.0
25-
- yarn run smoke default
26-
- yarn run smoke next
23+
- yarn run smoke cjs 15.6.2
24+
- yarn run smoke esm 15.6.2
25+
- yarn run smoke cjs 16.1.0
26+
- yarn run smoke esm 16.1.0
27+
- yarn run smoke cjs default
28+
- yarn run smoke esm default
29+
- yarn run smoke cjs next
30+
- yarn run smoke esm next

dist/cjs/.gitkeep

Whitespace-only changes.

dist/esm/.gitkeep

Whitespace-only changes.

package.json

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22
"name": "react-element-to-jsx-string",
33
"version": "13.2.0",
44
"description": "Turn a ReactElement into the corresponding JSX string.",
5-
"main": "dist/index.js",
5+
"main": "dist/cjs/index.js",
6+
"module": "dist/esm/index.js",
7+
"browser": "dist/cjs/index.js",
68
"scripts": {
7-
"build": "babel ./src/ --ignore=*.spec.js --source-maps --out-dir ./dist/ && flow-copy-source -v --ignore=*.spec.js src/ dist/",
9+
"clean": "npm run esm:clean && npm run cjs:clean",
10+
"build": "npm run esm:build && npm run cjs:build",
11+
"preesm:build": "npm run esm:clean",
12+
"esm:build":
13+
"babel ./src/ --ignore=*.spec.js --source-maps --out-dir ./dist/esm && flow-copy-source -v --ignore=*.spec.js src/ dist/esm",
14+
"esm:clean": "rm -rf ./dist/esm/*",
15+
"precjs:build": "npm run cjs:clean",
16+
"cjs:build": "rollup --config rollup.config.js",
17+
"cjs:clean": "rm -rf ./dist/cjs/*",
818
"lint": "eslint .",
919
"lint:fix": "npm run lint -- --fix",
1020
"flow": "flow",
1121
"precommit": "lint-staged",
1222
"prepublish": "npm run build",
13-
"prebuild": "mkdir -p dist && rm -rf ./dist/*",
14-
"prettier:fix": "prettier --write \"**/*.{js,json}\"",
23+
"prettier:fix":
24+
"prettier --write --single-quote --trailing-comma es5 \"{src/**/*.js,package.json}\"",
1525
"test": "jest",
1626
"test:watch": "jest --watch",
1727
"release": "./release.sh",
@@ -35,6 +45,7 @@
3545
"babel-cli": "6.26.0",
3646
"babel-eslint": "8.2.3",
3747
"babel-jest": "22.2.2",
48+
"babel-plugin-external-helpers": "^6.22.0",
3849
"babel-preset-es2015": "6.24.1",
3950
"babel-preset-flow": "6.23.0",
4051
"babel-preset-react": "6.24.1",
@@ -59,10 +70,20 @@
5970
"mversion": "1.10.1",
6071
"prettier": "1.8.2",
6172
"react": "16.3.2",
62-
"react-test-renderer": "16.3.2"
73+
"react-dom": "16.3.2",
74+
"react-test-renderer": "16.3.2",
75+
"rollup": "0.58.2",
76+
"rollup-plugin-babel": "3.0.4",
77+
"rollup-plugin-commonjs": "9.1.3",
78+
"rollup-plugin-flow": "1.1.1",
79+
"rollup-plugin-node-builtins": "2.1.2",
80+
"rollup-plugin-node-globals": "1.2.1",
81+
"rollup-plugin-node-resolve": "3.3.0",
82+
"rollup-plugin-sourcemaps": "0.4.2"
6383
},
6484
"peerDependencies": {
65-
"react": "^0.14.8 || ^15.0.1 || ^16.0.0"
85+
"react": "^0.14.8 || ^15.0.1 || ^16.0.0",
86+
"react-dom": "^0.14.8 || ^15.0.1 || ^16.0.0"
6687
},
6788
"dependencies": {
6889
"is-plain-object": "2.0.4",

rollup.config.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import fs from 'fs';
2+
import babel from 'rollup-plugin-babel';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import flow from 'rollup-plugin-flow';
5+
import resolve from 'rollup-plugin-node-resolve';
6+
import builtins from 'rollup-plugin-node-builtins';
7+
import globals from 'rollup-plugin-node-globals';
8+
9+
const extractPackagePeerDependencies = () => {
10+
const packageNpm = JSON.parse(
11+
fs.readFileSync('./package.json', { encoding: 'utf8' })
12+
);
13+
14+
return Object.keys(packageNpm.peerDependencies || {});
15+
};
16+
17+
export default {
18+
input: 'src/index.js',
19+
output: {
20+
file: 'dist/cjs/index.js',
21+
format: 'cjs',
22+
sourcemap: true,
23+
},
24+
external: extractPackagePeerDependencies(),
25+
plugins: [
26+
babel({
27+
babelrc: false,
28+
exclude: 'node_modules/**', // only transpile our source code
29+
presets: [
30+
[
31+
'es2015',
32+
{
33+
modules: false,
34+
},
35+
],
36+
'stage-2',
37+
'react',
38+
'flow',
39+
],
40+
plugins: ['external-helpers'],
41+
externalHelpers: true,
42+
}),
43+
resolve({
44+
module: true,
45+
jsnext: true,
46+
main: true,
47+
browser: true,
48+
}),
49+
commonjs({
50+
sourceMap: true,
51+
namedExports: {
52+
react: ['isValidElement'],
53+
},
54+
}),
55+
globals(),
56+
builtins(),
57+
flow(),
58+
],
59+
};

tests/smoke/run.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33
const execFileSync = require('child_process').execFileSync;
44
const path = require('path');
55

6-
execFileSync(path.join(__dirname, 'prepare.js'), [process.argv[2]], {
6+
const buildType = process.argv[2];
7+
if (!buildType) {
8+
throw new Error('The build type to test is missing');
9+
}
10+
11+
const requestedReactVersion = process.argv[3];
12+
if (!requestedReactVersion) {
13+
throw new Error('React version to use for the test is missing');
14+
}
15+
16+
execFileSync(path.join(__dirname, 'prepare.js'), [requestedReactVersion], {
717
cwd: __dirname,
818
stdio: 'inherit',
919
});
1020

11-
execFileSync(path.join(__dirname, 'smoke.js'), {
21+
execFileSync(path.join(__dirname, 'smoke.js'), [buildType], {
1222
cwd: __dirname,
1323
stdio: 'inherit',
1424
});

tests/smoke/smoke.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
/* eslint-disable no-console */
44
/* eslint-disable import/no-extraneous-dependencies */
55

6+
const requireReactElementToJsxString = buildType => {
7+
if (buildType === 'esm') {
8+
return require(`./../../dist/esm`).default;
9+
} else if (buildType === 'cjs') {
10+
return require('./../../dist/cjs');
11+
}
12+
13+
throw new Error(`Unknown build type: "${buildType}"`);
14+
};
15+
616
const expect = require('expect');
717
const React = require('react');
8-
const reactElementToJsxString = require('./../../dist/').default;
18+
const reactElementToJsxString = requireReactElementToJsxString(process.argv[2]);
919

1020
console.log(`Tested "react" version: "${React.version}"`);
1121

0 commit comments

Comments
 (0)