Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task2 webpack #2

Merged
merged 4 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port

build
16 changes: 16 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
],
"plugins": [
[
"babel-plugin-styled-components",
{
"ssr": false,
"pure": true
}
]
]
}
47 changes: 47 additions & 0 deletions config/webpack.config.common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Configuration } from "webpack";
import { join, resolve } from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";

export const config: Configuration = {
entry: resolve("src", "index.tsx"),
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ["babel-loader"],
},
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use: ["ts-loader"],
},
{
test: /\.(jpg|jpeg|png|gif|mp3|svg)$/,
use: ["file-loader"],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: join("src", "index.html"),
}),
],
optimization: {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would, probably, make more sense for production mode only.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree here. Having a separate vendor chunk is useful in dev mode as well. It's better to keep the two modes as similar as possible to avoid prod bugs which not be reproduced on dev mode.
Also, it's a generally accepted practice to have a separate vendor chunk rather then build everything in one file

splitChunks: {
cacheGroups: {
vendors: {
test: /node_modules/,
name: "vendors",
chunks: "all",
},
},
},
runtimeChunk: {
name: "manifest",
},
},
};
22 changes: 22 additions & 0 deletions config/webpack.config.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import { merge } from 'webpack-merge';
import { resolve } from 'path';

import { config } from './webpack.config.common';
import { WebpackDevConfiguration } from './webpack.models';

export const webpackDevConfigPart: WebpackDevConfiguration = {
mode: 'development',
devtool: 'source-map',
output: {
path: resolve("build"),
filename: "[name].bundle.js",
clean: true,
},
devServer: {
open: true,
hot: true,
},
};

export default merge(config, webpackDevConfigPart);
34 changes: 34 additions & 0 deletions config/webpack.config.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { resolve } from 'path';
import { Configuration } from "webpack";
import { merge } from "webpack-merge";
import CompressionPlugin from "compression-webpack-plugin";

import { config } from "./webpack.config.common";

const webpackProdConfigPart: Configuration = {
mode: "production",
output: {
path: resolve("build"),
filename: "[name].[chunkhash].bundle.js",
clean: true,
},
performance: {
hints: "warning",
maxAssetSize: 1_000_000,
// Calculates sizes of gziped bundles.
assetFilter: function (assetFilename: string) {
return assetFilename.endsWith(".js.gz");
},
},
plugins: [
/**
* cast to any since the plugin type is incompatible with webpack 5
* https://github.com/DefinitelyTyped/DefinitelyTyped/pull/55708
*/
new CompressionPlugin({
algorithm: "gzip",
}) as any,
],
};

export default merge(config, webpackProdConfigPart);
6 changes: 6 additions & 0 deletions config/webpack.models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Configuration } from 'webpack';
import { Configuration as WebpackDevServerConfig } from 'webpack-dev-server';

export interface WebpackDevConfiguration extends Configuration {
devServer: WebpackDevServerConfig;
}
Loading