forked from osmosis-labs/osmosis-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
175 lines (168 loc) · 4.13 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* eslint-disable @typescript-eslint/no-var-requires */
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const WriteFilePlugin = require('write-file-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const isEnvDevelopment = process.env.NODE_ENV !== 'production';
const isEnvAnalyzer = process.env.ANALYZER === 'true';
const commonResolve = dir => ({
extensions: ['.ts', '.tsx', '.js', '.jsx', '.css', '.scss'],
alias: {
assets: path.resolve(__dirname, dir),
},
plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })],
});
const sassRule = {
test: /(\.s?css)|(\.sass)$/,
exclude: /node_modules/,
oneOf: [
{
test: /\.(s?css)|(sass)$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
],
},
// if ext includes module as prefix, it perform by css loader.
{
test: /.module(\.s?css)|(\.sass)$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[local]-[hash:base64]',
},
localsConvention: 'camelCase',
},
},
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
],
},
{
use: [
'style-loader',
{ loader: 'css-loader', options: { modules: false } },
{
loader: 'sass-loader',
options: {
implementation: require('sass'),
},
},
],
},
],
};
const cssRule = {
test: /\.css$/,
use: ['style-loader', 'css-loader'],
};
const tsRule = {
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('ts-loader'),
options: {},
},
],
};
const jsxRule = {
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
};
const fileRule = {
test: /\.(svg|png|jpe?g|gif|woff|woff2|eot|ttf)$/i,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
publicPath: 'assets',
outputPath: 'assets',
},
},
],
};
// https://webpack.js.org/guides/public-path/
const ASSET_PATH = process.env.ASSET_PATH || '/';
const webConfig = () => {
return {
mode: isEnvDevelopment ? 'development' : 'production',
// In development environment, turn on source map.
devtool: isEnvDevelopment ? 'source-map' : false,
// In development environment, webpack watch the file changes, and recompile
watch: isEnvDevelopment,
devServer: {
port: 8080,
historyApiFallback: true,
},
entry: {
main: ['./src/index.tsx'],
},
output: {
path: path.resolve(__dirname, isEnvDevelopment ? 'dist' : 'prod'),
filename: '[name].bundle.js',
publicPath: ASSET_PATH,
},
resolve: commonResolve('src/assets'),
module: {
rules: [sassRule, cssRule, tsRule, jsxRule, fileRule],
},
optimization: {
usedExports: true,
},
plugins: [
// Remove all and write anyway
// TODO: Optimizing build process
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [{ from: 'public', to: 'public' }],
}),
new ForkTsCheckerWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'styles.css',
chunkFilename: '[name].css',
}),
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
chunks: ['main'],
}),
new WriteFilePlugin(),
new webpack.EnvironmentPlugin(['NODE_ENV']),
isEnvAnalyzer &&
new BundleAnalyzerPlugin({
analyzerMode: isEnvAnalyzer ? 'server' : 'disabled',
}),
// This makes it possible for us to safely use env vars on our code
new webpack.DefinePlugin({
'process.env.ASSET_PATH': JSON.stringify(ASSET_PATH),
}),
].filter(Boolean),
};
};
module.exports = webConfig;