Skip to content

feat: add esbuild as bundler #959

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

Merged
merged 21 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
1fcffbd
feat: add esbuild config for dev and production envs
danielbarion Feb 22, 2023
f280dfb
refactor: update classNames to camelcase because of esbuild
danielbarion Feb 22, 2023
6322ba7
chore: add script to use esbuild as bundler
danielbarion Feb 22, 2023
f836503
chore: add a new html to rollup and update the existent one to work w…
danielbarion Feb 22, 2023
6ac6621
chore: raise version of react to test docs with the package linked
danielbarion Feb 22, 2023
2f2fcda
chore: add a create-react-app folder to test the new esbuild
danielbarion Feb 22, 2023
a82b5f9
chore: add postbuild to generate the types after esbuild script build
danielbarion Mar 2, 2023
a80a8c2
feat: add source map to generated files
danielbarion Mar 3, 2023
171e138
chore: update esbuild and bundlesize
danielbarion Mar 4, 2023
4c85f08
chore: remove cra-app and revert react version to v16
danielbarion Mar 4, 2023
5749584
chore: upgrade packages of react-tooltip and docs
danielbarion Mar 4, 2023
18e0314
fix: remove support to node 14 in github action due to dependencies
danielbarion Mar 4, 2023
cbf169d
refactor: cli.js -> prebuild.js
gabrieljablonski Mar 5, 2023
9daa533
feat: live reload on dev environment
gabrieljablonski Mar 5, 2023
9c3c9f0
build: umd -> iife on package.json
gabrieljablonski Mar 5, 2023
af53118
build: throw build error instead of silent fail
gabrieljablonski Mar 5, 2023
5033845
refactor: `forEach`/`push` -> `map`
gabrieljablonski Mar 5, 2023
a57422e
build: improve postbuild cleanup
gabrieljablonski Mar 5, 2023
d434781
chore: refactor package.json to remove buildformats from it
danielbarion Mar 5, 2023
a6bd00b
docs: update bundlesize comparison
gabrieljablonski Mar 5, 2023
d0863fc
docs: link images to bundlephobia
gabrieljablonski Mar 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
needs: lint
strategy:
matrix:
version: [14, 16, 18]
version: [16, 18]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ yarn-error.log
!.babelrc
!.gitignore
!.npmignore

# yalc
/.yalc

8 changes: 4 additions & 4 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"files": [
{
"path": "./dist/react-tooltip.cjs.min.js",
"maxSize": "13.5 kB"
"maxSize": "10 kB"
},
{
"path": "./dist/react-tooltip.esm.min.js",
"maxSize": "13.5 kB"
"maxSize": "10 kB"
},
{
"path": "./dist/react-tooltip.umd.min.js",
"maxSize": "13.5 kB"
"path": "./dist/react-tooltip.iife.min.js",
"maxSize": "10 kB"
}
]
}
5 changes: 5 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*


# yalc
/.yalc
yalc.lock
6 changes: 3 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"clsx": "^1.2.1",
"prism-react-renderer": "^1.3.5",
"raw-loader": "^4.0.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-tooltip": "5.8.3"
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-tooltip": "5.8.4-beta.2"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "2.3.0",
Expand Down
12 changes: 6 additions & 6 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6025,7 +6025,7 @@ react-dev-utils@^12.0.1:
strip-ansi "^6.0.1"
text-table "^0.2.0"

[email protected]:
react-dom@^18.2.0:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz#22aaf38708db2674ed9ada224ca4aa708d821e3d"
integrity sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==
Expand Down Expand Up @@ -6125,15 +6125,15 @@ react-textarea-autosize@^8.3.2:
use-composed-ref "^1.3.0"
use-latest "^1.2.1"

[email protected].3:
version "5.8.3"
resolved "https://registry.yarnpkg.com/react-tooltip/-/react-tooltip-5.8.3.tgz#d9c94eafb6c3d4d7be43b4acfe6f3cbde82df9ab"
integrity sha512-h7maAlm2Xeymc14gWKhhrzsENeB83N65EzZ+AcQIGrOpNE0yefVRJIHhNcWHEJ0FEtf7VZXxtsj5glVXKxEtvA==
[email protected].4-beta.2:
version "5.8.4-beta.2"
resolved "https://registry.yarnpkg.com/react-tooltip/-/react-tooltip-5.8.4-beta.2.tgz#5a553ea1101e492f60ea0075df37cb7326d7f4aa"
integrity sha512-GVdpVwbbm+fMIQKiWYJBWpRJ3kYSvzWr/A9j8JUQuEmxSW/JJ+Lj+6jJcF7oVU9iA9tgkbzd+tg52f5GukxfNg==
dependencies:
"@floating-ui/dom" "1.1.1"
classnames "^2.3.2"

[email protected]:
react@^18.2.0:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5"
integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==
Expand Down
30 changes: 30 additions & 0 deletions esbuild.config.dev.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as esbuild from 'esbuild'
import cssModulesPlugin from 'esbuild-css-modules-plugin'
import fs from 'fs'

const ctx = await esbuild.context({
entryPoints: ['./src/index-dev.tsx'],
bundle: true,
outdir: 'build',
treeShaking: true,
logLevel: 'info',
plugins: [
cssModulesPlugin({
v2: true,
v2CssModulesOption: {
pattern: `react-tooltip__[local]_[hash]`,
},
}),
],
})

fs.copyFile('./public/index.html', './build/index.html', (err) => {
if (err) throw err
})

await ctx.watch()
await ctx.serve({
servedir: 'build',
port: 3000,
host: 'localhost',
})
97 changes: 97 additions & 0 deletions esbuild.config.prod.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as esbuild from 'esbuild'
import cssModulesPlugin from 'esbuild-css-modules-plugin'
import fs from 'fs'

const buildsConfig = [
{
format: 'esm',
outfile: 'dist/react-tooltip.esm.js',
minify: false,
},
{
format: 'cjs',
outfile: 'dist/react-tooltip.cjs.js',
minify: false,
},
{
format: 'iife',
outfile: 'dist/react-tooltip.iife.js',
minify: false,
},
{
format: 'esm',
outfile: 'dist/react-tooltip.js', // for styles be exported as `react-tooltip.css`
minify: false,
},
{
format: 'esm',
outfile: 'dist/react-tooltip.esm.min.js',
minify: true,
},
{
format: 'cjs',
outfile: 'dist/react-tooltip.cjs.min.js',
minify: true,
},
{
format: 'iife',
outfile: 'dist/react-tooltip.iife.min.js',
minify: true,
},
{
format: 'esm',
outfile: 'dist/react-tooltip.min.js',
minify: true,
},
]

const builds = await Promise.all(
buildsConfig.map(({ format, outfile, minify }) =>
esbuild.build({
entryPoints: ['./src/index.tsx'],
bundle: true,
outfile,
format,
treeShaking: true,
minify,
sourcemap: true,
external: ['react', 'react-dom', 'prop-types'],
plugins: [
cssModulesPlugin({
// inject: true,
v2: true,
v2CssModulesOption: {
pattern: `react-tooltip__[local]_[hash]`,
},
}),
],
}),
),
)

const toDelete = new Set()
builds.forEach((build) => {
const outputs = Object.keys(build.metafile.outputs)
outputs.forEach((output) => {
/**
* delete all redundant `.css` and `.css.map` files
* except the ones we actually want to keep
*/
if (/react-tooltip\.css(\.map)?$/.test(output)) {
return
}
if (/\.css(\.map)?$/.test(output)) {
toDelete.add(output)
}
})
})

/**
* delete the extra build files from the CSS build
*/
toDelete.add('dist/react-tooltip.js')
toDelete.add('dist/react-tooltip.js.map')

toDelete.forEach((file) => {
fs.unlink(file, () => null)
})
57 changes: 32 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
{
"name": "react-tooltip",
"version": "5.8.3",
"version": "5.8.4-beta.2",
"description": "react tooltip component",
"scripts": {
"dev": "node ./cli.js --env=development && node --max_old_space_size=2048 ./node_modules/rollup/dist/bin/rollup -c rollup.config.dev.js --watch",
"build": "node ./cli.js --env=production && npm run types && node --max_old_space_size=2048 ./node_modules/rollup/dist/bin/rollup -c rollup.config.prod.js && npm run bundlesize",
"dev-rollup": "node ./prebuild.js --env=development && node --max_old_space_size=2048 ./node_modules/rollup/dist/bin/rollup -c rollup.config.dev.js --watch",
"build-rollup": "node ./prebuild.js --env=production && npm run types && node --max_old_space_size=2048 ./node_modules/rollup/dist/bin/rollup -c rollup.config.prod.js && npm run bundlesize",
"dev": "node ./prebuild.js --env=development && node ./esbuild.config.dev.mjs",
"build": "node ./prebuild.js --env=production && node ./esbuild.config.prod.mjs",
"types": "node --max_old_space_size=2048 ./node_modules/rollup/dist/bin/rollup -c rollup.config.types.js",
"eslint": "eslint --ext=js --ext=jsx --ext=ts --ext=tsx --fix ./src",
"stylelint": "stylelint \"src/**/*.css\"",
"prettier": "prettier --config ./.prettierrc.json --write \"src/**/*{.js,.jsx,.ts,.tsx,.css}\"",
"prepare": "husky install",
"bundlesize": "bundlesize",
"test": "jest"
"esbuild": "esbuild",
"test": "jest",
"postbuild": "npm run types && npm run bundlesize",
"prepublishOnly": "npm run build"
},
"main": "dist/react-tooltip.cjs.min.js",
"module": "dist/react-tooltip.esm.min.js",
"buildFormats": [
{
"file": "dist/react-tooltip.umd.js",
"format": "umd"
"file": "dist/react-tooltip.iife.js",
"format": "iife"
},
{
"file": "dist/react-tooltip.cjs.js",
Expand Down Expand Up @@ -55,28 +60,30 @@
"@types/css": "^0.0.33",
"@types/css-modules": "^1.0.2",
"@types/jest": "29.4.0",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.0.6",
"@types/react": "18.0.28",
"@types/react-dom": "18.0.11",
"@types/react-test-renderer": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.42.1",
"@typescript-eslint/eslint-plugin": "5.54.0",
"@typescript-eslint/parser": "5.54.0",
"bundlesize": "^0.18.1",
"css-loader": "6.7.2",
"eslint": "8.28.0",
"css-loader": "6.7.3",
"esbuild": "0.17.11",
"esbuild-css-modules-plugin": "^2.7.1",
"eslint": "8.35.0",
"eslint-config-airbnb": "19.0.4",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-config-prettier": "8.6.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jsx-a11y": "6.7.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "7.31.11",
"eslint-plugin-react": "7.32.2",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^8.0.1",
"jest": "29.4.2",
"jest-environment-jsdom": "29.4.2",
"husky": "8.0.3",
"jest": "29.4.3",
"jest-environment-jsdom": "29.4.3",
"jest-transform-css": "6.0.1",
"lint-staged": "13.0.3",
"postcss": "8.4.19",
"prettier": "^2.4.1",
"lint-staged": "13.1.2",
"postcss": "8.4.21",
"prettier": "2.8.4",
"process": "^0.11.10",
"prop-types": "^15.7.2",
"react": "16.14.0",
Expand All @@ -86,14 +93,14 @@
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-browsersync": "^1.3.3",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-dts": "^5.0.0",
"rollup-plugin-dts": "5.2.0",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-html-scaffold": "^0.2.0",
"rollup-plugin-postcss": "^4.0.1",
"rollup-plugin-progress": "^1.1.2",
"rollup-plugin-string": "^3.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-visualizer": "^5.5.2",
"rollup-plugin-visualizer": "5.9.0",
"style-loader": "^3.3.0",
"stylelint": "^13.13.1",
"stylelint-config-prettier": "^8.0.2",
Expand All @@ -119,7 +126,7 @@
]
},
"dependencies": {
"@floating-ui/dom": "1.1.1",
"@floating-ui/dom": "1.2.3",
"classnames": "^2.3.2"
}
}
7 changes: 5 additions & 2 deletions cli.js → prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ const bundleFolder = {
// Build folder
const dir = bundleFolder[parameters.env]

const log = (message) => console.log(`### ${message} ###`)
const log = (message) => {
// eslint-disable-next-line no-console
console.log(`### ${message} ###`)
}

log(`Building for env: ${parameters.env}`)

// check if directory exists
if (fs.existsSync(dir)) {
rimraf(dir, function () {
rimraf(dir, () => {
fs.mkdirSync(dir)
})
} else {
Expand Down
19 changes: 19 additions & 0 deletions public/index-rollup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#009ee2" />

<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Dev - React Tooltip</title>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app. Please enable JavaScript 😭</noscript>

<div id="app"></div>

<script type="module" src="<%= appBundle %>"></script>
</body>
</html>
7 changes: 5 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Dev - React Tooltip</title>
<link rel="stylesheet" href="index.css" />
<link rel="stylesheet" href="/index-dev.css" />
<script>
new EventSource('/esbuild').addEventListener('change', () => location.reload())
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app. Please enable JavaScript 😭</noscript>

<div id="app"></div>

<script type="module" src="<%= appBundle %>"></script>
<script type="module" src="/index-dev.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion rollup.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const globals = {
const plugins = [
progress(),
html({
input: './public/index.html',
input: './public/index-rollup.html',
output: './build/index.html',
template: { appBundle: 'index.js' },
}),
Expand Down
Loading