@workleap/webpack-configs 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/build.d.ts +18 -19
  3. package/dist/build.js +219 -3
  4. package/dist/build.js.map +1 -0
  5. package/dist/dev.d.ts +16 -17
  6. package/dist/dev.js +208 -3
  7. package/dist/dev.js.map +1 -0
  8. package/dist/index.d.ts +6 -12
  9. package/dist/index.js +15 -7
  10. package/dist/index.js.map +1 -0
  11. package/dist/transformers/applyTransformers.d.ts +4 -7
  12. package/dist/transformers/applyTransformers.js +20 -1
  13. package/dist/transformers/applyTransformers.js.map +1 -0
  14. package/dist/transformers/moduleRules.d.ts +15 -17
  15. package/dist/transformers/moduleRules.js +166 -1
  16. package/dist/transformers/moduleRules.js.map +1 -0
  17. package/dist/transformers/plugins.d.ts +11 -14
  18. package/dist/transformers/plugins.js +69 -1
  19. package/dist/transformers/plugins.js.map +1 -0
  20. package/dist/types.d.ts +1 -1
  21. package/dist/types.js +6 -1
  22. package/dist/types.js.map +1 -0
  23. package/dist/utils.d.ts +4 -6
  24. package/dist/utils.js +22 -1
  25. package/dist/utils.js.map +1 -0
  26. package/package.json +24 -22
  27. package/src/build.ts +240 -0
  28. package/src/dev.ts +233 -0
  29. package/src/index.ts +7 -0
  30. package/src/transformers/applyTransformers.ts +28 -0
  31. package/src/transformers/moduleRules.ts +229 -0
  32. package/src/transformers/plugins.ts +102 -0
  33. package/src/types.ts +1 -0
  34. package/src/utils.ts +19 -0
  35. package/dist/chunk-2YARCRX5.js +0 -15
  36. package/dist/chunk-34O5ZLZ6.js +0 -154
  37. package/dist/chunk-5ACA7GOB.js +0 -17
  38. package/dist/chunk-6F4PWJZI.js +0 -1
  39. package/dist/chunk-CW54GSNS.js +0 -197
  40. package/dist/chunk-JPURRV2F.js +0 -71
  41. package/dist/chunk-JU2EHEXW.js +0 -186
package/src/build.ts ADDED
@@ -0,0 +1,240 @@
1
+ import type { Config as SvgrOptions } from "@svgr/core";
2
+ import type { Config as SwcConfig } from "@swc/core";
3
+ import HtmlWebpackPlugin from "html-webpack-plugin";
4
+ import MiniCssExtractPlugin from "mini-css-extract-plugin";
5
+ import { createRequire } from "node:module";
6
+ import path from "node:path";
7
+ import TerserPlugin from "terser-webpack-plugin";
8
+ import webpack from "webpack";
9
+ import { applyTransformers, type WebpackConfigTransformer } from "./transformers/applyTransformers.ts";
10
+ import type { WebpackConfig } from "./types.ts";
11
+ import { isObject } from "./utils.ts";
12
+
13
+ // Aliases
14
+ const DefinePlugin = webpack.DefinePlugin;
15
+
16
+ // Using node:module.createRequire until
17
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve
18
+ // is available
19
+ const require = createRequire(import.meta.url);
20
+
21
+ export function defineBuildHtmlWebpackPluginConfig(options: HtmlWebpackPlugin.Options = {}): HtmlWebpackPlugin.Options {
22
+ const {
23
+ template = path.resolve("./public/index.html"),
24
+ ...rest
25
+ } = options;
26
+
27
+ return {
28
+ ...rest,
29
+ template
30
+ };
31
+ }
32
+
33
+ type MiniCssExtractPluginOptions = NonNullable<ConstructorParameters<typeof MiniCssExtractPlugin>[number]>;
34
+
35
+ export function defineMiniCssExtractPluginConfig(options: MiniCssExtractPluginOptions = {}): MiniCssExtractPluginOptions {
36
+ const {
37
+ filename = "[name].css",
38
+ ...rest
39
+ } = options;
40
+
41
+ return {
42
+ ...rest,
43
+ filename
44
+ };
45
+ }
46
+
47
+ export type WebpackOptimization = NonNullable<WebpackConfig["optimization"]>;
48
+ export type OptimizeOption = boolean | "readable";
49
+
50
+ export function getOptimizationConfig(optimize: OptimizeOption): WebpackOptimization {
51
+ if (optimize === "readable") {
52
+ return {
53
+ minimize: true,
54
+ minimizer: [
55
+ new TerserPlugin({
56
+ minify: TerserPlugin.swcMinify,
57
+ terserOptions: {
58
+ toplevel: true,
59
+ mangle: false,
60
+ keep_classnames: true,
61
+ keep_fnames: true,
62
+ compress: {
63
+ toplevel: true,
64
+ hoist_props: false
65
+ },
66
+ output: {
67
+ comments: true
68
+ }
69
+ }
70
+ })
71
+ ],
72
+ chunkIds: "named",
73
+ moduleIds: "named",
74
+ mangleExports: false,
75
+ mangleWasmImports: false
76
+ };
77
+ } else if (optimize) {
78
+ return {
79
+ minimize: true,
80
+ minimizer: [
81
+ new TerserPlugin({
82
+ minify: TerserPlugin.swcMinify
83
+ })
84
+ ]
85
+ };
86
+ }
87
+
88
+ // Doesn't turnoff everything but is good enough to help with debugging scenarios.
89
+ return {
90
+ minimize: false,
91
+ chunkIds: "named",
92
+ moduleIds: "named",
93
+ concatenateModules: false,
94
+ flagIncludedChunks: false,
95
+ mangleExports: false,
96
+ mangleWasmImports: false,
97
+ removeAvailableModules: false,
98
+ usedExports: false
99
+ };
100
+ }
101
+
102
+ export interface DefineBuildConfigOptions {
103
+ entry?: string;
104
+ outputPath?: string;
105
+ publicPath?: `${string}/` | "auto";
106
+ moduleRules?: NonNullable<WebpackConfig["module"]>["rules"];
107
+ plugins?: WebpackConfig["plugins"];
108
+ htmlWebpackPlugin?: boolean | HtmlWebpackPlugin.Options;
109
+ miniCssExtractPluginOptions?: MiniCssExtractPluginOptions;
110
+ optimize?: OptimizeOption;
111
+ cssModules?: boolean;
112
+ svgr?: boolean | SvgrOptions;
113
+ environmentVariables?: Record<string, unknown>;
114
+ transformers?: WebpackConfigTransformer[];
115
+ verbose?: boolean;
116
+ }
117
+
118
+ export function defineBuildConfig(swcConfig: SwcConfig, options: DefineBuildConfigOptions = {}) {
119
+ const {
120
+ entry = path.resolve("./src/index.tsx"),
121
+ outputPath = path.resolve("dist"),
122
+ // The trailing / is very important, otherwise paths will not be resolved correctly.
123
+ publicPath = "http://localhost:8080/",
124
+ moduleRules = [],
125
+ plugins = [],
126
+ htmlWebpackPlugin = defineBuildHtmlWebpackPluginConfig(),
127
+ miniCssExtractPluginOptions = defineMiniCssExtractPluginConfig(),
128
+ optimize = true,
129
+ cssModules = false,
130
+ svgr = true,
131
+ // Using an empty object literal as the default value to ensure
132
+ // "process.env" is always available.
133
+ environmentVariables = {},
134
+ transformers = [],
135
+ verbose = false
136
+ } = options;
137
+
138
+ const config: WebpackConfig = {
139
+ mode: "production",
140
+ target: "web",
141
+ entry,
142
+ output: {
143
+ path: outputPath,
144
+ filename: "[name].js",
145
+ publicPath,
146
+ clean: true,
147
+ assetModuleFilename: "[name][ext][query]"
148
+ },
149
+ optimization: getOptimizationConfig(optimize),
150
+ infrastructureLogging: verbose ? {
151
+ appendOnly: true,
152
+ level: "verbose",
153
+ debug: /PackFileCache/
154
+ } : undefined,
155
+ module: {
156
+ rules: [
157
+ {
158
+ test: /\.(js|jsx|ts|tsx)$/i,
159
+ exclude: /node_modules/,
160
+ loader: require.resolve("swc-loader"),
161
+ options: swcConfig
162
+ },
163
+ {
164
+ // https://stackoverflow.com/questions/69427025/programmatic-webpack-jest-esm-cant-resolve-module-without-js-file-exten
165
+ test: /\.js$/i,
166
+ include: /node_modules/,
167
+ resolve: {
168
+ fullySpecified: false
169
+ }
170
+ },
171
+ {
172
+ test: /\.css$/i,
173
+ use: [
174
+ { loader: MiniCssExtractPlugin.loader },
175
+ {
176
+ loader: require.resolve("css-loader"),
177
+ options: cssModules
178
+ ? {
179
+ // Must match the number of loaders applied before this one.
180
+ importLoaders: 1,
181
+ modules: true
182
+ }
183
+ : undefined
184
+ },
185
+ { loader: require.resolve("postcss-loader") }
186
+ ]
187
+ },
188
+ ...(svgr
189
+ ? [
190
+ {
191
+ test: /\.svg$/i,
192
+ loader: require.resolve("@svgr/webpack"),
193
+ options: isObject(svgr) ? svgr : undefined
194
+ },
195
+ {
196
+ test: /\.(png|jpe?g|gif)$/i,
197
+ type: "asset/resource"
198
+ }
199
+ ]
200
+ : [
201
+ {
202
+ test: /\.(png|jpe?g|gif|svg)$/i,
203
+ type: "asset/resource"
204
+ }
205
+ ]),
206
+ ...moduleRules
207
+ ]
208
+ },
209
+ resolve: {
210
+ extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
211
+ alias: {
212
+ // Fixes Module not found: Error: Can't resolve '@swc/helpers/_/_class_private_field_init'.
213
+ // View https://github.com/vercel/next.js/pull/38174 for more information and https://github.com/vercel/next.js/issues/48593.
214
+ "@swc/helpers": path.dirname(require.resolve("@swc/helpers/package.json"))
215
+ }
216
+ },
217
+ plugins: [
218
+ htmlWebpackPlugin && new HtmlWebpackPlugin(isObject(htmlWebpackPlugin) ? htmlWebpackPlugin : defineBuildHtmlWebpackPluginConfig()),
219
+ new MiniCssExtractPlugin(miniCssExtractPluginOptions),
220
+ // Stringify the environment variables because the plugin does a direct text replacement. Otherwise, "production" would become production
221
+ // after replacement and cause an undefined var error because the production var doesn't exist.
222
+ // For more information, view: https://webpack.js.org/plugins/define-plugin.
223
+ new DefinePlugin({
224
+ "process.env": Object.keys(environmentVariables).reduce((acc, key) => {
225
+ acc[key] = JSON.stringify(environmentVariables[key]);
226
+
227
+ return acc;
228
+ }, {} as Record<string, string>)
229
+ }),
230
+ ...plugins
231
+ ].filter(Boolean) as WebpackConfig["plugins"]
232
+ };
233
+
234
+ const transformedConfig = applyTransformers(config, transformers, {
235
+ environment: "build",
236
+ verbose
237
+ });
238
+
239
+ return transformedConfig;
240
+ }
package/src/dev.ts ADDED
@@ -0,0 +1,233 @@
1
+ import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin";
2
+ import type { ReactRefreshPluginOptions } from "@pmmmwh/react-refresh-webpack-plugin/types/lib/types.d.ts";
3
+ import type { Config as SvgrOptions } from "@svgr/core";
4
+ import type { Config as SwcConfig } from "@swc/core";
5
+ import HtmlWebpackPlugin from "html-webpack-plugin";
6
+ import { createRequire } from "node:module";
7
+ import path from "node:path";
8
+ import webpack from "webpack";
9
+ import type { ServerOptions } from "webpack-dev-server";
10
+ import { applyTransformers, type WebpackConfigTransformer } from "./transformers/applyTransformers.ts";
11
+ import { isNil, isObject } from "./utils.ts";
12
+
13
+ // Add the "devServer" option to WebpackConfig typings.
14
+ import "webpack-dev-server";
15
+ import type { WebpackConfig } from "./types.ts";
16
+
17
+ // Aliases
18
+ const DefinePlugin = webpack.DefinePlugin;
19
+
20
+ // Using node:module.createRequire until
21
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve
22
+ // is available
23
+ const require = createRequire(import.meta.url);
24
+
25
+ export function defineDevHtmlWebpackPluginConfig(options: HtmlWebpackPlugin.Options = {}): HtmlWebpackPlugin.Options {
26
+ const {
27
+ template = path.resolve("./public/index.html"),
28
+ ...rest
29
+ } = options;
30
+
31
+ return {
32
+ ...rest,
33
+ template
34
+ };
35
+ }
36
+
37
+ export function defineFastRefreshPluginConfig(options: ReactRefreshPluginOptions = {}) {
38
+ return options;
39
+ }
40
+
41
+ export interface DefineDevConfigOptions {
42
+ entry?: string;
43
+ https?: boolean | ServerOptions | undefined;
44
+ host?: string;
45
+ port?: number;
46
+ publicPath?: `${string}/` | "auto";
47
+ cache?: boolean;
48
+ moduleRules?: NonNullable<WebpackConfig["module"]>["rules"];
49
+ plugins?: WebpackConfig["plugins"];
50
+ htmlWebpackPlugin?: boolean | HtmlWebpackPlugin.Options;
51
+ fastRefresh?: boolean | ReactRefreshPluginOptions;
52
+ cssModules?: boolean;
53
+ overlay?: false;
54
+ svgr?: boolean | SvgrOptions;
55
+ environmentVariables?: Record<string, unknown>;
56
+ transformers?: WebpackConfigTransformer[];
57
+ verbose?: boolean;
58
+ }
59
+
60
+ function preflight() {
61
+ if (!require.resolve("webpack-dev-server")) {
62
+ throw new Error("[webpack-configs] To use the \"dev\" config, install https://www.npmjs.com/package/webpack-dev-server as a \"devDependency\".");
63
+ }
64
+ }
65
+
66
+ function trySetSwcFastRefresh(config: SwcConfig, enabled: boolean) {
67
+ if (config?.jsc?.transform?.react) {
68
+ config.jsc.transform.react.refresh = enabled;
69
+ }
70
+
71
+ return config;
72
+ }
73
+
74
+ function trySetFastRefreshOverlay(options: ReactRefreshPluginOptions, overlay?: boolean) {
75
+ if (overlay === false && isNil(options.overlay)) {
76
+ options.overlay = false;
77
+ }
78
+
79
+ return options;
80
+ }
81
+
82
+ export function defineDevConfig(swcConfig: SwcConfig, options: DefineDevConfigOptions = {}) {
83
+ preflight();
84
+
85
+ const {
86
+ entry = path.resolve("./src/index.tsx"),
87
+ https = false,
88
+ host = "localhost",
89
+ port = 8080,
90
+ publicPath,
91
+ cache = true,
92
+ moduleRules = [],
93
+ plugins = [],
94
+ htmlWebpackPlugin = defineDevHtmlWebpackPluginConfig(),
95
+ fastRefresh = true,
96
+ cssModules = false,
97
+ overlay,
98
+ svgr = true,
99
+ // Using an empty object literal as the default value to ensure
100
+ // "process.env" is always available.
101
+ environmentVariables = {},
102
+ transformers = [],
103
+ verbose = false
104
+ } = options;
105
+
106
+ const config: WebpackConfig = {
107
+ mode: "development",
108
+ target: "web",
109
+ devtool: "eval-cheap-module-source-map",
110
+ devServer: {
111
+ // According to the Fast Refresh plugin documentation, hot should be "true" to enable Fast Refresh:
112
+ // https://github.com/pmmmwh/react-refresh-webpack-plugin#usage.
113
+ hot: true,
114
+ host,
115
+ port,
116
+ historyApiFallback: true,
117
+ client: (overlay === false || fastRefresh) ? {
118
+ overlay: false
119
+ } : undefined,
120
+ server: https ? {
121
+ type: "https",
122
+ options: isObject(https) ? https : undefined
123
+ } : undefined
124
+ },
125
+ entry,
126
+ output: {
127
+ // The trailing / is very important, otherwise paths will not be resolved correctly.
128
+ publicPath: publicPath ?? `${https ? "https" : "http"}://${host}:${port}/`
129
+ },
130
+ cache: cache && {
131
+ type: "memory",
132
+ maxGenerations: 1
133
+ },
134
+ // See: https://webpack.js.org/guides/build-performance/#avoid-extra-optimization-steps
135
+ optimization: {
136
+ // Keep "runtimeChunk" to false, otherwise it breaks module federation
137
+ // (at least for the remote application).
138
+ runtimeChunk: false,
139
+ removeAvailableModules: false,
140
+ removeEmptyChunks: false,
141
+ splitChunks: false
142
+ },
143
+ infrastructureLogging: verbose ? {
144
+ appendOnly: true,
145
+ level: "verbose",
146
+ debug: /PackFileCache/
147
+ } : undefined,
148
+ module: {
149
+ rules: [
150
+ {
151
+ test: /\.(js|jsx|ts|tsx)$/i,
152
+ exclude: /node_modules/,
153
+ loader: require.resolve("swc-loader"),
154
+ options: trySetSwcFastRefresh(swcConfig, fastRefresh !== false)
155
+ },
156
+ {
157
+ // https://stackoverflow.com/questions/69427025/programmatic-webpack-jest-esm-cant-resolve-module-without-js-file-exten
158
+ test: /\.js$/i,
159
+ include: /node_modules/,
160
+ resolve: {
161
+ fullySpecified: false
162
+ }
163
+ },
164
+ {
165
+ test: /\.css$/i,
166
+ use: [
167
+ { loader: require.resolve("style-loader") },
168
+ {
169
+ loader: require.resolve("css-loader"),
170
+ options: cssModules
171
+ ? {
172
+ // Must match the number of loaders applied before this one.
173
+ importLoaders: 1,
174
+ modules: true
175
+ }
176
+ : undefined
177
+ },
178
+ { loader: require.resolve("postcss-loader") }
179
+ ]
180
+ },
181
+ ...(svgr
182
+ ? [
183
+ {
184
+ test: /\.svg$/i,
185
+ loader: require.resolve("@svgr/webpack"),
186
+ options: isObject(svgr) ? svgr : undefined
187
+ },
188
+ {
189
+ test: /\.(png|jpe?g|gif)$/i,
190
+ type: "asset/resource"
191
+ }
192
+ ]
193
+ : [
194
+ {
195
+ test: /\.(png|jpe?g|gif|svg)$/i,
196
+ type: "asset/resource"
197
+ }
198
+ ]),
199
+ ...moduleRules
200
+ ]
201
+ },
202
+ resolve: {
203
+ extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
204
+ alias: {
205
+ // Fixes Module not found: Error: Can't resolve '@swc/helpers/_/_class_private_field_init'.
206
+ // View https://github.com/vercel/next.js/pull/38174 for more information and https://github.com/vercel/next.js/issues/48593.
207
+ "@swc/helpers": path.dirname(require.resolve("@swc/helpers/package.json"))
208
+ }
209
+ },
210
+ plugins: [
211
+ htmlWebpackPlugin && new HtmlWebpackPlugin(isObject(htmlWebpackPlugin) ? htmlWebpackPlugin : defineDevHtmlWebpackPluginConfig()),
212
+ // Stringify the environment variables because the plugin does a direct text replacement. Otherwise, "production" would become production
213
+ // after replacement and cause an undefined var error.
214
+ // For more information, view: https://webpack.js.org/plugins/define-plugin/.
215
+ new DefinePlugin({
216
+ "process.env": Object.keys(environmentVariables).reduce((acc, key) => {
217
+ acc[key] = JSON.stringify(environmentVariables[key]);
218
+
219
+ return acc;
220
+ }, {} as Record<string, string>)
221
+ }),
222
+ fastRefresh && new ReactRefreshWebpackPlugin(trySetFastRefreshOverlay(isObject(fastRefresh) ? fastRefresh : defineFastRefreshPluginConfig(), overlay)),
223
+ ...plugins
224
+ ].filter(Boolean)
225
+ };
226
+
227
+ const transformedConfig = applyTransformers(config, transformers, {
228
+ environment: "dev",
229
+ verbose
230
+ });
231
+
232
+ return transformedConfig;
233
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./build.ts";
2
+ export * from "./dev.ts";
3
+ export type { WebpackConfigTransformer, WebpackConfigTransformerContext } from "./transformers/applyTransformers.ts";
4
+ export * from "./transformers/moduleRules.ts";
5
+ export * from "./transformers/plugins.ts";
6
+ export * from "./types.ts";
7
+
@@ -0,0 +1,28 @@
1
+ import type { WebpackConfig } from "../types.ts";
2
+
3
+ export interface WebpackConfigTransformerContext {
4
+ environment: "dev" | "build";
5
+ verbose: boolean;
6
+ }
7
+
8
+ export type WebpackConfigTransformer = (config: WebpackConfig, context: WebpackConfigTransformerContext) => WebpackConfig;
9
+
10
+ export function applyTransformers(config: WebpackConfig, transformers: WebpackConfigTransformer[], context: WebpackConfigTransformerContext) {
11
+ let count = 0;
12
+
13
+ const transformedConfig = transformers.reduce((acc, transformer) => {
14
+ const newConfig = transformer(acc, context);
15
+
16
+ count += 1;
17
+
18
+ return newConfig;
19
+ }, config);
20
+
21
+ if (context.verbose) {
22
+ if (count > 0) {
23
+ console.log(`[webpack-configs] Applied ${count} configuration transformers.`);
24
+ }
25
+ }
26
+
27
+ return transformedConfig;
28
+ }