babel-gettext-extractor
Advanced tools
Comparing version 4.0.0 to 4.1.0
28
index.js
@@ -66,9 +66,27 @@ var utils = require('./utils'); | ||
if (typeof fileName === 'function') { | ||
fileName = fileName(this.file); | ||
} | ||
if (!fileName) { | ||
return; | ||
} | ||
if (fileName !== currentFileName) { | ||
currentFileName = fileName; | ||
data = { | ||
charset: 'UTF-8', | ||
headers: headers, | ||
translations: { context: {} }, | ||
}; | ||
if (fs.existsSync(fileName)) { | ||
const fileContents = fs.readFileSync(fileName, 'utf8'); | ||
data = gettextParser.po.parse(fileContents); | ||
data.headers = { | ||
...(data.headers || {}), | ||
...headers, | ||
}; | ||
data.translations.context = data.translations.context || {}; | ||
} else { | ||
data = { | ||
charset: 'UTF-8', | ||
headers: headers, | ||
translations: { context: {} }, | ||
}; | ||
} | ||
@@ -75,0 +93,0 @@ headers['plural-forms'] = headers['plural-forms'] |
{ | ||
"name": "babel-gettext-extractor", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": " Extract gettext string with babel", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
[![Build Status](https://travis-ci.org/mozilla/babel-gettext-extractor.svg?branch=master)](https://travis-ci.org/mozilla/babel-gettext-extractor) | ||
[![npm version](https://img.shields.io/npm/v/babel-gettext-extractor.svg)](https://www.npmjs.com/package/babel-gettext-extractor) | ||
@@ -42,3 +43,3 @@ # babel-gettext-extractor | ||
"functionNames": <Object>, | ||
"fileName": <String>, | ||
"fileName": <String|Function>, | ||
"baseDirectory": <String>, | ||
@@ -75,4 +76,25 @@ "stripTemplateLiteralIndent": <Boolean> | ||
The filename where the end result is placed. | ||
The filename where the end result is placed. If you supply a function, it will receive the current file babel is working | ||
on and you can return the full path to where you want to save your translation template for this particular file. | ||
example: | ||
```javascript | ||
[ | ||
require("babel-gettext-extractor"), | ||
{ | ||
fileName: (file) => { | ||
const sourceFile = file.opts.sourceFileName; | ||
if (/^node_modules\//.test(sourceFile)) { | ||
return false; | ||
} | ||
return sourceFile | ||
.split(/[\/\\.]/) | ||
.filter(name => !['src', 'packages', 'js'].includes(name)) | ||
.slice(0, 2) | ||
.join('-') + '-template.pot'; | ||
}, | ||
}, | ||
] | ||
``` | ||
### baseDirectory ### | ||
@@ -79,0 +101,0 @@ |
@@ -7,3 +7,2 @@ var assert = require('assert'); | ||
describe('babel-gettext-extractor', function() { | ||
@@ -163,3 +162,37 @@ describe('#extract()', function() { | ||
}); | ||
it('Should decide on a filename dynamically', function() { | ||
const code = '_t("Dynamic Filenames")'; | ||
var result = babel.transform(code, { | ||
plugins: [ | ||
[plugin, { | ||
functionNames: { | ||
_t: ['msgid'], | ||
}, | ||
fileName: (file) => 'test/' + file.opts.sourceFileName + '-dynamic-filename.po', | ||
}], | ||
], | ||
}); | ||
assert(!!result); | ||
var content = fs.readFileSync('./test/unknown-dynamic-filename.po'); | ||
assert(content.indexOf('msgid "Dynamic Filenames"') !== -1); | ||
}); | ||
it('Should skip a file if the dynamic filename is false', function() { | ||
const code = '_t("Dynamic Filenames")'; | ||
var result = babel.transform(code, { | ||
plugins: [ | ||
[plugin, { | ||
functionNames: { | ||
_t: ['msgid'], | ||
}, | ||
fileName: () => false, | ||
}], | ||
], | ||
}); | ||
assert(!!result); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
18854
371
114
0