From 2848c76d21c732d08f960ab2e9a061413ff1a267 Mon Sep 17 00:00:00 2001 From: magicodex Date: Fri, 20 Oct 2023 11:42:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=94=A8babel=E8=BD=AC=E6=8D=A2jsx?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E7=9A=84=E4=BB=A3=E7=A0=81=E6=8A=BD=E7=A6=BB?= =?UTF-8?q?=E6=88=90=E7=8B=AC=E7=AB=8B=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/render-to-html-string.js | 27 +++----------------- bin/transform-jsx-to-js.js | 48 ++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 bin/transform-jsx-to-js.js diff --git a/bin/render-to-html-string.js b/bin/render-to-html-string.js index e723a1f..b48c3c7 100644 --- a/bin/render-to-html-string.js +++ b/bin/render-to-html-string.js @@ -1,9 +1,8 @@ "use strict"; var fs = require('fs'); -var babelCore = require('@babel/core'); var reactDOMServer = require('react-dom/server'); -var beautifyJs = require('js-beautify/js').js; +var transformJsxToJs = require('./transform-jsx-to-js'); module.exports = renderToHtmlString; @@ -26,30 +25,10 @@ function renderToHtmlString(jsxContent, options) { var deleteTempFile = !(options.deleteTempFile === false); // 转换 JSX 成 JS 脚本 - var transformResult = babelCore.transformSync(jsxContent, { - "presets": [ - [ - "@babel/preset-react", - { - "pragma": "React.createElement", - "pragmaFrag": "React.Fragment", - "throwIfNamespace": true, - "runtime": "classic" - } - ] - ] + var jsContent = transformJsxToJs(jsxContent, { + prettyFormat: prettyFormat }); - var jsContent = transformResult.code; - // 美化 JS 内容 - if (prettyFormat) { - jsContent = beautifyJs(jsContent, { - indent_size: 2, - wrap_line_length: 120, - space_in_empty_paren: true - }); - } - // 写入 JS 内容到临时文件 fs.writeFileSync(tempFilePath, jsContent); // 加载 JS 脚本 diff --git a/bin/transform-jsx-to-js.js b/bin/transform-jsx-to-js.js new file mode 100644 index 0000000..da28abf --- /dev/null +++ b/bin/transform-jsx-to-js.js @@ -0,0 +1,48 @@ +"use strict"; + +var babelCore = require('@babel/core'); +var beautifyJs = require('js-beautify/js').js; + +module.exports = transformJsxToJs; + +/** + * @description 转换 jsx 内容成 js 内容 + * @param {string} jsxContent + * @param {object} [options] + * @returns {string} + */ +function transformJsxToJs(jsxContent, options) { + if (jsxContent === null || jsxContent === undefined) { + return ''; + } + + options = (options || {}); + var prettyFormat = (options.prettyFormat === true); + + // 转换 JSX 成 JS 脚本 + var transformResult = babelCore.transformSync(jsxContent, { + "presets": [ + [ + "@babel/preset-react", + { + "pragma": "React.createElement", + "pragmaFrag": "React.Fragment", + "throwIfNamespace": true, + "runtime": "classic" + } + ] + ] + }); + + var jsContent = transformResult.code; + // 美化 JS 内容 + if (prettyFormat) { + jsContent = beautifyJs(jsContent, { + indent_size: 2, + wrap_line_length: 120, + space_in_empty_paren: true + }); + } + + return jsContent; +} diff --git a/package.json b/package.json index d07b2fe..6171fbe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsx-render-to-html", - "version": "1.1.5", + "version": "1.1.6", "description": "转换 jsx 文件成 html 文件", "license": "MIT", "author": "mh.z",