Skip to content

Commit

Permalink
fix(i18n): make react-i18next as optional peer deps
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jan 12, 2025
1 parent 0319d88 commit ec94491
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 33 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@
"@babel/core": "^7.25.2",
"@babel/types": "^7.25.6",
"@cypress/browserify-preprocessor": "^3.0.2",
"@next-core/brick-container": "*",
"@next-core/brick-playground": "*",
"@next-core/test-next": "*",
"@types/doctrine": "^0.0.9",
"@types/fs-extra": "^9.0.13",
"@types/history": "^4.7.11",
Expand Down
14 changes: 11 additions & 3 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@
"@next-core/types": "^1.14.0",
"@next-core/utils": "^1.7.28",
"i18next": "^22.5.1",
"i18next-browser-languagedetector": "^7.2.1",
"react-i18next": "^12.3.1"
"i18next-browser-languagedetector": "^7.2.1"
},
"devDependencies": {
"@next-core/build-next-libs": "^1.0.22",
"@next-core/test-next": "^1.1.7"
"@next-core/test-next": "^1.1.7",
"react-i18next": "^12.3.1"
},
"peerDependencies": {
"react-i18next": "^12.3.1"
},
"peerDependenciesMeta": {
"react-i18next": {
"optional": true
}
}
}
1 change: 0 additions & 1 deletion packages/react-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"devDependencies": {
"@next-core/build-next-libs": "^1.0.22",
"@next-core/test-next": "^1.1.7",
"@testing-library/react": "^16.0.1",
"jest-fetch-mock": "^3.0.3"
}
}
2 changes: 1 addition & 1 deletion packages/test-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@babel/core": "^7.25.2",
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/react": "^16.1.0",
"babel-jest": "^29.7.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.7.0",
Expand Down
33 changes: 16 additions & 17 deletions packages/test-next/src/transforms/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import { pascalCase } from "pascal-case";
// https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/jest/fileTransform.js
export default {
/**
* @param {string} src
* @param {string} _src
* @param {string} filename
*/
process(src, filename) {
process(_src, filename) {
const assetFilename = JSON.stringify(path.basename(filename));
if (filename.endsWith(".svg")) {
const pascalCaseFilename = pascalCase(path.parse(filename).name);
const componentName = `Svg${pascalCaseFilename}`;
return {
code: `const React = require('react');
module.exports = {
__esModule: true,
default: React.forwardRef(function ${componentName}(props, ref) {
return {
$$typeof: Symbol.for('react.element'),
type: 'svg',
ref: ref,
key: null,
props: Object.assign({}, props, {
children: ${assetFilename}
})
};
}),
};`,
code: `"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var _default = exports.default = /*#__PURE__*/_react.default.forwardRef(function ${componentName}(props, ref) {
return /*#__PURE__*/_react.default.createElement("svg", _extends({}, props, {
ref: ref
}), ${assetFilename});
});`,
};
}
return {
Expand Down
4 changes: 4 additions & 0 deletions packages/yo/src/templates/brick/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { i18n } from "@next-core/i18n";

export enum K {}
// HELLO = "HELLO",

Expand All @@ -13,6 +15,8 @@ export const NS = "bricks/{{pkgName}}/{{>lastTagName}}";

export const locales = { en, zh };

export const t = i18n.getFixedT(null, NS);

type Locale = { [k in K]: string } & {
[k in K as `${k}_plural`]?: string;
};
7 changes: 3 additions & 4 deletions packages/yo/src/templates/brick/index.tsx.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { createDecorators } from "@next-core/element";
import { ReactNextElement } from "@next-core/react-element";
import "@next-core/theme";
{{#if useI18n}}
import { useTranslation, initializeReactI18n } from "@next-core/i18n/react";
import { K, NS, locales } from "./i18n.js";
import { initializeI18n } from "@next-core/i18n";
import { K, NS, locales, t } from "./i18n.js";
{{/if}}
import styleText from "./styles.shadow.css";

{{#if useI18n}}
initializeReactI18n(NS, locales);
initializeI18n(NS, locales);

{{/if}}
const { defineElement, property } = createDecorators();
Expand Down Expand Up @@ -39,7 +39,6 @@ export interface {{>className}}ComponentProps extends {{>className}}Props {

export function {{>className}}Component(props: {{>className}}ComponentProps) {
{{#if useI18n}}
const { t } = useTranslation(NS);
const hello = t(K.HELLO);
return <div>{hello} world!</div>;
{{else}}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2658,10 +2658,10 @@
lodash "^4.17.21"
redent "^3.0.0"

"@testing-library/react@^16.0.1":
version "16.0.1"
resolved "https://registry.npmjs.org/@testing-library/react/-/react-16.0.1.tgz#29c0ee878d672703f5e7579f239005e4e0faa875"
integrity sha512-dSmwJVtJXmku+iocRhWOUFbrERC76TX2Mnf0ATODz8brzAZrMBbzLwQixlBSanZxR6LddK3eiwpSFZgDET1URg==
"@testing-library/react@^16.1.0":
version "16.1.0"
resolved "https://registry.npmjs.org/@testing-library/react/-/react-16.1.0.tgz#aa0c61398bac82eaf89776967e97de41ac742d71"
integrity sha512-Q2ToPvg0KsVL0ohND9A3zLJWcOXXcO8IDu3fj11KhNt0UlCWyFyvnCIBkd12tidB2lkiVRG8VFqdhcqhqnAQtg==
dependencies:
"@babel/runtime" "^7.12.5"

Expand Down

0 comments on commit ec94491

Please sign in to comment.