@itera-web/cli 1.1.59 → 1.1.61
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/plugins/sortSrciptTag.js +33 -48
- package/package.json +1 -1
|
@@ -4,70 +4,55 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
var cheerio_1 = __importDefault(require("cheerio"));
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var Console = require('../public/log').Console;
|
|
7
|
+
var webpack_1 = require("webpack");
|
|
8
|
+
var log_1 = require("../public/log");
|
|
10
9
|
var SortScriptTagPlugin = (function () {
|
|
11
10
|
function SortScriptTagPlugin() {
|
|
12
11
|
}
|
|
13
12
|
SortScriptTagPlugin.prototype.apply = function (compiler) {
|
|
14
|
-
compiler.hooks.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
var outputPath = compiler.options.output.path;
|
|
28
|
-
var baseHtmlPath = path_1.default.join(outputPath, 'base.html');
|
|
29
|
-
if (fs_1.default.existsSync(baseHtmlPath)) {
|
|
30
|
-
Console.cyan('开始引入排序');
|
|
31
|
-
var baseHtmlContent = fs_1.default.readFileSync(baseHtmlPath, 'utf8');
|
|
32
|
-
var $_1 = cheerio_1.default.load(baseHtmlContent, { decodeEntities: false });
|
|
33
|
-
var srcTag = $_1('script');
|
|
13
|
+
compiler.hooks.thisCompilation.tap('SortScriptTagPlugin', function (compilation) {
|
|
14
|
+
compilation.hooks.processAssets.tap({
|
|
15
|
+
name: 'SortScriptTagPlugin',
|
|
16
|
+
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
|
|
17
|
+
}, function (assets) {
|
|
18
|
+
log_1.Console.cyan('开始排序引用');
|
|
19
|
+
var htmlName = 'index.html';
|
|
20
|
+
var asset = compilation.getAsset(htmlName);
|
|
21
|
+
if (!asset)
|
|
22
|
+
return;
|
|
23
|
+
var html = asset.source.source().toString();
|
|
24
|
+
var $ = cheerio_1.default.load(html, { decodeEntities: false });
|
|
25
|
+
var srcTag = $('script');
|
|
34
26
|
var scripts = srcTag.toArray().reverse();
|
|
35
|
-
var
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
});
|
|
40
|
-
var otherScripts_1 = [];
|
|
27
|
+
var priorityList = ['react', 'main'];
|
|
28
|
+
var priorityScripts = {};
|
|
29
|
+
priorityList.forEach(function (k) { return (priorityScripts[k] = null); });
|
|
30
|
+
var otherScripts = [];
|
|
41
31
|
scripts.forEach(function (script) {
|
|
42
|
-
var src = $
|
|
32
|
+
var src = $(script).attr('src') || '';
|
|
43
33
|
var matched = false;
|
|
44
|
-
for (var _i = 0,
|
|
45
|
-
var key =
|
|
46
|
-
if (src.includes("js/".concat(key, ".")) && !
|
|
47
|
-
|
|
34
|
+
for (var _i = 0, priorityList_1 = priorityList; _i < priorityList_1.length; _i++) {
|
|
35
|
+
var key = priorityList_1[_i];
|
|
36
|
+
if (src.includes("js/".concat(key, ".")) && !priorityScripts[key]) {
|
|
37
|
+
priorityScripts[key] = script;
|
|
48
38
|
matched = true;
|
|
49
39
|
break;
|
|
50
40
|
}
|
|
51
41
|
}
|
|
52
42
|
if (!matched)
|
|
53
|
-
|
|
43
|
+
otherScripts.push(script);
|
|
54
44
|
});
|
|
55
45
|
srcTag.remove();
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
$
|
|
59
|
-
}
|
|
46
|
+
priorityList.forEach(function (key) {
|
|
47
|
+
if (priorityScripts[key])
|
|
48
|
+
$('body').append(priorityScripts[key]);
|
|
60
49
|
});
|
|
61
|
-
|
|
62
|
-
$
|
|
50
|
+
otherScripts.forEach(function (script) {
|
|
51
|
+
$('body').append(script);
|
|
63
52
|
});
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
compiler.hooks.failed.tap('SortScriptTagPlugin', function (err) {
|
|
70
|
-
Console.error('构建失败:', err);
|
|
53
|
+
var newHtml = $.html();
|
|
54
|
+
compilation.updateAsset(htmlName, new webpack_1.sources.RawSource(newHtml));
|
|
55
|
+
});
|
|
71
56
|
});
|
|
72
57
|
};
|
|
73
58
|
return SortScriptTagPlugin;
|