1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-11-30 15:58:09 +09:00
hotomoe/webpack.config.ts

254 lines
6.0 KiB
TypeScript
Raw Normal View History

2017-05-17 00:00:56 +09:00
/**
* webpack configuration
*/
2018-02-22 05:05:19 +09:00
import * as fs from 'fs';
2018-03-15 05:26:24 +09:00
import * as webpack from 'webpack';
import chalk from 'chalk';
2018-03-02 06:26:31 +09:00
import jsonImporter from 'node-sass-json-importer';
2018-03-15 12:56:50 +09:00
const minifyHtml = require('html-minifier').minify;
2018-03-15 05:26:24 +09:00
const WebpackOnBuildPlugin = require('on-build-webpack');
2018-03-15 15:11:05 +09:00
//const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
2018-03-15 05:26:24 +09:00
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
2018-03-15 05:27:53 +09:00
2018-03-29 01:20:40 +09:00
import I18nReplacer from './src/build/i18n';
import { pattern as faPattern, replacement as faReplacement } from './src/build/fa';
2018-03-15 05:26:24 +09:00
const constants = require('./src/const.json');
2018-04-02 13:15:53 +09:00
import config from './src/config';
2018-03-29 01:20:40 +09:00
import { licenseHtml } from './src/build/license';
2018-02-16 03:23:10 +09:00
2018-03-15 12:56:50 +09:00
import locales from './locales';
2018-03-15 05:26:24 +09:00
const meta = require('./package.json');
2018-03-03 07:32:18 +09:00
const version = meta.version;
2018-03-29 14:48:47 +09:00
const codename = meta.codename;
2017-05-17 00:00:56 +09:00
2018-03-15 05:27:53 +09:00
//#region Replacer definitions
2018-02-16 03:23:10 +09:00
global['faReplacement'] = faReplacement;
2018-02-18 15:27:06 +09:00
global['collapseSpacesReplacement'] = html => {
2018-03-15 12:56:50 +09:00
return minifyHtml(html, {
2018-02-18 15:27:06 +09:00
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
keepClosingSlash: true
2018-02-22 05:05:19 +09:00
}).replace(/\t/g, '');
};
global['base64replacement'] = (_, key) => {
2018-03-29 20:32:18 +09:00
return fs.readFileSync(__dirname + '/src/client/' + key, 'base64');
2018-02-18 15:27:06 +09:00
};
2018-03-15 05:27:53 +09:00
//#endregion
2018-02-18 15:27:06 +09:00
2018-03-15 12:56:50 +09:00
const langs = Object.keys(locales);
2018-04-16 15:32:40 +09:00
// 無圧縮スクリプトを用意するのは重いので一時的に無効化
//const entries = process.env.NODE_ENV == 'production'
// ? langs.map(l => [l, false]).concat(langs.map(l => [l, true]))
// : langs.map(l => [l, false]);
2018-03-18 17:21:58 +09:00
const entries = process.env.NODE_ENV == 'production'
2018-04-16 15:32:40 +09:00
? langs.map(l => [l, true])
2018-04-15 06:16:50 +09:00
: langs.map(l => [l, false]);
2018-03-15 12:56:50 +09:00
module.exports = entries.map(x => {
2018-03-18 17:21:58 +09:00
const [lang, isProduction] = x;
2018-03-15 12:56:50 +09:00
2017-05-17 00:00:56 +09:00
// Chunk name
const name = lang;
// Entries
const entry = {
2018-03-29 20:32:18 +09:00
desktop: './src/client/app/desktop/script.ts',
mobile: './src/client/app/mobile/script.ts',
//ch: './src/client/app/ch/script.ts',
//stats: './src/client/app/stats/script.ts',
//status: './src/client/app/status/script.ts',
dev: './src/client/app/dev/script.ts',
auth: './src/client/app/auth/script.ts',
sw: './src/client/app/sw.js'
2017-05-17 00:00:56 +09:00
};
const output = {
2018-03-29 20:32:18 +09:00
path: __dirname + '/built/client/assets',
2018-03-18 17:21:58 +09:00
filename: `[name].${version}.${lang}.${isProduction ? 'min' : 'raw'}.js`
2017-05-17 00:00:56 +09:00
};
2018-03-15 12:56:50 +09:00
const i18nReplacer = new I18nReplacer(lang as string);
2018-02-16 03:23:10 +09:00
global['i18nReplacement'] = i18nReplacer.replacement;
2018-03-15 05:26:24 +09:00
//#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,
2018-03-29 14:48:47 +09:00
_CODENAME_: codename,
2018-03-15 05:26:24 +09:00
_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,
2018-03-15 05:26:24 +09:00
_DEV_URL_: config.dev_url,
_LANG_: lang,
_HOST_: config.host,
2018-03-26 13:21:41 +09:00
_HOSTNAME_: config.hostname,
2018-03-15 05:26:24 +09:00
_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 = [
2018-03-15 15:11:05 +09:00
//new HardSourceWebpackPlugin(),
2018-03-15 05:26:24 +09:00
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({
2018-03-18 17:21:58 +09:00
'process.env.NODE_ENV': JSON.stringify(isProduction ? 'production' : 'development')
2018-03-15 05:26:24 +09:00
}),
new WebpackOnBuildPlugin(stats => {
fs.writeFileSync('./version.json', JSON.stringify({
version
}), 'utf-8');
})
];
2018-03-18 17:21:58 +09:00
if (isProduction) {
2018-03-15 05:26:24 +09:00
plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
}
2017-05-17 00:00:56 +09:00
return {
name,
entry,
2018-02-16 03:23:10 +09:00
module: {
rules: [{
test: /\.vue$/,
exclude: /node_modules/,
2018-03-03 09:49:47 +09:00
use: [{
2018-02-16 03:23:10 +09:00
loader: 'vue-loader',
options: {
cssSourceMap: false,
preserveWhitespace: false
}
2018-02-22 05:05:19 +09:00
}, {
loader: 'replace',
query: {
search: /%base64:(.+?)%/g.toString(),
replace: 'base64replacement'
}
2018-02-16 03:23:10 +09:00
}, {
loader: 'replace',
query: {
search: i18nReplacer.pattern.toString(),
2018-04-15 02:51:29 +09:00
replace: 'i18nReplacement',
2018-04-15 06:16:50 +09:00
i18n: true,
lang
2018-02-16 03:23:10 +09:00
}
}, {
loader: 'replace',
query: {
search: faPattern.toString(),
replace: 'faReplacement'
}
2018-02-18 15:27:06 +09:00
}, {
loader: 'replace',
query: {
search: /^<template>([\s\S]+?)\r?\n<\/template>/.toString(),
replace: 'collapseSpacesReplacement'
}
2018-02-16 03:23:10 +09:00
}]
}, {
test: /\.styl$/,
exclude: /node_modules/,
2018-03-03 13:47:55 +09:00
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader',
options: {
minimize: true
}
}, {
loader: 'stylus-loader'
2018-03-19 22:48:00 +09:00
}]
2018-03-02 06:26:31 +09:00
}, {
test: /\.scss$/,
exclude: /node_modules/,
use: [{
loader: 'style-loader'
}, {
2018-03-03 13:47:55 +09:00
loader: 'css-loader',
options: {
minimize: true
}
2018-03-02 06:26:31 +09:00
}, {
loader: 'sass-loader',
options: {
importer: jsonImporter,
}
}]
2018-02-21 05:55:19 +09:00
}, {
test: /\.css$/,
2018-03-03 13:47:55 +09:00
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader',
options: {
minimize: true
}
}]
2018-03-02 06:26:31 +09:00
}, {
test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
2018-03-03 09:49:47 +09:00
loader: 'url-loader'
2018-02-16 03:23:10 +09:00
}, {
test: /\.ts$/,
exclude: /node_modules/,
2018-02-16 03:26:59 +09:00
use: [{
loader: 'ts-loader',
options: {
2018-03-03 07:32:18 +09:00
happyPackMode: true,
2018-03-29 20:32:18 +09:00
configFile: __dirname + '/src/client/app/tsconfig.json',
2018-02-16 03:26:59 +09:00
appendTsSuffixTo: [/\.vue$/]
}
}, {
loader: 'replace',
query: {
search: i18nReplacer.pattern.toString(),
2018-04-15 02:51:29 +09:00
replace: 'i18nReplacement',
2018-04-15 06:16:50 +09:00
i18n: true,
lang
2018-02-16 03:26:59 +09:00
}
}, {
loader: 'replace',
query: {
search: faPattern.toString(),
replace: 'faReplacement'
}
}]
2018-02-16 03:23:10 +09:00
}]
},
2018-03-15 05:26:24 +09:00
plugins,
2017-11-13 18:05:35 +09:00
output,
resolve: {
extensions: [
2018-02-25 17:03:39 +09:00
'.js', '.ts', '.json'
2018-03-03 13:47:55 +09:00
],
alias: {
2018-03-29 20:32:18 +09:00
'const.styl': __dirname + '/src/client/const.styl'
2018-03-03 13:47:55 +09:00
}
2018-02-15 13:18:34 +09:00
},
2018-02-16 02:53:54 +09:00
resolveLoader: {
modules: ['node_modules', './webpack/loaders']
},
2018-03-15 05:26:24 +09:00
cache: true,
2018-03-15 20:33:28 +09:00
devtool: false, //'source-map',
2018-03-18 17:21:58 +09:00
mode: isProduction ? 'production' : 'development'
2017-05-17 00:00:56 +09:00
};
});