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",