@@ -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-
147export 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 [ a c ] s s $ / 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 [ a c ] s s $ / 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 : / \. t s x ? $ / ,
55+ loader : "ts-loader" ,
56+ exclude : / n o d e _ m o d u l e s / ,
57+ options : {
58+ configFile : "tsconfig.build.json" ,
59+ compilerOptions : {
4260 sourceMap : mode === "development" ,
4361 } ,
4462 } ,
45- "sass-loader" ,
46- ] ,
47- } ,
48- {
49- test : / \. t s x ? $ / ,
50- loader : "ts-loader" ,
51- exclude : / n o d e _ m o d u l e s / ,
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