@arkxio/ark-dev-utils 0.1.3 → 0.1.5

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;
@@ -1254,12 +1268,36 @@
1254
1268
  // 注入环境判断脚本
1255
1269
  const isDev = process.env.NODE_ENV === 'development';
1256
1270
 
1271
+ // 收集所有生成的JS文件
1272
+ const outputPath = compilation.options.output.publicPath || '';
1273
+ const jsFiles = [];
1274
+
1275
+ // Webpack 5 正确的API:使用 getAssets()
1276
+ const assets = compilation.getAssets ? compilation.getAssets() : compilation.assets;
1277
+
1278
+ // 遍历所有资产,收集JS文件
1279
+ assets.forEach((asset) => {
1280
+ const assetName = asset.name || asset;
1281
+ if (assetName.endsWith('.js')) {
1282
+ console.log('Found JS file:', assetName);
1283
+ // 排除 ark-meta.json 和 ark_userChunk 文件
1284
+ if (assetName.indexOf('js/app.') !== -1 ||
1285
+ assetName.indexOf('js/runtime.') !== -1 ||
1286
+ assetName.indexOf('js/vendors.') !== -1 ||
1287
+ assetName.indexOf('js/main.') !== -1) {
1288
+ jsFiles.push(outputPath + assetName);
1289
+ }
1290
+ }
1291
+ });
1292
+
1293
+ console.log('Final JS files for chunkJsSrcList:', jsFiles);
1294
+
1257
1295
  // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
1258
1296
  const customAssets = [];
1259
1297
 
1260
1298
  await extractArkMetaJson({
1261
1299
  ...useExtractOptions,
1262
- // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
1300
+ chunkJsFiles: jsFiles,
1263
1301
  isDev: isDev,
1264
1302
  compilation: compilation,
1265
1303
  customAssets: customAssets
@@ -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;
@@ -1254,12 +1268,36 @@
1254
1268
  // 注入环境判断脚本
1255
1269
  const isDev = process.env.NODE_ENV === 'development';
1256
1270
 
1271
+ // 收集所有生成的JS文件
1272
+ const outputPath = compilation.options.output.publicPath || '';
1273
+ const jsFiles = [];
1274
+
1275
+ // Webpack 5 正确的API:使用 getAssets()
1276
+ const assets = compilation.getAssets ? compilation.getAssets() : compilation.assets;
1277
+
1278
+ // 遍历所有资产,收集JS文件
1279
+ assets.forEach((asset) => {
1280
+ const assetName = asset.name || asset;
1281
+ if (assetName.endsWith('.js')) {
1282
+ console.log('Found JS file:', assetName);
1283
+ // 排除 ark-meta.json 和 ark_userChunk 文件
1284
+ if (assetName.indexOf('js/app.') !== -1 ||
1285
+ assetName.indexOf('js/runtime.') !== -1 ||
1286
+ assetName.indexOf('js/vendors.') !== -1 ||
1287
+ assetName.indexOf('js/main.') !== -1) {
1288
+ jsFiles.push(outputPath + assetName);
1289
+ }
1290
+ }
1291
+ });
1292
+
1293
+ console.log('Final JS files for chunkJsSrcList:', jsFiles);
1294
+
1257
1295
  // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
1258
1296
  const customAssets = [];
1259
1297
 
1260
1298
  await extractArkMetaJson({
1261
1299
  ...useExtractOptions,
1262
- // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
1300
+ chunkJsFiles: jsFiles,
1263
1301
  isDev: isDev,
1264
1302
  compilation: compilation,
1265
1303
  customAssets: customAssets
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;
@@ -1259,12 +1273,36 @@ class ArkWebpackPlugin {
1259
1273
  // 注入环境判断脚本
1260
1274
  const isDev = process.env.NODE_ENV === 'development';
1261
1275
 
1276
+ // 收集所有生成的JS文件
1277
+ const outputPath = compilation.options.output.publicPath || '';
1278
+ const jsFiles = [];
1279
+
1280
+ // Webpack 5 正确的API:使用 getAssets()
1281
+ const assets = compilation.getAssets ? compilation.getAssets() : compilation.assets;
1282
+
1283
+ // 遍历所有资产,收集JS文件
1284
+ assets.forEach((asset) => {
1285
+ const assetName = asset.name || asset;
1286
+ if (assetName.endsWith('.js')) {
1287
+ console.log('Found JS file:', assetName);
1288
+ // 排除 ark-meta.json 和 ark_userChunk 文件
1289
+ if (assetName.indexOf('js/app.') !== -1 ||
1290
+ assetName.indexOf('js/runtime.') !== -1 ||
1291
+ assetName.indexOf('js/vendors.') !== -1 ||
1292
+ assetName.indexOf('js/main.') !== -1) {
1293
+ jsFiles.push(outputPath + assetName);
1294
+ }
1295
+ }
1296
+ });
1297
+
1298
+ console.log('Final JS files for chunkJsSrcList:', jsFiles);
1299
+
1262
1300
  // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
1263
1301
  const customAssets = [];
1264
1302
 
1265
1303
  await extractArkMetaJson({
1266
1304
  ...useExtractOptions,
1267
- // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
1305
+ chunkJsFiles: jsFiles,
1268
1306
  isDev: isDev,
1269
1307
  compilation: compilation,
1270
1308
  customAssets: customAssets
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.5",
4
4
  "description": "ark dev utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -94,12 +94,36 @@ export default class ArkWebpackPlugin {
94
94
  // 注入环境判断脚本
95
95
  const isDev = process.env.NODE_ENV === 'development';
96
96
 
97
+ // 收集所有生成的JS文件
98
+ const outputPath = compilation.options.output.publicPath || '';
99
+ const jsFiles = [];
100
+
101
+ // Webpack 5 正确的API:使用 getAssets()
102
+ const assets = compilation.getAssets ? compilation.getAssets() : compilation.assets;
103
+
104
+ // 遍历所有资产,收集JS文件
105
+ assets.forEach((asset) => {
106
+ const assetName = asset.name || asset;
107
+ if (assetName.endsWith('.js')) {
108
+ console.log('Found JS file:', assetName);
109
+ // 排除 ark-meta.json 和 ark_userChunk 文件
110
+ if (assetName.indexOf('js/app.') !== -1 ||
111
+ assetName.indexOf('js/runtime.') !== -1 ||
112
+ assetName.indexOf('js/vendors.') !== -1 ||
113
+ assetName.indexOf('js/main.') !== -1) {
114
+ jsFiles.push(outputPath + assetName);
115
+ }
116
+ }
117
+ });
118
+
119
+ console.log('Final JS files for chunkJsSrcList:', jsFiles);
120
+
97
121
  // 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
98
122
  const customAssets = [];
99
123
 
100
124
  const arkMeta = await extractArkMetaJson({
101
125
  ...useExtractOptions,
102
- // chunkJsFiles 将从 HTML 中的内联 script 提取,不需要在这里传递
126
+ chunkJsFiles: jsFiles,
103
127
  isDev: isDev,
104
128
  compilation: compilation,
105
129
  customAssets: customAssets
@@ -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;