Skip to content

Commit

Permalink
feat: upgrade webpack config and add babel cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sashtje committed Sep 29, 2023
1 parent 48b6004 commit 6acf420
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
7 changes: 6 additions & 1 deletion .browserslistrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Browsers that we support
[production]
defaults
not IE 11

[development]
last 2 Chrome versions
last 2 Firefox versions
las 1 Safari version
32 changes: 21 additions & 11 deletions config/build/buildPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
import webpack from 'webpack';
import HTMLWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import CircularDependencyPlugin from 'circular-dependency-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import CopyPlugin from 'copy-webpack-plugin';

import { BuildOptions } from './types/config';

export function buildPlugins({
paths, isDev, apiUrl, project,
}: BuildOptions): webpack.WebpackPluginInstance[] {
const isProd = !isDev;

const plugins = [
new HTMLWebpackPlugin({
template: paths.html,
}), new webpack.ProgressPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].css',
}),
new webpack.ProgressPlugin(),
new webpack.DefinePlugin({
__IS_DEV__: JSON.stringify(isDev),
__API__: JSON.stringify(apiUrl),
__PROJECT__: JSON.stringify(project),
}),
new CopyPlugin({
patterns: [
{ from: paths.locales, to: paths.buildLocales },
],
}),
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
Expand Down Expand Up @@ -59,5 +53,21 @@ export function buildPlugins({
);
}

if (isProd) {
plugins.push(
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash:8].css',
chunkFilename: 'css/[name].[contenthash:8].css',
}),
);
plugins.push(
new CopyPlugin({
patterns: [
{ from: paths.locales, to: paths.buildLocales },
],
}),
);
}

return plugins;
}
2 changes: 1 addition & 1 deletion config/build/buildWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function buildWebpackConfig(options: BuildOptions): webpack.Configuration
rules: buildLoaders(options),
},
resolve: buildResolvers(options),
devtool: isDev ? 'inline-source-map' : undefined,
devtool: isDev ? 'eval-cheap-module-source-map' : undefined,
devServer: isDev ? buildDevServer(options) : undefined,
};
}
1 change: 1 addition & 0 deletions config/build/loaders/buildBabelLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function buildBabelLoader(options: BuildBabelLoaderProps) {
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: ['@babel/preset-env'],
plugins: [
'@babel/plugin-transform-runtime',
Expand Down
1 change: 1 addition & 0 deletions config/build/loaders/buildCssLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MiniCssExtractPlugin from 'mini-css-extract-plugin';
export function buildCssLoader(isDev: boolean) {
return {
test: /\.s[ac]ss$/i,
exclude: /node_modules/,
use: [
// Creates 'style' nodes from JS strings
isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
Expand Down

0 comments on commit 6acf420

Please sign in to comment.