@arkxio/ark-dev-utils 0.1.2 → 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.
@@ -582,8 +582,8 @@
582
582
  * @param {IInnerFillAssetListOptions} options
583
583
  */
584
584
  async function writeInnerText(childDom, fileType, options) {
585
- const { homePage, buildDirFullPath } = options;
586
- const innerText = getInnerText(childDom);
585
+ const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
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,7 +592,38 @@
592
592
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
593
593
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
594
594
 
595
- await writeFile(fileAbsolutePath, innerText);
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
+
609
+ // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
610
+ if (compilation && compilation.emitAsset) {
611
+ const { sources } = compilation.compiler.webpack;
612
+ compilation.emitAsset(
613
+ scriptName,
614
+ new sources.RawSource(innerText)
615
+ );
616
+ verbose(`emit asset ${scriptName} via webpack compilation`);
617
+ } else {
618
+ // 生产模式或没有 compilation 对象时,直接写入文件系统
619
+ await writeFile(fileAbsolutePath, innerText);
620
+ }
621
+
622
+ // 将自定义资产信息保存到 customAssets 数组,供后续使用
623
+ if (customAssets) {
624
+ customAssets.push({ scriptName, fileWebPath, content: innerText });
625
+ }
626
+
596
627
  verbose(`write done, the web file will be ${fileWebPath} later`);
597
628
  return fileWebPath;
598
629
  }
@@ -1236,11 +1267,16 @@
1236
1267
  compiler.hooks.emit.tapPromise('ArkMetaJsonGeneratorPlugin', async (compilation) => {
1237
1268
  // 注入环境判断脚本
1238
1269
  const isDev = process.env.NODE_ENV === 'development';
1270
+
1271
+ // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
1272
+ const customAssets = [];
1273
+
1239
1274
  await extractArkMetaJson({
1240
1275
  ...useExtractOptions,
1241
1276
  // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
1242
1277
  isDev: isDev,
1243
- compilation: compilation
1278
+ compilation: compilation,
1279
+ customAssets: customAssets
1244
1280
  });
1245
1281
 
1246
1282
  return Promise.resolve();
@@ -582,8 +582,8 @@
582
582
  * @param {IInnerFillAssetListOptions} options
583
583
  */
584
584
  async function writeInnerText(childDom, fileType, options) {
585
- const { homePage, buildDirFullPath } = options;
586
- const innerText = getInnerText(childDom);
585
+ const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
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,7 +592,38 @@
592
592
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
593
593
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
594
594
 
595
- await writeFile(fileAbsolutePath, innerText);
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
+
609
+ // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
610
+ if (compilation && compilation.emitAsset) {
611
+ const { sources } = compilation.compiler.webpack;
612
+ compilation.emitAsset(
613
+ scriptName,
614
+ new sources.RawSource(innerText)
615
+ );
616
+ verbose(`emit asset ${scriptName} via webpack compilation`);
617
+ } else {
618
+ // 生产模式或没有 compilation 对象时,直接写入文件系统
619
+ await writeFile(fileAbsolutePath, innerText);
620
+ }
621
+
622
+ // 将自定义资产信息保存到 customAssets 数组,供后续使用
623
+ if (customAssets) {
624
+ customAssets.push({ scriptName, fileWebPath, content: innerText });
625
+ }
626
+
596
627
  verbose(`write done, the web file will be ${fileWebPath} later`);
597
628
  return fileWebPath;
598
629
  }
@@ -1236,11 +1267,16 @@
1236
1267
  compiler.hooks.emit.tapPromise('ArkMetaJsonGeneratorPlugin', async (compilation) => {
1237
1268
  // 注入环境判断脚本
1238
1269
  const isDev = process.env.NODE_ENV === 'development';
1270
+
1271
+ // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
1272
+ const customAssets = [];
1273
+
1239
1274
  await extractArkMetaJson({
1240
1275
  ...useExtractOptions,
1241
1276
  // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
1242
1277
  isDev: isDev,
1243
- compilation: compilation
1278
+ compilation: compilation,
1279
+ customAssets: customAssets
1244
1280
  });
1245
1281
 
1246
1282
  return Promise.resolve();
package/lib/index.js CHANGED
@@ -587,8 +587,8 @@ function resetScriptIdx() {
587
587
  * @param {IInnerFillAssetListOptions} options
588
588
  */
589
589
  async function writeInnerText(childDom, fileType, options) {
590
- const { homePage, buildDirFullPath } = options;
591
- const innerText = getInnerText(childDom);
590
+ const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
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,7 +597,38 @@ async function writeInnerText(childDom, fileType, options) {
597
597
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
598
598
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
599
599
 
600
- await writeFile(fileAbsolutePath, innerText);
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
+
614
+ // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
615
+ if (compilation && compilation.emitAsset) {
616
+ const { sources } = compilation.compiler.webpack;
617
+ compilation.emitAsset(
618
+ scriptName,
619
+ new sources.RawSource(innerText)
620
+ );
621
+ verbose(`emit asset ${scriptName} via webpack compilation`);
622
+ } else {
623
+ // 生产模式或没有 compilation 对象时,直接写入文件系统
624
+ await writeFile(fileAbsolutePath, innerText);
625
+ }
626
+
627
+ // 将自定义资产信息保存到 customAssets 数组,供后续使用
628
+ if (customAssets) {
629
+ customAssets.push({ scriptName, fileWebPath, content: innerText });
630
+ }
631
+
601
632
  verbose(`write done, the web file will be ${fileWebPath} later`);
602
633
  return fileWebPath;
603
634
  }
@@ -1241,11 +1272,16 @@ class ArkWebpackPlugin {
1241
1272
  compiler.hooks.emit.tapPromise('ArkMetaJsonGeneratorPlugin', async (compilation) => {
1242
1273
  // 注入环境判断脚本
1243
1274
  const isDev = process.env.NODE_ENV === 'development';
1275
+
1276
+ // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
1277
+ const customAssets = [];
1278
+
1244
1279
  await extractArkMetaJson({
1245
1280
  ...useExtractOptions,
1246
1281
  // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
1247
1282
  isDev: isDev,
1248
- compilation: compilation
1283
+ compilation: compilation,
1284
+ customAssets: customAssets
1249
1285
  });
1250
1286
 
1251
1287
  return Promise.resolve();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkxio/ark-dev-utils",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "ark dev utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -93,11 +93,16 @@ export default class ArkWebpackPlugin {
93
93
  compiler.hooks.emit.tapPromise('ArkMetaJsonGeneratorPlugin', async (compilation) => {
94
94
  // 注入环境判断脚本
95
95
  const isDev = process.env.NODE_ENV === 'development';
96
+
97
+ // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
98
+ const customAssets = [];
99
+
96
100
  const arkMeta = await extractArkMetaJson({
97
101
  ...useExtractOptions,
98
102
  // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
99
103
  isDev: isDev,
100
- compilation: compilation
104
+ compilation: compilation,
105
+ customAssets: customAssets
101
106
  });
102
107
 
103
108
  return Promise.resolve();
@@ -78,8 +78,8 @@ export function resetScriptIdx() {
78
78
  * @param {IInnerFillAssetListOptions} options
79
79
  */
80
80
  async function writeInnerText(childDom, fileType, options) {
81
- const { homePage, buildDirFullPath } = options;
82
- const innerText = getInnerText(childDom);
81
+ const { homePage, buildDirFullPath, compilation, customAssets = [] } = options;
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,7 +88,38 @@ async function writeInnerText(childDom, fileType, options) {
88
88
  const fileAbsolutePath = `${buildDirFullPath}/${scriptName}`;
89
89
  const fileWebPath = `${slash.noEnd(homePage)}/${scriptName}`;
90
90
 
91
- await writeFile(fileAbsolutePath, innerText);
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
+
105
+ // 在开发模式下,使用 compilation.emitAsset 确保文件能被 dev server 访问
106
+ if (compilation && compilation.emitAsset) {
107
+ const { sources } = compilation.compiler.webpack;
108
+ compilation.emitAsset(
109
+ scriptName,
110
+ new sources.RawSource(innerText)
111
+ );
112
+ verbose(`emit asset ${scriptName} via webpack compilation`);
113
+ } else {
114
+ // 生产模式或没有 compilation 对象时,直接写入文件系统
115
+ await writeFile(fileAbsolutePath, innerText);
116
+ }
117
+
118
+ // 将自定义资产信息保存到 customAssets 数组,供后续使用
119
+ if (customAssets) {
120
+ customAssets.push({ scriptName, fileWebPath, content: innerText });
121
+ }
122
+
92
123
  verbose(`write done, the web file will be ${fileWebPath} later`);
93
124
  return fileWebPath;
94
125
  }
package/typings.d.ts CHANGED
@@ -235,6 +235,10 @@ export interface IInnerFillAssetListOptions {
235
235
  enableRelativePath: IUserExtractOptions['enableRelativePath'];
236
236
  enablePrefixHomePage: IUserExtractOptions['enablePrefixHomePage'];
237
237
  enableAssetInnerText: IUserExtractOptions['enableAssetInnerText'];
238
+ /** webpack compilation 对象,用于在开发模式下通过 emitAsset 输出文件 */
239
+ compilation?: any;
240
+ /** 自定义资产列表,用于收集从 HTML 内联脚本提取的代码 */
241
+ customAssets?: Array<{ scriptName: string; fileWebPath: string; content: string }>;
238
242
  }
239
243
 
240
244
  export interface ICreateSubAppOptions {