@rsbuild/plugin-assets-retry 0.0.18
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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +180 -0
- package/dist/index.mjs +184 -0
- package/dist/runtime.js +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Rsbuild
|
|
6
|
+
|
|
7
|
+
Unleash the power of Rspack with the out-of-the-box build tool.
|
|
8
|
+
|
|
9
|
+
## Documentation
|
|
10
|
+
|
|
11
|
+
https://rsbuild.dev/
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
|
|
16
|
+
|
|
17
|
+
## License
|
|
18
|
+
|
|
19
|
+
Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { RsbuildPlugin, RsbuildPluginAPI } from '@rsbuild/core';
|
|
2
|
+
import { CrossOrigin } from '@rsbuild/shared';
|
|
3
|
+
|
|
4
|
+
type PluginAssetsRetryOptions = {
|
|
5
|
+
max?: number;
|
|
6
|
+
type?: string[];
|
|
7
|
+
test?: string | ((url: string) => boolean);
|
|
8
|
+
domain?: string[];
|
|
9
|
+
crossOrigin?: boolean | CrossOrigin;
|
|
10
|
+
inlineScript?: boolean;
|
|
11
|
+
onFail?: (options: AssetsRetryHookContext) => void;
|
|
12
|
+
onRetry?: (options: AssetsRetryHookContext) => void;
|
|
13
|
+
onSuccess?: (options: AssetsRetryHookContext) => void;
|
|
14
|
+
};
|
|
15
|
+
type AssetsRetryHookContext = {
|
|
16
|
+
url: string;
|
|
17
|
+
times: number;
|
|
18
|
+
domain: string;
|
|
19
|
+
tagName: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare const pluginAssetsRetry: (options?: PluginAssetsRetryOptions) => RsbuildPlugin<RsbuildPluginAPI>;
|
|
23
|
+
|
|
24
|
+
export { PluginAssetsRetryOptions, pluginAssetsRetry };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
var __accessCheck = (obj, member, msg) => {
|
|
33
|
+
if (!member.has(obj))
|
|
34
|
+
throw TypeError("Cannot " + msg);
|
|
35
|
+
};
|
|
36
|
+
var __privateGet = (obj, member, getter) => {
|
|
37
|
+
__accessCheck(obj, member, "read from private field");
|
|
38
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
39
|
+
};
|
|
40
|
+
var __privateAdd = (obj, member, value) => {
|
|
41
|
+
if (member.has(obj))
|
|
42
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
43
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
44
|
+
};
|
|
45
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
46
|
+
__accessCheck(obj, member, "write to private field");
|
|
47
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
48
|
+
return value;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/AssetsRetryPlugin.ts
|
|
52
|
+
var AssetsRetryPlugin_exports = {};
|
|
53
|
+
__export(AssetsRetryPlugin_exports, {
|
|
54
|
+
AssetsRetryPlugin: () => AssetsRetryPlugin
|
|
55
|
+
});
|
|
56
|
+
var import_path, import_webpack_sources, import_fs_extra, import_shared, _retryOptions, AssetsRetryPlugin;
|
|
57
|
+
var init_AssetsRetryPlugin = __esm({
|
|
58
|
+
"src/AssetsRetryPlugin.ts"() {
|
|
59
|
+
"use strict";
|
|
60
|
+
import_path = __toESM(require("path"));
|
|
61
|
+
import_webpack_sources = require("webpack-sources");
|
|
62
|
+
import_fs_extra = require("@rsbuild/shared/fs-extra");
|
|
63
|
+
import_shared = require("@rsbuild/shared");
|
|
64
|
+
AssetsRetryPlugin = class {
|
|
65
|
+
constructor(options) {
|
|
66
|
+
__privateAdd(this, _retryOptions, void 0);
|
|
67
|
+
const {
|
|
68
|
+
distDir,
|
|
69
|
+
HtmlPlugin,
|
|
70
|
+
inlineScript = true,
|
|
71
|
+
...retryOptions
|
|
72
|
+
} = options;
|
|
73
|
+
this.name = "AssetsRetryPlugin";
|
|
74
|
+
__privateSet(this, _retryOptions, retryOptions);
|
|
75
|
+
this.distDir = distDir;
|
|
76
|
+
this.HtmlPlugin = HtmlPlugin;
|
|
77
|
+
this.inlineScript = inlineScript;
|
|
78
|
+
this.scriptPath = "";
|
|
79
|
+
}
|
|
80
|
+
async getRetryCode() {
|
|
81
|
+
const { default: serialize } = await import("serialize-javascript");
|
|
82
|
+
const runtimeFilePath = import_path.default.join(__dirname, "./runtime.js");
|
|
83
|
+
const runtimeCode = await import_fs_extra.fs.readFile(runtimeFilePath, "utf-8");
|
|
84
|
+
return `(function(){${runtimeCode};init(${serialize(
|
|
85
|
+
__privateGet(this, _retryOptions)
|
|
86
|
+
)});})()`;
|
|
87
|
+
}
|
|
88
|
+
async getScriptPath() {
|
|
89
|
+
if (!this.scriptPath) {
|
|
90
|
+
const version = await (0, import_shared.getRsbuildVersion)();
|
|
91
|
+
this.scriptPath = import_path.default.join(this.distDir, `assets-retry.${version}.js`);
|
|
92
|
+
}
|
|
93
|
+
return this.scriptPath;
|
|
94
|
+
}
|
|
95
|
+
apply(compiler) {
|
|
96
|
+
if (!this.inlineScript) {
|
|
97
|
+
compiler.hooks.thisCompilation.tap(
|
|
98
|
+
this.name,
|
|
99
|
+
(compilation) => {
|
|
100
|
+
compilation.hooks.processAssets.tapPromise(
|
|
101
|
+
{
|
|
102
|
+
name: this.name,
|
|
103
|
+
stage: import_shared.COMPILATION_PROCESS_STAGE.PROCESS_ASSETS_STAGE_PRE_PROCESS
|
|
104
|
+
},
|
|
105
|
+
async (assets) => {
|
|
106
|
+
const scriptPath = await this.getScriptPath();
|
|
107
|
+
assets[scriptPath] = new import_webpack_sources.RawSource(
|
|
108
|
+
await this.getRetryCode(),
|
|
109
|
+
false
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
compiler.hooks.compilation.tap(this.name, (compilation) => {
|
|
117
|
+
this.HtmlPlugin.getHooks(compilation).alterAssetTagGroups.tapPromise(
|
|
118
|
+
this.name,
|
|
119
|
+
async (data) => {
|
|
120
|
+
const scriptTag = (0, import_shared.generateScriptTag)();
|
|
121
|
+
if (this.inlineScript) {
|
|
122
|
+
data.headTags.unshift({
|
|
123
|
+
...scriptTag,
|
|
124
|
+
innerHTML: await this.getRetryCode()
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
const publicPath = (0, import_shared.getPublicPathFromCompiler)(compiler);
|
|
128
|
+
const url = (0, import_shared.withPublicPath)(await this.getScriptPath(), publicPath);
|
|
129
|
+
data.headTags.unshift({
|
|
130
|
+
...scriptTag,
|
|
131
|
+
attributes: {
|
|
132
|
+
...scriptTag.attributes,
|
|
133
|
+
src: url
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return data;
|
|
138
|
+
}
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
_retryOptions = new WeakMap();
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// src/index.ts
|
|
148
|
+
var src_exports = {};
|
|
149
|
+
__export(src_exports, {
|
|
150
|
+
pluginAssetsRetry: () => pluginAssetsRetry
|
|
151
|
+
});
|
|
152
|
+
module.exports = __toCommonJS(src_exports);
|
|
153
|
+
var import_shared2 = require("@rsbuild/shared");
|
|
154
|
+
var pluginAssetsRetry = (options = {}) => ({
|
|
155
|
+
name: "plugin-assets-retry",
|
|
156
|
+
setup(api) {
|
|
157
|
+
api.modifyBundlerChain(async (chain, { CHAIN_ID, target, HtmlPlugin }) => {
|
|
158
|
+
const config = api.getNormalizedConfig();
|
|
159
|
+
if (!options || (0, import_shared2.isHtmlDisabled)(config, target)) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const { AssetsRetryPlugin: AssetsRetryPlugin2 } = await Promise.resolve().then(() => (init_AssetsRetryPlugin(), AssetsRetryPlugin_exports));
|
|
163
|
+
const distDir = (0, import_shared2.getDistPath)(config.output, "js");
|
|
164
|
+
if (options.crossOrigin === void 0) {
|
|
165
|
+
options.crossOrigin = config.html.crossorigin;
|
|
166
|
+
}
|
|
167
|
+
chain.plugin(CHAIN_ID.PLUGIN.ASSETS_RETRY).use(AssetsRetryPlugin2, [
|
|
168
|
+
{
|
|
169
|
+
...options,
|
|
170
|
+
distDir,
|
|
171
|
+
HtmlPlugin
|
|
172
|
+
}
|
|
173
|
+
]);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
pluginAssetsRetry
|
|
180
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res) => function __init() {
|
|
4
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
+
};
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __accessCheck = (obj, member, msg) => {
|
|
11
|
+
if (!member.has(obj))
|
|
12
|
+
throw TypeError("Cannot " + msg);
|
|
13
|
+
};
|
|
14
|
+
var __privateGet = (obj, member, getter) => {
|
|
15
|
+
__accessCheck(obj, member, "read from private field");
|
|
16
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
17
|
+
};
|
|
18
|
+
var __privateAdd = (obj, member, value) => {
|
|
19
|
+
if (member.has(obj))
|
|
20
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
21
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
22
|
+
};
|
|
23
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
24
|
+
__accessCheck(obj, member, "write to private field");
|
|
25
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
26
|
+
return value;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// ../../node_modules/.pnpm/@modern-js+module-tools@2.39.2_typescript@5.2.2/node_modules/@modern-js/module-tools/shims/esm.js
|
|
30
|
+
import { fileURLToPath } from "url";
|
|
31
|
+
import path from "path";
|
|
32
|
+
var getFilename, getDirname, __dirname;
|
|
33
|
+
var init_esm = __esm({
|
|
34
|
+
"../../node_modules/.pnpm/@modern-js+module-tools@2.39.2_typescript@5.2.2/node_modules/@modern-js/module-tools/shims/esm.js"() {
|
|
35
|
+
"use strict";
|
|
36
|
+
getFilename = () => fileURLToPath(import.meta.url);
|
|
37
|
+
getDirname = () => path.dirname(getFilename());
|
|
38
|
+
__dirname = /* @__PURE__ */ getDirname();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// ../../scripts/require_shims.js
|
|
43
|
+
import { createRequire } from "module";
|
|
44
|
+
var init_require_shims = __esm({
|
|
45
|
+
"../../scripts/require_shims.js"() {
|
|
46
|
+
"use strict";
|
|
47
|
+
global.require = createRequire(import.meta.url);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// src/AssetsRetryPlugin.ts
|
|
52
|
+
var AssetsRetryPlugin_exports = {};
|
|
53
|
+
__export(AssetsRetryPlugin_exports, {
|
|
54
|
+
AssetsRetryPlugin: () => AssetsRetryPlugin
|
|
55
|
+
});
|
|
56
|
+
import path2 from "path";
|
|
57
|
+
import { RawSource } from "webpack-sources";
|
|
58
|
+
import { fs } from "@rsbuild/shared/fs-extra";
|
|
59
|
+
import {
|
|
60
|
+
withPublicPath,
|
|
61
|
+
generateScriptTag,
|
|
62
|
+
getRsbuildVersion,
|
|
63
|
+
getPublicPathFromCompiler,
|
|
64
|
+
COMPILATION_PROCESS_STAGE
|
|
65
|
+
} from "@rsbuild/shared";
|
|
66
|
+
var _retryOptions, AssetsRetryPlugin;
|
|
67
|
+
var init_AssetsRetryPlugin = __esm({
|
|
68
|
+
"src/AssetsRetryPlugin.ts"() {
|
|
69
|
+
"use strict";
|
|
70
|
+
init_esm();
|
|
71
|
+
init_require_shims();
|
|
72
|
+
AssetsRetryPlugin = class {
|
|
73
|
+
constructor(options) {
|
|
74
|
+
__privateAdd(this, _retryOptions, void 0);
|
|
75
|
+
const {
|
|
76
|
+
distDir,
|
|
77
|
+
HtmlPlugin,
|
|
78
|
+
inlineScript = true,
|
|
79
|
+
...retryOptions
|
|
80
|
+
} = options;
|
|
81
|
+
this.name = "AssetsRetryPlugin";
|
|
82
|
+
__privateSet(this, _retryOptions, retryOptions);
|
|
83
|
+
this.distDir = distDir;
|
|
84
|
+
this.HtmlPlugin = HtmlPlugin;
|
|
85
|
+
this.inlineScript = inlineScript;
|
|
86
|
+
this.scriptPath = "";
|
|
87
|
+
}
|
|
88
|
+
async getRetryCode() {
|
|
89
|
+
const { default: serialize } = await import("serialize-javascript");
|
|
90
|
+
const runtimeFilePath = path2.join(__dirname, "./runtime.js");
|
|
91
|
+
const runtimeCode = await fs.readFile(runtimeFilePath, "utf-8");
|
|
92
|
+
return `(function(){${runtimeCode};init(${serialize(
|
|
93
|
+
__privateGet(this, _retryOptions)
|
|
94
|
+
)});})()`;
|
|
95
|
+
}
|
|
96
|
+
async getScriptPath() {
|
|
97
|
+
if (!this.scriptPath) {
|
|
98
|
+
const version = await getRsbuildVersion();
|
|
99
|
+
this.scriptPath = path2.join(this.distDir, `assets-retry.${version}.js`);
|
|
100
|
+
}
|
|
101
|
+
return this.scriptPath;
|
|
102
|
+
}
|
|
103
|
+
apply(compiler) {
|
|
104
|
+
if (!this.inlineScript) {
|
|
105
|
+
compiler.hooks.thisCompilation.tap(
|
|
106
|
+
this.name,
|
|
107
|
+
(compilation) => {
|
|
108
|
+
compilation.hooks.processAssets.tapPromise(
|
|
109
|
+
{
|
|
110
|
+
name: this.name,
|
|
111
|
+
stage: COMPILATION_PROCESS_STAGE.PROCESS_ASSETS_STAGE_PRE_PROCESS
|
|
112
|
+
},
|
|
113
|
+
async (assets) => {
|
|
114
|
+
const scriptPath = await this.getScriptPath();
|
|
115
|
+
assets[scriptPath] = new RawSource(
|
|
116
|
+
await this.getRetryCode(),
|
|
117
|
+
false
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
compiler.hooks.compilation.tap(this.name, (compilation) => {
|
|
125
|
+
this.HtmlPlugin.getHooks(compilation).alterAssetTagGroups.tapPromise(
|
|
126
|
+
this.name,
|
|
127
|
+
async (data) => {
|
|
128
|
+
const scriptTag = generateScriptTag();
|
|
129
|
+
if (this.inlineScript) {
|
|
130
|
+
data.headTags.unshift({
|
|
131
|
+
...scriptTag,
|
|
132
|
+
innerHTML: await this.getRetryCode()
|
|
133
|
+
});
|
|
134
|
+
} else {
|
|
135
|
+
const publicPath = getPublicPathFromCompiler(compiler);
|
|
136
|
+
const url = withPublicPath(await this.getScriptPath(), publicPath);
|
|
137
|
+
data.headTags.unshift({
|
|
138
|
+
...scriptTag,
|
|
139
|
+
attributes: {
|
|
140
|
+
...scriptTag.attributes,
|
|
141
|
+
src: url
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return data;
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
_retryOptions = new WeakMap();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// src/index.ts
|
|
156
|
+
init_esm();
|
|
157
|
+
init_require_shims();
|
|
158
|
+
import { getDistPath, isHtmlDisabled } from "@rsbuild/shared";
|
|
159
|
+
var pluginAssetsRetry = (options = {}) => ({
|
|
160
|
+
name: "plugin-assets-retry",
|
|
161
|
+
setup(api) {
|
|
162
|
+
api.modifyBundlerChain(async (chain, { CHAIN_ID, target, HtmlPlugin }) => {
|
|
163
|
+
const config = api.getNormalizedConfig();
|
|
164
|
+
if (!options || isHtmlDisabled(config, target)) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const { AssetsRetryPlugin: AssetsRetryPlugin2 } = await Promise.resolve().then(() => (init_AssetsRetryPlugin(), AssetsRetryPlugin_exports));
|
|
168
|
+
const distDir = getDistPath(config.output, "js");
|
|
169
|
+
if (options.crossOrigin === void 0) {
|
|
170
|
+
options.crossOrigin = config.html.crossorigin;
|
|
171
|
+
}
|
|
172
|
+
chain.plugin(CHAIN_ID.PLUGIN.ASSETS_RETRY).use(AssetsRetryPlugin2, [
|
|
173
|
+
{
|
|
174
|
+
...options,
|
|
175
|
+
distDir,
|
|
176
|
+
HtmlPlugin
|
|
177
|
+
}
|
|
178
|
+
]);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
export {
|
|
183
|
+
pluginAssetsRetry
|
|
184
|
+
};
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var TAG_TYPE={link:HTMLLinkElement,script:HTMLScriptElement,img:HTMLImageElement},TYPES=Object.keys(TAG_TYPE);function findCurrentDomain(e,t){for(var n="",r=0;r<t.length;r++)if(-1!==e.indexOf(t[r])){n=t[r];break}return n||e}function findNextDomain(e,t){var n=findCurrentDomain(e,t),r=t.indexOf(n);return t[(r+1)%t.length]||e}function getRequestUrl(e){return e instanceof HTMLScriptElement||e instanceof HTMLImageElement?e.src:e instanceof HTMLLinkElement?e.href:null}var defaultConfig={max:3,type:TYPES,domain:[],crossOrigin:!1};function validateTargetInfo(e,t){var n,r=t.target,i=null===(n=r.tagName)||void 0===n?void 0:n.toLocaleLowerCase(),a=e.type,o=getRequestUrl(r);return!!(i&&-1!==a.indexOf(i)&&TAG_TYPE[i]&&r instanceof TAG_TYPE[i]&&o)&&{target:r,tagName:i,url:o}}function createElement(e,t){var n=!0===t.crossOrigin?"anonymous":t.crossOrigin,r=n?'crossorigin="'.concat(n,'"'):"",i=t.times?'data-rsbuild-retry-times="'.concat(t.times,'"'):"",a=t.isAsync?"data-rsbuild-async":"";if(e instanceof HTMLScriptElement){var o=document.createElement("script");return o.src=t.url,n&&(o.crossOrigin=n),t.times&&(o.dataset.rsbuildRetryTimes=String(t.times)),t.isAsync&&(o.dataset.rsbuildAsync=""),{element:o,str:'<script src="'.concat(t.url,'" ').concat(r," ").concat(i," ").concat(a,"><\/script>")}}if(e instanceof HTMLLinkElement){var s=document.createElement("link");return s.rel=e.rel||"stylesheet",e.as&&(s.as=e.as),s.href=t.url,n&&(s.crossOrigin=n),t.times&&(s.dataset.rsbuildRetryTimes=String(t.times)),{element:s,str:'<link rel="'.concat(s.rel,'" href="').concat(t.url,'" ').concat(r," ").concat(i," ").concat(s.as?'as="'.concat(s.as,'"'):"","></link>")}}}function reloadElementResource(e,t,n){e instanceof HTMLScriptElement&&(n.isAsync?document.body.appendChild(t.element):document.write(t.str)),e instanceof HTMLLinkElement&&document.getElementsByTagName("head")[0].appendChild(t.element),e instanceof HTMLImageElement&&(e.src=n.url,e.dataset.rsbuildRetryTimes=String(n.times))}function retry(e,t){var n=validateTargetInfo(e,t);if(!1!==n){var r=n.target,i=n.tagName,a=n.url,o=e.test;if(o){if("string"==typeof o){var s=new RegExp(o);o=function(e){return s.test(e)}}if("function"!=typeof o||!o(a))return}var c=findCurrentDomain(a,e.domain);if(!(e.domain&&e.domain.length>0&&-1===e.domain.indexOf(c))){var l=Number(r.dataset.rsbuildRetryTimes)||0;if(l!==e.max){var m=findNextDomain(c,e.domain),d=Boolean(r.dataset.rsbuildAsync)||r.async||r.defer,u={url:a.replace(c,m),times:l+1,crossOrigin:e.crossOrigin,isAsync:d},f=createElement(r,u);if(e.onRetry&&"function"==typeof e.onRetry){var g={times:l,domain:c,url:a,tagName:i};e.onRetry(g)}reloadElementResource(r,f,u)}else if("function"==typeof e.onFail){var y={times:l,domain:c,url:a,tagName:i};e.onFail(y)}}}}function load(e,t){var n=validateTargetInfo(e,t);if(!1!==n){var r=n.target,i=n.tagName,a=n.url,o=findCurrentDomain(a,e.domain),s=Number(r.dataset.rsbuildRetryTimes)||0;if(0!==s&&"function"==typeof e.onSuccess){var c={times:s,domain:o,url:a,tagName:i};e.onSuccess(c)}}}function resourceMonitor(e,t){"undefined"!=typeof window&&void 0!==window.document&&(document.addEventListener("error",(function(t){t&&t.target instanceof Element&&e(t)}),!0),document.addEventListener("load",(function(e){e&&e.target instanceof Element&&t(e)}),!0))}function init(e){var t=Object.assign({},defaultConfig,e);Array.isArray(t.type)&&0!==t.type.length||(t.type=defaultConfig.type),Array.isArray(t.domain)&&0!==t.domain.length||(t.domain=defaultConfig.domain),Array.isArray(t.domain)&&(t.domain=t.domain.filter(Boolean));try{resourceMonitor((function(e){try{retry(t,e)}catch(e){console.error("retry error captured",e)}}),(function(e){try{load(t,e)}catch(e){console.error("load error captured",e)}}))}catch(e){console.error("monitor error captured",e)}}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsbuild/plugin-assets-retry",
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"description": "Assets retry plugin for Rsbuild",
|
|
5
|
+
"homepage": "https://rsbuild.dev",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/web-infra-dev/rsbuild",
|
|
9
|
+
"directory": "packages/plugin-assets-retry"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"main": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"serialize-javascript": "^6.0.0",
|
|
26
|
+
"webpack-sources": "^3.2.3",
|
|
27
|
+
"@rsbuild/shared": "0.0.18"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@babel/core": "^7.23.2",
|
|
31
|
+
"@babel/preset-env": "^7.23.2",
|
|
32
|
+
"@babel/preset-typescript": "^7.23.2",
|
|
33
|
+
"@types/serialize-javascript": "^5.0.1",
|
|
34
|
+
"html-webpack-plugin": "5.5.3",
|
|
35
|
+
"terser": "5.19.2",
|
|
36
|
+
"typescript": "^5.2.2",
|
|
37
|
+
"@rsbuild/core": "0.0.18",
|
|
38
|
+
"@rsbuild/test-helper": "0.0.18"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public",
|
|
42
|
+
"provenance": true,
|
|
43
|
+
"registry": "https://registry.npmjs.org/"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "modern build && node scripts/postCompile.js",
|
|
47
|
+
"dev": "modern build --watch"
|
|
48
|
+
}
|
|
49
|
+
}
|