-
Notifications
You must be signed in to change notification settings - Fork 148
/
webpack.common.ts
153 lines (146 loc) · 3.92 KB
/
webpack.common.ts
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
// Copyright Contributors to the Amundsen project.
// SPDX-License-Identifier: Apache-2.0
import * as path from 'path';
import * as fs from 'fs';
import * as webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
// import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import MomentLocalesPlugin from 'moment-locales-webpack-plugin';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import appConfig from './js/config/config';
function resolve(dir) {
return path.join(__dirname, dir);
}
const TSX_PATTERN = /\.ts|\.tsx$/;
const JSX_PATTERN = /\.jsx?$/;
const CSS_PATTERN = /\.(sa|sc|c)ss$/;
const IMAGE_PATTERN = /\.(png|svg|jpg|gif)$/;
const VENDORS_PATTERN = /[\\/]node_modules[\\/](react|react-dom)[\\/]/;
const FONT_PATTERN = /\.(ttf|woff2|otf)$/;
const RESOLVED_EXTENSIONS = ['.tsx', '.ts', '.js', '.jsx', '.css', '.scss'];
const PATHS = {
dist: resolve('/dist'),
pages: resolve('/js/pages'),
components: resolve('/js/components'),
features: resolve('/js/features'),
config: resolve('/js/config'),
ducks: resolve('/js/ducks'),
interfaces: resolve('/js/interfaces'),
utils: resolve('/js/utils'),
css: resolve('/css/'),
};
// Process of Templates
const walkSync = (dir: string, filelist: string[] = []) => {
fs.readdirSync(dir).forEach((file) => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file));
});
return filelist;
};
const templatesList = walkSync('templates');
const htmlWebpackPluginConfig = templatesList.map(
(file) =>
new HtmlWebpackPlugin({
filename: file,
template: file,
config: appConfig,
inject: false,
})
);
const config: webpack.Configuration = {
entry: {
main: [resolve('/css/styles.scss'), resolve('/js/index.tsx')],
},
output: {
publicPath: '/static/dist/',
path: PATHS.dist,
filename: '[name].[contenthash].js',
},
devtool: 'source-map',
resolve: {
alias: {
pages: PATHS.pages,
components: PATHS.components,
features: PATHS.features,
config: PATHS.config,
ducks: PATHS.ducks,
interfaces: PATHS.interfaces,
utils: PATHS.utils,
},
extensions: RESOLVED_EXTENSIONS,
fallback: {
// Needed by react-markdown as of 5.0.2
path: require.resolve('path-browserify'),
},
},
module: {
rules: [
{
test: TSX_PATTERN,
loader: 'ts-loader',
},
{
test: JSX_PATTERN,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: CSS_PATTERN,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: [PATHS.css],
},
},
},
],
},
{
test: IMAGE_PATTERN,
use: 'file-loader',
},
{
test: FONT_PATTERN,
use: 'file-loader',
},
// Hacky, required for react-bootstrap @ 0.33.1 - remove after upgrading,
// see: https://github.com/webpack/webpack/issues/11467
// Tracked at https://github.com/amundsen-io/amundsen/issues/818
{
test: /\.m?js$/,
resolve: {
fullySpecified: false,
},
},
],
},
plugins: [
new CleanWebpackPlugin(),
new MomentLocalesPlugin(), // To strip all locales except “en”
...htmlWebpackPluginConfig,
// fix "process is not defined" error:
new webpack.ProvidePlugin({
process: 'process/browser',
}),
],
optimization: {
moduleIds: 'deterministic',
splitChunks: {
cacheGroups: {
default: false,
major: {
name: 'vendors',
test: VENDORS_PATTERN,
chunks: 'all',
},
},
},
},
};
export default config;