paricafe/webpack.config.ts

241 lines
5.4 KiB
TypeScript
Raw Normal View History

2017-05-16 10:00:56 -05:00
/**
* webpack configuration
*/
2018-02-21 14:05:19 -06:00
import * as fs from 'fs';
2018-03-14 15:26:24 -05:00
import * as webpack from 'webpack';
import chalk from 'chalk';
2018-03-01 15:26:31 -06:00
import jsonImporter from 'node-sass-json-importer';
2018-02-18 00:27:06 -06:00
const minify = require('html-minifier').minify;
2018-03-14 15:26:24 -05:00
const WebpackOnBuildPlugin = require('on-build-webpack');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
2018-03-14 15:27:53 -05:00
2018-03-14 15:26:24 -05:00
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';
2018-02-15 12:23:10 -06:00
2018-03-14 15:26:24 -05:00
import langs from './locales';
const meta = require('./package.json');
2018-03-02 16:32:18 -06:00
const version = meta.version;
2017-05-16 10:00:56 -05:00
2018-03-14 15:26:24 -05:00
const env = process.env.NODE_ENV;
const isProduction = env === 'production';
2018-03-14 15:27:53 -05:00
//#region Replacer definitions
2018-02-15 12:23:10 -06:00
global['faReplacement'] = faReplacement;
2018-02-18 00:27:06 -06:00
global['collapseSpacesReplacement'] = html => {
return minify(html, {
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
keepClosingSlash: true
2018-02-21 14:05:19 -06:00
}).replace(/\t/g, '');
};
global['base64replacement'] = (_, key) => {
2018-03-14 15:26:24 -05:00
return fs.readFileSync(__dirname + '/src/web/' + key, 'base64');
2018-02-18 00:27:06 -06:00
};
2018-03-14 15:27:53 -05:00
//#endregion
2018-02-18 00:27:06 -06:00
2017-12-16 23:35:30 -06:00
module.exports = Object.keys(langs).map(lang => {
2017-05-16 10:00:56 -05:00
// Chunk name
const name = lang;
// Entries
const entry = {
2017-11-13 03:05:35 -06:00
desktop: './src/web/app/desktop/script.ts',
2018-02-21 11:00:30 -06:00
mobile: './src/web/app/mobile/script.ts',
2018-02-09 19:27:05 -06:00
//ch: './src/web/app/ch/script.ts',
//stats: './src/web/app/stats/script.ts',
//status: './src/web/app/status/script.ts',
2018-02-27 09:11:28 -06:00
dev: './src/web/app/dev/script.ts',
auth: './src/web/app/auth/script.ts',
2017-11-20 14:09:45 -06:00
sw: './src/web/app/sw.js'
2017-05-16 10:00:56 -05:00
};
const output = {
2018-03-14 15:26:24 -05:00
path: __dirname + '/built/web/assets',
2017-05-16 10:00:56 -05:00
filename: `[name].${version}.${lang}.js`
};
2018-02-15 12:23:10 -06:00
const i18nReplacer = new I18nReplacer(lang);
global['i18nReplacement'] = i18nReplacer.replacement;
2018-03-14 15:26:24 -05: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,
_STATUS_URL_: config.status_url,
_STATS_URL_: config.stats_url,
_DOCS_URL_: config.docs_url,
_API_URL_: config.api_url,
_DEV_URL_: config.dev_url,
_CH_URL_: config.ch_url,
_LANG_: lang,
_HOST_: config.host,
_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(process.env.NODE_ENV)
}
}),
new WebpackOnBuildPlugin(stats => {
fs.writeFileSync('./version.json', JSON.stringify({
version
}), 'utf-8');
})
];
if (isProduction) {
plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
plugins.push(minify());
}
2017-05-16 10:00:56 -05:00
return {
name,
entry,
2018-02-15 12:23:10 -06:00
module: {
rules: [{
test: /\.vue$/,
exclude: /node_modules/,
2018-03-02 18:49:47 -06:00
use: [{
2018-02-15 12:23:10 -06:00
loader: 'vue-loader',
options: {
cssSourceMap: false,
preserveWhitespace: false
}
2018-02-21 14:05:19 -06:00
}, {
loader: 'replace',
query: {
search: /%base64:(.+?)%/g.toString(),
replace: 'base64replacement'
}
2018-02-15 12:23:10 -06:00
}, {
loader: 'replace',
query: {
search: i18nReplacer.pattern.toString(),
replace: 'i18nReplacement'
}
}, {
loader: 'replace',
query: {
search: faPattern.toString(),
replace: 'faReplacement'
}
2018-02-18 00:27:06 -06:00
}, {
loader: 'replace',
query: {
search: /^<template>([\s\S]+?)\r?\n<\/template>/.toString(),
replace: 'collapseSpacesReplacement'
}
2018-02-15 12:23:10 -06:00
}]
}, {
test: /\.styl$/,
exclude: /node_modules/,
2018-03-02 22:47:55 -06:00
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader',
options: {
minimize: true
}
}, {
loader: 'stylus-loader'
}
2018-02-15 12:23:10 -06:00
]
2018-03-01 15:26:31 -06:00
}, {
test: /\.scss$/,
exclude: /node_modules/,
use: [{
loader: 'style-loader'
}, {
2018-03-02 22:47:55 -06:00
loader: 'css-loader',
options: {
minimize: true
}
2018-03-01 15:26:31 -06:00
}, {
loader: 'sass-loader',
options: {
importer: jsonImporter,
}
}]
2018-02-20 14:55:19 -06:00
}, {
test: /\.css$/,
2018-03-02 22:47:55 -06:00
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader',
options: {
minimize: true
}
}]
2018-03-01 15:26:31 -06:00
}, {
test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
2018-03-02 18:49:47 -06:00
loader: 'url-loader'
2018-02-15 12:23:10 -06:00
}, {
test: /\.ts$/,
exclude: /node_modules/,
2018-02-15 12:26:59 -06:00
use: [{
loader: 'ts-loader',
options: {
2018-03-02 16:32:18 -06:00
happyPackMode: true,
2018-02-15 12:26:59 -06:00
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'
}
}]
2018-02-15 12:23:10 -06:00
}]
},
2018-03-14 15:26:24 -05:00
plugins,
2017-11-13 03:05:35 -06:00
output,
resolve: {
extensions: [
2018-02-25 02:03:39 -06:00
'.js', '.ts', '.json'
2018-03-02 22:47:55 -06:00
],
alias: {
2018-03-14 15:26:24 -05:00
'const.styl': __dirname + '/src/web/const.styl'
2018-03-02 22:47:55 -06:00
}
2018-02-14 22:18:34 -06:00
},
2018-02-15 11:53:54 -06:00
resolveLoader: {
modules: ['node_modules', './webpack/loaders']
},
2018-03-14 15:26:24 -05:00
cache: true,
devtool: 'source-map'
2017-05-16 10:00:56 -05:00
};
});