easygettext
Advanced tools
Comparing version 2.13.0 to 2.14.0
{ | ||
"name": "easygettext", | ||
"version": "2.13.0", | ||
"version": "2.14.0", | ||
"description": "Simple tools to extract gettext strings", | ||
@@ -8,3 +8,3 @@ "main": "src/extract-cli.js", | ||
"type": "git", | ||
"url": "https://github.com/polyconseil/easygettext" | ||
"url": "https://github.com/PitcherAG/easygettext" | ||
}, | ||
@@ -11,0 +11,0 @@ "publishConfig": { |
@@ -13,2 +13,4 @@ exports.DEFAULT_ATTRIBUTES = [ | ||
exports.DEFAULT_VUE_GETTEXT_FUNCTIONS = { | ||
'_': ['msgid'], | ||
'$t': ['msgid'], | ||
'$gettext': ['msgid'], | ||
@@ -15,0 +17,0 @@ '$ngettext': ['msgid', 'plural', null], |
@@ -56,56 +56,76 @@ const cheerio = require('cheerio'); | ||
function preprocessScript(data, type) { | ||
let scriptData = ''; | ||
let templateData = ''; | ||
let scriptLang = undefined; | ||
switch (type) { | ||
case 'vue': | ||
const vueFile = vueCompiler.parse({ compiler, source: data, needMap: false }); | ||
if(vueFile.script) { | ||
scriptData = vueFile.script.content.trim(); | ||
scriptLang = vueFile.script.lang; | ||
const contents = []; | ||
if (type === 'vue') { | ||
const vueFile = vueCompiler.parse({ | ||
compiler, | ||
source: data, | ||
needMap: false | ||
}); | ||
if (vueFile.script) { | ||
contents.push({ | ||
content: vueFile.script.content.trim(), | ||
lang: vueFile.script.lang || 'js' | ||
}) | ||
} | ||
if(vueFile.template) { | ||
templateData = vueFile.template.content; | ||
templateData = vueCompiler.compileTemplate({compiler, source: templateData}).code; | ||
const vueTemplate = vueCompiler.compileTemplate({ | ||
compiler, | ||
source: vueFile.template.content | ||
}); | ||
contents.push({ | ||
content: vueTemplate.code, | ||
lang: 'js' | ||
}) | ||
} | ||
break; | ||
default: | ||
scriptData = data || ''; | ||
break; | ||
} else { | ||
contents.push({ | ||
content: data || '', | ||
lang: type | ||
}) | ||
} | ||
return [{content: scriptData, lang: scriptLang}, {content: templateData, lang: 'js'}]; | ||
return contents; | ||
} | ||
function preprocessTemplate(data, type) { | ||
let templateData = data || ''; | ||
switch (type) { | ||
case 'jade': | ||
case 'pug': | ||
// Add empty require function to the context to avoid errors with webpack require inside pug | ||
templateData = pug.render(data, {filename: 'source.html', pretty: true, require: function() {}}).trim(); | ||
break; | ||
case 'vue': | ||
const $ = cheerio.load(templateData, { | ||
xmlMode: true, | ||
decodeEntities: false, | ||
withStartIndices: true, | ||
}); | ||
function preprocessTemplate(data, type = 'html') { | ||
let templateData = null; | ||
templateData = $('template').map(function() { | ||
let lang = $(this).attr('lang'); | ||
if (data) { | ||
if (type === 'jade' || type === 'pug') { | ||
templateData = pug.render(data, { | ||
filename: 'source.html', | ||
pretty: true, | ||
require: () => { | ||
}, | ||
}).trim(); | ||
} else if (type === 'vue') { | ||
const $ = cheerio.load(data, { | ||
xmlMode: true, | ||
decodeEntities: false, | ||
withStartIndices: true, | ||
}); | ||
if (lang) { | ||
return preprocessTemplate($(this).html(), lang); | ||
templateData = $('template').map(function() { | ||
let lang = $(this).attr('lang'); | ||
if (lang) { | ||
return preprocessTemplate($(this).html(), lang); | ||
} | ||
return $(this).html().trim(); | ||
}).toArray(); | ||
// if there is just one template, use it as a string | ||
if (templateData.length === 1) { | ||
templateData = templateData[0]; | ||
} | ||
return $(this).html().trim(); | ||
}).toArray(); | ||
// if there is just one template, use it as a string | ||
if (templateData.length === 1) { | ||
templateData = templateData[0]; | ||
} else if (type === 'html') { | ||
templateData = data; | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return templateData; | ||
@@ -237,3 +257,3 @@ } | ||
body: endDelimiter === '' ? `(${bodyCore})` : `(${bodyCore}?(?!${end}))`, | ||
filters: this.options.attributes.join('|'), | ||
filters: this.options.attributes.join('?(\(.*\))|'), | ||
}; | ||
@@ -259,9 +279,16 @@ } | ||
extract(filename, ext, content) { | ||
this.parse(filename, preprocessTemplate(content, ext)); | ||
const templateData = preprocessTemplate(content, ext); | ||
if (templateData) { | ||
this.parse(filename, preprocessTemplate(content, ext)); | ||
} | ||
preprocessScript(content, ext).forEach( | ||
({content, lang})=> ( | ||
(lang === 'ts' || (!lang && ext === 'ts')) | ||
? this.parseTypeScript(filename, content) | ||
: this.parseJavascript(filename, content) | ||
) | ||
({content, lang}) => { | ||
if (lang === 'js') { | ||
this.parseJavascript(filename, content); | ||
} else if (lang === 'ts') { | ||
this.parseTypeScript(filename, content); | ||
} | ||
}, | ||
); | ||
@@ -268,0 +295,0 @@ } |
@@ -144,3 +144,10 @@ const extract = require('./extract.js'); | ||
const [script] = extract.preprocessScript(fixtures.VUE_COMPONENT_WITHOUT_SCRIPT_TAG, 'vue'); | ||
expect(script.content).toBe(''); | ||
expect(script.content).toBe('var render = function() {\n' + | ||
' var _vm = this\n' + | ||
' var _h = _vm.$createElement\n' + | ||
' var _c = _vm._self._c || _h\n' + | ||
' return _c("h1", [_vm._v("Hello")])\n' + | ||
'}\n' + | ||
'var staticRenderFns = []\n' + | ||
'render._withStripped = true\n'); | ||
}); | ||
@@ -147,0 +154,0 @@ |
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
105741
2542