refactor: [UEPR-17] Set optimization to be optional and added exclude logic

This commit is contained in:
MiroslavDionisiev 2024-06-25 15:33:15 +03:00
parent 1e77df3ec1
commit c89bfcd099

View file

@ -33,8 +33,9 @@ class ScratchWebpackConfigBuilder {
* @param {boolean} [options.enableReact] Whether to enable React and JSX support.
* @param {string} [options.libraryName] The name of the library to build. Shorthand for `output.library.name`.
* @param {string|URL} [options.srcPath] The absolute path to the source files. Defaults to `src` under `rootPath`.
* @param {boolean} [options.shouldOptimize] Whether to enable optimization.
*/
constructor ({distPath, enableReact, libraryName, rootPath, srcPath}) {
constructor({ distPath, enableReact, libraryName, rootPath, srcPath, shouldOptimize}) {
const isProduction = process.env.NODE_ENV === 'production';
const mode = isProduction ? 'production' : 'development';
@ -42,6 +43,7 @@ class ScratchWebpackConfigBuilder {
this._rootPath = toPath(rootPath) || '.'; // '.' will cause a webpack error since src must be absolute
this._srcPath = toPath(srcPath) ?? path.resolve(this._rootPath, 'src');
this._distPath = toPath(distPath) ?? path.resolve(this._rootPath, 'dist');
this._shouldOptimize = shouldOptimize
/**
* @type {Configuration}
@ -52,14 +54,16 @@ class ScratchWebpackConfigBuilder {
entry: libraryName ? {
[libraryName]: path.resolve(this._srcPath, 'index')
} : path.resolve(this._srcPath, 'index'),
optimization: {
minimize: isProduction,
splitChunks: {
chunks: 'all',
filename: DEFAULT_CHUNK_FILENAME
},
mergeDuplicateChunks: true
},
optimization: shouldOptimize
? {
minimize: isProduction,
splitChunks: {
chunks: 'all',
filename: DEFAULT_CHUNK_FILENAME,
},
mergeDuplicateChunks: true,
}
: {},
output: {
clean: true,
filename: '[name].js',
@ -91,6 +95,12 @@ class ScratchWebpackConfigBuilder {
{
test: enableReact ? /\.[cm]?jsx?$/ : /\.[cm]?js$/,
loader: 'babel-loader',
exclude: [
{
and: [/node_modules/],
not: [/node_modules\/scratch-/]
}
],
options: {
presets: [
'@babel/preset-env',
@ -139,7 +149,8 @@ class ScratchWebpackConfigBuilder {
libraryName: this._libraryName,
rootPath: this._rootPath,
srcPath: this._srcPath,
distPath: this._distPath
distPath: this._distPath,
shouldOptimize: this._shouldOptimize
}).merge(this._config);
}