@arkxio/ark-dev-utils 0.1.8 → 0.1.10
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.
- package/dist/index.js +136 -128
- package/dist/index.min.js +136 -128
- package/lib/index.js +136 -128
- package/package.json +51 -51
- package/src/ArkWebpackPlugin.js +143 -135
- package/LICENSE +0 -170
package/lib/index.js
CHANGED
|
@@ -1184,134 +1184,142 @@ var ExternalsDefault = {
|
|
|
1184
1184
|
|
|
1185
1185
|
};
|
|
1186
1186
|
|
|
1187
|
-
class ArkWebpackPlugin {
|
|
1188
|
-
|
|
1189
|
-
constructor(options) {
|
|
1190
|
-
this.metaOptions = options;
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
apply(compiler) {
|
|
1194
|
-
// 获取项目根目录路径
|
|
1195
|
-
compiler.context;
|
|
1196
|
-
const useExtractOptions = this.metaOptions;
|
|
1197
|
-
|
|
1198
|
-
// 在每次编译开始时重置脚本索引计数器,确保文件名一致性
|
|
1199
|
-
compiler.hooks.thisCompilation.tap('ArkResetScriptIdx', () => {
|
|
1200
|
-
resetScriptIdx();
|
|
1201
|
-
});
|
|
1202
|
-
|
|
1203
|
-
compiler.hooks.thisCompilation.tap('ArkDynamicCdnChunkLoaderPlugin', (compilation) => {
|
|
1204
|
-
compilation.hooks.processAssets.tap(
|
|
1205
|
-
{
|
|
1206
|
-
name: 'ArkDynamicCdnChunkLoaderPlugin',
|
|
1207
|
-
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
|
|
1208
|
-
},
|
|
1209
|
-
(assets) => {
|
|
1210
|
-
// 新增分块文件过滤逻辑
|
|
1211
|
-
const chunkFiles = new Set();
|
|
1212
|
-
compilation.chunks.forEach(chunk => {
|
|
1213
|
-
chunk.files.forEach(file => {
|
|
1214
|
-
if (file.endsWith('.js')) chunkFiles.add(file);
|
|
1215
|
-
});
|
|
1216
|
-
});
|
|
1217
|
-
// 调试日志
|
|
1218
|
-
verbose('[分块文件列表]', Array.from(chunkFiles));
|
|
1219
|
-
|
|
1220
|
-
Object.entries(assets).forEach(([filename, source]) => {
|
|
1221
|
-
// 仅处理分块JS文件
|
|
1222
|
-
if (!chunkFiles.has(filename)) return;
|
|
1223
|
-
|
|
1224
|
-
// 检查资源内容有效性
|
|
1225
|
-
if (
|
|
1226
|
-
!source ||
|
|
1227
|
-
!source.source ||
|
|
1228
|
-
typeof source.source() !== 'string' ||
|
|
1229
|
-
source.source().trim() === ''
|
|
1230
|
-
) {
|
|
1231
|
-
console.warn(`资源 ${filename} 内容为空或无效,跳过处理`);
|
|
1232
|
-
return;
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
let content = source.source().toString();
|
|
1236
|
-
|
|
1237
|
-
// 替换逻辑
|
|
1238
|
-
// /(__webpack_require__\.l)\(url,/g)
|
|
1239
|
-
// const webpackRequireLPattern = /(__webpack_require__\.l)\(url,/g;
|
|
1240
|
-
// if (webpackRequireLPattern.test(content)) {
|
|
1241
|
-
// verbose('检测到 __webpack_require__.l 模式');
|
|
1242
|
-
// content = content.replace(
|
|
1243
|
-
// webpackRequireLPattern,
|
|
1244
|
-
// `(function(url, done, key, chunkId) {
|
|
1245
|
-
// if (typeof window.ArkConfig !== 'undefined' && window.ArkConfig.cdnHost) {
|
|
1246
|
-
// if (!url.startsWith(window.ArkConfig.cdnHost) && url.startsWith('https://unpkg.com')) {
|
|
1247
|
-
// url = window.ArkConfig.cdnHost + url.slice('https://unpkg.com'.length);
|
|
1248
|
-
// }
|
|
1249
|
-
// }
|
|
1250
|
-
// return $1(url, done, key, chunkId);
|
|
1251
|
-
// })(url,`
|
|
1252
|
-
// );
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1187
|
+
class ArkWebpackPlugin {
|
|
1188
|
+
|
|
1189
|
+
constructor(options) {
|
|
1190
|
+
this.metaOptions = options;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
apply(compiler) {
|
|
1194
|
+
// 获取项目根目录路径
|
|
1195
|
+
compiler.context;
|
|
1196
|
+
const useExtractOptions = this.metaOptions;
|
|
1197
|
+
|
|
1198
|
+
// 在每次编译开始时重置脚本索引计数器,确保文件名一致性
|
|
1199
|
+
compiler.hooks.thisCompilation.tap('ArkResetScriptIdx', () => {
|
|
1200
|
+
resetScriptIdx();
|
|
1201
|
+
});
|
|
1202
|
+
|
|
1203
|
+
compiler.hooks.thisCompilation.tap('ArkDynamicCdnChunkLoaderPlugin', (compilation) => {
|
|
1204
|
+
compilation.hooks.processAssets.tap(
|
|
1205
|
+
{
|
|
1206
|
+
name: 'ArkDynamicCdnChunkLoaderPlugin',
|
|
1207
|
+
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
|
|
1208
|
+
},
|
|
1209
|
+
(assets) => {
|
|
1210
|
+
// 新增分块文件过滤逻辑
|
|
1211
|
+
const chunkFiles = new Set();
|
|
1212
|
+
compilation.chunks.forEach(chunk => {
|
|
1213
|
+
chunk.files.forEach(file => {
|
|
1214
|
+
if (file.endsWith('.js')) chunkFiles.add(file);
|
|
1215
|
+
});
|
|
1216
|
+
});
|
|
1217
|
+
// 调试日志
|
|
1218
|
+
verbose('[分块文件列表]', Array.from(chunkFiles));
|
|
1219
|
+
|
|
1220
|
+
Object.entries(assets).forEach(([filename, source]) => {
|
|
1221
|
+
// 仅处理分块JS文件
|
|
1222
|
+
if (!chunkFiles.has(filename)) return;
|
|
1223
|
+
|
|
1224
|
+
// 检查资源内容有效性
|
|
1225
|
+
if (
|
|
1226
|
+
!source ||
|
|
1227
|
+
!source.source ||
|
|
1228
|
+
typeof source.source() !== 'string' ||
|
|
1229
|
+
source.source().trim() === ''
|
|
1230
|
+
) {
|
|
1231
|
+
console.warn(`资源 ${filename} 内容为空或无效,跳过处理`);
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
let content = source.source().toString();
|
|
1236
|
+
|
|
1237
|
+
// 替换逻辑
|
|
1238
|
+
// /(__webpack_require__\.l)\(url,/g)
|
|
1239
|
+
// const webpackRequireLPattern = /(__webpack_require__\.l)\(url,/g;
|
|
1240
|
+
// if (webpackRequireLPattern.test(content)) {
|
|
1241
|
+
// verbose('检测到 __webpack_require__.l 模式');
|
|
1242
|
+
// content = content.replace(
|
|
1243
|
+
// webpackRequireLPattern,
|
|
1244
|
+
// `(function(url, done, key, chunkId) {
|
|
1245
|
+
// if (typeof window.ArkConfig !== 'undefined' && window.ArkConfig.cdnHost) {
|
|
1246
|
+
// if (!url.startsWith(window.ArkConfig.cdnHost) && url.startsWith('https://unpkg.com')) {
|
|
1247
|
+
// url = window.ArkConfig.cdnHost + url.slice('https://unpkg.com'.length);
|
|
1248
|
+
// }
|
|
1249
|
+
// }
|
|
1250
|
+
// return $1(url, done, key, chunkId);
|
|
1251
|
+
// })(url,`
|
|
1252
|
+
// );
|
|
1253
|
+
|
|
1254
|
+
let findUnpkgModel = false;
|
|
1255
|
+
const webpackRequireLPatternEval = /"https:\/\/unpkg\.com"/g;
|
|
1256
|
+
if (webpackRequireLPatternEval.test(content)) {
|
|
1257
|
+
content = content.replace(webpackRequireLPatternEval, 'window.ArkConfig.cdnHost');
|
|
1258
|
+
findUnpkgModel = true;
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
const webpackRequireLPattern = /"(https:\/\/unpkg\.com)/g;
|
|
1262
|
+
if (webpackRequireLPattern.test(content)) {
|
|
1263
|
+
findUnpkgModel = true;
|
|
1264
|
+
verbose('检测到 unpkg.com 模式');
|
|
1265
|
+
content = content.replace(
|
|
1266
|
+
webpackRequireLPattern,
|
|
1267
|
+
`window.ArkConfig.cdnHost + "`
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
if (findUnpkgModel) {
|
|
1272
|
+
// 更新资源
|
|
1273
|
+
compilation.updateAsset(filename, new compiler.webpack.sources.RawSource(content));
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
);
|
|
1279
|
+
});
|
|
1280
|
+
|
|
1281
|
+
compiler.hooks.emit.tapPromise('ArkMetaJsonGeneratorPlugin', async (compilation) => {
|
|
1282
|
+
// 注入环境判断脚本
|
|
1283
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
1284
|
+
|
|
1285
|
+
// 收集所有生成的JS文件
|
|
1286
|
+
const outputPath = compilation.options.output.publicPath || '';
|
|
1287
|
+
const jsFiles = [];
|
|
1288
|
+
|
|
1289
|
+
// Webpack 5 正确的API:使用 getAssets()
|
|
1290
|
+
const assets = compilation.getAssets ? compilation.getAssets() : compilation.assets;
|
|
1291
|
+
|
|
1292
|
+
// 遍历所有资产,收集JS文件
|
|
1293
|
+
assets.forEach((asset) => {
|
|
1294
|
+
const assetName = asset.name || asset;
|
|
1295
|
+
if (assetName.endsWith('.js')) {
|
|
1296
|
+
console.log('Found JS file:', assetName);
|
|
1297
|
+
// 排除 ark-meta.json 和 ark_userChunk 文件
|
|
1298
|
+
if (assetName.indexOf('js/app.') !== -1 ||
|
|
1299
|
+
assetName.indexOf('js/runtime.') !== -1 ||
|
|
1300
|
+
assetName.indexOf('js/vendors.') !== -1 ||
|
|
1301
|
+
assetName.indexOf('js/main.') !== -1) {
|
|
1302
|
+
jsFiles.push(outputPath + assetName);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
});
|
|
1306
|
+
|
|
1307
|
+
console.log('Final JS files for chunkJsSrcList:', jsFiles);
|
|
1308
|
+
|
|
1309
|
+
// 创建 customAssets 数组用于收集自定义资产(如从 HTML 内联 script 提取的代码)
|
|
1310
|
+
const customAssets = [];
|
|
1311
|
+
|
|
1312
|
+
await extractArkMetaJson({
|
|
1313
|
+
...useExtractOptions,
|
|
1314
|
+
chunkJsFiles: jsFiles,
|
|
1315
|
+
isDev: isDev,
|
|
1316
|
+
compilation: compilation,
|
|
1317
|
+
customAssets: customAssets
|
|
1318
|
+
});
|
|
1319
|
+
|
|
1320
|
+
return Promise.resolve();
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1315
1323
|
}
|
|
1316
1324
|
|
|
1317
1325
|
var index = {
|
package/package.json
CHANGED
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@arkxio/ark-dev-utils",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "ark dev utils",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "https://github.com/arkxos/arkjs.git",
|
|
8
|
-
"directory": "packages/ark-dev-utils"
|
|
9
|
-
},
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"author": {
|
|
12
|
-
"name": "fantasticsoul"
|
|
13
|
-
},
|
|
14
|
-
"main": "lib/index.js",
|
|
15
|
-
"files": [
|
|
16
|
-
"dist",
|
|
17
|
-
"lib",
|
|
18
|
-
"src",
|
|
19
|
-
"typings.d.ts",
|
|
20
|
-
"jsconfig.json",
|
|
21
|
-
".babelrc.js",
|
|
22
|
-
"README.md"
|
|
23
|
-
],
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
},
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@arkxio/ark-dev-utils",
|
|
3
|
+
"version": "0.1.10",
|
|
4
|
+
"description": "ark dev utils",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/arkxos/arkjs.git",
|
|
8
|
+
"directory": "packages/ark-dev-utils"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "fantasticsoul"
|
|
13
|
+
},
|
|
14
|
+
"main": "lib/index.js",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"lib",
|
|
18
|
+
"src",
|
|
19
|
+
"typings.d.ts",
|
|
20
|
+
"jsconfig.json",
|
|
21
|
+
".babelrc.js",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "npm run build:commonjs && npm run build:es && npm run build:umd && npm run build:umd:min",
|
|
26
|
+
"build:commonjs": "cross-env BUILD_ENV=commonjs rollup -c",
|
|
27
|
+
"build:es": "cross-env BUILD_ENV=es rollup -c",
|
|
28
|
+
"build:umd": "cross-env BUILD_ENV=development rollup -c",
|
|
29
|
+
"build:umd:min": "cross-env BUILD_ENV=production rollup -c"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"jsdom": ">=19.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@babel/preset-env": "^7.12.11",
|
|
36
|
+
"@babel/preset-react": "^7.12.10",
|
|
37
|
+
"babel-cli": "^6.26.0",
|
|
38
|
+
"fs-extra": "^10.1.0",
|
|
39
|
+
"rollup": "^2.23.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"jsdom": ">=19.0.0",
|
|
43
|
+
"webpack": "5.75.0"
|
|
44
|
+
},
|
|
45
|
+
"bundleDependencies": false,
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
},
|
|
50
|
+
"deprecated": false
|
|
51
|
+
}
|