Merge pull request #1 from MiroslavDionisiev/UEPR-17

[UEPR-17] Add flag for omitting chunk splitting and added logic to exclude node_modules
This commit is contained in:
Miroslav Dionisiev 2024-07-16 09:51:37 +03:00 committed by GitHub
commit d44b4d2509
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

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.shouldSplitChunks] Whether to enable spliting code to chunks.
*/
constructor ({distPath, enableReact, libraryName, rootPath, srcPath}) {
constructor ({ distPath, enableReact, libraryName, rootPath, srcPath, shouldSplitChunks }) {
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._shouldSplitChunks = shouldSplitChunks
/**
* @type {Configuration}
@ -54,11 +56,15 @@ class ScratchWebpackConfigBuilder {
} : path.resolve(this._srcPath, 'index'),
optimization: {
minimize: isProduction,
splitChunks: {
chunks: 'all',
filename: DEFAULT_CHUNK_FILENAME
},
mergeDuplicateChunks: true
...(
shouldSplitChunks ? {
splitChunks: {
chunks: 'all',
filename: DEFAULT_CHUNK_FILENAME,
},
mergeDuplicateChunks: true
} : {}
)
},
output: {
clean: true,
@ -91,6 +97,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 +151,8 @@ class ScratchWebpackConfigBuilder {
libraryName: this._libraryName,
rootPath: this._rootPath,
srcPath: this._srcPath,
distPath: this._distPath
distPath: this._distPath,
shouldSplitChunks: this._shouldSplitChunks
}).merge(this._config);
}