Skip to content

Commit 0058ac2

Browse files
committed
feat(compiler): move output to @replexica/translations
1 parent 45c113d commit 0058ac2

9 files changed

Lines changed: 20 additions & 24 deletions

File tree

demo/next/next.config.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const nextConfig = {};
55

66
/** @type {import('@replexica/compiler').ReplexicaConfig} */
77
const replexicaConfig = {
8-
outDir: '.next',
9-
i18nDir: 'src/i18n',
108
sourceLocale: 'en',
119
debug: true,
1210
};

demo/next/src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function RootLayout({
1717
children: React.ReactNode;
1818
}>) {
1919
const locale = await loadLocaleFromCookie();
20-
const localeData = await import(`@replexica/output/${locale}.json`).then((m) => m.default);
20+
const localeData = await import(`@replexica/translations/${locale}.json`).then((m) => m.default);
2121
return (
2222
<ReplexicaIntlProvider data={localeData}>
2323
<html lang={locale}>

packages/compiler/src/compiler/compiler.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ import { ReplexicaFileData, ReplexicaCompilerData } from "./types";
88
import { ReplexicaScopeExtractor } from "./scope";
99

1010
export class ReplexicaCompiler {
11-
public static fromCode(code: string, relativeFilePath: string, i18nImportPrefix: string) {
11+
public static fromCode(code: string, relativeFilePath: string) {
1212
const ast = parseCodeIntoBabelAst(code);
1313
const isServer = !hasDirective(ast, 'use client');
1414

15-
return new ReplexicaCompiler(relativeFilePath, code, ast, isServer, i18nImportPrefix);
15+
return new ReplexicaCompiler(relativeFilePath, code, ast, isServer);
1616
}
1717

1818
private constructor(
1919
public readonly relativeFilePath: string,
2020
private readonly code: string,
2121
public readonly ast: t.File,
2222
private readonly isServer: boolean,
23-
private readonly i18nImportPrefix: string,
2423
) {
2524
this.fileId = generateFileId(relativeFilePath, 0);
2625
}
@@ -44,7 +43,7 @@ export class ReplexicaCompiler {
4443
const scopes = extractor.fromNode(path);
4544
for (const scope of scopes) {
4645
const hints = scope.extractHints();
47-
const data = scope.injectIntl(_compiler.fileId, _compiler.isServer, _compiler.i18nImportPrefix);
46+
const data = scope.injectIntl(_compiler.fileId, _compiler.isServer);
4847
_compiler._data[scope.id] = { hints, data };
4948
}
5049
}

packages/compiler/src/compiler/scope/attribute.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class ReplexicaAttributeScope extends ReplexicaBaseScope implements IRepl
5757
return this._id;
5858
}
5959

60-
public injectIntl(fileId: string, isServer: boolean, i18nImportPrefix: string): ReplexicaScopeData {
60+
public injectIntl(fileId: string, isServer: boolean): ReplexicaScopeData {
6161
const result: ReplexicaScopeData = {};
6262
if (!this._chunk) { return result; }
6363

@@ -185,7 +185,7 @@ export class ReplexicaAttributeScope extends ReplexicaBaseScope implements IRepl
185185
// );
186186

187187
// add the following props to the injected element:
188-
// loadLocaleData={(locale) => import(`${i18nImportPrefix}/${locale}.json`)}
188+
// loadLocaleData={(locale) => import(`@replexica/translations/${locale}.json`)}
189189
jsxElement.node.openingElement.attributes.push(
190190
t.jsxAttribute(
191191
t.jsxIdentifier('loadLocaleData'),
@@ -195,7 +195,7 @@ export class ReplexicaAttributeScope extends ReplexicaBaseScope implements IRepl
195195
t.callExpression(
196196
t.identifier('import'),
197197
[t.templateLiteral([
198-
t.templateElement({ raw: `${i18nImportPrefix}/`, cooked: `${i18nImportPrefix}/` }, false),
198+
t.templateElement({ raw: `@replexica/translations/`, cooked: `@replexica/translations/` }, false),
199199
t.templateElement({ raw: '.json', cooked: '.json' }, true),
200200
], [t.identifier('locale')])],
201201
),

packages/compiler/src/compiler/scope/content.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class ReplexicaContentScope extends ReplexicaBaseScope implements IReplex
5757
return this._id;
5858
}
5959

60-
public injectIntl(fileId: string, isServer: boolean, i18nImportPrefix: string): ReplexicaScopeData {
60+
public injectIntl(fileId: string, isServer: boolean): ReplexicaScopeData {
6161
const result: ReplexicaScopeData = {};
6262

6363
const programNode = this.path.findParent((p) => p.isProgram()) as NodePath<t.Program> | null;
@@ -121,7 +121,7 @@ export class ReplexicaContentScope extends ReplexicaBaseScope implements IReplex
121121
// );
122122

123123
// add the following props to the injected element:
124-
// loadLocaleData={(locale) => import(`${i18nImportPrefix}/${locale}.json`)}
124+
// loadLocaleData={(locale) => import(`@replexica/translations/${locale}.json`)}
125125
injectedElement.attributes.push(
126126
t.jsxAttribute(
127127
t.jsxIdentifier('loadLocaleData'),
@@ -131,7 +131,7 @@ export class ReplexicaContentScope extends ReplexicaBaseScope implements IReplex
131131
t.callExpression(
132132
t.identifier('import'),
133133
[t.templateLiteral([
134-
t.templateElement({ raw: `${i18nImportPrefix}/`, cooked: `${i18nImportPrefix}/` }, false),
134+
t.templateElement({ raw: `@replexica/translations/`, cooked: `@replexica/translations/` }, false),
135135
t.templateElement({ raw: '.json', cooked: '.json' }, true),
136136
], [t.identifier('locale')])],
137137
),

packages/compiler/src/compiler/scope/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ReplexicaScopeData, ReplexicaScopeHint } from '../types';
44

55
export interface IReplexicaScope {
66
get id(): string;
7-
injectIntl(fileId: string, isServer: boolean, i18nImportPrefix: string): ReplexicaScopeData;
7+
injectIntl(fileId: string, isServer: boolean): ReplexicaScopeData;
88
extractHints(): ReplexicaScopeHint[];
99
}
1010

packages/compiler/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ const unplugin = createUnplugin<ReplexicaConfig>((options) => ({
1717
const relativeFilePath = path.relative(process.cwd(), absoluteFilePath);
1818

1919
const relativeFileDir = path.relative(process.cwd(), path.dirname(absoluteFilePath));
20-
const i18nImportPrefix = path.relative(relativeFileDir, options.i18nDir);
2120

2221
const compiler = ReplexicaCompiler
23-
.fromCode(code, relativeFilePath, i18nImportPrefix)
22+
.fromCode(code, relativeFilePath)
2423
.withScope(ReplexicaSkipScope)
2524
.withScope(ReplexicaAttributeScope)
2625
.withScope(ReplexicaContentScope)

packages/compiler/src/output.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import path from 'path';
44
import { ReplexicaCompilerData, ReplexicaCompilerPayload } from "./compiler";
55
import { ReplexicaLocaleData, ReplexicaConfig } from "./types";
66

7-
const debugDir = '.debug/replexica';
87
export class ReplexicaOutputProcessor {
98
public static create(relativeFilePath: string, options: ReplexicaConfig) {
109
return new ReplexicaOutputProcessor(relativeFilePath, options);
@@ -14,9 +13,12 @@ export class ReplexicaOutputProcessor {
1413
private readonly relativeFilePath: string,
1514
private readonly options: ReplexicaConfig,
1615
) {}
16+
17+
private _outDir = path.join(process.cwd(), `node_modules/@replexica/translations`);
18+
private _debugDir = path.join(process.cwd(), '.debug/replexica');
1719

1820
public saveData(data: ReplexicaCompilerData) {
19-
const filePath = path.join(process.cwd(), this.options.outDir, '.replexica.json');
21+
const filePath = path.join(this._outDir, '.replexica.json');
2022
const existingData: ReplexicaCompilerPayload = this._loadObject<ReplexicaCompilerPayload>(filePath) || this._createEmptyCompilerPayload();
2123
const newData = {
2224
...existingData,
@@ -31,7 +33,7 @@ export class ReplexicaOutputProcessor {
3133

3234
public saveSourceLocaleData(data: ReplexicaCompilerData) {
3335
const existingData: ReplexicaLocaleData =
34-
this._loadObject<ReplexicaLocaleData>(path.join(process.cwd(), this.options.i18nDir, `${this.options.sourceLocale}.json`)) ||
36+
this._loadObject<ReplexicaLocaleData>(path.join(this._outDir, `${this.options.sourceLocale}.json`)) ||
3537
this._createEmptyLocaleData();
3638

3739
const newLocaleData: ReplexicaLocaleData = {
@@ -54,17 +56,17 @@ export class ReplexicaOutputProcessor {
5456
delete newLocaleData[fileId];
5557
}
5658
}
57-
const filePath = path.join(process.cwd(), this.options.i18nDir, `${this.options.sourceLocale}.json`);
59+
const filePath = path.join(this._outDir, `${this.options.sourceLocale}.json`);
5860
this._saveObject(filePath, newLocaleData);
5961
}
6062

6163
public saveAst(ast: File) {
62-
const filePath = path.join(process.cwd(), debugDir, this.relativeFilePath + '.json');
64+
const filePath = path.join(process.cwd(), this._debugDir, this.relativeFilePath + '.json');
6365
this._saveObject(filePath, ast);
6466
}
6567

6668
public saveOutput(output: string) {
67-
const filePath = path.join(process.cwd(), debugDir, this.relativeFilePath + '.txt');
69+
const filePath = path.join(process.cwd(), this._debugDir, this.relativeFilePath + '.txt');
6870
this._saveText(filePath, output);
6971
}
7072

packages/compiler/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export type ReplexicaConfig = {
2-
outDir: string;
3-
i18nDir: string;
42
sourceLocale: string;
53
debug?: boolean;
64
};

0 commit comments

Comments
 (0)