about summary refs log tree commit diff
path: root/config/webpack
diff options
context:
space:
mode:
Diffstat (limited to 'config/webpack')
-rw-r--r--config/webpack/configuration.js7
-rw-r--r--config/webpack/generateLocalePacks.js108
-rw-r--r--config/webpack/shared.js11
3 files changed, 60 insertions, 66 deletions
diff --git a/config/webpack/configuration.js b/config/webpack/configuration.js
index 9cdd6f934..a38d9d11d 100644
--- a/config/webpack/configuration.js
+++ b/config/webpack/configuration.js
@@ -30,6 +30,9 @@ for (let i = 0; i < flavourFiles.length; i++) {
   if (!data.pack_directory) {
     data.pack_directory = dirname(flavourFile);
   }
+  if (data.locales) {
+    data.locales = join(dirname(flavourFile), data.locales);
+  }
   if (data.pack && typeof data.pack === 'object') {
     flavours[data.name] = data;
   }
@@ -45,9 +48,9 @@ for (let i = 0; i < skinFiles.length; i++) {
   const data = flavours[name].skin;
   if (lstatSync(skinFile).isDirectory()) {
     data[skin] = {};
-    const skinPacks = glob.sync(resolve(skinFile, '*.{css,scss}'));
+    const skinPacks = glob.sync(join(skinFile, '*.{css,scss}'));
     for (let j = 0; j < skinPacks.length; j++) {
-      const pack = skinPacks[i];
+      const pack = skinPacks[j];
       data[skin][basename(pack, extname(pack))] = pack;
     }
   } else if ((skin = skin.match(/^(.*)\.s?css$/i))) {
diff --git a/config/webpack/generateLocalePacks.js b/config/webpack/generateLocalePacks.js
index a943589f7..09fba4a18 100644
--- a/config/webpack/generateLocalePacks.js
+++ b/config/webpack/generateLocalePacks.js
@@ -1,70 +1,66 @@
+// A message from upstream:
+// ========================
 // To avoid adding a lot of boilerplate, locale packs are
 // automatically generated here. These are written into the tmp/
 // directory and then used to generate locale_en.js, locale_fr.js, etc.
 
-const fs = require('fs');
-const path = require('path');
+// Glitch note:
+// ============
+// This code has been entirely rewritten to support glitch flavours.
+// However, the underlying process is exactly the same.
+
+const { existsSync, readdirSync, writeFileSync } = require('fs');
+const { join, resolve } = require('path');
 const rimraf = require('rimraf');
 const mkdirp = require('mkdirp');
+const { flavours } = require('./configuration.js');
 
-const localesJsonPath = path.join(__dirname, '../../app/javascript/mastodon/locales');
-const locales = fs.readdirSync(localesJsonPath).filter(filename => {
-  return /\.json$/.test(filename) &&
-    !/defaultMessages/.test(filename) &&
-    !/whitelist/.test(filename);
-}).map(filename => filename.replace(/\.json$/, ''));
-
-const outPath = path.join(__dirname, '../../tmp/packs');
-
-rimraf.sync(outPath);
-mkdirp.sync(outPath);
-
-const outPaths = [];
-
-locales.forEach(locale => {
-  const localePath = path.join(outPath, `locale_${locale}.js`);
-  const baseLocale = locale.split('-')[0]; // e.g. 'zh-TW' -> 'zh'
-  const localeDataPath = [
-    // first try react-intl
-    `../../node_modules/react-intl/locale-data/${baseLocale}.js`,
-    // then check locales/locale-data
-    `../../app/javascript/mastodon/locales/locale-data/${baseLocale}.js`,
-    // fall back to English (this is what react-intl does anyway)
-    '../../node_modules/react-intl/locale-data/en.js',
-  ].filter(filename => fs.existsSync(path.join(outPath, filename)))
-    .map(filename => filename.replace(/..\/..\/node_modules\//, ''))[0];
-
-  let glitchInject = `
-const mergedMessages = messages;
-`;
-
-  const glitchPath = `../../app/javascript/glitch/locales/${locale}.json`;
-  if (fs.existsSync(path.join(outPath, glitchPath))) {
-    glitchInject = `
-import glitchMessages from ${JSON.stringify(glitchPath)};
-
-let mergedMessages = messages;
-Object.keys(glitchMessages).forEach(function (key) {
-   mergedMessages[key] = glitchMessages[key];
-});
-
-`;
+module.exports = Object.keys(flavours).reduce(function (map, entry) {
+  const flavour = flavours[entry];
+  if (!flavour.locales) {
+    return map;
   }
+  const locales = readdirSync(flavour.locales).filter(
+    filename => /\.js(?:on)?$/.test(filename) && !/defaultMessages|whitelist|index/.test(filename)
+  );
+  const outPath = resolve('tmp', 'locales', entry);
+
+  rimraf.sync(outPath);
+  mkdirp.sync(outPath);
 
-  const localeContent = `//
-// locale_${locale}.js
+  locales.forEach(function (locale) {
+    const localeName = locale.replace(/\.js(?:on)?$/, '');
+    const localePath = join(outPath, `${localeName}.js`);
+    const baseLocale = localeName.split('-')[0]; // e.g. 'zh-TW' -> 'zh'
+    const localeDataPath = [
+      // first try react-intl
+      `node_modules/react-intl/locale-data/${baseLocale}.js`,
+      // then check locales/locale-data
+      `app/javascript/locales/locale-data/${baseLocale}.js`,
+      // fall back to English (this is what react-intl does anyway)
+      'node_modules/react-intl/locale-data/en.js',
+    ].filter(
+      filename => existsSync(filename)
+    ).map(
+      filename => filename.replace(/(?:node_modules|app\/javascript)\//, '')
+    )[0];
+    const localeContent = `//
+// locales/${entry}/${localeName}.js
 // automatically generated by generateLocalePacks.js
 //
-import messages from '../../app/javascript/mastodon/locales/${locale}.json';
-import localeData from ${JSON.stringify(localeDataPath)};
-import { setLocale } from 'locales';
-${glitchInject}
-setLocale({messages: mergedMessages, localeData: localeData});
-`;
-  fs.writeFileSync(localePath, localeContent, 'utf8');
-  outPaths.push(localePath);
-});
 
-module.exports = outPaths;
+import messages from '../../../${flavour.locales}/${locale.replace(/\.js$/, '')}';
+import localeData from '${localeDataPath}';
+import { setLocale } from 'locales';
 
+setLocale({
+  localeData,
+  messages,
+});
+`;
+    writeFileSync(localePath, localeContent, 'utf8');
+    map[`locales/${entry}/${localeName}`] = localePath;
+  });
 
+  return map;
+}, {});
diff --git a/config/webpack/shared.js b/config/webpack/shared.js
index e4b057ffb..35b9bbd1c 100644
--- a/config/webpack/shared.js
+++ b/config/webpack/shared.js
@@ -1,13 +1,12 @@
 // Note: You must restart bin/webpack-dev-server for changes to take effect
 
 const webpack = require('webpack');
-const { basename, join, resolve } = require('path');
+const { join, resolve } = require('path');
 const { sync } = require('glob');
 const ExtractTextPlugin = require('extract-text-webpack-plugin');
 const ManifestPlugin = require('webpack-manifest-plugin');
-const extname = require('path-complete-extname');
 const { env, settings, core, flavours, output, loadersDir } = require('./configuration.js');
-const localePackPaths = require('./generateLocalePacks');
+const localePacks = require('./generateLocalePacks');
 
 function reducePacks (data, into = {}) {
   if (!data.pack) {
@@ -48,11 +47,7 @@ function reducePacks (data, into = {}) {
 module.exports = {
   entry: Object.assign(
     { locales: resolve('app', 'javascript', 'locales') },
-    localePackPaths.reduce((map, entry) => {
-      const localMap = map;
-      localMap[basename(entry, extname(entry, extname(entry)))] = resolve(entry);
-      return localMap;
-    }, {}),
+    localePacks,
     reducePacks(core),
     Object.keys(flavours).reduce((map, entry) => reducePacks(flavours[entry], map), {})
   ),