diff --git a/index.js b/index.js index 46074f6..004161d 100644 --- a/index.js +++ b/index.js @@ -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') { @@ -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, }; diff --git a/test/test.js b/test/test.js index 0647f0b..b25455b 100644 --- a/test/test.js +++ b/test/test.js @@ -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() { @@ -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")';