Skip to content

Commit 6c2d99a

Browse files
committed
feat(compiler): add root export
1 parent 5fe67c0 commit 6c2d99a

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

packages/compiler/src/index.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,56 @@
1-
import { code, compile } from './code';
1+
import { createUnplugin } from "unplugin"
2+
import { ReplexicaAttributeScope, ReplexicaCompiler, ReplexicaContentScope, ReplexicaSkipScope } from "./compiler";
3+
import { ReplexicaOutputProcessor } from "./output";
4+
import path from 'path';
5+
import { createNextPlugin } from "./plugins";
6+
import { ReplexicaConfig } from "./types";
27

8+
const unplugin = createUnplugin<ReplexicaConfig>((options) => ({
9+
name: '@replexica/compiler',
10+
enforce: 'pre',
11+
transformInclude(id) {
12+
// .tsx and .jsx files
13+
return /\.(t|j)sx$/.test(id);
14+
},
15+
transform(code, absoluteFilePath) {
16+
try {
17+
const relativeFilePath = path.relative(process.cwd(), absoluteFilePath);
18+
19+
const relativeFileDir = path.relative(process.cwd(), path.dirname(absoluteFilePath));
20+
const i18nImportPrefix = path.relative(relativeFileDir, options.i18nDir);
21+
22+
const compiler = ReplexicaCompiler
23+
.fromCode(code, relativeFilePath, i18nImportPrefix)
24+
.withScope(ReplexicaSkipScope)
25+
.withScope(ReplexicaAttributeScope)
26+
.withScope(ReplexicaContentScope)
27+
.injectIntl();
28+
29+
const result = compiler.generate();
30+
31+
const outputProcessor = ReplexicaOutputProcessor.create(relativeFilePath, options);
32+
outputProcessor.saveData(compiler.data);
33+
outputProcessor.saveSourceLocaleData(compiler.data);
34+
35+
if (options.debug) {
36+
outputProcessor.saveAst(compiler.ast);
37+
outputProcessor.saveOutput(result.code);
38+
}
39+
40+
return {
41+
code: result.code,
42+
map: result.map,
43+
};
44+
} catch (error: any) {
45+
throw new ReplexicaError(error.message);
46+
}
47+
},
48+
}));
49+
50+
class ReplexicaError extends Error {
51+
52+
}
53+
export * from './types';
354
export default {
4-
code,
5-
compile,
55+
next: createNextPlugin(unplugin),
656
};

0 commit comments

Comments
 (0)