@arkxio/ark-dev-utils 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -583,7 +583,7 @@
583
583
  */
584
584
  async function writeInnerText(childDom, fileType, options) {
585
585
  const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
586
- const innerText = getInnerText(childDom);
586
+ let innerText = getInnerText(childDom);
587
587
  if (!innerText) return '';
588
588
 
589
589
  verbose(`found a user customized ${fileType} tag node in html, try extract its content and write them to local fs`);
@@ -592,6 +592,20 @@
592
592
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
593
593
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
594
594
 
595
+ // 对于 JS 文件,移除或替换 document.write() 调用
596
+ // 因为异步加载的脚本中不能使用 document.write()
597
+ if (fileType === 'js') {
598
+ // 方法1:完全移除 document.write() 调用(推荐)
599
+ // innerText = innerText.replace(/document\.write\s*\([^)]*\)/g, '');
600
+
601
+ // 方法2:用 console.warn 替换 document.write(),保留代码逻辑但避免错误
602
+ innerText = innerText.replace(
603
+ /document\.write\s*\(([^)]*)\)/g,
604
+ 'console.warn("[ark-micro] document.write is not allowed in asynchronously loaded scripts:", $1)'
605
+ );
606
+ verbose(`Replaced document.write calls in ${scriptName}`);
607
+ }
608
+
595
609
  // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
596
610
  if (compilation && compilation.emitAsset) {
597
611
  const { sources } = compilation.compiler.webpack;
@@ -583,7 +583,7 @@
583
583
  */
584
584
  async function writeInnerText(childDom, fileType, options) {
585
585
  const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
586
- const innerText = getInnerText(childDom);
586
+ let innerText = getInnerText(childDom);
587
587
  if (!innerText) return '';
588
588
 
589
589
  verbose(`found a user customized ${fileType} tag node in html, try extract its content and write them to local fs`);
@@ -592,6 +592,20 @@
592
592
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
593
593
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
594
594
 
595
+ // 对于 JS 文件,移除或替换 document.write() 调用
596
+ // 因为异步加载的脚本中不能使用 document.write()
597
+ if (fileType === 'js') {
598
+ // 方法1:完全移除 document.write() 调用(推荐)
599
+ // innerText = innerText.replace(/document\.write\s*\([^)]*\)/g, '');
600
+
601
+ // 方法2:用 console.warn 替换 document.write(),保留代码逻辑但避免错误
602
+ innerText = innerText.replace(
603
+ /document\.write\s*\(([^)]*)\)/g,
604
+ 'console.warn("[ark-micro] document.write is not allowed in asynchronously loaded scripts:", $1)'
605
+ );
606
+ verbose(`Replaced document.write calls in ${scriptName}`);
607
+ }
608
+
595
609
  // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
596
610
  if (compilation && compilation.emitAsset) {
597
611
  const { sources } = compilation.compiler.webpack;
package/lib/index.js CHANGED
@@ -588,7 +588,7 @@ function resetScriptIdx() {
588
588
  */
589
589
  async function writeInnerText(childDom, fileType, options) {
590
590
  const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
591
- const innerText = getInnerText(childDom);
591
+ let innerText = getInnerText(childDom);
592
592
  if (!innerText) return '';
593
593
 
594
594
  verbose(`found a user customized ${fileType} tag node in html, try extract its content and write them to local fs`);
@@ -597,6 +597,20 @@ async function writeInnerText(childDom, fileType, options) {
597
597
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
598
598
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
599
599
 
600
+ // 对于 JS 文件,移除或替换 document.write() 调用
601
+ // 因为异步加载的脚本中不能使用 document.write()
602
+ if (fileType === 'js') {
603
+ // 方法1:完全移除 document.write() 调用(推荐)
604
+ // innerText = innerText.replace(/document\.write\s*\([^)]*\)/g, '');
605
+
606
+ // 方法2:用 console.warn 替换 document.write(),保留代码逻辑但避免错误
607
+ innerText = innerText.replace(
608
+ /document\.write\s*\(([^)]*)\)/g,
609
+ 'console.warn("[ark-micro] document.write is not allowed in asynchronously loaded scripts:", $1)'
610
+ );
611
+ verbose(`Replaced document.write calls in ${scriptName}`);
612
+ }
613
+
600
614
  // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
601
615
  if (compilation && compilation.emitAsset) {
602
616
  const { sources } = compilation.compiler.webpack;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkxio/ark-dev-utils",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "ark dev utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -79,7 +79,7 @@ export function resetScriptIdx() {
79
79
  */
80
80
  async function writeInnerText(childDom, fileType, options) {
81
81
  const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
82
- const innerText = getInnerText(childDom);
82
+ let innerText = getInnerText(childDom);
83
83
  if (!innerText) return '';
84
84
 
85
85
  verbose(`found a user customized ${fileType} tag node in html, try extract its content and write them to local fs`);
@@ -88,6 +88,20 @@ async function writeInnerText(childDom, fileType, options) {
88
88
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
89
89
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
90
90
 
91
+ // 对于 JS 文件,移除或替换 document.write() 调用
92
+ // 因为异步加载的脚本中不能使用 document.write()
93
+ if (fileType === 'js') {
94
+ // 方法1:完全移除 document.write() 调用(推荐)
95
+ // innerText = innerText.replace(/document\.write\s*\([^)]*\)/g, '');
96
+
97
+ // 方法2:用 console.warn 替换 document.write(),保留代码逻辑但避免错误
98
+ innerText = innerText.replace(
99
+ /document\.write\s*\(([^)]*)\)/g,
100
+ 'console.warn("[ark-micro] document.write is not allowed in asynchronously loaded scripts:", $1)'
101
+ );
102
+ verbose(`Replaced document.write calls in ${scriptName}`);
103
+ }
104
+
91
105
  // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
92
106
  if (compilation && compilation.emitAsset) {
93
107
  const { sources } = compilation.compiler.webpack;