@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
@@ -1,197 +0,0 @@
1
- import { isObject } from './chunk-2YARCRX5.js';
2
- import { applyTransformers } from './chunk-5ACA7GOB.js';
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
-
10
- var DefinePlugin = webpack.DefinePlugin;
11
- var require2 = createRequire(import.meta.url);
12
- function defineBuildHtmlWebpackPluginConfig(options = {}) {
13
- const {
14
- template = path.resolve("./public/index.html"),
15
- ...rest
16
- } = options;
17
- return {
18
- ...rest,
19
- template
20
- };
21
- }
22
- function defineMiniCssExtractPluginConfig(options = {}) {
23
- const {
24
- filename = "[name].css",
25
- ...rest
26
- } = options;
27
- return {
28
- ...rest,
29
- filename
30
- };
31
- }
32
- function getOptimizationConfig(optimize) {
33
- if (optimize === "readable") {
34
- return {
35
- minimize: true,
36
- minimizer: [
37
- new TerserPlugin({
38
- minify: TerserPlugin.swcMinify,
39
- terserOptions: {
40
- toplevel: true,
41
- mangle: false,
42
- keep_classnames: true,
43
- keep_fnames: true,
44
- compress: {
45
- toplevel: true,
46
- hoist_props: false
47
- },
48
- output: {
49
- comments: true
50
- }
51
- }
52
- })
53
- ],
54
- chunkIds: "named",
55
- moduleIds: "named",
56
- mangleExports: false,
57
- mangleWasmImports: false
58
- };
59
- } else if (optimize) {
60
- return {
61
- minimize: true,
62
- minimizer: [
63
- new TerserPlugin({
64
- minify: TerserPlugin.swcMinify
65
- })
66
- ]
67
- };
68
- }
69
- return {
70
- minimize: false,
71
- chunkIds: "named",
72
- moduleIds: "named",
73
- concatenateModules: false,
74
- flagIncludedChunks: false,
75
- mangleExports: false,
76
- mangleWasmImports: false,
77
- removeAvailableModules: false,
78
- usedExports: false
79
- };
80
- }
81
- function defineBuildConfig(swcConfig, options = {}) {
82
- const {
83
- entry = path.resolve("./src/index.tsx"),
84
- outputPath = path.resolve("dist"),
85
- // The trailing / is very important, otherwise paths will not be resolved correctly.
86
- publicPath = "http://localhost:8080/",
87
- moduleRules = [],
88
- plugins = [],
89
- htmlWebpackPlugin = defineBuildHtmlWebpackPluginConfig(),
90
- miniCssExtractPluginOptions = defineMiniCssExtractPluginConfig(),
91
- optimize = true,
92
- cssModules = false,
93
- svgr = true,
94
- // Using an empty object literal as the default value to ensure
95
- // "process.env" is always available.
96
- environmentVariables = {},
97
- transformers = [],
98
- verbose = false
99
- } = options;
100
- const config = {
101
- mode: "production",
102
- target: "web",
103
- entry,
104
- output: {
105
- path: outputPath,
106
- filename: "[name].js",
107
- publicPath,
108
- clean: true,
109
- assetModuleFilename: "[name][ext][query]"
110
- },
111
- optimization: getOptimizationConfig(optimize),
112
- infrastructureLogging: verbose ? {
113
- appendOnly: true,
114
- level: "verbose",
115
- debug: /PackFileCache/
116
- } : void 0,
117
- module: {
118
- rules: [
119
- {
120
- test: /\.(js|jsx|ts|tsx)$/i,
121
- exclude: /node_modules/,
122
- loader: require2.resolve("swc-loader"),
123
- options: swcConfig
124
- },
125
- {
126
- // https://stackoverflow.com/questions/69427025/programmatic-webpack-jest-esm-cant-resolve-module-without-js-file-exten
127
- test: /\.js$/i,
128
- include: /node_modules/,
129
- resolve: {
130
- fullySpecified: false
131
- }
132
- },
133
- {
134
- test: /\.css$/i,
135
- use: [
136
- { loader: MiniCssExtractPlugin.loader },
137
- {
138
- loader: require2.resolve("css-loader"),
139
- options: cssModules ? {
140
- // Must match the number of loaders applied before this one.
141
- importLoaders: 1,
142
- modules: true
143
- } : void 0
144
- },
145
- { loader: require2.resolve("postcss-loader") }
146
- ]
147
- },
148
- ...svgr ? [
149
- {
150
- test: /\.svg$/i,
151
- loader: require2.resolve("@svgr/webpack"),
152
- options: isObject(svgr) ? svgr : void 0
153
- },
154
- {
155
- test: /\.(png|jpe?g|gif)$/i,
156
- type: "asset/resource"
157
- }
158
- ] : [
159
- {
160
- test: /\.(png|jpe?g|gif|svg)$/i,
161
- type: "asset/resource"
162
- }
163
- ],
164
- ...moduleRules
165
- ]
166
- },
167
- resolve: {
168
- extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
169
- alias: {
170
- // Fixes Module not found: Error: Can't resolve '@swc/helpers/_/_class_private_field_init'.
171
- // View https://github.com/vercel/next.js/pull/38174 for more information and https://github.com/vercel/next.js/issues/48593.
172
- "@swc/helpers": path.dirname(require2.resolve("@swc/helpers/package.json"))
173
- }
174
- },
175
- plugins: [
176
- htmlWebpackPlugin && new HtmlWebpackPlugin(isObject(htmlWebpackPlugin) ? htmlWebpackPlugin : defineBuildHtmlWebpackPluginConfig()),
177
- new MiniCssExtractPlugin(miniCssExtractPluginOptions),
178
- // Stringify the environment variables because the plugin does a direct text replacement. Otherwise, "production" would become production
179
- // after replacement and cause an undefined var error because the production var doesn't exist.
180
- // For more information, view: https://webpack.js.org/plugins/define-plugin.
181
- new DefinePlugin({
182
- "process.env": Object.keys(environmentVariables).reduce((acc, key) => {
183
- acc[key] = JSON.stringify(environmentVariables[key]);
184
- return acc;
185
- }, {})
186
- }),
187
- ...plugins
188
- ].filter(Boolean)
189
- };
190
- const transformedConfig = applyTransformers(config, transformers, {
191
- environment: "build",
192
- verbose
193
- });
194
- return transformedConfig;
195
- }
196
-
197
- export { defineBuildConfig, defineBuildHtmlWebpackPluginConfig, defineMiniCssExtractPluginConfig, getOptimizationConfig };
@@ -1,71 +0,0 @@
1
- // src/transformers/plugins.ts
2
- function matchConstructorName(name) {
3
- const matcher = (plugin) => {
4
- return plugin?.constructor.name === name;
5
- };
6
- matcher.info = {
7
- type: matchConstructorName.name,
8
- value: name
9
- };
10
- return matcher;
11
- }
12
- function findPlugin(config, matcher) {
13
- const matches = [];
14
- config.plugins?.forEach((x, index, array) => {
15
- if (matcher(x, index, array)) {
16
- matches.push({
17
- plugin: x,
18
- index
19
- });
20
- }
21
- });
22
- if (matches.length > 1) {
23
- const matcherInfo = matcher.info;
24
- throw new Error(`[webpack-configs] Found more than 1 matching plugin.
25
- [webp-configs] Matcher: "${JSON.stringify(matcherInfo)}"
26
- [webpack-configs] Matches: "${JSON.stringify(matches.map((x) => x.plugin))}"`);
27
- }
28
- return matches[0];
29
- }
30
- function replacePlugin(config, matcher, newPlugin) {
31
- const match = findPlugin(config, matcher);
32
- if (match) {
33
- config.plugins[match.index] = newPlugin;
34
- } else {
35
- const matcherInfo = matcher.info;
36
- throw new Error(`[webpack-configs] Couldn't replace the plugin because no match has been found.
37
- [webpack-configs] Matcher: "${JSON.stringify(matcherInfo)}"`);
38
- }
39
- }
40
- function addBeforePlugin(config, matcher, newPlugins) {
41
- const match = findPlugin(config, matcher);
42
- if (match) {
43
- config.plugins?.splice(match.index, 0, ...newPlugins);
44
- } else {
45
- const matcherInfo = matcher.info;
46
- throw new Error(`[webpack-configs] Couldn't add the new plugins because no match has been found.
47
- [webpack-configs] Matcher: "${JSON.stringify(matcherInfo)}"`);
48
- }
49
- }
50
- function addAfterPlugin(config, matcher, newPlugins) {
51
- const match = findPlugin(config, matcher);
52
- if (match) {
53
- config.plugins?.splice(match.index + 1, 0, ...newPlugins);
54
- } else {
55
- const matcherInfo = matcher.info;
56
- throw new Error(`[webpack-configs] Couldn't add the new plugins because no match has been found.
57
- [webpack-configs] Matcher: "${JSON.stringify(matcherInfo)}"`);
58
- }
59
- }
60
- function removePlugin(config, matcher) {
61
- const countBefore = config.plugins?.length ?? 0;
62
- config.plugins = config.plugins?.filter((...args) => !matcher(...args));
63
- const countAfter = config.plugins?.length ?? 0;
64
- if (countBefore === countAfter) {
65
- const matcherInfo = matcher.info;
66
- throw new Error(`[webpack-configs] Didn't remove any plugin because no match has been found.
67
- [webpack-configs] Matcher: "${JSON.stringify(matcherInfo)}"`);
68
- }
69
- }
70
-
71
- export { addAfterPlugin, addBeforePlugin, findPlugin, matchConstructorName, removePlugin, replacePlugin };
@@ -1,186 +0,0 @@
1
- import { isObject, isNil } from './chunk-2YARCRX5.js';
2
- import { applyTransformers } from './chunk-5ACA7GOB.js';
3
- import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
4
- import HtmlWebpackPlugin from 'html-webpack-plugin';
5
- import { createRequire } from 'node:module';
6
- import path from 'node:path';
7
- import webpack from 'webpack';
8
- import 'webpack-dev-server';
9
-
10
- var DefinePlugin = webpack.DefinePlugin;
11
- var require2 = createRequire(import.meta.url);
12
- function defineDevHtmlWebpackPluginConfig(options = {}) {
13
- const {
14
- template = path.resolve("./public/index.html"),
15
- ...rest
16
- } = options;
17
- return {
18
- ...rest,
19
- template
20
- };
21
- }
22
- function defineFastRefreshPluginConfig(options = {}) {
23
- return options;
24
- }
25
- function preflight() {
26
- if (!require2.resolve("webpack-dev-server")) {
27
- throw new Error('[webpack-configs] To use the "dev" config, install https://www.npmjs.com/package/webpack-dev-server as a "devDependency".');
28
- }
29
- }
30
- function trySetSwcFastRefresh(config, enabled) {
31
- if (config?.jsc?.transform?.react) {
32
- config.jsc.transform.react.refresh = enabled;
33
- }
34
- return config;
35
- }
36
- function trySetFastRefreshOverlay(options, overlay) {
37
- if (overlay === false && isNil(options.overlay)) {
38
- options.overlay = false;
39
- }
40
- return options;
41
- }
42
- function defineDevConfig(swcConfig, options = {}) {
43
- preflight();
44
- const {
45
- entry = path.resolve("./src/index.tsx"),
46
- https = false,
47
- host = "localhost",
48
- port = 8080,
49
- publicPath,
50
- cache = true,
51
- moduleRules = [],
52
- plugins = [],
53
- htmlWebpackPlugin = defineDevHtmlWebpackPluginConfig(),
54
- fastRefresh = true,
55
- cssModules = false,
56
- overlay,
57
- svgr = true,
58
- // Using an empty object literal as the default value to ensure
59
- // "process.env" is always available.
60
- environmentVariables = {},
61
- transformers = [],
62
- verbose = false
63
- } = options;
64
- const config = {
65
- mode: "development",
66
- target: "web",
67
- devtool: "eval-cheap-module-source-map",
68
- devServer: {
69
- // According to the Fast Refresh plugin documentation, hot should be "true" to enable Fast Refresh:
70
- // https://github.com/pmmmwh/react-refresh-webpack-plugin#usage.
71
- hot: true,
72
- host,
73
- port,
74
- historyApiFallback: true,
75
- client: overlay === false || fastRefresh ? {
76
- overlay: false
77
- } : void 0,
78
- server: https ? {
79
- type: "https",
80
- options: isObject(https) ? https : void 0
81
- } : void 0
82
- },
83
- entry,
84
- output: {
85
- // The trailing / is very important, otherwise paths will not be resolved correctly.
86
- publicPath: publicPath ?? `${https ? "https" : "http"}://${host}:${port}/`
87
- },
88
- cache: cache && {
89
- type: "memory",
90
- maxGenerations: 1
91
- },
92
- // See: https://webpack.js.org/guides/build-performance/#avoid-extra-optimization-steps
93
- optimization: {
94
- // Keep "runtimeChunk" to false, otherwise it breaks module federation
95
- // (at least for the remote application).
96
- runtimeChunk: false,
97
- removeAvailableModules: false,
98
- removeEmptyChunks: false,
99
- splitChunks: false
100
- },
101
- infrastructureLogging: verbose ? {
102
- appendOnly: true,
103
- level: "verbose",
104
- debug: /PackFileCache/
105
- } : void 0,
106
- module: {
107
- rules: [
108
- {
109
- test: /\.(js|jsx|ts|tsx)$/i,
110
- exclude: /node_modules/,
111
- loader: require2.resolve("swc-loader"),
112
- options: trySetSwcFastRefresh(swcConfig, fastRefresh !== false)
113
- },
114
- {
115
- // https://stackoverflow.com/questions/69427025/programmatic-webpack-jest-esm-cant-resolve-module-without-js-file-exten
116
- test: /\.js$/i,
117
- include: /node_modules/,
118
- resolve: {
119
- fullySpecified: false
120
- }
121
- },
122
- {
123
- test: /\.css$/i,
124
- use: [
125
- { loader: require2.resolve("style-loader") },
126
- {
127
- loader: require2.resolve("css-loader"),
128
- options: cssModules ? {
129
- // Must match the number of loaders applied before this one.
130
- importLoaders: 1,
131
- modules: true
132
- } : void 0
133
- },
134
- { loader: require2.resolve("postcss-loader") }
135
- ]
136
- },
137
- ...svgr ? [
138
- {
139
- test: /\.svg$/i,
140
- loader: require2.resolve("@svgr/webpack"),
141
- options: isObject(svgr) ? svgr : void 0
142
- },
143
- {
144
- test: /\.(png|jpe?g|gif)$/i,
145
- type: "asset/resource"
146
- }
147
- ] : [
148
- {
149
- test: /\.(png|jpe?g|gif|svg)$/i,
150
- type: "asset/resource"
151
- }
152
- ],
153
- ...moduleRules
154
- ]
155
- },
156
- resolve: {
157
- extensions: [".js", ".jsx", ".ts", ".tsx", ".css"],
158
- alias: {
159
- // Fixes Module not found: Error: Can't resolve '@swc/helpers/_/_class_private_field_init'.
160
- // View https://github.com/vercel/next.js/pull/38174 for more information and https://github.com/vercel/next.js/issues/48593.
161
- "@swc/helpers": path.dirname(require2.resolve("@swc/helpers/package.json"))
162
- }
163
- },
164
- plugins: [
165
- htmlWebpackPlugin && new HtmlWebpackPlugin(isObject(htmlWebpackPlugin) ? htmlWebpackPlugin : defineDevHtmlWebpackPluginConfig()),
166
- // Stringify the environment variables because the plugin does a direct text replacement. Otherwise, "production" would become production
167
- // after replacement and cause an undefined var error.
168
- // For more information, view: https://webpack.js.org/plugins/define-plugin/.
169
- new DefinePlugin({
170
- "process.env": Object.keys(environmentVariables).reduce((acc, key) => {
171
- acc[key] = JSON.stringify(environmentVariables[key]);
172
- return acc;
173
- }, {})
174
- }),
175
- fastRefresh && new ReactRefreshWebpackPlugin(trySetFastRefreshOverlay(isObject(fastRefresh) ? fastRefresh : defineFastRefreshPluginConfig(), overlay)),
176
- ...plugins
177
- ].filter(Boolean)
178
- };
179
- const transformedConfig = applyTransformers(config, transformers, {
180
- environment: "dev",
181
- verbose
182
- });
183
- return transformedConfig;
184
- }
185
-
186
- export { defineDevConfig, defineDevHtmlWebpackPluginConfig, defineFastRefreshPluginConfig };