diff --git a/.eslintignore b/.eslintignore index b7dab5e..e21d4ed 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,6 @@ node_modules -build \ No newline at end of file +build +tools +public +typings +craco.config.js diff --git a/package.json b/package.json index e0fa03e..ed8f895 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,9 @@ "@typescript-eslint/parser": "4.19.0", "antd-dayjs-webpack-plugin": "^1.0.6", "babel-plugin-lodash": "^3.3.4", + "chalk": "^4.1.0", "craco-alias": "^2.2.0", + "diff": "^5.0.0", "eslint": "^7.22.0", "eslint-plugin-react": "^7.22.0", "eslint-plugin-react-hooks": "^4.2.0", diff --git a/src/global/LocaleContext.tsx b/src/global/LocaleContext.tsx index 608cf42..edd4a55 100644 --- a/src/global/LocaleContext.tsx +++ b/src/global/LocaleContext.tsx @@ -46,7 +46,7 @@ // the TypeScript typings, which causes build warnings due to a missing // loader for those files. import( - /* webpackInclude: /\b(de|fr|nl|pl|pt-br|vi)\.js$/ */ + /* webpackInclude: /\b(dayjsLocale|de|fr|nl|pl|pt-br|vi)\.js$/ */ /* webpackMode: "lazy" */ /* webpackChunkName: "locale-dayjs-[request]" */ `dayjs/locale/${dayjsLocale}` @@ -71,7 +71,7 @@ // Load the locale debug("loading timeago locale %s for language %s", timeagoLocale, langCode); import( - /* webpackInclude: /\b(de|fr|nl|pl|pt-br|vi)\.js$/ */ + /* webpackInclude: /\b(timeagoLocale|de|fr|nl|pl|pt-br|vi)\.js$/ */ /* webpackMode: "lazy" */ /* webpackChunkName: "locale-timeago-[request]" */ `react-timeago/lib/language-strings/${timeagoLocale}` @@ -96,7 +96,7 @@ // Load the locale debug("loading antd locale %s for language %s", antLocaleCode, langCode); import( - /* webpackInclude: /\b(de_DE|fr_FR|nl_NL|pl_PL|pt_BR|vi_VN)\.js$/ */ + /* webpackInclude: /\b(antLocale|de_DE|fr_FR|nl_NL|pl_PL|pt_BR|vi_VN)\.js$/ */ /* webpackMode: "lazy" */ /* webpackChunkName: "locale-antd-[request]" */ `antd/lib/locale/${antLocaleCode}` diff --git a/tools/addLanguages.js b/tools/addLanguages.js new file mode 100644 index 0000000..5d3bbd1 --- /dev/null +++ b/tools/addLanguages.js @@ -0,0 +1,60 @@ +// Copyright (c) 2020-2021 Drew Lemmy +// This file is part of KristWeb 2 under AGPL-3.0. +// Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt + +// This script adds the language codes from `languages.json` to LocaleContext, +// allowing the library locales (dayjs, timeago, antd) to be included in the +// build. + +const { promises: fs } = require("fs"); +const path = require("path"); + +const chalk = require("chalk"); +const Diff = require("diff"); + +const languagesJSON = require("../src/__data__/languages.json"); + +function replaceLocales(src, localeType) { + // Get the needed locale filenames for this library + const locales = [...new Set(Object.values(languagesJSON) + .map(l => l[localeType]) + .filter(l => !!l))]; + + // Construct the regex to search for the locale magic comment. This looks like + // `/* webpackInclude: /\b(LOCALE_TYPE|LOCALE|LOCALE|LOCALE|...)\.js$/ */` + const searchRe = new RegExp(`(\\/\\* webpackInclude: \\/\\\\b\\(${localeType}\\|).+(\\)\\\\\\.js\\$\\/ \\*\\/)`); + + // Find and replace the magic comment, populating the new locales + const localeList = locales.join("|"); + return src.replace(searchRe, "$1" + localeList + "$2"); +} + +async function main() { + // Load the contents of LocaleContext + const srcDir = path.resolve(__dirname, "../src/global"); + const srcFile = path.join(srcDir, "LocaleContext.tsx"); + + let src = (await fs.readFile(srcFile)).toString(); + const originalSrc = src; // Used to diff at the end + + // Perform the magic comment replacements for each library + src = replaceLocales(src, "dayjsLocale"); + src = replaceLocales(src, "timeagoLocale"); + src = replaceLocales(src, "antLocale"); + + // Print the line-by-line diff + const diff = Diff.diffLines(originalSrc, src); + diff.forEach(part => { + const colour = part.added ? chalk.green : + part.removed ? chalk.red : chalk.grey; + process.stderr.write(colour(part.value)); + }); + console.log(); + + // Save the modified file + await fs.writeFile(srcFile, src); +} + +main() + .then(() => console.log("Done!")) + .catch(console.error); diff --git a/yarn.lock b/yarn.lock index c562f10..2cb394b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4550,6 +4550,11 @@ resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1" integrity sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q== +diff@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/diff/-/diff-5.0.0.tgz#7ed6ad76d859d030787ec35855f5b1daf31d852b" + integrity sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w== + diffie-hellman@^5.0.0: version "5.0.3" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875"