Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit d9e4e8d

Browse files
authored
Merge pull request #59 from zillow/8x
feat!: 8x features and improvements
2 parents 9ab934c + 8e58c30 commit d9e4e8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+6100
-3160
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/coverage
2-
/es
3-
/lib
42
/node_modules
53
/templates

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/.nyc_output
22
/coverage
3-
/es
4-
/lib
53
/node_modules
64
npm-debug.log*
75
.DS_Store

.husky/commit-msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npx --no -- commitlint --edit $1

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npm run eslint && npm run test

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
14

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
if: tag IS blank
33
language: node_js
44
node_js:
5-
- "12"
6-
- "10"
5+
- "14"
76
env:
87
global:
98
- NO_UPDATE_NOTIFIER=1

.versionrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ module.exports = {
1313
{ type: 'refactor', section: 'Code Refactoring' },
1414
{ type: 'test', section: 'Tests' },
1515
{ type: 'build', section: 'Build System' },
16-
{ type: 'ci', section: 'Continuous Integration' }
17-
]
16+
{ type: 'ci', section: 'Continuous Integration' },
17+
],
1818
};

MIGRATE-7-to-8.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Migrate from 7x to 8x
2+
3+
`create-react-styleguide@8` introduced breaking changes around the build processes and config files.
4+
5+
## Package config
6+
7+
You will need to modify output file pointers and dependencies.
8+
9+
## Output files
10+
11+
The new build process uses Rollup and creates 4 bundles, dev and prod for ESM and CJS. An application, which imports your package, will select a correct bundle, based on node environment and bundler capabilities. In example, ESM for Vite or CJS for Webpack 4.
12+
13+
Modify your `package.json` to use the following:
14+
15+
```json
16+
{
17+
"main": "dist/cjs/index.js",
18+
"module": "dist/es/index.js",
19+
"exports": {
20+
".": {
21+
"import": "./dist/es/index.js",
22+
"require": "./dist/cjs/index.js"
23+
}
24+
}
25+
}
26+
```
27+
28+
### Dependencies
29+
30+
All `babel`, `eslint`, and `jest` packages are now required by CRS and not needed as dependencies in your project. Also, all peer dependencies became regular dependencies. Rollup build will exclude them from the final bundle, but they will still be available in `node_modules` as transient dependencies. Please be mindful that a project, which uses your library, can overwrite the dependencies, in example `react` or `lodash` version.
31+
32+
Your project package dependencies should look like this:
33+
34+
```json
35+
{
36+
"dependencies": {
37+
"prop-types": "x.y.z",
38+
"react": "x.y.z",
39+
"react-dom": "x.y.z",
40+
"styled-components": "x.y.z",
41+
"// other-prod-packages": "x.y.z"
42+
},
43+
"devDependencies": {
44+
"create-react-styleguide": "x.y.z",
45+
"husky": "x.y.z",
46+
"react-test-renderer": "x.y.z",
47+
"// other-dev-packages": "x.y.z"
48+
}
49+
}
50+
```
51+
52+
## NEW Rollup config
53+
54+
Add `rollup.config.js` to the root of your project with the snippet from `./templates/base/rollup.config.js`.
55+
56+
Feel free to customize it to your liking. In example:
57+
58+
```diff
59+
const { rollupConfig } = require('create-react-styleguide');
60+
+const yourRollupPlugin = require('your-rollup-plugin');
61+
62+
module.exports = {
63+
...rollupConfig,
64+
+ plugins: [...rollupConfig.plugins, yourRollupPlugin()],
65+
};
66+
```
67+
68+
## NEW Prettier config
69+
70+
Add, or modify, `prettier.config.js` on the root of your project with the snippet from `./templates/base/prettier.config.js`.
71+
72+
## Babel config
73+
74+
Modify your `babel.config.js` and use the snippet from `./templates/base/babel.config.js` as inspiration.
75+
76+
Feel free to customize it to your liking. In example:
77+
78+
```diff
79+
const { babelConfig } = require('create-react-styleguide');
80+
81+
module.exports = {
82+
...babelConfig,
83+
+ presets: [...babelConfig.presets, ['your-babel-preset', {}]],
84+
+ plugins: [...babelConfig.plugins, ['your-babel-plugin', {}]],
85+
};
86+
```
87+
88+
In order to modify `babel-preset-zillow` you will need a more advanced changes, in example:
89+
90+
```js
91+
const { babelConfig, NODE_ENVIRONMENTS } = require('create-react-styleguide');
92+
93+
const { NODE_ENV } = process.env;
94+
const isTest = NODE_ENV === NODE_ENVIRONMENTS.TEST;
95+
96+
// Find our custom `babel-zillow-preset` config
97+
const zillowPreset = babelConfig.presets.find(preset => preset[0] === 'babel-preset-zillow');
98+
99+
// Modify `styled-components` preset config
100+
zillowPreset[1]['styled-components'] = {
101+
...zillowPreset[1]['styled-components'],
102+
namespace: isTest ? 'sc' : `my-library-name`,
103+
};
104+
105+
// Modify `remove-prop-types` preset config
106+
zillowPreset[1].removePropTypes = {
107+
...zillowPreset[1].removePropTypes,
108+
additionalLibraries: [/\/custom-prop-types$/],
109+
};
110+
111+
// Export custom config
112+
module.exports = {
113+
...babelConfig,
114+
presets: [...babelConfig.presets.filter(preset => preset[0] !== zillowPreset[0]), zillowPreset],
115+
};
116+
```
117+
118+
### Remove prop types config
119+
120+
Previously, `prop-types` import and object would remain in the output bundle. In 8x we try our best to remove them in production mode. However, in some components, the import and prop types object persist. In those cases you can annotate a component and force a removal. [See here](https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types#with-comment-annotation) for more information.
121+
122+
```jsx
123+
SomeComponent.propTypes /* remove-proptypes */ = {};
124+
```
125+
126+
## Jest config
127+
128+
Modify your `jest.config.js` and use the snippet from `./templates/base/jest.config.js` as inspiration.
129+
130+
Feel free to customize it to your liking. In example:
131+
132+
```diff
133+
const { jestConfig } = require('create-react-styleguide');
134+
135+
module.exports = {
136+
...jestConfig,
137+
+ setupFilesAfterEnv: [...jestConfig.setupFilesAfterEnv, '<rootDir>/jest.setup.js'],
138+
+ coveragePathIgnorePatterns: [
139+
+ ...jestConfig.coveragePathIgnorePatterns,
140+
+ '<rootDir>/src/thirdparty/',
141+
+ ],
142+
+ coverageThreshold: {
143+
+ global: {
144+
+ branches: 80,
145+
+ functions: 80,
146+
+ lines: 80,
147+
+ statements: 80,
148+
+ },
149+
+ },
150+
};
151+
```

README.md

Lines changed: 86 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ You can see all options for project generation with the following command:
2929
npx create-react-styleguide --help
3030
```
3131

32-
#### `--stable`
32+
### `--stable`
3333

3434
By default, the project will be built with the latest caret (^) version of all dependencies. If this configuration fails, use the `--stable` flag to generate with a known working configuration.
3535

3636
```
3737
npx create-react-styleguide new my-new-styleguide --stable
3838
```
3939

40-
## npm scripts
40+
## NPM Scripts
4141

4242
Generated projects include the following npm scripts out of the box:
4343

4444
| Script | Description |
4545
| ------------------------ | ----------------------------------------------------------- |
4646
| npm start | Start the styleguide server on http://localhost:6060 |
47-
| npm run build | Build the component library to the `lib` folder |
47+
| npm run build | Build the component library to the `dist` folder |
4848
| npm run build:styleguide | Build the styleguide to the `styleguide` folder |
4949
| npm run build:watch | Watch the `src` folder for changes and run the build script |
5050
| npm run clean | Clean generated folders |
@@ -55,36 +55,95 @@ Generated projects include the following npm scripts out of the box:
5555
| npm run test:update | Update unit test snapshots |
5656
| npm run test:watch | Run unit tests while watching for changes |
5757

58-
## Document your styleguide
58+
## Document Styleguide
5959

6060
By default, we expose some meta data from your `package.json` file at the top of your styleguide -- at the very least, make sure you set the `"version"`, `"homepage"`, and `"author"` properties. You can further document your library with markdown files using the [`sections`](https://react-styleguidist.js.org/docs/configuration.html#sections-1) configuration from [React Styleguidist](https://react-styleguidist.js.org/). By convention, additional markdown documentation should go in the `docs` folder so that they get properly packaged when [linking multiple styleguides](#linking-multiple-styleguides).
6161

6262
![Customized style guide](assets/customized.png)
6363

64-
## Adding SVG support
64+
## Modify Rollup Config
6565

66-
You can add SVG support with the [inline-react-svg](https://github.com/airbnb/babel-plugin-inline-react-svg) babel plugin. `npm i --save-dev babel-plugin-inline-react-svg` and then update your `babel.config.js` file as follows:
66+
You can customize Rollup configuration to your requirements. In example, you can update `rollup.config.js` with a sample plugin and a banner for each output file.
6767

6868
```diff
69-
module.exports = {
70-
presets: [['zillow', { modules: false }]],
71-
+ plugins: ['inline-react-svg'],
72-
env: {
73-
cjs: {
74-
presets: ['zillow']
75-
},
76-
test: {
77-
presets: ['zillow']
78-
}
79-
}
80-
};
69+
const { rollupConfig } = require('create-react-styleguide');
70+
+const sampleRollupPlugin = require('sample-rollup-plugin');
71+
72+
module.exports = {
73+
...rollupConfig,
74+
75+
+ output: {
76+
+ ...rollupConfig.output,
77+
+ banner: '/* (c) My Company Inc. */',
78+
+ },
79+
80+
+ plugins: [
81+
+ ...rollupConfig.plugins,
82+
+ sampleRollupPlugin(),
83+
+ ],
84+
};
85+
```
86+
87+
## Modify Babel Config
88+
89+
You can customize Babel configuration to your requirements. In example, you can add SVG support with the [inline-react-svg](https://github.com/airbnb/babel-plugin-inline-react-svg) babel plugin. `npm i --save-dev babel-plugin-inline-react-svg` and then update `babel.config.js` file as follows:
90+
91+
```diff
92+
const { babelConfig } = require('create-react-styleguide');
93+
94+
module.exports = {
95+
...babelConfig,
96+
+ plugins: [
97+
+ ...babelConfig.plugins,
98+
+ 'inline-react-svg',
99+
+ ],
100+
};
81101
```
82102

83103
You should now be able to import and use SVGs as if they were react components!
84104

85-
## Linking multiple styleguides
105+
## Modify Jest Config
106+
107+
You can customize Jest configuration to your requirements. In example, you can update `jest.config.js` with new threshold values.
108+
109+
```diff
110+
const { jestConfig } = require('create-react-styleguide');
111+
112+
module.exports = {
113+
...jestConfig,
114+
+ coverageThreshold: {
115+
+ global: {
116+
+ branches: 80,
117+
+ functions: 80,
118+
+ lines: 80,
119+
+ statements: 80,
120+
+ },
121+
+ },
122+
};
123+
```
124+
125+
## Styleguide Config
126+
127+
Require the module:
86128

87-
A useful feature of create-react-styleguide is the ability to link multiple CRS component libraries into a single project. This means that separate teams can manage and own their own individual CRS libraries, and then bring them all together into a master project for broader visibility.
129+
```js
130+
// styleguide.config.js
131+
const { createStyleguideConfig } = require('create-react-styleguide');
132+
```
133+
134+
### `createStyleguideConfig(config, options)`
135+
136+
Creates a [React Styleguidist configuration object](https://react-styleguidist.js.org/docs/configuration.html) with some default configuration.
137+
138+
- `config [object]` - A configuration object to be shallowly merged with the rest of the configuration
139+
- `options.styleguides [array]` - An array of CRS npm modules (the module must be installed as a dependency to your project)
140+
- `options.packageSection [boolean]` - Include `package.json` details as a top level section (default: `true`)
141+
- `options.packageSectionComponents [boolean]` - Include `components` configuration in the top-level package section (default: `false`)
142+
- `options.componentsSection [boolean]` - Include `components` configuration as its own separate section (default: `true`)
143+
144+
## Linking Styleguides
145+
146+
A useful feature of `create-react-styleguide` is the ability to link multiple CRS component libraries into a single project. This means that separate teams can manage and own their own individual CRS libraries, and then bring them all together into a master project for broader visibility.
88147

89148
For a styleguide to be linked, it must first be published to npm. Running `npm publish` will build and publish your component library so that it can be consumed by the master project.
90149

@@ -107,7 +166,7 @@ That's it! Running `npm start` will now show components from all linked librarie
107166

108167
![Linked style guide](assets/linked.png)
109168

110-
## Deploying your styleguide to GitHub Pages
169+
## Deploying Styleguide to GitHub Pages
111170

112171
Install the `gh-pages` module:
113172

@@ -126,36 +185,11 @@ Add the following scripts to your `package.json`:
126185

127186
Running `npm run deploy` will now deploy your styleguide to Github Pages!
128187

129-
## Node API
130-
131-
Require the module:
132-
133-
```javascript
134-
const { createStyleguideConfig, createJestConfig } = require('create-react-styleguide');
135-
```
136-
137-
### `createStyleguideConfig(config, options)`
138-
139-
Creates a [React Styleguidist configuration object](https://react-styleguidist.js.org/docs/configuration.html) with some default configuration.
140-
141-
- `config [object]` - A configuration object to be shallowly merged with the rest of the configuration
142-
- `options.styleguides [array]` - An array of CRS npm modules (the module must be installed as a dependency to your project)
143-
- `options.packageSection [boolean]` - Include `package.json` details as a top level section (default: `true`)
144-
- `options.packageSectionComponents [boolean]` - Include `components` configuration in the top-level package section (default: `false`)
145-
- `options.componentsSection [boolean]` - Include `components` configuration as its own separate section (default: `true`)
146-
- `options.ie11Transforms [array]` - An array of additional modules that need babel transforms for IE11 compatibility ([#1327](https://github.com/styleguidist/react-styleguidist/pull/1327))
147-
148-
### `createJestConfig(config)`
149-
150-
Creates a [Jest configuration object](https://jestjs.io/docs/en/configuration) with some default configuration.
151-
152-
- `config [object]` - A configuration object to be shallowly merged with the rest of the configuration
153-
154188
## Environment Variables
155189

156190
### `DEBUG`
157191

158-
Both `createStyleguideConfig` and `createJestConfig` will log their results to the console when the `DEBUG` environment variable is set to any non-empty value (such as `true` or `1`). A quick way to see the configuration these functions are creating is to run the following:
192+
All config files will log their results to the console when the `DEBUG` environment variable is set to any non-empty value (such as `true` or `1`). A quick way to see the configuration these functions are creating is to run the following:
159193

160194
```
161195
DEBUG=true node styleguide.config.js
@@ -179,4 +213,8 @@ PORT=12345 npm start
179213

180214
`create-react-styleguide` leverages [react-styleguidist](https://react-styleguidist.js.org/) under the covers for its living style guide.
181215

182-
Builds are created by simple running the `src` directory through [Babel](https://babeljs.io/) using whatever configuration is in your `.babelrc` file. The build will run twice, once with the default configuration which builds ES modules compatible with tree shaking, and once with the `"cjs"` env configuration which builds CommonJS modules.
216+
Builds are created by running the `src` directory through [Rollup](https://rollupjs.org/) and [Babel](https://babeljs.io/). The build will create 4 bundles, dev and prod for CJS and ESM formats. An application, which imports your package, will select a correct bundle, based on node environment and bundler capabilities. In example, ESM for Vite or CJS for Webpack 4.
217+
218+
## Migration Guide from 7x to 8x
219+
220+
Please read our [migration guide](./MIGRATE-7-to-8.md) to streamline your update efforts.

0 commit comments

Comments
 (0)