Skip to content

Commit

Permalink
fix: Fix i18n build for old webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Sep 13, 2023
1 parent cf8a81c commit 3cb64ce
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
20 changes: 19 additions & 1 deletion esbuild/SparsedBuild.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const assert = require('node:assert');
const {statSync, existsSync} = require('node:fs');
const {join, resolve} = require('node:path');
const {join, resolve, extname} = require('node:path');

const INLINE_LOADERS = ['text', 'binary', 'base64', 'dataurl'];

const inlinedFiles = (loader) => {
const inline = Object.keys(loader).filter((ext) => {
return INLINE_LOADERS.includes(loader[ext]);
});

return (file) => inline.includes(extname(file));
};

class SparsedBuild {
constructor(context, {extension} = {}) {
Expand All @@ -22,16 +32,20 @@ class SparsedBuild {
setup: ({onResolve, initialOptions}) => {
const {
bundle,
loader = {},
resolveExtensions = ['.tsx', '.ts', '.jsx', '.js', '.css', '.json'],
} = initialOptions;

assert(bundle === true, `Option 'bundle' should be 'true' for sparsed build`);

const shouldSkip = inlinedFiles(loader);

onResolve({filter: /.*/}, async ({path, resolveDir, kind}) => {
if (kind === 'entry-point') {
return {};
}

// TODO: resolve ts aliases
if (!path.match(/^\.{1,2}/)) {
return {external: true};
}
Expand All @@ -42,6 +56,10 @@ class SparsedBuild {
true,
);

if (shouldSkip(fullpath)) {
return {};
}

if (!this._has(fullpath)) {
await this.build(fullpath);
}
Expand Down
14 changes: 8 additions & 6 deletions esbuild/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ const {resolve} = require('node:path');
const postcss = require('postcss');
const postcssPresetEnv = require('postcss-preset-env');

const tsconfigJson = require('../tsconfig.json');

const {FileWatcher} = require('./FileWatcher');
const {SparsedBuild} = require('./SparsedBuild');

const {
compilerOptions: {target},
} = tsconfigJson;

async function build({path, format}) {
const tsconfig = require(`../tsconfig${format ? '.' + format : ''}.json`);
const {
compilerOptions: {target},
} = tsconfig;

const watcher = new FileWatcher(process.argv.indexOf('--watch') > -1);
const sparsed = new SparsedBuild(
async (entry) => {
Expand All @@ -29,6 +28,9 @@ async function build({path, format}) {
outbase: './src',
outdir: `./build${format ? '/' + format : ''}`,
format: format,
loader: {
'.json': 'text',
},
plugins: [
sparsed.plugin,
sassPlugin({
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"url": "[email protected]:yandex-cloud/docs-components.git"
},
"files": [
"build"
"build",
"i18n"
],
"exports": {
".": {
Expand All @@ -30,6 +31,7 @@
}
},
"./styles": "./build/index.css",
"./i18n/*": "./i18n/*",
"./themes/*": {
"style": "./build/themes/*/index.css",
"default": "./build/themes/*/index.css"
Expand Down
9 changes: 6 additions & 3 deletions src/config/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import i18n, {TFunction} from 'i18next';
import {initReactI18next} from 'react-i18next';

import en from '../i18n/en.json';
import ru from '../i18n/ru.json';
import en from '../../i18n/en.json';
import ru from '../../i18n/ru.json';
import {Lang} from '../models';

export type Loc = Record<string, typeof en>;
Expand All @@ -17,7 +17,10 @@ let initializePromise: Promise<TFunction> | null = null;
export const configureI18N = ({lang, loc}: I18NConfig) => {
if (initializePromise === null) {
lang = lang || Lang.En;
loc = loc || {ru, en};
loc = loc || {
ru: JSON.parse(ru as unknown as string),
en: JSON.parse(en as unknown as string),
};

initializePromise = i18n.use(initReactI18next).init({
lng: lang,
Expand Down

0 comments on commit 3cb64ce

Please sign in to comment.