From 64f719f1f7498e5753faa9bd3c8586a8c5a9efe3 Mon Sep 17 00:00:00 2001 From: Josh Black Date: Tue, 11 Oct 2022 13:44:08 -0500 Subject: [PATCH] fix(bundle): add entrypoints for nested directories (#2419) * feat(bundle): add exports for test-helpers * chore: update input pattern * chore: ignore theme-preval * chore: add changeset * docs: update comments * Update rollup.config.js * chore: remove fast-glob dep * fix: add support for hooks, polyfills, and utils * chore: add ignore patterns * fix: add constants so all members are exported * fix: update rollup config to not process process.env.NODE_ENV outside of umd build * refactor(rollup): inline babel config to prevent commonjs transform from running --- .changeset/plenty-eagles-refuse.md | 5 ++ package-lock.json | 103 +++++++++++++++++++++++++---- package.json | 2 + rollup.config.js | 90 +++++++++++++++++++++++-- script/build | 2 +- 5 files changed, 182 insertions(+), 20 deletions(-) create mode 100644 .changeset/plenty-eagles-refuse.md diff --git a/.changeset/plenty-eagles-refuse.md b/.changeset/plenty-eagles-refuse.md new file mode 100644 index 00000000000..5e906e2fb50 --- /dev/null +++ b/.changeset/plenty-eagles-refuse.md @@ -0,0 +1,5 @@ +--- +'@primer/react': patch +--- + +Refactor rollup to support all lib-esm/\* export patterns and include utils entrypoint diff --git a/package-lock.json b/package-lock.json index b67cef96499..e029fd8fc10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,6 +47,7 @@ "@rollup/plugin-babel": "5.3.1", "@rollup/plugin-commonjs": "22.0.2", "@rollup/plugin-node-resolve": "14.1.0", + "@rollup/plugin-replace": "5.0.0", "@rollup/plugin-typescript": "8.5.0", "@size-limit/preset-big-lib": "7.0.3", "@storybook/addon-a11y": "6.5.9", @@ -100,6 +101,7 @@ "eslint-plugin-primer-react": "0.7.4", "eslint-plugin-react": "7.24.0", "eslint-plugin-react-hooks": "4.2.0", + "fast-glob": "3.2.12", "front-matter": "4.0.2", "husky": "7.0.4", "jest": "29.0.1", @@ -6474,6 +6476,52 @@ "rollup": "^2.78.0" } }, + "node_modules/@rollup/plugin-replace": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-5.0.0.tgz", + "integrity": "sha512-TiPmjMuBjQM+KLWK16O5TAM/eW4yXBYyQ17FbfeNzBC1t2kzX2aXoa8AlS9XTSmg6/2TNvkER1lMEEeN4Lhavw==", + "dev": true, + "dependencies": { + "@rollup/pluginutils": "^4.2.1", + "magic-string": "^0.26.4" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "rollup": "^1.20.0||^2.0.0||^3.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/plugin-replace/node_modules/@rollup/pluginutils": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz", + "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", + "dev": true, + "dependencies": { + "estree-walker": "^2.0.1", + "picomatch": "^2.2.2" + }, + "engines": { + "node": ">= 8.0.0" + } + }, + "node_modules/@rollup/plugin-replace/node_modules/magic-string": { + "version": "0.26.7", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.26.7.tgz", + "integrity": "sha512-hX9XH3ziStPoPhJxLq1syWuZMxbDvGNbVchfrdCtanC7D13888bMFow61x8axrx+GfHLtVeAx2kxL7tTGRl+Ow==", + "dev": true, + "dependencies": { + "sourcemap-codec": "^1.4.8" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/@rollup/plugin-typescript": { "version": "8.5.0", "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-8.5.0.tgz", @@ -21734,20 +21782,19 @@ "dev": true }, "node_modules/fast-glob": { - "version": "3.2.5", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.5.tgz", - "integrity": "sha512-2DtFcgT68wiTTiwZ2hNdJfcHNke9XOfnwmBRWXhmeKM8rF0TGwmC/Qto3S7RoZKp5cilZbxzO5iTNTQsJ+EeDg==", + "version": "3.2.12", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.12.tgz", + "integrity": "sha512-DVj4CQIYYow0BlaelwK1pHl5n5cRSJfM60UA0zK891sVInoPri2Ekj7+e1CT3/3qxXenpI+nBBmQAcJPJgaj4w==", "dev": true, "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", - "glob-parent": "^5.1.0", + "glob-parent": "^5.1.2", "merge2": "^1.3.0", - "micromatch": "^4.0.2", - "picomatch": "^2.2.1" + "micromatch": "^4.0.4" }, "engines": { - "node": ">=8" + "node": ">=8.6.0" } }, "node_modules/fast-glob/node_modules/braces": { @@ -46177,6 +46224,37 @@ "resolve": "^1.19.0" } }, + "@rollup/plugin-replace": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-replace/-/plugin-replace-5.0.0.tgz", + "integrity": "sha512-TiPmjMuBjQM+KLWK16O5TAM/eW4yXBYyQ17FbfeNzBC1t2kzX2aXoa8AlS9XTSmg6/2TNvkER1lMEEeN4Lhavw==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^4.2.1", + "magic-string": "^0.26.4" + }, + "dependencies": { + "@rollup/pluginutils": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-4.2.1.tgz", + "integrity": "sha512-iKnFXr7NkdZAIHiIWE+BX5ULi/ucVFYWD6TbAV+rZctiRTY2PL6tsIKhoIOaoskiWAkgu+VsbXgUVDNLHf+InQ==", + "dev": true, + "requires": { + "estree-walker": "^2.0.1", + "picomatch": "^2.2.2" + } + }, + "magic-string": { + "version": "0.26.7", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.26.7.tgz", + "integrity": "sha512-hX9XH3ziStPoPhJxLq1syWuZMxbDvGNbVchfrdCtanC7D13888bMFow61x8axrx+GfHLtVeAx2kxL7tTGRl+Ow==", + "dev": true, + "requires": { + "sourcemap-codec": "^1.4.8" + } + } + } + }, "@rollup/plugin-typescript": { "version": "8.5.0", "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-8.5.0.tgz", @@ -57629,17 +57707,16 @@ "dev": true }, "fast-glob": { - "version": "3.2.5", - "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.5.tgz", - "integrity": "sha512-2DtFcgT68wiTTiwZ2hNdJfcHNke9XOfnwmBRWXhmeKM8rF0TGwmC/Qto3S7RoZKp5cilZbxzO5iTNTQsJ+EeDg==", + "version": "3.2.12", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.12.tgz", + "integrity": "sha512-DVj4CQIYYow0BlaelwK1pHl5n5cRSJfM60UA0zK891sVInoPri2Ekj7+e1CT3/3qxXenpI+nBBmQAcJPJgaj4w==", "dev": true, "requires": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", - "glob-parent": "^5.1.0", + "glob-parent": "^5.1.2", "merge2": "^1.3.0", - "micromatch": "^4.0.2", - "picomatch": "^2.2.1" + "micromatch": "^4.0.4" }, "dependencies": { "braces": { diff --git a/package.json b/package.json index 125a8d9072e..6f7a99c4263 100644 --- a/package.json +++ b/package.json @@ -116,6 +116,7 @@ "@rollup/plugin-babel": "5.3.1", "@rollup/plugin-commonjs": "22.0.2", "@rollup/plugin-node-resolve": "14.1.0", + "@rollup/plugin-replace": "5.0.0", "@rollup/plugin-typescript": "8.5.0", "@size-limit/preset-big-lib": "7.0.3", "@storybook/addon-a11y": "6.5.9", @@ -169,6 +170,7 @@ "eslint-plugin-primer-react": "0.7.4", "eslint-plugin-react": "7.24.0", "eslint-plugin-react-hooks": "4.2.0", + "fast-glob": "3.2.12", "front-matter": "4.0.2", "husky": "7.0.4", "jest": "29.0.1", diff --git a/rollup.config.js b/rollup.config.js index d6124961423..73a2dcb8d51 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,15 +1,59 @@ import commonjs from '@rollup/plugin-commonjs' import resolve from '@rollup/plugin-node-resolve' import babel from '@rollup/plugin-babel' +import replace from '@rollup/plugin-replace' +import glob from 'fast-glob' import {terser} from 'rollup-plugin-terser' import visualizer from 'rollup-plugin-visualizer' import packageJson from './package.json' +const input = new Set([ + // "exports" + // "." + 'src/index.ts', + + // "./drafts" + 'src/drafts/index.ts', + + // "./deprecated" + 'src/deprecated/index.ts', + + // Make sure all members are exported + 'src/constants.ts', + + ...glob.sync( + [ + // "./lib-esm/hooks/*" + 'src/hooks/*', + + // "./lib-esm/polyfills/*" + 'src/polyfills/*', + + // "./lib-esm/utils/*" + 'src/utils/*' + ], + { + cwd: __dirname, + ignore: [ + '**/__tests__/**', + '*.stories.tsx', + + // File currently imports from package.json + 'src/utils/test-deprecations.tsx', + + // Files use dependencies which are not listed by package + 'src/utils/testing.tsx', + 'src/utils/test-matchers.tsx' + ] + } + ) +]) + const extensions = ['.js', '.jsx', '.ts', '.tsx'] const external = [ - ...Object.keys(packageJson.peerDependencies), - ...Object.keys(packageJson.dependencies), - ...Object.keys(packageJson.devDependencies) + ...Object.keys(packageJson.peerDependencies ?? {}), + ...Object.keys(packageJson.dependencies ?? {}), + ...Object.keys(packageJson.devDependencies ?? {}) ] function isExternal(external) { return id => { @@ -36,7 +80,7 @@ function isExternal(external) { } const baseConfig = { - input: ['src/index.ts', 'src/drafts/index.ts', 'src/deprecated/index.ts'], + input: Array.from(input), plugins: [ // Note: it's important that the babel plugin is ordered first for plugins // like babel-plugin-preval to work as-intended @@ -44,7 +88,33 @@ const baseConfig = { extensions, exclude: /node_modules/, babelHelpers: 'inline', - babelrc: false + babelrc: false, + configFile: false, + presets: [ + '@babel/preset-typescript', + [ + '@babel/preset-react', + { + modules: false + } + ] + ], + plugins: [ + 'macros', + 'preval', + 'add-react-displayname', + 'babel-plugin-styled-components', + '@babel/plugin-proposal-nullish-coalescing-operator', + '@babel/plugin-proposal-optional-chaining', + [ + 'babel-plugin-transform-replace-expressions', + { + replace: { + __DEV__: "process.env.NODE_ENV !== 'production'" + } + } + ] + ] }), commonjs(), resolve({ @@ -84,7 +154,15 @@ export default [ ...baseConfig, input: 'src/index.ts', external: ['styled-components', 'react', 'react-dom'], - plugins: [...baseConfig.plugins, terser(), visualizer({sourcemap: true})], + plugins: [ + replace({ + 'process.env.NODE_ENV': JSON.stringify('production'), + preventAssignment: true + }), + ...baseConfig.plugins, + terser(), + visualizer({sourcemap: true}) + ], output: ['esm', 'umd'].map(format => ({ file: `dist/browser.${format}.js`, format, diff --git a/script/build b/script/build index 8adcd4aa10e..eabd8e7a692 100755 --- a/script/build +++ b/script/build @@ -6,7 +6,7 @@ set -e npm run clean # Bundle -npx cross-env NODE_ENV=production rollup -c +npx rollup -c # Type check npx tsc --project tsconfig.build.json