-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.ts
More file actions
167 lines (162 loc) · 6.51 KB
/
webpack.config.ts
File metadata and controls
167 lines (162 loc) · 6.51 KB
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
import HtmlWebpackPlugin from "html-webpack-plugin";
import * as path from "path";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
import TerserPlugin from "terser-webpack-plugin";
import TsconfigPathsPlugin from "tsconfig-paths-webpack-plugin";
import HtmlWebpackHarddiskPlugin from "html-webpack-harddisk-plugin";
import OptimizeCssAssetsWebpackPlugin from "optimize-css-assets-webpack-plugin";
import autoprefixer from "autoprefixer";
import {glob} from "glob";
const version = "1.0.0";
const isDev = true;
const webapp = path.join(__dirname, "src");
const artifact = path.join(__dirname, "docs");
const webBuild = artifact;
//модули внутри node_modules, которые нам нужно явно затранспайлить, т.к. они сбилдены в es6+
const transpileNodeModules = ["cyrillic-to-translit-js", "query-string", "strict-uri-encode", "split-on-first", "rifm"];
const moduleRegexps = transpileNodeModules.map((module) => new RegExp(`node_modules[\\\\/]${module}`));
export default (env: any, argv: any) => {
const __watch = (watch: any, another: any) => (argv.watch || argv.hot || argv.host ? watch : another);
return {
mode: isDev ? "development" : "production",
entry: {
application: [path.join(webapp, "./index.tsx"), glob.sync("./src/scss/components/**/*.scss"), path.join(webapp, "./scss/index.scss")].flat().filter((e) => !!e),
},
output: {
path: webBuild,
filename: "[name].js",
chunkFilename: "[name].chunk.js",
pathinfo: false,
publicPath: "",
},
target: "web",
devtool: __watch("eval-source-map", "source-map"),
bail: true,
plugins: [
new HtmlWebpackPlugin({
template: path.join(webapp, "./index_template.html"),
filename: path.join(argv.hot || argv.host ? webapp : artifact, "./index.html"),
inject: true,
hash: true,
templateParameters: {
version: new Date().getTime(),
},
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
},
alwaysWriteToDisk: true,
}),
new HtmlWebpackHarddiskPlugin(),
!argv.noCssOptimization &&
new OptimizeCssAssetsWebpackPlugin({
cssProcessor: require("cssnano"),
cssProcessorOptions: {map: {inline: false}},
cssProcessorPluginOptions: {preset: ["default", {discardComments: {removeAll: true}}]},
}),
new MiniCssExtractPlugin({filename: "[name].css"}),
].filter((p) => !!p),
module: {
rules: [
{
test: /\.scss$/,
use: [
{loader: MiniCssExtractPlugin.loader},
{
loader: "css-loader",
options: {
url: false,
sourceMap: true,
},
},
{
loader: "sass-loader",
options: {
sourceMap: true,
implementation: require("sass"),
},
},
{
loader: "postcss-loader",
options: {
postcssOptions: {
parser: "postcss-scss",
plugins: [
autoprefixer({
cascade: true,
add: true,
remove: true,
}),
],
},
},
},
],
},
{
test: /\.tsx?$/,
exclude: (modulePath: string) => /node_modules|vendor/.test(modulePath) && moduleRegexps.some((moduleRegexp) => moduleRegexp.test(modulePath)),
loader: "babel-loader",
options: {
cacheDirectory: true, // включить кэширование
presets: ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-transform-runtime",
],
},
},
],
},
optimization: {
minimize: __watch(false, true),
removeAvailableModules: true,
removeEmptyChunks: true,
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
},
}),
],
},
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".min.js", ".json"],
modules: ["node_modules", "./src/vendor"],
plugins: [
new TsconfigPathsPlugin({
extensions: [".ts", ".tsx", ".js", ".jsx"],
}),
],
},
stats: {
assets: false,
children: false,
chunks: false,
chunkModules: false,
modules: false,
},
performance: {hints: false},
devServer: {
contentBase: webapp,
watchContentBase: true,
host: "lk-local.rt.ru",
port: 8443,
https: true,
hot: false,
inline: false,
stats: {
colors: true,
modules: false,
chunks: false,
chunkModules: false,
children: false,
},
headers: {"Access-Control-Allow-Origin": "*"},
},
};
};