babel-plugin-react-intl
Advanced tools
Comparing version 7.7.3 to 7.8.0
@@ -14,3 +14,9 @@ import { SourceLocation } from '@babel/types'; | ||
}; | ||
export declare function resolveOutputPath(messagesDir: string, filename: string): string; | ||
/** | ||
* | ||
* @param workspaceRoot | ||
* @param messagesDir | ||
* @param filename Absolute path to the file | ||
*/ | ||
export declare function resolveOutputPath(workspaceRoot: string, messagesDir: string, filename: string): string; | ||
declare const _default: any; | ||
@@ -17,0 +23,0 @@ export default _default; |
40
index.js
@@ -122,3 +122,3 @@ "use strict"; | ||
} | ||
function storeMessage(_a, path, _b, filename, messages) { | ||
function storeMessage(_a, path, _b, workspaceRoot, filename, messages) { | ||
var id = _a.id, description = _a.description, defaultMessage = _a.defaultMessage; | ||
@@ -139,3 +139,3 @@ var extractSourceLocation = _b.extractSourceLocation; | ||
if (extractSourceLocation) { | ||
loc = __assign({ file: p.relative(process.cwd(), filename) }, path.node.loc); | ||
loc = __assign({ file: p.relative(workspaceRoot, filename) }, path.node.loc); | ||
} | ||
@@ -202,20 +202,14 @@ messages.set(id, __assign({ id: id, description: description, defaultMessage: defaultMessage }, loc)); | ||
} | ||
function resolveOutputPath(messagesDir, filename) { | ||
// If no filename is specified, that means this babel plugin is called programmatically | ||
// via NodeJS API by other programs (e.g. by feeding us with file content directly). In | ||
// this case we will only make extracted messages accessible via Babel result objects. | ||
var basename = filename ? p.basename(filename, p.extname(filename)) : null; | ||
// Make sure the relative path is "absolute" before | ||
// joining it with the `messagesDir`. | ||
var relativePath = p.join(p.sep, p.relative(process.cwd(), filename)); | ||
// Solve when the window user has symlink on the directory, because | ||
// process.cwd on windows returns the symlink root, | ||
// and filename (from babel) returns the original root | ||
if (process.platform === 'win32') { | ||
var name_1 = p.parse(process.cwd()).name; | ||
if (relativePath.includes(name_1)) { | ||
relativePath = relativePath.slice(relativePath.indexOf(name_1) + name_1.length); | ||
} | ||
/** | ||
* | ||
* @param workspaceRoot | ||
* @param messagesDir | ||
* @param filename Absolute path to the file | ||
*/ | ||
function resolveOutputPath(workspaceRoot, messagesDir, filename) { | ||
if (!filename.startsWith(workspaceRoot)) { | ||
throw new Error("File \"" + filename + "\" is not under workspace root \"" + workspaceRoot + "\". \nPlease configure workspaceRoot to be a folder that contains all files being extracted"); | ||
} | ||
return p.join(messagesDir, p.dirname(relativePath), basename + '.json'); | ||
var _a = p.parse(p.relative(workspaceRoot, filename)), name = _a.name, dir = _a.dir; | ||
return p.join(messagesDir, dir, name + ".json"); | ||
} | ||
@@ -229,3 +223,3 @@ exports.resolveOutputPath = resolveOutputPath; | ||
}); | ||
var messagesDir = options.messagesDir, outputEmptyJson = options.outputEmptyJson, pragma = options.pragma; | ||
var messagesDir = options.messagesDir, _a = options.workspaceRoot, workspaceRoot = _a === void 0 ? process.cwd() : _a, outputEmptyJson = options.outputEmptyJson, pragma = options.pragma; | ||
/** | ||
@@ -262,3 +256,3 @@ * Store this in the node itself so that multiple passes work. Specifically | ||
filename && | ||
(messagesFilename = resolveOutputPath(messagesDir, filename)) && | ||
(messagesFilename = resolveOutputPath(workspaceRoot, messagesDir, filename)) && | ||
(outputEmptyJson || descriptors.length)) { | ||
@@ -330,3 +324,3 @@ fs_extra_1.outputJSONSync(messagesFilename, descriptors); | ||
var descriptor = evaluateMessageDescriptor(descriptorPath, true, filename, overrideIdFn); | ||
storeMessage(descriptor, path, opts, filename, this.ReactIntlMessages); | ||
storeMessage(descriptor, path, opts, workspaceRoot, filename, this.ReactIntlMessages); | ||
var idAttr = void 0; | ||
@@ -391,3 +385,3 @@ var descriptionAttr = void 0; | ||
var descriptor = evaluateMessageDescriptor(descriptorPath, false, filename, overrideIdFn); | ||
storeMessage(descriptor, messageDescriptor, opts, filename, messages); | ||
storeMessage(descriptor, messageDescriptor, opts, workspaceRoot, filename, messages); | ||
// Remove description since it's not used at runtime. | ||
@@ -394,0 +388,0 @@ messageDescriptor.replaceWith(core_1.types.objectExpression(__spreadArrays([ |
@@ -16,3 +16,4 @@ /** | ||
pragma?: string; | ||
workspaceRoot?: string; | ||
} | ||
//# sourceMappingURL=options.d.ts.map |
@@ -15,5 +15,6 @@ { | ||
"outputEmptyJson": { "type": "boolean" }, | ||
"pragma": { "type": "string" } | ||
"pragma": { "type": "string" }, | ||
"workspaceRoot": { "type": "string" } | ||
}, | ||
"additionalProperties": false | ||
} |
{ | ||
"name": "babel-plugin-react-intl", | ||
"version": "7.7.3", | ||
"version": "7.8.0", | ||
"description": "Extracts string messages for translation from modules that use React Intl.", | ||
@@ -5,0 +5,0 @@ "repository": { |
509
25724