Skip to content

Commit c64e038

Browse files
authored
fix(build): Add minified ESM builds (#252)
* Add minified esm build * Update README with available bundles * Update package lock
1 parent 1a8e7b0 commit c64e038

File tree

5 files changed

+184
-11
lines changed

5 files changed

+184
-11
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ yarn-error.log
7070

7171
# builded code
7272
dist
73+
min
7374
en
75+
en-min
7476
e2e_test
7577
locales.js

README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ required to handle uploads.
2121

2222
* [Install](#install)
2323
* [Usage](#usage)
24+
* [Available bundles](#available-bundles)
2425
* [Configuration](#configuration)
2526
* [Component configuration](#component-configuration)
2627
* [Widget configuration](#widget-configuration)
@@ -38,12 +39,6 @@ npm i @uploadcare/react-widget
3839
```jsx
3940
import { Widget } from "@uploadcare/react-widget";
4041

41-
/*
42-
* Also, you can save 30% in bundle size by using English-only version:
43-
*
44-
* import { Widget } from "@uploadcare/react-widget/en";
45-
*/
46-
4742
<Widget publicKey="YOUR_PUBLIC_KEY" />;
4843
```
4944

@@ -60,6 +55,14 @@ set.
6055

6156
You can refer to our [integration guide][react-guide] for more details.
6257

58+
## Available bundles
59+
By default, npm and other package managers import the full (all locales) CommonJS or ESM bundle.
60+
61+
To reduce your bundle size, you can also import one of the following:
62+
* The english-only bundle (saves ~27% in bundle size) as `@uploadcare/react-widget/en`
63+
* The minified all-locales bundle (saves ~44% in bundle size) as `@uploadcare/react-widget/min`
64+
* The minified english-only bundle (saves ~60% in bundle size) as `@uploadcare/react-widget/en-min/`
65+
6366
## Configuration
6467

6568
### Component configuration

package-lock.json

+93-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"lint:types": "dtslint types --onlyTestTsNext",
1111
"lint:js": "eslint ./src/** ./test/** rollup.config.js",
12-
"clean": "rimraf dist en",
12+
"clean": "rimraf dist en min en-min",
1313
"build": "rollup -c",
1414
"dummy": "parcel dummy/index.html",
1515
"prepublishOnly": "npm run clean && npm run build",
@@ -21,7 +21,9 @@
2121
},
2222
"files": [
2323
"dist/*",
24+
"min/*",
2425
"en/*",
26+
"en-min/*",
2527
"locales.js",
2628
"types/index.d.ts"
2729
],
@@ -98,6 +100,7 @@
98100
"rollup": "2.43.1",
99101
"rollup-plugin-babel": "4.4.0",
100102
"rollup-plugin-module-replacement": "1.2.1",
103+
"rollup-plugin-terser": "7.0.2",
101104
"shipjs": "0.23.1",
102105
"typescript": "4.2.3",
103106
"uploadcare-widget-tab-effects": "1.4.7"

rollup.config.js

+76-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import babel from 'rollup-plugin-babel'
22
import commonjs from '@rollup/plugin-commonjs'
33
import replace from '@rollup/plugin-replace'
44
import replacement from 'rollup-plugin-module-replacement'
5+
import { terser } from 'rollup-plugin-terser'
56

67
import uploadcare from 'uploadcare-widget'
78

89
export default [
9-
// esm build width all locales
10+
// esm build with all locales
1011
{
1112
input: 'src/index.js',
1213
external: [
@@ -32,7 +33,7 @@ export default [
3233
]
3334
},
3435

35-
// cjs build width all locales
36+
// cjs build with all locales
3637
{
3738
input: 'src/index.js',
3839
external: [
@@ -58,7 +59,43 @@ export default [
5859
]
5960
},
6061

61-
// esm build width en locale (30% smaller)
62+
// minified esm build with all locales
63+
// Saves ~13 KB on react-widget and ~227 KB on uploadcare-widget
64+
{
65+
input: 'src/index.js',
66+
external: [
67+
'react',
68+
'react-fast-compare',
69+
'uploadcare-widget/uploadcare.min',
70+
'@uploadcare/client-suspense'
71+
],
72+
output: {
73+
format: 'esm',
74+
dir: 'min',
75+
sourcemap: false
76+
},
77+
plugins: [
78+
replacement({
79+
entries: [
80+
{
81+
find: 'uploadcare-widget',
82+
replacement: 'uploadcare-widget/uploadcare.min'
83+
}
84+
]
85+
}),
86+
commonjs({
87+
include: /node_modules/,
88+
sourceMap: false
89+
}),
90+
babel({
91+
exclude: 'node_modules/**',
92+
presets: [['@babel/env', { modules: false }]]
93+
}),
94+
terser()
95+
]
96+
},
97+
98+
// esm build with en locale only (saves ~147 KB on uploadcare-widget)
6299
{
63100
input: 'src/index.js',
64101
external: [
@@ -92,6 +129,42 @@ export default [
92129
]
93130
},
94131

132+
// minified esm build with en locale only
133+
// Saves ~13 KB on react-widget and ~314 KB on uploadcare-widget
134+
{
135+
input: 'src/index.js',
136+
external: [
137+
'react',
138+
'react-fast-compare',
139+
'uploadcare-widget/uploadcare.lang.en.min',
140+
'@uploadcare/client-suspense'
141+
],
142+
output: {
143+
format: 'esm',
144+
dir: 'en-min',
145+
sourcemap: false
146+
},
147+
plugins: [
148+
replacement({
149+
entries: [
150+
{
151+
find: 'uploadcare-widget',
152+
replacement: 'uploadcare-widget/uploadcare.lang.en.min'
153+
}
154+
]
155+
}),
156+
commonjs({
157+
include: /node_modules/,
158+
sourceMap: false
159+
}),
160+
babel({
161+
exclude: 'node_modules/**',
162+
presets: [['@babel/env', { modules: false }]]
163+
}),
164+
terser()
165+
]
166+
},
167+
95168
{
96169
input: 'src/langs.js',
97170
output: {

0 commit comments

Comments
 (0)