Skip to content

Commit be4a3f6

Browse files
author
John Conway
authored
Merge pull request #12 from PilotConway/switch-to-library
Switch from CRA to Library Build
2 parents 1a8ec29 + b968247 commit be4a3f6

25 files changed

+1031
-4798
lines changed

.eslintignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/dist
2+
/scripts
3+
/node_modules
4+
/.github
5+
/coverage
6+
/.vscode
7+
babel.config.js
8+
jest.config.js
9+
rollup.config.js

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
/coverage
3+
/dist

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## v0.1.0 (alpha 2)
2+
3+
- [NEW] Converted from create-react-app to a babel/rollup library build.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 John Conway
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

babel.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env', '@babel/preset-react'],
3+
plugins: [
4+
'@babel/transform-runtime',
5+
'@babel/plugin-syntax-dynamic-import',
6+
'@babel/plugin-syntax-import-meta',
7+
'@babel/plugin-proposal-class-properties',
8+
'@babel/plugin-proposal-json-strings',
9+
],
10+
};

jest.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
3+
setupFiles: [],
4+
setupFilesAfterEnv: [],
5+
testMatch: [
6+
'<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
7+
'<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
8+
],
9+
testEnvironment: 'jest-environment-jsdom-fourteen',
10+
transform: {
11+
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
12+
},
13+
transformIgnorePatterns: [
14+
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$',
15+
'^.+\\.module\\.(css|sass|scss)$',
16+
],
17+
modulePaths: [],
18+
moduleFileExtensions: [
19+
'web.js',
20+
'js',
21+
'web.ts',
22+
'ts',
23+
'web.tsx',
24+
'tsx',
25+
'json',
26+
'web.jsx',
27+
'jsx',
28+
'node',
29+
],
30+
watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'],
31+
};

package.json

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,72 @@
11
{
2-
"name": "rxjs-rest-api-with-hooks",
3-
"version": "1.0.0",
4-
"description": "Prototyping a rest api using RxJS ajax and hooks. This project is helping me test out some ideas before we write the new implementation. ",
2+
"name": "@pilotconway/react-request-hook",
3+
"productName": "react-request-hook",
4+
"version": "0.1.0-alpha2",
5+
"description": "Library for communicating with a RESTful server using React Hooks.",
6+
"type": "module",
7+
"main": "dist/index.js",
8+
"module": "dist/esm/index.js",
9+
"browser": "./dist/react-request-hook.umd.js",
510
"keywords": [],
6-
"main": "src/index.js",
7-
"dependencies": {
8-
"@material-ui/core": "4.0.2",
9-
"@material-ui/icons": "^4.1.0",
10-
"color": "3.1.2",
11+
"files": [
12+
"dist"
13+
],
14+
"publishConfig": {
15+
"registry": "https://npm.pkg.github.com/"
16+
},
17+
"peerDependencies": {
1118
"prop-types": "15.7.2",
1219
"react": "16.8.6",
1320
"react-dom": "16.8.6",
14-
"react-scripts": "3.0.1",
1521
"rxjs": "6.5.2"
1622
},
23+
"dependencies": {
24+
"@material-ui/core": "4.0.2",
25+
"@material-ui/icons": "^4.1.0",
26+
"color": "3.1.2"
27+
},
1728
"devDependencies": {
29+
"@babel/cli": "^7.5.0",
30+
"@babel/core": "7.4.3",
31+
"@babel/plugin-syntax-import-meta": "^7.2.0",
32+
"@babel/preset-env": "^7.5.4",
33+
"@babel/preset-react": "^7.0.0",
34+
"babel-eslint": "10.0.1",
35+
"babel-jest": "^24.8.0",
36+
"babel-loader": "8.0.5",
37+
"babel-plugin-named-asset-import": "^0.3.2",
38+
"babel-preset-react-app": "^9.0.0",
1839
"codecov": "^3.5.0",
40+
"eslint": "^5.16.0",
41+
"eslint-config-react-app": "^4.0.1",
42+
"eslint-loader": "2.1.2",
43+
"eslint-plugin-flowtype": "2.50.1",
44+
"eslint-plugin-import": "2.16.0",
45+
"eslint-plugin-jsx-a11y": "6.2.1",
46+
"eslint-plugin-react": "7.12.4",
47+
"eslint-plugin-react-hooks": "^1.5.0",
48+
"file-loader": "3.0.1",
49+
"fs-extra": "7.0.1",
50+
"jest": "^24.8.0",
51+
"jest-environment-jsdom-fourteen": "0.1.0",
52+
"jest-watch-typeahead": "^0.3.1",
1953
"nock": "^10.0.6",
20-
"typescript": "3.3.3"
54+
"rollup": "^1.17.0",
55+
"rollup-plugin-babel": "^4.3.3",
56+
"rollup-plugin-commonjs": "^10.0.1",
57+
"rollup-plugin-eslint": "^7.0.0",
58+
"rollup-plugin-node-builtins": "^2.1.2",
59+
"rollup-plugin-node-resolve": "^5.2.0",
60+
"rollup-plugin-terser": "^5.1.1"
2161
},
2262
"scripts": {
23-
"start": "react-scripts start",
24-
"build": "react-scripts build",
25-
"test": "react-scripts test --env=jsdom --coverage",
26-
"eject": "react-scripts eject"
27-
},
28-
"browserslist": [
29-
">0.2%",
30-
"not dead",
31-
"not ie <= 11",
32-
"not op_mini all"
33-
]
63+
"build": "yarn build:cjs && yarn build:esm && yarn build:es && yarn build:umd && yarn copy-files",
64+
"build:cjs": "NODE_ENV=production BABEL_ENV=cjs babel ./src --out-dir ./dist --ignore \"**/*.spec.js\"",
65+
"build:esm": "NODE_ENV=production BABEL_ENV=esm babel ./src --out-dir ./dist/esm --ignore \"**/*.spec.js\"",
66+
"build:es": "NODE_ENV=production BABEL_ENV=es babel ./src --out-dir ./dist/es --ignore \"**/*.spec.js\"",
67+
"build:umd": "BABEL_ENV=production-umd rollup -c ./rollup.config.js",
68+
"copy-files": "node ./scripts/copy-files.js",
69+
"dist:clean": "rm -rf dist/ && rm -rf node_modules/",
70+
"test": "jest"
71+
}
3472
}

public/index.html

Lines changed: 0 additions & 43 deletions
This file was deleted.

rollup.config.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import resolve from 'rollup-plugin-node-resolve';
2+
import builtins from 'rollup-plugin-node-builtins';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import babel from 'rollup-plugin-babel';
5+
import { eslint } from 'rollup-plugin-eslint';
6+
import { terser } from 'rollup-plugin-terser';
7+
import pkg from './package.json';
8+
9+
const external = ['react', 'react-dom'];
10+
11+
const resolveOptions = {
12+
extensions: ['.js', '.jsx', '.json'],
13+
browser: true,
14+
customResolveOptions: {
15+
moduleDirectory: ['node_modules', '../../node_modules'],
16+
},
17+
};
18+
19+
const commonjsOptions = {
20+
include: /node_modules/,
21+
namedExports: {
22+
'../../node_modules/react-is/index.js': ['ForwardRef'],
23+
},
24+
};
25+
26+
const babelOptions = {
27+
root: './',
28+
exclude: /node_modules/,
29+
runtimeHelpers: true,
30+
};
31+
32+
export default [
33+
// browser-friendly UMD build
34+
{
35+
input: 'src/index.js',
36+
output: {
37+
name: pkg.productName,
38+
file: pkg.browser,
39+
format: 'umd',
40+
},
41+
external,
42+
plugins: [
43+
eslint(),
44+
builtins(),
45+
resolve(resolveOptions),
46+
babel(babelOptions),
47+
commonjs(commonjsOptions),
48+
terser(),
49+
],
50+
},
51+
];

scripts/copy-files.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-disable no-console */
2+
const fsExtra = require('fs-extra');
3+
const path = require('path');
4+
5+
// Copies a file from the root repository into the `./dist` directory.
6+
async function copyFile(filename, destinationFilename = null) {
7+
const dist = path.resolve(__dirname, '../dist/', destinationFilename || path.basename(filename));
8+
await fsExtra.copy(filename, dist);
9+
console.log(`Copied ${filename} to ${dist}`);
10+
}
11+
12+
/**
13+
* Copies the package.json to the `./dist` directory and then edits the
14+
* main/modules entries in the package.json to remove the `dist` portion. This
15+
* allows for the `dist` directory to be published to npm or github package repo.
16+
* The original package.json must have the `dist` portion of the path otherwise
17+
* the package won't work inside workspace directories locally since the root
18+
* directory is what is linked via yarn workspace not the dist directory.
19+
*/
20+
async function copyPackageJson() {
21+
const packageJson = await fsExtra.readFile(path.resolve(__dirname, '../package.json'), 'utf8');
22+
const { scripts, devDependencies, files, ...packageDataOther } = JSON.parse(packageJson);
23+
24+
const newPackageJson = {
25+
...packageDataOther,
26+
main: './index.js',
27+
module: './index.es.js',
28+
};
29+
30+
const output = path.resolve(__dirname, '../dist/package.json');
31+
32+
await fsExtra.writeFile(output, JSON.stringify(newPackageJson, null, 2), 'utf8');
33+
console.log(`Created ${output}`);
34+
}
35+
36+
/**
37+
* Copies files from the root of the project into the built dist directory
38+
* that are needed for the dist to be a publishable package to npm for github
39+
* package repo.
40+
*
41+
* Copies package.json, index.js, README, CHANGELOG, and LICENSE.
42+
*/
43+
async function run() {
44+
try {
45+
await copyFile('./LICENSE');
46+
await copyFile('./CHANGELOG.md');
47+
await copyFile('./README.md');
48+
await copyFile('./src/index.js', 'index.es.js');
49+
await copyPackageJson();
50+
} catch (error) {
51+
console.error('Copy failed', error);
52+
}
53+
}
54+
55+
run();

src/Request/Request.jsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@
2323
* function. You can call these functions to get the first, last, next or previous pages.
2424
* Under the hood it's using the Link header urls to run a new request to get to that page.
2525
*
26-
* ## TODO
27-
*
28-
* Right now we are just passing the entire URL in as the endpoint, but, in production environments, we should
29-
* instead pull the client object from a provider in the hook and then be able to setup the client with
30-
* the url endpoints, headers, sessions, etc... for all client requests. This way, we can just pass something
31-
* like `/users` as the endpoint and it will know where to go.
32-
*
33-
* For browsers, this should be simpler because we will already have the domain and port, but, since this code will
34-
* have to run in electron, we will also have other mechanisms to pass in the domain or change the domain based on
35-
* which backend server (primary or secondary) we are connected to.
36-
*
3726
*/
3827
// import React from 'react';
3928
import PropTypes from 'prop-types';
@@ -91,6 +80,6 @@ Request.defaultProps = {
9180
};
9281

9382
export default function Request({ endpoint, params, options, children }) {
94-
const [data, loading, error, links, client] = useEndpointData(endpoint);
83+
const [data, loading, error, links, client] = useEndpointData(endpoint, { ...options, params });
9584
return children({ data, loading, error, links, client });
9685
}

0 commit comments

Comments
 (0)