Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add build step to generate react docgen metadata #4662

Merged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
"/dist/",
"/es/",
"/lib/",
"/build/",
"e2e",
"examples",
"/umd/"
Expand Down
3 changes: 3 additions & 0 deletions packages/react/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Storybook
storybook-static

# Generated metadata
react-docgen.json
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"build": "yarn clean && node scripts/build.js",
"build-storybook": "build-storybook",
"clean": "rimraf es lib umd storybook-static",
"clean": "rimraf es lib umd storybook-static build react-docgen.json",
"prepublish": "yarn build",
"start": "yarn storybook",
"storybook": "rimraf node_modules/.cache/storybook && start-storybook -p 9000"
Expand Down
12 changes: 12 additions & 0 deletions packages/react/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { execSync } = require('child_process');
const { inInstall } = require('in-publish');
const path = require('path');
const fs = require('fs');
const mapValues = require('lodash/mapValues');

if (inInstall()) {
process.exit(0);
Expand Down Expand Up @@ -32,6 +34,16 @@ try {
exec(`${babelPath} src -q -d lib --ignore "${ignoreGlobs}"`, {
BABEL_ENV: 'cjs',
});

// Create docgen metadata
exec(`${babelPath} src -q -d build/docgen --ignore "${ignoreGlobs}"`, {
BABEL_ENV: 'docgen',
});
fs.writeFileSync(
'react-docgen.json',
JSON.stringify(mapValues(require(`../build/docgen`), '__docgenInfo'))
);

exec(
`${rollupPath} -c scripts/rollup.config.js -o umd/carbon-components-react.js`,
{
Expand Down
12 changes: 12 additions & 0 deletions packages/react/scripts/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

const BABEL_ENV = process.env.BABEL_ENV;

const docgenConfig = {
plugins: [
[
'babel-plugin-react-docgen',
{
removeMethods: true,
},
],
],
};

module.exports = () => ({
presets: [
[
Expand All @@ -14,4 +25,5 @@ module.exports = () => ({
},
],
],
...(BABEL_ENV === 'docgen' && docgenConfig),
});