Skip to content

Commit 50f0b44

Browse files
committed
refactor: update webpack config
1 parent 680b828 commit 50f0b44

File tree

2 files changed

+59
-53
lines changed

2 files changed

+59
-53
lines changed

packages/components/button/webpack.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import { getWebpackConfig } from "@rck/webpack-config";
22
import packageJson from "./package.json";
33

44
export default getWebpackConfig({
5-
packageName: packageJson.name,
5+
cssHashSalt: packageJson.name,
66
});

packages/config/webpack/src/index.ts

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,69 @@ import { type Configuration } from "webpack";
44
// CSS MODULES - https://webpack.js.org/loaders/css-loader/#modules
55
// Optimization - https://webpack.js.org/guides/build-performance/
66

7-
const mode: Configuration["mode"] =
8-
process.env.NODE_ENV === "development" || process.env.NODE_ENV === "production"
9-
? process.env.NODE_ENV
10-
: (() => {
11-
throw new Error("Invalid webpack mode");
12-
})();
13-
147
export interface WebpackConfigOptions {
15-
packageName: string;
8+
mode?: Configuration["mode"];
9+
cssHashSalt?: string;
1610
}
1711

18-
export const getWebpackConfig = ({ packageName }: WebpackConfigOptions): Configuration => ({
19-
mode,
20-
output: {
21-
filename: "index.js",
22-
},
23-
module: {
24-
rules: [
25-
{
26-
test: /\.s[ac]ss$/i,
27-
use: [
28-
"style-loader",
29-
{
30-
loader: "css-loader",
31-
options: {
32-
modules: {
33-
// Detect automatically if should apply CSS modules parsing or not
34-
auto: true,
35-
// Class name hashing mode
36-
localIdentName:
37-
mode === "development" ? "[file]__[local]__[hash:base64:5]" : "[hash:base64]",
38-
// Use package json name to create unique salt
39-
// Solves conflicts of equal classname compilation from two different builds
40-
localIdentHashSalt: packageName,
12+
export const getWebpackConfig = (options?: WebpackConfigOptions): Configuration => {
13+
const mode: Configuration["mode"] =
14+
options?.mode ??
15+
// Fallback to node env if not defined
16+
(() =>
17+
process.env.NODE_ENV === "development" || process.env.NODE_ENV === "production"
18+
? process.env.NODE_ENV
19+
: (() => {
20+
throw new Error("Invalid webpack mode");
21+
})())();
22+
23+
return {
24+
mode,
25+
output: {
26+
filename: "index.js",
27+
},
28+
module: {
29+
rules: [
30+
{
31+
test: /\.s[ac]ss$/i,
32+
use: [
33+
"style-loader",
34+
{
35+
loader: "css-loader",
36+
options: {
37+
modules: {
38+
// Detect automatically if should apply CSS modules parsing or not
39+
auto: true,
40+
// Class name hashing mode
41+
localIdentName:
42+
mode === "development" ? "[file]__[local]__[hash:base64:5]" : "[hash:base64]",
43+
// Use package json name to create unique salt
44+
// Solves conflicts of equal classname compilation from two different builds
45+
localIdentHashSalt: options?.cssHashSalt,
46+
},
47+
sourceMap: mode === "development",
4148
},
49+
},
50+
"sass-loader",
51+
],
52+
},
53+
{
54+
test: /\.tsx?$/,
55+
loader: "ts-loader",
56+
exclude: /node_modules/,
57+
options: {
58+
configFile: "tsconfig.build.json",
59+
compilerOptions: {
4260
sourceMap: mode === "development",
4361
},
4462
},
45-
"sass-loader",
46-
],
47-
},
48-
{
49-
test: /\.tsx?$/,
50-
loader: "ts-loader",
51-
exclude: /node_modules/,
52-
options: {
53-
configFile: "tsconfig.build.json",
54-
compilerOptions: {
55-
sourceMap: mode === "development",
56-
},
5763
},
58-
},
59-
],
60-
},
61-
resolve: {
62-
extensions: [".tsx", ".ts", ".js"],
63-
},
64-
externals: ["react"],
65-
stats: mode === "production" ? "normal" : "minimal",
66-
});
64+
],
65+
},
66+
resolve: {
67+
extensions: [".tsx", ".ts", ".js"],
68+
},
69+
externals: ["react"],
70+
stats: mode === "production" ? "normal" : "minimal",
71+
};
72+
};

0 commit comments

Comments
 (0)