Skip to content

Commit

Permalink
feat: update webpack config to chunk node modules (#22)
Browse files Browse the repository at this point in the history
build(webpack): update webpack config to chunk node modules
  • Loading branch information
kieranroneill authored Nov 11, 2023
1 parent 63e6899 commit a4f7d16
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
20 changes: 15 additions & 5 deletions webpack/webpack.common.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
import { resolve } from 'path';
import { Configuration } from 'webpack';

// Constants
// constants
import { APP_TITLE, BUILD_PATH, SRC_PATH } from './constants';

const COMMON_PATH: string = resolve(SRC_PATH, 'common');
Expand All @@ -30,6 +30,7 @@ const config: Configuration = {
'index.ts'
),
},

module: {
rules: [
{
Expand All @@ -45,11 +46,13 @@ const config: Configuration = {
},
],
},

output: {
clean: true,
filename: '[name].js',
path: BUILD_PATH,
},

plugins: [
new CopyPlugin({
patterns: [
Expand All @@ -66,7 +69,7 @@ const config: Configuration = {
},
],
}),
/* HTMLs */
/* htmls */
new HtmlWebpackPlugin({
chunks: ['background-app'],
filename: 'background-app.html',
Expand All @@ -89,15 +92,16 @@ const config: Configuration = {
title: APP_TITLE,
}),
],

resolve: {
alias: {
// Common
// common
['@common/enums']: resolve(COMMON_PATH, 'enums'),
['@common/errors']: resolve(COMMON_PATH, 'errors'),
['@common/events']: resolve(COMMON_PATH, 'events'),
['@common/types']: resolve(COMMON_PATH, 'types'),
['@common/utils']: resolve(COMMON_PATH, 'utils'),
// Extension
// extension
['@extension/components']: resolve(EXTENSION_PATH, 'components'),
['@extension/config']: resolve(EXTENSION_PATH, 'config'),
['@extension/constants']: resolve(EXTENSION_PATH, 'constants'),
Expand Down Expand Up @@ -157,13 +161,19 @@ const config: Configuration = {
['@extension/translations']: resolve(EXTENSION_PATH, 'translations'),
['@extension/types']: resolve(EXTENSION_PATH, 'types'),
['@extension/utils']: resolve(EXTENSION_PATH, 'utils'),
// External
// external
['@external/constants']: resolve(EXTERNAL_PATH, 'constants'),
['@external/services']: resolve(EXTERNAL_PATH, 'services'),
['@external/types']: resolve(EXTERNAL_PATH, 'types'),
},

extensions: ['.js', '.ts', '.tsx'],
},

stats: {
assetsSpace: 100,
modules: false,
},
};

export default config;
44 changes: 38 additions & 6 deletions webpack/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Configuration, DefinePlugin } from 'webpack';
import { Configuration as DevelopmentConfiguration } from 'webpack-dev-server';
import { merge } from 'webpack-merge';

// Config
// config
import { version } from '../package.json';
import { browser_specific_settings } from '../src/manifest.json';
import commonConfig from './webpack.common.config';

// Constants
// constants
import {
APP_TITLE,
DEVELOPMENT_ENVIRONMENT,
Expand All @@ -19,14 +19,15 @@ import {
PRODUCTION_ENVIRONMENT,
} from './constants';

// Plugins
// plugins
import WebExtPlugin from './plugins/WebExtPlugin';

const dappPort: number = 8080;
const maxSize: number = 4000000; // 4 MB

const configs: (Configuration | DevelopmentConfiguration)[] = [
/**
* Development
* development
*/
merge(commonConfig, {
devtool: 'cheap-module-source-map',
Expand Down Expand Up @@ -71,11 +72,15 @@ const configs: (Configuration | DevelopmentConfiguration)[] = [
}),
],
}),

/**
* Production
* production
*/
merge(commonConfig, {
devtool: 'source-map',

mode: 'production',

module: {
rules: [
{
Expand All @@ -92,7 +97,33 @@ const configs: (Configuration | DevelopmentConfiguration)[] = [
},
],
},

name: PRODUCTION_ENVIRONMENT,

optimization: {
splitChunks: {
cacheGroups: {
vendor: {
chunks: 'all',
maxSize,
name: 'vendor',
reuseExistingChunk: true,
test: /[\\/]node_modules[\\/]/,
},
},
},

runtimeChunk: {
name: 'runtime',
},
},

performance: {
hints: 'warning',
maxAssetSize: maxSize,
maxEntrypointSize: 10000000, // 10 MB
},

plugins: [
new DefinePlugin({
__EXTENSION_ID__: JSON.stringify(browser_specific_settings.gecko.id),
Expand All @@ -102,8 +133,9 @@ const configs: (Configuration | DevelopmentConfiguration)[] = [
}),
],
}),

/**
* Example dApp
* example dapp
*/
{
devtool: 'cheap-module-source-map',
Expand Down

0 comments on commit a4f7d16

Please sign in to comment.