forked from Alethio/ethereum-lite-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
196 lines (182 loc) · 7.72 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// @ts-check
var webpack = require("webpack");
var path = require("path");
var fs = require("fs");
var UglifyJsPlugin = require("uglifyjs-webpack-plugin");
var CopyWebpackPlugin = require("copy-webpack-plugin");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var InterpolateHtmlPlugin = require("interpolate-html-plugin");
var WebappWebpackPlugin = require('webapp-webpack-plugin');
var GitRevisionPlugin = require("git-revision-webpack-plugin");
var ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
var createStyledComponentsTransformer = require("typescript-plugin-styled-components").default;
function getConfig(isProduction) {
var isDebug = !isProduction;
var sourceRoot = path.join(__dirname, "src");
var appRoot = path.join(__dirname, "src/app");
var nodeModulesPath = path.join(__dirname, "node_modules");
var sourcePublicRoot = path.join(__dirname, "src/public");
var ethstatsUiPublicRoot = path.join(nodeModulesPath, "@alethio/ui/public");
var assetsRoot = path.join(__dirname, "src/assets");
var outputRoot = path.join(__dirname, "dist");
var localesInfo = JSON.parse(fs.readFileSync(path.join(appRoot, "translation", "locales.json"), "utf8"));
var defaultLocale = localesInfo.find(l => !!l.default).locale;
var availableLocales = localesInfo.map(l => l.locale);
var translation = require(path.join(appRoot, "translation", defaultLocale + ".json"));
var gitRevisionPlugin = new GitRevisionPlugin({
branch: true
});
var tsConfigJsonFilename = "tsconfig.webpack.json";
var styledComponentsTransformer = createStyledComponentsTransformer();
var basePath = (process.env.APP_BASE_PATH || "").replace(/^\//, "").replace(/\/$/, "");
var plugins = [
new ForkTsCheckerWebpackPlugin({
tsconfig: path.resolve(".", tsConfigJsonFilename),
tslint: path.resolve(".", isProduction ? "tslint.prod.json" : "tslint.json"),
async: false
}),
// These are preprocessor constants which are replaced inline (that"s why the extra quotes)
new webpack.DefinePlugin({
// This is needed to be able to activate some features for development (like ReduxDevTools)
// and, also, some libs (like React) have an optimized (smaller&faster) builds
// if NODE_ENV is set to "production"
"process.env.NODE_ENV": JSON.stringify(isDebug ? "development" : "production"),
// Git version info is needed by Sentry
"GIT_VERSION": JSON.stringify(gitRevisionPlugin.version()),
"GIT_COMMITHASH": JSON.stringify(gitRevisionPlugin.commithash()),
"GIT_BRANCH": JSON.stringify(gitRevisionPlugin.branch()),
"APP_AVAILABLE_LOCALES": JSON.stringify(availableLocales.join(",")),
"APP_DEFAULT_LOCALE": JSON.stringify(defaultLocale)
}),
new CopyWebpackPlugin([
{
from: sourcePublicRoot,
to: outputRoot,
ignore: ["index.html"]
},
{
from: path.join(ethstatsUiPublicRoot, "fonts"),
to: path.join(outputRoot, "fonts"),
},
{
from: path.join(ethstatsUiPublicRoot, "css/fonts.css"),
to: path.join(outputRoot, "css/fonts.css"),
transform(content, path) {
return content.toString().replace(/\/fonts/g, (basePath ? "/" + basePath : "") + "/fonts");
}
},
{
from: path.join(ethstatsUiPublicRoot, "css/normalize.css"),
to: path.join(outputRoot, "css/normalize.css"),
}
]),
new HtmlWebpackPlugin({
inject: "body",
hash: true,
title: translation["title"],
description: translation["description"],
template: path.join(sourcePublicRoot, "index.html"),
baseUrl: process.env.APP_BASE_URL ? process.env.APP_BASE_URL.replace(/\/$/, "") + "/" : "https://lite-explorer.aleth.io/",
basePath: basePath ? "/" + basePath + "/" : "/"
}),
new InterpolateHtmlPlugin({
"APP_DEFAULT_LOCALE": defaultLocale
}),
new WebappWebpackPlugin({
logo: path.join(assetsRoot, "alethio-small.svg"),
title: translation["title"]
})
];
if (!isDebug) {
plugins.push(new webpack.HashedModuleIdsPlugin());
}
return {
mode: isDebug ? "development" : "production",
context: appRoot,
entry: {
// Entry points (bundles)
// The key is a bundle name, the value is the entry file
// If you are using async require, you also need to add
// __webpack_public_path__ to the top of the entry file
"js/app": "./index"
},
output: {
path: outputRoot,
jsonpFunction: "__webpackJsonp",
hotUpdateFunction: "__webpackHotUpdate",
sourcePrefix: "",
crossOriginLoading: "anonymous",
filename: "[name].bundle.js",
chunkFilename: "js/[contentHash].bundle.js",
pathinfo: !!isDebug,
publicPath: (basePath ? "/" + basePath : "") + "/"
},
optimization: isDebug ? void 0 : {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
// Don't merge statements with comma. This makes breakpoints unusable in debugger.
compress: {
sequences: false,
join_vars: false,
// Dropping unused variables causes problems with libraries such as MobX
unused: false,
warnings: false,
// FIX https://github.com/mishoo/UglifyJS2/issues/1317
if_return: false,
// FIX https://github.com/mishoo/UglifyJS2/issues/2498
properties: false
},
mangle: {
safari10: true
}
},
sourceMap: true
})
]
},
module: {
rules: [
{
test: /\.jsx?$/,
enforce: "pre",
loader: "source-map-loader"
},
{
test: /\.tsx?$/,
loader: "ts-loader?configFile=" + tsConfigJsonFilename,
options: {
transpileOnly: true,
getCustomTransformers: () => ({ before: [styledComponentsTransformer] })
}
},
{
test: /\.(png|jpe?g|gif|svg)$/i,
loader: "url-loader",
options: {
limit: 8192,
outputPath: "img/"
}
}
]
},
resolve: {
modules: [
sourceRoot,
"./node_modules"
],
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
},
// If incremental build performance becomes an issue, this option can be enabled
// to generate faster source maps
devtool: /*isDebug ? 'eval-source-map' : */"source-map",
// Uncomment this to use polling instead of file watchers (e.g. too few available
// from environment)
watchOptions: {
aggregateTimeout: 0
//poll: true
},
plugins: plugins
};
}
module.exports = getConfig;