Skip to content

Commit b30ee1e

Browse files
authored
Merge pull request #46 from lewisl9029/esm-externals-sourcemaps
Add ES module output, sourcemaps, minimize bundle size
2 parents 8a93a5c + f4344ab commit b30ee1e

File tree

5 files changed

+1090
-2618
lines changed

5 files changed

+1090
-2618
lines changed

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.3.1",
44
"description": "react library for generating avatar",
55
"main": "dist/index.js",
6+
"module": "dist/index.esm.js",
67
"types": "dist/index.d.ts",
78
"scripts": {},
89
"keywords": [
@@ -16,6 +17,7 @@
1617
"react": ">=16.0.0"
1718
},
1819
"dependencies": {
20+
"@babel/runtime": "^7.14.3",
1921
"chroma-js": "^2.1.2",
2022
"prop-types": "^15.7.2"
2123
},
@@ -47,7 +49,7 @@
4749
"eslint-plugin-react-hooks": "^4.2.0",
4850
"file-loader": "^6.2.0",
4951
"file-saver": "^2.0.5",
50-
"html-webpack-plugin": "^3.2.0",
52+
"html-webpack-plugin": "^5.5.0",
5153
"jest": "^27.0.6",
5254
"mini-css-extract-plugin": "^1.6.2",
5355
"postcss": "^8.3.6",
@@ -67,10 +69,10 @@
6769
"ts-jest": "^27.0.4",
6870
"tsconfig-paths-webpack-plugin": "^3.5.1",
6971
"typescript": "^4.4.2",
70-
"webpack": "^4.41.0",
71-
"webpack-cli": "^3.3.10",
72-
"webpack-dev-server": "^3.8.2",
73-
"webpack-merge": "^4.2.2"
72+
"webpack": "^5.77.0",
73+
"webpack-cli": "^5.0.1",
74+
"webpack-dev-server": "^4.13.1",
75+
"webpack-merge": "^5.8.0"
7476
},
7577
"repository": {
7678
"type": "git",

webpack/base.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const tailwindcss = require("tailwindcss");
44
const webpack = require("webpack");
55
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
66

7-
module.exports = {
7+
module.exports = ({ includeReactHotLoader = false } = {}) => ({
88
context: path.resolve(__dirname, ".."),
99
resolve: {
1010
modules: [
@@ -47,7 +47,7 @@ module.exports = {
4747
"@babel/plugin-proposal-private-methods",
4848
"@babel/plugin-syntax-async-generators",
4949
"@babel/plugin-transform-regenerator",
50-
'react-hot-loader/babel',
50+
...(includeReactHotLoader ? ['react-hot-loader/babel'] : []),
5151
],
5252
},
5353
},
@@ -80,4 +80,4 @@ module.exports = {
8080
}
8181
]
8282
}
83-
};
83+
});

webpack/demo.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const path = require("path");
22
const webpack = require("webpack");
3-
const merge = require("webpack-merge");
3+
const merge = require("webpack-merge").default;
44
const HtmlWebpackPlugin = require("html-webpack-plugin");
55
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
66
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
77

88
const baseConfig = require("./base");
99

10-
module.exports = merge(baseConfig, {
10+
module.exports = merge(baseConfig({ includeReactHotLoader: true }), {
1111
mode: "development",
1212
devtool: "eval-cheap-module-source-map",
1313
resolve: {
@@ -32,11 +32,7 @@ module.exports = merge(baseConfig, {
3232
},
3333
devServer: {
3434
hot: true,
35-
hotOnly: true,
36-
contentBase: path.resolve(__dirname, "../demo/dist"),
37-
publicPath: "/",
3835
historyApiFallback: true,
39-
stats: "minimal",
4036
port: 8080
4137
},
4238
plugins: [

webpack/production.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
const path = require("path");
22
const webpack = require("webpack");
3-
const merge = require("webpack-merge");
3+
const merge = require("webpack-merge").default;
44
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
55
const TerserJSPlugin = require("terser-webpack-plugin");
6+
const packageJson = require("../package.json");
67

78
const baseConfig = require("./base");
89

9-
module.exports = merge(baseConfig, {
10+
const dependencyNameToExternals = (dependencyName) => [
11+
dependencyName,
12+
new RegExp(`^${dependencyName}/.*`),
13+
];
14+
15+
const common = merge(baseConfig(), {
1016
mode: "production",
11-
devtool: false,
17+
devtool: "source-map",
18+
externals: [
19+
...Object.keys(packageJson.dependencies).flatMap(dependencyNameToExternals),
20+
...Object.keys(packageJson.peerDependencies).flatMap(
21+
dependencyNameToExternals
22+
),
23+
],
1224
resolve: {
1325
unsafeCache: true,
1426
plugins: [
@@ -18,11 +30,6 @@ module.exports = merge(baseConfig, {
1830
entry: {
1931
index: path.resolve(__dirname, "../src/index.tsx")
2032
},
21-
output: {
22-
filename: "[name].js",
23-
path: path.resolve(__dirname, "../dist"),
24-
libraryTarget: 'commonjs2'
25-
},
2633
plugins: [
2734
new webpack.DefinePlugin({
2835
"process.env.NODE_ENV": JSON.stringify("production")
@@ -32,5 +39,28 @@ module.exports = merge(baseConfig, {
3239
minimizer: [
3340
new TerserJSPlugin({ extractComments: false })
3441
]
35-
}
36-
});
42+
},
43+
experiments: {
44+
outputModule: true,
45+
},
46+
});
47+
48+
module.exports = [
49+
{
50+
...common,
51+
output: {
52+
filename: "[name].js",
53+
path: path.resolve(__dirname, "../dist"),
54+
libraryTarget: "commonjs2",
55+
},
56+
},
57+
{
58+
...common,
59+
output: {
60+
filename: "[name].esm.js",
61+
path: path.resolve(__dirname, "../dist"),
62+
environment: { module: true },
63+
libraryTarget: "module",
64+
},
65+
},
66+
];

0 commit comments

Comments
 (0)