Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESM support #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export NODE_PATH = ./

MOCHA_CMD = mocha --require @babel/register

test_esm_extract:
$(MOCHA_CMD) ./tests/functional/test_esm_extract.js

test_extract_gettext:
$(MOCHA_CMD) ./tests/functional/test_extract_gettext_simple.js

Expand Down Expand Up @@ -121,7 +124,7 @@ test_alias_extract:
test_extract_js_format:
$(MOCHA_CMD) ./tests/functional/test_extract_js_format.js


test_fun: test_esm_extract
test_fun: test_extract_gettext
test_fun: test_extract_fn_gettext
test_fun: test_extract_gettext_with_formatting
Expand Down
2 changes: 2 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const ALIAS_TO_FUNC_MAP = Object.keys(FUNC_TO_ALIAS_MAP).reduce((obj, key
return obj;
}, {});

export const FUNC_ALIASES = () => Object.values(FUNC_TO_ALIAS_MAP).flat();

export const PO_PRIMITIVES = {
MSGSTR: 'msgstr',
MSGID: 'msgid',
Expand Down
6 changes: 4 additions & 2 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import mkdirp from 'mkdirp';
import path from 'path';

import { ALIAS_TO_FUNC_MAP } from './defaults';
import { ALIAS_TO_FUNC_MAP, FUNC_ALIASES } from './defaults';
import { buildPotData, makePotStr } from './po-helpers';
import { extractPoEntry, getExtractor } from './extract';
import {
Expand Down Expand Up @@ -230,7 +230,9 @@ export default function ttagPlugin() {
context.addImport(local.name);
});
} else {
throw new Error('You should use ttag imports in form: "import { t } from \'ttag\'"');
FUNC_ALIASES().forEach((value) => {
context.addImport(value);
});
}

if (context.isResolveMode()) {
Expand Down
29 changes: 29 additions & 0 deletions tests/functional/test_esm_extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from 'chai';
import * as babel from '@babel/core';
import fs from 'fs';
import ttag from 'src/plugin';
import { rmDirSync } from 'src/utils';
import dedent from 'dedent';

const output = 'debug/translations.pot';

const options = {
plugins: [[ttag, { extract: { output } }]],
};

describe('ESM extract', () => {
before(() => {
rmDirSync('debug');
});

it('should extract by default import', () => {
const input = dedent(`
import defaultTtag from 'ttag';
const {t} = defaultTtag;
t\`import default extract test\`
`);
babel.transform(input, options);
const result = fs.readFileSync(output).toString();
expect(result).to.contain('import default extract test');
});
});