Skip to content

Commit 7edc10e

Browse files
authored
Merge pull request #984 from GerkinDev/chore/dependencies-external
build: rollup port of #977
2 parents aae3011 + 8774089 commit 7edc10e

File tree

14 files changed

+847
-1038
lines changed

14 files changed

+847
-1038
lines changed

.github/workflows/bundlesize.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
run: yarn install
1515

1616
- name: Build ReactTooltip component package
17-
run: yarn build
17+
run: yarn build-rollup
1818

1919
- name: Bundlesize
2020
run: yarn run bundlesize

bundlesize.config.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"files": [
33
{
4-
"path": "./dist/react-tooltip.cjs.min.js",
4+
"path": "./dist/react-tooltip.min.cjs",
55
"maxSize": "10 kB"
66
},
77
{
8-
"path": "./dist/react-tooltip.esm.min.js",
8+
"path": "./dist/react-tooltip.min.mjs",
99
"maxSize": "10 kB"
1010
},
1111
{
12-
"path": "./dist/react-tooltip.iife.min.js",
12+
"path": "./dist/react-tooltip.umd.min.js",
1313
"maxSize": "10 kB"
1414
}
1515
]

jest.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default {
133133
// setupFiles: [],
134134

135135
// A list of paths to modules that run some code to configure or set up the testing framework before each test
136-
// setupFilesAfterEnv: [],
136+
setupFilesAfterEnv: ['./src/test/jest-setup.js'],
137137

138138
// The number of seconds after which a test is considered as slow and reported as such in the results.
139139
// slowTestThreshold: 5,

package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
"esbuild": "esbuild",
1717
"test": "jest",
1818
"postbuild": "npm run types && npm run bundlesize",
19-
"prepublishOnly": "npm run build"
19+
"prepublishOnly": "npm run build-rollup"
2020
},
21-
"main": "dist/react-tooltip.cjs.min.js",
22-
"module": "dist/react-tooltip.esm.min.js",
2321
"types": "dist/react-tooltip.d.ts",
2422
"license": "MIT",
2523
"private": false,
@@ -34,6 +32,17 @@
3432
"bugs": {
3533
"url": "https://github.com/ReactTooltip/react-tooltip/issues"
3634
},
35+
"exports": {
36+
".": {
37+
"types": "./dist/react-tooltip.d.ts",
38+
"require": "./dist/react-tooltip.min.cjs",
39+
"import": "./dist/react-tooltip.min.mjs",
40+
"default": "./dist/react-tooltip.min.cjs"
41+
},
42+
"./dist/react-tooltip.css": "./dist/react-tooltip.min.css",
43+
"./dist/react-tooltip.d.ts": "./dist/react-tooltip.d.ts",
44+
"./package.json": "./package.json"
45+
},
3746
"homepage": "https://github.com/ReactTooltip/react-tooltip#readme",
3847
"devDependencies": {
3948
"@rollup/plugin-commonjs": "22.0.2",
@@ -46,6 +55,7 @@
4655
"@types/css": "^0.0.33",
4756
"@types/css-modules": "^1.0.2",
4857
"@types/jest": "29.4.0",
58+
"@types/node": "^18.15.3",
4959
"@types/react": "18.0.28",
5060
"@types/react-dom": "18.0.11",
5161
"@types/react-test-renderer": "^18.0.0",

rollup.config.prod.js

+35-62
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,55 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
77
import ts from '@rollup/plugin-typescript'
88
import { terser } from 'rollup-plugin-terser'
99
import typescript from 'typescript'
10+
import * as pkg from './package.json'
1011

1112
const input = ['src/index.tsx']
1213

1314
const name = 'ReactTooltip'
1415

15-
const external = ['react', 'react-dom', 'prop-types']
16-
17-
const globals = {
18-
react: 'React',
19-
'react-dom': 'ReactDOM',
20-
classnames: 'classNames',
21-
'prop-types': 'PropTypes',
22-
}
16+
const external = [
17+
...Object.keys(pkg.peerDependencies ?? {}),
18+
...Object.keys(pkg.dependencies ?? {}),
19+
]
2320

2421
const buildFormats = [
2522
{
2623
file: 'dist/react-tooltip.umd.js',
2724
format: 'umd',
25+
globals: {
26+
'@floating-ui/dom': 'FloatingUIDOM',
27+
react: 'React',
28+
'react-dom': 'ReactDOM',
29+
classnames: 'classNames',
30+
'prop-types': 'PropTypes',
31+
},
2832
},
2933
{
30-
file: 'dist/react-tooltip.cjs.js',
34+
file: 'dist/react-tooltip.cjs',
3135
format: 'cjs',
3236
},
3337
{
34-
file: 'dist/react-tooltip.esm.js',
38+
file: 'dist/react-tooltip.mjs',
3539
format: 'es',
3640
},
3741
]
3842

39-
// splitted to be reusable by minified css build and unminified css
40-
const pluginsBeforePostCSS = [
43+
const sharedPlugins = [
4144
progress(),
4245
replace({
4346
preventAssignment: true,
4447
values: {
4548
'process.env.NODE_ENV': JSON.stringify('development'),
4649
},
4750
}),
48-
]
49-
50-
// splitted to be reusable by minified css build and unminified css
51-
const pluginsAfterPostCSS = [
51+
postcss({
52+
extract: 'react-tooltip.min.css', // this will generate a specific file and override on multiples build, but the css will be the same
53+
autoModules: true,
54+
include: '**/*.css',
55+
extensions: ['.css'],
56+
plugins: [],
57+
minimize: true,
58+
}),
5259
nodeResolve(),
5360
ts({
5461
typescript,
@@ -61,58 +68,24 @@ const pluginsAfterPostCSS = [
6168
include: 'node_modules/**',
6269
}),
6370
]
64-
65-
const plugins = [
66-
...pluginsBeforePostCSS,
67-
postcss({
68-
// extract: true, // this will generate a css file based on output file name
69-
extract: 'react-tooltip.css', // this will generate a specific file and override on multiples build, but the css will be the same
70-
autoModules: true,
71-
include: '**/*.css',
72-
extensions: ['.css'],
73-
plugins: [],
74-
}),
75-
...pluginsAfterPostCSS,
76-
]
77-
78-
const pluginsForCSSMinification = [
79-
...pluginsBeforePostCSS,
80-
postcss({
81-
extract: 'react-tooltip.min.css', // this will generate a specific file and override on multiples build, but the css will be the same
82-
autoModules: true,
83-
include: '**/*.css',
84-
extensions: ['.css'],
85-
plugins: [],
86-
minimize: true,
87-
}),
88-
...pluginsAfterPostCSS,
89-
]
90-
91-
const defaultOutputData = buildFormats.map(({ file, format }) => ({
92-
file,
93-
format,
94-
plugins: [...plugins, filesize()],
95-
}))
96-
9771
// this step is just to build the minified css and es modules javascript
98-
const minifiedOutputData = buildFormats.map(({ file, format }) => ({
99-
file: file.replace('.js', '.min.js'),
100-
format,
101-
plugins: [...pluginsForCSSMinification, terser(), filesize()],
72+
const minifiedBuildFormats = buildFormats.map(({ file, ...rest }) => ({
73+
file: file.replace(/(\.[cm]?js)$/, '.min$1'),
74+
...rest,
75+
plugins: [terser(), filesize()],
10276
}))
10377

104-
const outputData = [...defaultOutputData, ...minifiedOutputData]
78+
const allBuildFormats = [...buildFormats, ...minifiedBuildFormats]
10579

106-
const config = outputData.map(({ file, format, plugins: specificPLugins }) => ({
80+
const config = {
10781
input,
108-
output: {
109-
file,
110-
format,
82+
output: allBuildFormats.map((buildFormat) => ({
11183
name,
112-
globals,
113-
},
84+
...buildFormat,
85+
sourcemap: true,
86+
})),
11487
external,
115-
plugins: specificPLugins,
116-
}))
88+
plugins: sharedPlugins,
89+
}
11790

11891
export default config

src/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable jsx-a11y/click-events-have-key-events */
33
import { TooltipController as Tooltip } from 'components/TooltipController'
44
import { IPosition } from 'components/Tooltip/TooltipTypes.d'
5-
import { useState } from 'react'
5+
import React, { useState } from 'react'
66
import { inline, offset } from '@floating-ui/dom'
77
import styles from './styles.module.css'
88

src/components/Tooltip/Tooltip.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState, useRef } from 'react'
1+
import React, { useEffect, useState, useRef } from 'react'
22
import classNames from 'classnames'
33
import debounce from 'utils/debounce'
44
import { useTooltip } from 'components/TooltipProvider'

src/components/TooltipContent/TooltipContent.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable react/no-danger */
2+
import React from 'react'
23
import type { ITooltipContent } from './TooltipContentTypes'
34

45
const TooltipContent = ({ content }: ITooltipContent) => {

src/components/TooltipController/TooltipController.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import React, { useEffect, useRef, useState } from 'react'
22
import { Tooltip } from 'components/Tooltip'
33
import type {
44
EventsType,

src/components/TooltipProvider/TooltipWrapper.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef } from 'react'
1+
import React, { useEffect, useRef } from 'react'
22
import classNames from 'classnames'
33
import { useTooltip } from './TooltipProvider'
44
import type { ITooltipWrapper } from './TooltipProviderTypes'

src/index-dev-react-18.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StrictMode, version } from 'react'
1+
import React, { StrictMode, version } from 'react'
22
// eslint-disable-next-line import/no-unresolved
33
import { createRoot } from 'react-dom/client' // this is we are now using react < 18
44
import './tokens.css'

src/test/jest-setup.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react'
2+
// whatever else you need in here
3+
4+
global.React = React

tsconfig.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
"target": "es2018" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
1717
"lib": [
1818
"es2018",
19-
"DOM"
19+
"DOM",
20+
"DOM.iterable"
2021
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
21-
"jsx": "react-jsx" /* Specify what JSX code is generated. */,
22+
"jsx": "react" /* Specify what JSX code is generated. */,
2223
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
2324
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
2425
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
2526
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
26-
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
27+
// "jsxImportSource": "react", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
2728
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
2829
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
2930
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */

0 commit comments

Comments
 (0)