Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Commit 43dae5b

Browse files
author
Long Ho
committed
feat(babel-plugin-react-intl): output file with empty [] if src has no messages
For build systems like bazel that relies on specific output mapping, not writing out a file can cause issues. This also fix #553 as a side-effect.
1 parent 30b9de9 commit 43dae5b

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

packages/babel-plugin-react-intl/src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66

77
import * as p from 'path';
8-
import {writeFileSync} from 'fs';
9-
import {mkdirpSync} from 'fs-extra';
8+
import {outputJSONSync} from 'fs-extra';
109
import {parse} from 'intl-messageformat-parser/dist';
1110
const {declare} = require('@babel/helper-plugin-utils') as any;
1211
import {types as t, PluginObj} from '@babel/core';
@@ -342,7 +341,7 @@ export default declare((api: any, options: OptionsSchema) => {
342341
const descriptors = Array.from(messages.values());
343342
state.metadata['react-intl'] = {messages: descriptors};
344343

345-
if (basename && messagesDir && descriptors.length > 0) {
344+
if (basename && messagesDir) {
346345
// Make sure the relative path is "absolute" before
347346
// joining it with the `messagesDir`.
348347
let relativePath = p.join(p.sep, p.relative(process.cwd(), filename));
@@ -364,10 +363,7 @@ export default declare((api: any, options: OptionsSchema) => {
364363
basename + '.json'
365364
);
366365

367-
const messagesFile = JSON.stringify(descriptors, null, 2);
368-
369-
mkdirpSync(p.dirname(messagesFilename));
370-
writeFileSync(messagesFilename, messagesFile);
366+
outputJSONSync(messagesFilename, descriptors);
371367
}
372368
},
373369

packages/babel-plugin-react-intl/test/__snapshots__/index.test.ts.snap

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,40 @@ Array [
270270
]
271271
`;
272272

273+
exports[`emit asserts for: output match: empty 1`] = `
274+
Object {
275+
"messages": Array [],
276+
}
277+
`;
278+
279+
exports[`emit asserts for: output match: empty 2`] = `
280+
"\\"use strict\\";
281+
282+
Object.defineProperty(exports, \\"__esModule\\", {
283+
value: true
284+
});
285+
exports.default = void 0;
286+
287+
var _react = _interopRequireWildcard(require(\\"react\\"));
288+
289+
var _macro = require(\\"@formatjs/macro\\");
290+
291+
function _getRequireWildcardCache() { if (typeof WeakMap !== \\"function\\") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
292+
293+
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== \\"object\\" && typeof obj !== \\"function\\") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
294+
295+
class Foo extends _react.Component {
296+
render() {
297+
return _react.default.createElement(\\"div\\", null);
298+
}
299+
300+
}
301+
302+
exports.default = Foo;"
303+
`;
304+
305+
exports[`emit asserts for: output match: empty 3`] = `Array []`;
306+
273307
exports[`emit asserts for: output match: enforceDefaultMessage 1`] = `
274308
Object {
275309
"messages": Array [
@@ -500,7 +534,7 @@ class Foo extends _react.Component {
500534
id: \\"actual.js.foo.bar.zoo.12.object\\",
501535
defaultMessage: \\"Hello World!\\"
502536
}), _react.default.createElement(_reactIntl.FormattedMessage, {
503-
id: \\"HELLO..5.object\\",
537+
id: \\"actual.js..5.object\\",
504538
defaultMessage: \\"NO ID\\"
505539
}), _react.default.createElement(_reactIntl.FormattedHTMLMessage, {
506540
id: \\"actual.js.foo.bar.delta.21.string\\",
@@ -542,7 +576,7 @@ Array [
542576
"metadata": "Additional metadata content.",
543577
"text": "Something for the translator. Another description",
544578
},
545-
"id": "HELLO..5.object",
579+
"id": "actual.js..5.object",
546580
},
547581
Object {
548582
"defaultMessage": "<h1>Hello World!</h1>",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React, {Component} from 'react';
2+
import {_} from '@formatjs/macro';
3+
4+
export default class Foo extends Component {
5+
render() {
6+
return <div></div>;
7+
}
8+
}

0 commit comments

Comments
 (0)