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
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ module.exports = function() {
var fileName = plugin.opts && plugin.opts.fileName || DEFAULT_FILE_NAME;
var headers = plugin.opts && plugin.opts.headers || DEFAULT_HEADERS;
var base = plugin.opts && plugin.opts.baseDirectory;
var universalSlash = plugin.opts && plugin.opts.universalSlash;

if (base) {
base = base.match(/^(.*?)\/*$/)[1] + '/';
base = base.match(/^(.*?)[\//]*$/)[1] + path.sep;
}

if (typeof fileName === 'function') {
Expand Down Expand Up @@ -145,6 +147,12 @@ module.exports = function() {
fn = fn.substr(base.length);
}

if (universalSlash === '/') {
fn = fn.replace(/\\/g, universalSlash)
} else if (universalSlash === '\\') {
fn = fn.replace(/\//g, universalSlash)
}

translate.comments = {
reference: fn + ':' + nodePath.node.loc.start.line,
};
Expand Down
80 changes: 80 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
var assert = require('assert');
var babel = require('@babel/core');
var path = require('path');
var rimraf = require('rimraf');

var fs = require('fs');
var plugin = require('../index.js');

afterEach((done) => rimraf(path.join(__dirname, '*.po'), done));

describe('babel-gettext-extractor', function() {
describe('#extract()', function() {
it('Should return a result for simple code example', function() {
Expand Down Expand Up @@ -177,6 +181,82 @@ describe('babel-gettext-extractor', function() {
assert(content.indexOf('msgid "title"') !== -1);
});

it('Should use a universal slash (windows to *nix)', () => {
var result = babel.transform('let t = _t("code");_t("hello");', {
filename: 'c:\\Users\\TheUser\\Project\\file.js',
plugins: [
[plugin, {
functionNames: {
_t: ['msgid'],
},
fileName: './test/first.po',
baseDirectory: 'c:\\Users\\TheUser\\Project\\',
universalSlash: '/',
}],
],
});
assert(!!result);

var content = fs.readFileSync('./test/first.po');
assert(content.indexOf('Project/file.js') !== -1);
});

it('Should use a universal slash (*nix to windows)', () => {
var result = babel.transform('let t = _t("code");_t("hello");', {
filename: '/home/user/projects/code/file.js',
plugins: [
[plugin, {
functionNames: {
_t: ['msgid'],
},
fileName: './test/slash-windows.po',
baseDirectory: '/home/user/projects/',
universalSlash: '\\',
}],
],
});
assert(!!result);

var content = fs.readFileSync('./test/slash-windows.po', 'utf8');
assert(content.indexOf('code\\file.js') !== -1);
});

it('should strip the base directory', () => {
var result = babel.transform('let t = _t("code");_t("hello");', {
filename: path.sep + ['this', 'is', 'the', 'path', 'file.js'].join(path.sep),
plugins: [
[plugin, {
baseDirectory: path.sep + ['this', 'is', 'the', 'path'].join(path.sep),
functionNames: {
_t: ['msgid'],
},
fileName: './test/strip-base-1.po',
}],
],
});
assert(!!result);
var content = fs.readFileSync('./test/strip-base-1.po');
assert(content.indexOf('#: file.js') !== -1);
});

it('should strip the base if it ends in slash', () => {
var result = babel.transform('let t = _t("code");_t("hello");', {
filename: path.sep + ['this', 'is', 'the', 'path', 'file.js'].join(path.sep),
plugins: [
[plugin, {
baseDirectory: path.sep + ['this', 'is', 'the', 'path'].join(path.sep) + path.sep,
functionNames: {
_t: ['msgid'],
},
fileName: './test/strip-base-2.po',
}],
],
});
assert(!!result);
var content = fs.readFileSync('./test/strip-base-2.po');
assert(content.indexOf('#: file.js') !== -1);
});

it('Should decide on a filename dynamically', function() {
const code = '_t("Dynamic Filenames")';

Expand Down