summaryrefslogtreecommitdiff
path: root/webpack.config.ts
blob: 88af49eb53efc2792b63a9228d29f70c0722852a (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * webpack configuration
 */

import * as fs from 'fs';
import * as webpack from 'webpack';
import chalk from 'chalk';
import jsonImporter from 'node-sass-json-importer';
const minifyHtml = require('html-minifier').minify;
const WebpackOnBuildPlugin = require('on-build-webpack');
//const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');

import I18nReplacer from './src/common/build/i18n';
import { pattern as faPattern, replacement as faReplacement } from './src/common/build/fa';
const constants = require('./src/const.json');
import config from './src/conf';
import { licenseHtml } from './src/common/build/license';

import locales from './locales';
const meta = require('./package.json');
const version = meta.version;
const codename = meta.codename;

//#region Replacer definitions
global['faReplacement'] = faReplacement;

global['collapseSpacesReplacement'] = html => {
	return minifyHtml(html, {
		collapseWhitespace: true,
		collapseInlineTagWhitespace: true,
		keepClosingSlash: true
	}).replace(/\t/g, '');
};

global['base64replacement'] = (_, key) => {
	return fs.readFileSync(__dirname + '/src/web/' + key, 'base64');
};
//#endregion

const langs = Object.keys(locales);

const entries = process.env.NODE_ENV == 'production'
	? langs.map(l => [l, false]).concat(langs.map(l => [l, true]))
	: [['ja', false]];

module.exports = entries.map(x => {
	const [lang, isProduction] = x;

	// Chunk name
	const name = lang;

	// Entries
	const entry = {
		desktop: './src/web/app/desktop/script.ts',
		mobile: './src/web/app/mobile/script.ts',
		//ch: './src/web/app/ch/script.ts',
		//stats: './src/web/app/stats/script.ts',
		//status: './src/web/app/status/script.ts',
		dev: './src/web/app/dev/script.ts',
		auth: './src/web/app/auth/script.ts',
		sw: './src/web/app/sw.js'
	};

	const output = {
		path: __dirname + '/built/web/assets',
		filename: `[name].${version}.${lang}.${isProduction ? 'min' : 'raw'}.js`
	};

	const i18nReplacer = new I18nReplacer(lang as string);
	global['i18nReplacement'] = i18nReplacer.replacement;

	//#region Define consts
	const consts = {
		_RECAPTCHA_SITEKEY_: config.recaptcha.site_key,
		_SW_PUBLICKEY_: config.sw ? config.sw.public_key : null,
		_THEME_COLOR_: constants.themeColor,
		_COPYRIGHT_: constants.copyright,
		_VERSION_: version,
		_CODENAME_: codename,
		_STATUS_URL_: config.status_url,
		_STATS_URL_: config.stats_url,
		_DOCS_URL_: config.docs_url,
		_API_URL_: config.api_url,
		_WS_URL_: config.ws_url,
		_DEV_URL_: config.dev_url,
		_LANG_: lang,
		_HOST_: config.host,
		_HOSTNAME_: config.hostname,
		_URL_: config.url,
		_LICENSE_: licenseHtml,
		_GOOGLE_MAPS_API_KEY_: config.google_maps_api_key
	};

	const _consts = {};

	Object.keys(consts).forEach(key => {
		_consts[key] = JSON.stringify(consts[key]);
	});
	//#endregion

	const plugins = [
		//new HardSourceWebpackPlugin(),
		new ProgressBarPlugin({
			format: chalk`  {cyan.bold yes we can} {bold [}:bar{bold ]} {green.bold :percent} {gray (:current/:total)} :elapseds`,
			clear: false
		}),
		new webpack.DefinePlugin(_consts),
		new webpack.DefinePlugin({
			'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development')
		}),
		new WebpackOnBuildPlugin(stats => {
			fs.writeFileSync('./version.json', JSON.stringify({
				version
			}), 'utf-8');
		})
	];

	if (isProduction) {
		plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
	}

	return {
		name,
		entry,
		module: {
			rules: [{
				test: /\.vue$/,
				exclude: /node_modules/,
				use: [{
					loader: 'vue-loader',
					options: {
						cssSourceMap: false,
						preserveWhitespace: false
					}
				}, {
					loader: 'replace',
					query: {
						search: /%base64:(.+?)%/g.toString(),
						replace: 'base64replacement'
					}
				}, {
					loader: 'replace',
					query: {
						search: i18nReplacer.pattern.toString(),
						replace: 'i18nReplacement'
					}
				}, {
					loader: 'replace',
					query: {
						search: faPattern.toString(),
						replace: 'faReplacement'
					}
				}, {
					loader: 'replace',
					query: {
						search: /^<template>([\s\S]+?)\r?\n<\/template>/.toString(),
						replace: 'collapseSpacesReplacement'
					}
				}]
			}, {
				test: /\.styl$/,
				exclude: /node_modules/,
				use: [{
					loader: 'style-loader'
				}, {
					loader: 'css-loader',
					options: {
						minimize: true
					}
				}, {
					loader: 'stylus-loader'
				}]
			}, {
				test: /\.scss$/,
				exclude: /node_modules/,
				use: [{
					loader: 'style-loader'
				}, {
					loader: 'css-loader',
					options: {
						minimize: true
					}
				}, {
					loader: 'sass-loader',
					options: {
						importer: jsonImporter,
					}
				}]
			}, {
				test: /\.css$/,
				use: [{
					loader: 'style-loader'
				}, {
					loader: 'css-loader',
					options: {
						minimize: true
					}
				}]
			}, {
				test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
				loader: 'url-loader'
			}, {
				test: /\.ts$/,
				exclude: /node_modules/,
				use: [{
					loader: 'ts-loader',
					options: {
						happyPackMode: true,
						configFile: __dirname + '/../src/web/app/tsconfig.json',
						appendTsSuffixTo: [/\.vue$/]
					}
				}, {
					loader: 'replace',
					query: {
						search: i18nReplacer.pattern.toString(),
						replace: 'i18nReplacement'
					}
				}, {
					loader: 'replace',
					query: {
						search: faPattern.toString(),
						replace: 'faReplacement'
					}
				}]
			}]
		},
		plugins,
		output,
		resolve: {
			extensions: [
				'.js', '.ts', '.json'
			],
			alias: {
				'const.styl': __dirname + '/src/web/const.styl'
			}
		},
		resolveLoader: {
			modules: ['node_modules', './webpack/loaders']
		},
		cache: true,
		devtool: false, //'source-map',
		mode: isProduction ? 'production' : 'development'
	};
});