Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/next-intl/src/extractor/ExtractionCompiler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export default class ExtractionCompiler implements Disposable {

constructor(
config: ExtractorConfig,
opts: {isDevelopment?: boolean; projectRoot?: string} = {}
opts: {
isDevelopment?: boolean;
projectRoot?: string;
sourceMap?: boolean;
} = {}
) {
this.manager = new CatalogManager(config, opts);
this.isDevelopment = opts.isDevelopment ?? false;
Expand All @@ -33,7 +37,7 @@ export default class ExtractionCompiler implements Disposable {
void this.manager.save();
}

return result.source;
return result;
}

private async performInitialScan(): Promise<void> {
Expand Down
19 changes: 15 additions & 4 deletions packages/next-intl/src/extractor/catalog/CatalogManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export default class CatalogManager {

constructor(
config: ExtractorConfig,
opts: {projectRoot?: string; isDevelopment?: boolean} = {}
opts: {
projectRoot?: string;
isDevelopment?: boolean;
sourceMap?: boolean;
} = {}
) {
this.config = config;
this.saveScheduler = new SaveScheduler<number>(50);
Expand All @@ -52,7 +56,8 @@ export default class CatalogManager {

this.messageExtractor = new MessageExtractor({
isDevelopment: this.isDevelopment,
projectRoot: this.projectRoot
projectRoot: this.projectRoot,
sourceMap: opts.sourceMap
});
}

Expand Down Expand Up @@ -177,8 +182,9 @@ export default class CatalogManager {
source: string
): Promise<{
messages: Array<ExtractedMessage>;
source: string;
code: string;
changed: boolean;
map?: string;
}> {
const result = await this.messageExtractor.processFileContent(
absoluteFilePath,
Expand Down Expand Up @@ -248,7 +254,12 @@ export default class CatalogManager {
}

const changed = this.haveMessagesChanged(prevFileMessages, fileMessages);
return {...result, changed};
return {
messages: result.messages,
code: result.code,
map: result.map,
changed
};
}

private haveMessagesChanged(
Expand Down
4 changes: 2 additions & 2 deletions packages/next-intl/src/extractor/extractor/LRUCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ it('should not evict entries when updating existing key at max capacity', () =>
it('should work with complex objects', () => {
type ComplexValue = {
messages: Array<{id: string; message: string}>;
source: string;
code: string;
};

const cache = new LRUCache<ComplexValue>(2);

const value1: ComplexValue = {
messages: [{id: 'key1', message: 'Hello'}],
source: 'const t = useTranslations(); t("Hello");'
code: 'const t = useTranslations(); t("Hello");'
};

cache.set('file1', value1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {bench} from 'vitest';
import MessageExtractor from './MessageExtractor.js';

const testCode = `
import {useExtracted} from 'next-intl';

function Component() {
const t = useExtracted();
t("Hello, {name}!", {name: 'Alice'});
t("Welcome!");
t.rich("Hello <b>World</b>!", {b: (chunks) => <b>{chunks}</b>});
t.markup("Click <a>here</a>", {a: (chunks) => \`<a>\${chunks}</a>\`});
t.has("Test message");
}

function AnotherComponent() {
const t = useExtracted('ui');
t("Button label");
t("Submit");
}
`;

bench('extract messages without source maps', async () => {
const extractor = new MessageExtractor({
isDevelopment: true,
projectRoot: '/project',
sourceMap: false
});

await extractor.processFileContent('/project/test.tsx', testCode);
});

bench('extract messages with source maps', async () => {
const extractor = new MessageExtractor({
isDevelopment: true,
projectRoot: '/project',
sourceMap: true
});

await extractor.processFileContent('/project/test.tsx', testCode);
});
Loading