-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
169 lines (160 loc) · 4.18 KB
/
webpack.config.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import path from 'path';
import webpack from 'webpack';
import WebpackNodeExternals from 'webpack-node-externals';
// import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import WebappWebpackPlugin from 'webapp-webpack-plugin';
const favicon_plugin = new WebappWebpackPlugin({
logo: path.resolve(process.cwd(), 'src/app/images/favicon.png'),
inject: false,
prefix: 'images/favicons',
favicons: {
theme_color: '##1d3c8d',
icons: {
android: true,
appleIcon: false,
appleStartup: false,
coast: false,
firefox: false,
windows: false,
yandex: false
}
}
});
// // type Modify<T, R> = Pick<T, Exclude<keyof T, keyof R>> & R;
// // type MyWebpackConfiguration = Modify<
// // webpack.Configuration,
// // {
// // output: webpack.Output;
// // module: webpack.Module;
// // }
// // >;
interface CustomWebpackOutput extends webpack.Output {
filename: string;
path: string;
publicPath: string;
}
export interface CustomWebpackConfiguration extends webpack.Configuration {
name: '' | 'server' | 'client';
output: CustomWebpackOutput;
module: webpack.Module;
plugins: webpack.Plugin[];
}
interface CustomProcessEnv extends NodeJS.ProcessEnv {
NODE_ENV?: 'development' | 'production' | 'none';
}
export default function(env: CustomProcessEnv = process.env, _argv: any): CustomWebpackConfiguration[] {
const is_production = env.NODE_ENV === 'production';
const base: CustomWebpackConfiguration = {
mode: is_production ? 'production' : 'development',
name: '',
// Enable sourcemaps for debugging webpack's output.
devtool: is_production ? 'source-map' : 'cheap-module-eval-source-map',
output: {
filename: '',
// path needs to be an ABSOLUTE file path
path: path.resolve(process.cwd(), 'dist'),
publicPath: is_production ? '/public/' : '/'
},
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: ['.ts', '.tsx', '.js', '.json']
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader'
}
]
},
{
test: /\.(gif|jpeg|jpg|png|svg)$/,
exclude: /favicon\.(gif|jpeg|jpg|png|svg)$/,
use: [
{
// loader-probe-image-size
loader: 'sharp-responsive-multi-image-loader',
options: {
context: path.resolve(__dirname, 'src'),
outputPath: 'images',
name: 'images/[name].[hash:7].[ext]',
name_prefix: 'images/[name].[hash:7]'
}
}
]
},
// loader for favicons - returns html
favicon_plugin.rule(),
{
test: /\.woff2?$/,
use: [
{
loader: 'file-loader',
options: {
context: path.resolve(__dirname, 'src'),
outputPath: 'fonts',
name: '[name].[hash:7].[ext]'
}
}
]
},
{
test: /\.(css|md)$/,
use: [
{
loader: 'raw-loader'
}
]
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
},
// github.com/kangax/html-minifier/issues/727
{
test: [
path.resolve(__dirname, 'node_modules/uglify-js/tools/node.js'),
path.resolve(__dirname, 'node_modules/express/lib/view.js')
],
loader: 'null-loader'
}
]
},
plugins: [
/* new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false }) */
],
watchOptions: { poll: 2000, aggregateTimeout: 500 }
};
return [
{
// server-specific configuration
...base,
name: 'server',
externals: [WebpackNodeExternals()],
entry: is_production ? ['./src/server/index.prod.ts'] : ['./src/server/serverRenderer.ts'],
target: 'node',
output: {
...base.output,
filename: 'server/js/server.js',
libraryTarget: 'commonjs2'
},
plugins: [...base.plugins, favicon_plugin]
},
{
// client-specific configuration
...base,
name: 'client',
entry: is_production ? ['./src/app/index.tsx'] : ['webpack-hot-middleware/client', './src/app/index.tsx'],
target: 'web',
output: {
...base.output,
filename: 'client/js/client.js'
},
plugins: is_production ? base.plugins : [...base.plugins, new webpack.HotModuleReplacementPlugin()]
}
];
}