This commit is contained in:
parent
05dd381502
commit
14f7ff13ec
4 changed files with 229 additions and 207 deletions
|
@ -7,7 +7,7 @@ import locale from '../../locales';
|
||||||
export default class Replacer {
|
export default class Replacer {
|
||||||
private lang: string;
|
private lang: string;
|
||||||
|
|
||||||
public pattern = /%i18n:([a-z0-9_\-@\.\!]+?)%/g;
|
public pattern = /%i18n:([a-z0-9_\-\.\!]+?)%/g;
|
||||||
|
|
||||||
constructor(lang: string) {
|
constructor(lang: string) {
|
||||||
this.lang = lang;
|
this.lang = lang;
|
||||||
|
@ -53,23 +53,20 @@ export default class Replacer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public replacement(ctx, match, key) {
|
public replacement(match, key) {
|
||||||
const client = '/src/client/app/';
|
let path = null;
|
||||||
let name = null;
|
|
||||||
|
|
||||||
const shouldEscape = key[0] == '!';
|
const shouldEscape = key[0] == '!';
|
||||||
if (shouldEscape) {
|
if (shouldEscape) {
|
||||||
key = key.substr(1);
|
key = key.substr(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key[0] == '@') {
|
if (key.indexOf('|') != -1) {
|
||||||
name = ctx.src.substr(ctx.src.indexOf(client) + client.length);
|
path = key.split('|')[0];
|
||||||
key = key.substr(1);
|
key = key.split('|')[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx && ctx.lang) this.lang = ctx.lang;
|
const txt = this.get(path, key);
|
||||||
|
|
||||||
const txt = this.get(name, key);
|
|
||||||
|
|
||||||
return shouldEscape
|
return shouldEscape
|
||||||
? txt.replace(/'/g, '\\x27').replace(/"/g, '\\x22')
|
? txt.replace(/'/g, '\\x27').replace(/"/g, '\\x22')
|
||||||
|
|
|
@ -11,8 +11,10 @@ const minifyHtml = require('html-minifier').minify;
|
||||||
const WebpackOnBuildPlugin = require('on-build-webpack');
|
const WebpackOnBuildPlugin = require('on-build-webpack');
|
||||||
//const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
|
//const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
|
||||||
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
|
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
|
||||||
|
const I18nPlugin = require('webpack-multilang-i18n-plugin');
|
||||||
|
|
||||||
import I18nReplacer from './src/build/i18n';
|
import I18nReplacer from './src/build/i18n';
|
||||||
|
import { pattern as i18nPattern, replacement as i18nReplacement } from './webpack/i18n';
|
||||||
import { pattern as faPattern, replacement as faReplacement } from './src/build/fa';
|
import { pattern as faPattern, replacement as faReplacement } from './src/build/fa';
|
||||||
const constants = require('./src/const.json');
|
const constants = require('./src/const.json');
|
||||||
import config from './src/config';
|
import config from './src/config';
|
||||||
|
@ -37,46 +39,33 @@ global['collapseSpacesReplacement'] = html => {
|
||||||
global['base64replacement'] = (_, key) => {
|
global['base64replacement'] = (_, key) => {
|
||||||
return fs.readFileSync(__dirname + '/src/client/' + key, 'base64');
|
return fs.readFileSync(__dirname + '/src/client/' + key, 'base64');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
global['i18nReplacement'] = i18nReplacement;
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
const langs = Object.keys(locales);
|
const langs = Object.keys(locales);
|
||||||
|
|
||||||
// 無圧縮スクリプトを用意するのは重いので一時的に無効化
|
const isProduction = process.env.NODE_ENV == 'production';
|
||||||
//const entries = process.env.NODE_ENV == 'production'
|
|
||||||
// ? langs.map(l => [l, false]).concat(langs.map(l => [l, true]))
|
|
||||||
// : langs.map(l => [l, false]);
|
|
||||||
const entries = process.env.NODE_ENV == 'production'
|
|
||||||
? langs.map(l => [l, true])
|
|
||||||
: langs.map(l => [l, false]);
|
|
||||||
|
|
||||||
module.exports = entries.map(x => {
|
// Entries
|
||||||
const [lang, isProduction] = x;
|
const entry = {
|
||||||
|
|
||||||
// Chunk name
|
|
||||||
const name = lang;
|
|
||||||
|
|
||||||
// Entries
|
|
||||||
const entry = {
|
|
||||||
desktop: './src/client/app/desktop/script.ts',
|
desktop: './src/client/app/desktop/script.ts',
|
||||||
mobile: './src/client/app/mobile/script.ts',
|
mobile: './src/client/app/mobile/script.ts',
|
||||||
//ch: './src/client/app/ch/script.ts',
|
|
||||||
//stats: './src/client/app/stats/script.ts',
|
//stats: './src/client/app/stats/script.ts',
|
||||||
//status: './src/client/app/status/script.ts',
|
//status: './src/client/app/status/script.ts',
|
||||||
dev: './src/client/app/dev/script.ts',
|
dev: './src/client/app/dev/script.ts',
|
||||||
auth: './src/client/app/auth/script.ts',
|
auth: './src/client/app/auth/script.ts',
|
||||||
sw: './src/client/app/sw.js'
|
sw: './src/client/app/sw.js'
|
||||||
};
|
};
|
||||||
|
|
||||||
const output = {
|
const output = {
|
||||||
path: __dirname + '/built/client/assets',
|
path: __dirname + '/built/client/assets',
|
||||||
filename: `[name].${version}.${lang}.${isProduction ? 'min' : 'raw'}.js`
|
filename: `[name].${version}.-.${isProduction ? 'min' : 'raw'}.js`
|
||||||
};
|
};
|
||||||
|
|
||||||
const i18nReplacer = new I18nReplacer(lang as string);
|
//#region Define consts
|
||||||
global['i18nReplacement'] = i18nReplacer.replacement;
|
const consts = {
|
||||||
|
|
||||||
//#region Define consts
|
|
||||||
const consts = {
|
|
||||||
_RECAPTCHA_SITEKEY_: config.recaptcha.site_key,
|
_RECAPTCHA_SITEKEY_: config.recaptcha.site_key,
|
||||||
_SW_PUBLICKEY_: config.sw ? config.sw.public_key : null,
|
_SW_PUBLICKEY_: config.sw ? config.sw.public_key : null,
|
||||||
_THEME_COLOR_: constants.themeColor,
|
_THEME_COLOR_: constants.themeColor,
|
||||||
|
@ -89,22 +78,22 @@ module.exports = entries.map(x => {
|
||||||
_API_URL_: config.api_url,
|
_API_URL_: config.api_url,
|
||||||
_WS_URL_: config.ws_url,
|
_WS_URL_: config.ws_url,
|
||||||
_DEV_URL_: config.dev_url,
|
_DEV_URL_: config.dev_url,
|
||||||
_LANG_: lang,
|
_LANG_: '%lang%',
|
||||||
_HOST_: config.host,
|
_HOST_: config.host,
|
||||||
_HOSTNAME_: config.hostname,
|
_HOSTNAME_: config.hostname,
|
||||||
_URL_: config.url,
|
_URL_: config.url,
|
||||||
_LICENSE_: licenseHtml,
|
_LICENSE_: licenseHtml,
|
||||||
_GOOGLE_MAPS_API_KEY_: config.google_maps_api_key
|
_GOOGLE_MAPS_API_KEY_: config.google_maps_api_key
|
||||||
};
|
};
|
||||||
|
|
||||||
const _consts = {};
|
const _consts = {};
|
||||||
|
|
||||||
Object.keys(consts).forEach(key => {
|
Object.keys(consts).forEach(key => {
|
||||||
_consts[key] = JSON.stringify(consts[key]);
|
_consts[key] = JSON.stringify(consts[key]);
|
||||||
});
|
});
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
const plugins = [
|
const plugins = [
|
||||||
//new HardSourceWebpackPlugin(),
|
//new HardSourceWebpackPlugin(),
|
||||||
new ProgressBarPlugin({
|
new ProgressBarPlugin({
|
||||||
format: chalk` {cyan.bold yes we can} {bold [}:bar{bold ]} {green.bold :percent} {gray (:current/:total)} :elapseds`,
|
format: chalk` {cyan.bold yes we can} {bold [}:bar{bold ]} {green.bold :percent} {gray (:current/:total)} :elapseds`,
|
||||||
|
@ -118,16 +107,31 @@ module.exports = entries.map(x => {
|
||||||
fs.writeFileSync('./built/client/meta.json', JSON.stringify({
|
fs.writeFileSync('./built/client/meta.json', JSON.stringify({
|
||||||
version
|
version
|
||||||
}), 'utf-8');
|
}), 'utf-8');
|
||||||
|
|
||||||
|
//#region i18n
|
||||||
|
langs.forEach(lang => {
|
||||||
|
Object.keys(entry).forEach(file => {
|
||||||
|
let src = fs.readFileSync(`${__dirname}/built/client/assets/${file}.${version}.-.${isProduction ? 'min' : 'raw'}.js`, 'utf8');
|
||||||
|
|
||||||
|
const i18nReplacer = new I18nReplacer(lang);
|
||||||
|
|
||||||
|
src = src.replace(i18nReplacer.pattern, i18nReplacer.replacement);
|
||||||
|
src = src.replace('%lang%', lang);
|
||||||
|
|
||||||
|
fs.writeFileSync(`${__dirname}/built/client/assets/${file}.${version}.${lang}.${isProduction ? 'min' : 'raw'}.js`, src, 'utf8');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
//#endregion
|
||||||
}),
|
}),
|
||||||
new VueLoaderPlugin()
|
new VueLoaderPlugin(),
|
||||||
];
|
new I18nPlugin()
|
||||||
|
];
|
||||||
|
|
||||||
if (isProduction) {
|
if (isProduction) {
|
||||||
plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
|
plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
module.exports = {
|
||||||
name,
|
|
||||||
entry,
|
entry,
|
||||||
module: {
|
module: {
|
||||||
rules: [{
|
rules: [{
|
||||||
|
@ -150,10 +154,9 @@ module.exports = entries.map(x => {
|
||||||
}, {
|
}, {
|
||||||
loader: 'replace',
|
loader: 'replace',
|
||||||
query: {
|
query: {
|
||||||
search: i18nReplacer.pattern.toString(),
|
search: i18nPattern.toString(),
|
||||||
replace: 'i18nReplacement',
|
replace: 'i18nReplacement',
|
||||||
i18n: true,
|
i18n: true
|
||||||
lang
|
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
loader: 'replace',
|
loader: 'replace',
|
||||||
|
@ -238,10 +241,9 @@ module.exports = entries.map(x => {
|
||||||
}, {
|
}, {
|
||||||
loader: 'replace',
|
loader: 'replace',
|
||||||
query: {
|
query: {
|
||||||
search: i18nReplacer.pattern.toString(),
|
search: i18nPattern.toString(),
|
||||||
replace: 'i18nReplacement',
|
replace: 'i18nReplacement',
|
||||||
i18n: true,
|
i18n: true
|
||||||
lang
|
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
loader: 'replace',
|
loader: 'replace',
|
||||||
|
@ -268,5 +270,4 @@ module.exports = entries.map(x => {
|
||||||
cache: true,
|
cache: true,
|
||||||
devtool: false, //'source-map',
|
devtool: false, //'source-map',
|
||||||
mode: isProduction ? 'production' : 'development'
|
mode: isProduction ? 'production' : 'development'
|
||||||
};
|
};
|
||||||
});
|
|
||||||
|
|
24
webpack/i18n.ts
Normal file
24
webpack/i18n.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
/**
|
||||||
|
* Replace i18n texts
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const pattern = /%i18n:([a-z0-9_\-@\.\!]+?)%/g;
|
||||||
|
|
||||||
|
export const replacement = (ctx, match, key) => {
|
||||||
|
const client = '/src/client/app/';
|
||||||
|
let name = null;
|
||||||
|
|
||||||
|
const shouldEscape = key[0] == '!';
|
||||||
|
if (shouldEscape) {
|
||||||
|
key = key.substr(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key[0] == '@') {
|
||||||
|
name = ctx.src.substr(ctx.src.indexOf(client) + client.length);
|
||||||
|
key = key.substr(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = name ? `${name}|${key}` : key;
|
||||||
|
|
||||||
|
return shouldEscape ? `%i18n:!${path}%` : `%i18n:${path}%`;
|
||||||
|
};
|
Loading…
Reference in a new issue