@bleedingdev/modern-js-plugin-polyfill 3.2.0-ultramodern.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present Modern.js
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,26 @@
1
+ <p align="center">
2
+ <a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
3
+ </p>
4
+
5
+ <h1 align="center">Modern.js</h1>
6
+
7
+ <p align="center">
8
+ A Progressive React Framework for modern web development.
9
+ </p>
10
+
11
+ ## Getting Started
12
+
13
+ Please follow [Quick Start](https://modernjs.dev/en/guides/get-started/quick-start) to get started with Modern.js.
14
+
15
+ ## Documentation
16
+
17
+ - [English Documentation](https://modernjs.dev/en/)
18
+ - [中文文档](https://modernjs.dev)
19
+
20
+ ## Contributing
21
+
22
+ Please read the [Contributing Guide](https://github.com/web-infra-dev/modern.js/blob/main/CONTRIBUTING.md).
23
+
24
+ ## License
25
+
26
+ Modern.js is [MIT licensed](https://github.com/web-infra-dev/modern.js/blob/main/LICENSE).
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ default: ()=>cli,
28
+ polyfillPlugin: ()=>polyfillPlugin
29
+ });
30
+ const external_const_js_namespaceObject = require("./const.js");
31
+ const polyfillPlugin = ()=>({
32
+ name: '@modern-js/plugin-polyfill',
33
+ setup: (api)=>{
34
+ api.modifyHtmlPartials(async ({ entrypoint, partials })=>{
35
+ const resolvedConfig = api.getNormalizedConfig();
36
+ if ('ua' === resolvedConfig.output.polyfill) partials.top.append(`<script src="${external_const_js_namespaceObject.defaultPolyfill}" crossorigin></script>`);
37
+ });
38
+ api._internalServerPlugins(async ({ plugins })=>{
39
+ plugins.push({
40
+ name: '@modern-js/plugin-polyfill/server'
41
+ });
42
+ return {
43
+ plugins
44
+ };
45
+ });
46
+ }
47
+ });
48
+ const cli = polyfillPlugin;
49
+ exports["default"] = __webpack_exports__["default"];
50
+ exports.polyfillPlugin = __webpack_exports__.polyfillPlugin;
51
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
52
+ "default",
53
+ "polyfillPlugin"
54
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
55
+ Object.defineProperty(exports, '__esModule', {
56
+ value: true
57
+ });
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ defaultPolyfill: ()=>defaultPolyfill,
28
+ getDefaultFeatures: ()=>getDefaultFeatures
29
+ });
30
+ const defaultPolyfill = '/__polyfill__';
31
+ const getDefaultFeatures = ()=>({
32
+ es6: {
33
+ flags: [
34
+ 'gated'
35
+ ]
36
+ }
37
+ });
38
+ exports.defaultPolyfill = __webpack_exports__.defaultPolyfill;
39
+ exports.getDefaultFeatures = __webpack_exports__.getDefaultFeatures;
40
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
41
+ "defaultPolyfill",
42
+ "getDefaultFeatures"
43
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
44
+ Object.defineProperty(exports, '__esModule', {
45
+ value: true
46
+ });
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "ua-parser-js" (module) {
4
+ module.exports = require("ua-parser-js");
5
+ }
6
+ };
7
+ var __webpack_module_cache__ = {};
8
+ function __webpack_require__(moduleId) {
9
+ var cachedModule = __webpack_module_cache__[moduleId];
10
+ if (void 0 !== cachedModule) return cachedModule.exports;
11
+ var module = __webpack_module_cache__[moduleId] = {
12
+ exports: {}
13
+ };
14
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
15
+ return module.exports;
16
+ }
17
+ (()=>{
18
+ __webpack_require__.n = (module)=>{
19
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
20
+ __webpack_require__.d(getter, {
21
+ a: getter
22
+ });
23
+ return getter;
24
+ };
25
+ })();
26
+ (()=>{
27
+ __webpack_require__.d = (exports1, definition)=>{
28
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
29
+ enumerable: true,
30
+ get: definition[key]
31
+ });
32
+ };
33
+ })();
34
+ (()=>{
35
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
36
+ })();
37
+ (()=>{
38
+ __webpack_require__.r = (exports1)=>{
39
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
40
+ value: 'Module'
41
+ });
42
+ Object.defineProperty(exports1, '__esModule', {
43
+ value: true
44
+ });
45
+ };
46
+ })();
47
+ var __webpack_exports__ = {};
48
+ (()=>{
49
+ __webpack_require__.r(__webpack_exports__);
50
+ __webpack_require__.d(__webpack_exports__, {
51
+ default: ()=>src
52
+ });
53
+ const polyfill_lib_namespaceObject = require("@modern-js/polyfill-lib");
54
+ const utils_namespaceObject = require("@modern-js/utils");
55
+ const external_const_js_namespaceObject = require("./const.js");
56
+ const cache_js_namespaceObject = require("./libs/cache.js");
57
+ var cache_js_default = /*#__PURE__*/ __webpack_require__.n(cache_js_namespaceObject);
58
+ const UAParser = __webpack_require__("ua-parser-js");
59
+ const src = ()=>({
60
+ name: '@modern-js/plugin-polyfill',
61
+ setup: (api)=>{
62
+ api.onPrepare(()=>{
63
+ const cache = new (cache_js_default())();
64
+ const route = external_const_js_namespaceObject.defaultPolyfill;
65
+ const features = (0, external_const_js_namespaceObject.getDefaultFeatures)();
66
+ const minify = 'production' === process.env.NODE_ENV;
67
+ const featureDig = Object.keys(features).map((name)=>{
68
+ const { flags = [
69
+ 'gated'
70
+ ] } = features[name];
71
+ const flagStr = flags.join(',');
72
+ return `${name}-${flagStr}`;
73
+ }).join(',');
74
+ const { serverBase } = api.getServerContext();
75
+ serverBase?.get('*', async (context, next)=>{
76
+ if (context.req.path !== route) return next();
77
+ const parsedUA = new UAParser.UAParser(context.req.header('user-agent')).getResult();
78
+ const { name = '', version = '' } = parsedUA.browser;
79
+ const cacheKey = (0, cache_js_namespaceObject.generateCacheKey)({
80
+ name,
81
+ version,
82
+ features: featureDig,
83
+ minify
84
+ });
85
+ const matched = cache.get(cacheKey);
86
+ if (matched) {
87
+ context.res.headers.set('content-type', utils_namespaceObject.mime.contentType('js'));
88
+ return context.body(matched);
89
+ }
90
+ const polyfill = await (0, polyfill_lib_namespaceObject.getPolyfillString)({
91
+ uaString: context.req.header('user-agent'),
92
+ minify,
93
+ features
94
+ });
95
+ cache.set(cacheKey, polyfill);
96
+ context.res.headers.set('content-type', utils_namespaceObject.mime.contentType('js'));
97
+ return context.body(polyfill);
98
+ });
99
+ });
100
+ }
101
+ });
102
+ })();
103
+ exports["default"] = __webpack_exports__["default"];
104
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
105
+ "default"
106
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
107
+ Object.defineProperty(exports, '__esModule', {
108
+ value: true
109
+ });
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ default: ()=>Cache,
37
+ generateCacheKey: ()=>generateCacheKey
38
+ });
39
+ const external_crypto_namespaceObject = require("crypto");
40
+ var external_crypto_default = /*#__PURE__*/ __webpack_require__.n(external_crypto_namespaceObject);
41
+ const external_lru_cache_namespaceObject = require("lru-cache");
42
+ const KB = 1024;
43
+ const MB = 1024 * KB;
44
+ const keyCache = new external_lru_cache_namespaceObject.LRUCache({
45
+ max: 10000
46
+ });
47
+ const generateCacheKey = (options)=>{
48
+ const { name, version, features, minify } = options;
49
+ const str = `${name}-${version}-${Number(minify)}-${features}`;
50
+ const matched = keyCache.get(str);
51
+ if (matched) return matched;
52
+ const hash = external_crypto_default().createHmac('sha256', '^polyfill$').update(str).digest('hex');
53
+ keyCache.set(str, hash);
54
+ return hash;
55
+ };
56
+ class Cache {
57
+ get(hash) {
58
+ return this.caches.get(hash);
59
+ }
60
+ set(hash, content) {
61
+ this.caches.set(hash, content);
62
+ }
63
+ constructor(){
64
+ this.caches = new external_lru_cache_namespaceObject.LRUCache({
65
+ maxSize: 200 * MB,
66
+ sizeCalculation: (v)=>v.length
67
+ });
68
+ }
69
+ }
70
+ exports["default"] = __webpack_exports__["default"];
71
+ exports.generateCacheKey = __webpack_exports__.generateCacheKey;
72
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
73
+ "default",
74
+ "generateCacheKey"
75
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
76
+ Object.defineProperty(exports, '__esModule', {
77
+ value: true
78
+ });
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.r = (exports1)=>{
5
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
6
+ value: 'Module'
7
+ });
8
+ Object.defineProperty(exports1, '__esModule', {
9
+ value: true
10
+ });
11
+ };
12
+ })();
13
+ var __webpack_exports__ = {};
14
+ __webpack_require__.r(__webpack_exports__);
15
+ for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
16
+ Object.defineProperty(exports, '__esModule', {
17
+ value: true
18
+ });
@@ -0,0 +1,21 @@
1
+ import { defaultPolyfill } from "./const.mjs";
2
+ const polyfillPlugin = ()=>({
3
+ name: '@modern-js/plugin-polyfill',
4
+ setup: (api)=>{
5
+ api.modifyHtmlPartials(async ({ entrypoint, partials })=>{
6
+ const resolvedConfig = api.getNormalizedConfig();
7
+ if ('ua' === resolvedConfig.output.polyfill) partials.top.append(`<script src="${defaultPolyfill}" crossorigin></script>`);
8
+ });
9
+ api._internalServerPlugins(async ({ plugins })=>{
10
+ plugins.push({
11
+ name: '@modern-js/plugin-polyfill/server'
12
+ });
13
+ return {
14
+ plugins
15
+ };
16
+ });
17
+ }
18
+ });
19
+ const cli = polyfillPlugin;
20
+ export default cli;
21
+ export { polyfillPlugin };
@@ -0,0 +1,9 @@
1
+ const defaultPolyfill = '/__polyfill__';
2
+ const getDefaultFeatures = ()=>({
3
+ es6: {
4
+ flags: [
5
+ 'gated'
6
+ ]
7
+ }
8
+ });
9
+ export { defaultPolyfill, getDefaultFeatures };
@@ -0,0 +1,56 @@
1
+ import { getPolyfillString } from "@modern-js/polyfill-lib";
2
+ import { mime } from "@modern-js/utils";
3
+ import { defaultPolyfill, getDefaultFeatures } from "./const.mjs";
4
+ import libs_cache, { generateCacheKey } from "./libs/cache.mjs";
5
+ import { __webpack_require__ } from "./rslib-runtime.mjs";
6
+ import * as __rspack_external_ua_parser_js_881bf303 from "ua-parser-js";
7
+ __webpack_require__.add({
8
+ "ua-parser-js" (module) {
9
+ module.exports = __rspack_external_ua_parser_js_881bf303;
10
+ }
11
+ });
12
+ const UAParser = __webpack_require__("ua-parser-js");
13
+ const src = ()=>({
14
+ name: '@modern-js/plugin-polyfill',
15
+ setup: (api)=>{
16
+ api.onPrepare(()=>{
17
+ const cache = new libs_cache();
18
+ const route = defaultPolyfill;
19
+ const features = getDefaultFeatures();
20
+ const minify = 'production' === process.env.NODE_ENV;
21
+ const featureDig = Object.keys(features).map((name)=>{
22
+ const { flags = [
23
+ 'gated'
24
+ ] } = features[name];
25
+ const flagStr = flags.join(',');
26
+ return `${name}-${flagStr}`;
27
+ }).join(',');
28
+ const { serverBase } = api.getServerContext();
29
+ serverBase?.get('*', async (context, next)=>{
30
+ if (context.req.path !== route) return next();
31
+ const parsedUA = new UAParser.UAParser(context.req.header('user-agent')).getResult();
32
+ const { name = '', version = '' } = parsedUA.browser;
33
+ const cacheKey = generateCacheKey({
34
+ name,
35
+ version,
36
+ features: featureDig,
37
+ minify
38
+ });
39
+ const matched = cache.get(cacheKey);
40
+ if (matched) {
41
+ context.res.headers.set('content-type', mime.contentType('js'));
42
+ return context.body(matched);
43
+ }
44
+ const polyfill = await getPolyfillString({
45
+ uaString: context.req.header('user-agent'),
46
+ minify,
47
+ features
48
+ });
49
+ cache.set(cacheKey, polyfill);
50
+ context.res.headers.set('content-type', mime.contentType('js'));
51
+ return context.body(polyfill);
52
+ });
53
+ });
54
+ }
55
+ });
56
+ export default src;
@@ -0,0 +1,32 @@
1
+ import crypto_0 from "crypto";
2
+ import { LRUCache } from "lru-cache";
3
+ const KB = 1024;
4
+ const MB = 1024 * KB;
5
+ const keyCache = new LRUCache({
6
+ max: 10000
7
+ });
8
+ const generateCacheKey = (options)=>{
9
+ const { name, version, features, minify } = options;
10
+ const str = `${name}-${version}-${Number(minify)}-${features}`;
11
+ const matched = keyCache.get(str);
12
+ if (matched) return matched;
13
+ const hash = crypto_0.createHmac('sha256', '^polyfill$').update(str).digest('hex');
14
+ keyCache.set(str, hash);
15
+ return hash;
16
+ };
17
+ class Cache {
18
+ get(hash) {
19
+ return this.caches.get(hash);
20
+ }
21
+ set(hash, content) {
22
+ this.caches.set(hash, content);
23
+ }
24
+ constructor(){
25
+ this.caches = new LRUCache({
26
+ maxSize: 200 * MB,
27
+ sizeCalculation: (v)=>v.length
28
+ });
29
+ }
30
+ }
31
+ export default Cache;
32
+ export { generateCacheKey };
@@ -0,0 +1,18 @@
1
+ var __webpack_modules__ = {};
2
+ var __webpack_module_cache__ = {};
3
+ function __webpack_require__(moduleId) {
4
+ var cachedModule = __webpack_module_cache__[moduleId];
5
+ if (void 0 !== cachedModule) return cachedModule.exports;
6
+ var module = __webpack_module_cache__[moduleId] = {
7
+ exports: {}
8
+ };
9
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
10
+ return module.exports;
11
+ }
12
+ __webpack_require__.m = __webpack_modules__;
13
+ (()=>{
14
+ __webpack_require__.add = function(modules) {
15
+ Object.assign(__webpack_require__.m, modules);
16
+ };
17
+ })();
18
+ export { __webpack_require__ };
File without changes
@@ -0,0 +1,22 @@
1
+ import "node:module";
2
+ import { defaultPolyfill } from "./const.mjs";
3
+ const polyfillPlugin = ()=>({
4
+ name: '@modern-js/plugin-polyfill',
5
+ setup: (api)=>{
6
+ api.modifyHtmlPartials(async ({ entrypoint, partials })=>{
7
+ const resolvedConfig = api.getNormalizedConfig();
8
+ if ('ua' === resolvedConfig.output.polyfill) partials.top.append(`<script src="${defaultPolyfill}" crossorigin></script>`);
9
+ });
10
+ api._internalServerPlugins(async ({ plugins })=>{
11
+ plugins.push({
12
+ name: '@modern-js/plugin-polyfill/server'
13
+ });
14
+ return {
15
+ plugins
16
+ };
17
+ });
18
+ }
19
+ });
20
+ const cli = polyfillPlugin;
21
+ export default cli;
22
+ export { polyfillPlugin };
@@ -0,0 +1,10 @@
1
+ import "node:module";
2
+ const defaultPolyfill = '/__polyfill__';
3
+ const getDefaultFeatures = ()=>({
4
+ es6: {
5
+ flags: [
6
+ 'gated'
7
+ ]
8
+ }
9
+ });
10
+ export { defaultPolyfill, getDefaultFeatures };
@@ -0,0 +1,57 @@
1
+ import "node:module";
2
+ import { getPolyfillString } from "@modern-js/polyfill-lib";
3
+ import { mime } from "@modern-js/utils";
4
+ import { defaultPolyfill, getDefaultFeatures } from "./const.mjs";
5
+ import libs_cache, { generateCacheKey } from "./libs/cache.mjs";
6
+ import { __webpack_require__ } from "./rslib-runtime.mjs";
7
+ import * as __rspack_external_ua_parser_js_881bf303 from "ua-parser-js";
8
+ __webpack_require__.add({
9
+ "ua-parser-js" (module) {
10
+ module.exports = __rspack_external_ua_parser_js_881bf303;
11
+ }
12
+ });
13
+ const UAParser = __webpack_require__("ua-parser-js");
14
+ const src = ()=>({
15
+ name: '@modern-js/plugin-polyfill',
16
+ setup: (api)=>{
17
+ api.onPrepare(()=>{
18
+ const cache = new libs_cache();
19
+ const route = defaultPolyfill;
20
+ const features = getDefaultFeatures();
21
+ const minify = 'production' === process.env.NODE_ENV;
22
+ const featureDig = Object.keys(features).map((name)=>{
23
+ const { flags = [
24
+ 'gated'
25
+ ] } = features[name];
26
+ const flagStr = flags.join(',');
27
+ return `${name}-${flagStr}`;
28
+ }).join(',');
29
+ const { serverBase } = api.getServerContext();
30
+ serverBase?.get('*', async (context, next)=>{
31
+ if (context.req.path !== route) return next();
32
+ const parsedUA = new UAParser.UAParser(context.req.header('user-agent')).getResult();
33
+ const { name = '', version = '' } = parsedUA.browser;
34
+ const cacheKey = generateCacheKey({
35
+ name,
36
+ version,
37
+ features: featureDig,
38
+ minify
39
+ });
40
+ const matched = cache.get(cacheKey);
41
+ if (matched) {
42
+ context.res.headers.set('content-type', mime.contentType('js'));
43
+ return context.body(matched);
44
+ }
45
+ const polyfill = await getPolyfillString({
46
+ uaString: context.req.header('user-agent'),
47
+ minify,
48
+ features
49
+ });
50
+ cache.set(cacheKey, polyfill);
51
+ context.res.headers.set('content-type', mime.contentType('js'));
52
+ return context.body(polyfill);
53
+ });
54
+ });
55
+ }
56
+ });
57
+ export default src;
@@ -0,0 +1,33 @@
1
+ import "node:module";
2
+ import crypto_0 from "crypto";
3
+ import { LRUCache } from "lru-cache";
4
+ const KB = 1024;
5
+ const MB = 1024 * KB;
6
+ const keyCache = new LRUCache({
7
+ max: 10000
8
+ });
9
+ const generateCacheKey = (options)=>{
10
+ const { name, version, features, minify } = options;
11
+ const str = `${name}-${version}-${Number(minify)}-${features}`;
12
+ const matched = keyCache.get(str);
13
+ if (matched) return matched;
14
+ const hash = crypto_0.createHmac('sha256', '^polyfill$').update(str).digest('hex');
15
+ keyCache.set(str, hash);
16
+ return hash;
17
+ };
18
+ class Cache {
19
+ get(hash) {
20
+ return this.caches.get(hash);
21
+ }
22
+ set(hash, content) {
23
+ this.caches.set(hash, content);
24
+ }
25
+ constructor(){
26
+ this.caches = new LRUCache({
27
+ maxSize: 200 * MB,
28
+ sizeCalculation: (v)=>v.length
29
+ });
30
+ }
31
+ }
32
+ export default Cache;
33
+ export { generateCacheKey };
@@ -0,0 +1,19 @@
1
+ import "node:module";
2
+ var __webpack_modules__ = {};
3
+ var __webpack_module_cache__ = {};
4
+ function __webpack_require__(moduleId) {
5
+ var cachedModule = __webpack_module_cache__[moduleId];
6
+ if (void 0 !== cachedModule) return cachedModule.exports;
7
+ var module = __webpack_module_cache__[moduleId] = {
8
+ exports: {}
9
+ };
10
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
11
+ return module.exports;
12
+ }
13
+ __webpack_require__.m = __webpack_modules__;
14
+ (()=>{
15
+ __webpack_require__.add = function(modules) {
16
+ Object.assign(__webpack_require__.m, modules);
17
+ };
18
+ })();
19
+ export { __webpack_require__ };
@@ -0,0 +1 @@
1
+ import "node:module";
@@ -0,0 +1,3 @@
1
+ import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2
+ export declare const polyfillPlugin: () => CliPlugin<AppTools>;
3
+ export default polyfillPlugin;
@@ -0,0 +1,4 @@
1
+ export declare const defaultPolyfill = "/__polyfill__";
2
+ export declare const getDefaultFeatures: () => Record<string, {
3
+ flags: string[];
4
+ }>;
@@ -0,0 +1,3 @@
1
+ import type { ServerPlugin } from '@modern-js/server-core';
2
+ declare const _default: () => ServerPlugin;
3
+ export default _default;
@@ -0,0 +1,14 @@
1
+ type CacheQueryOptions = {
2
+ features: string;
3
+ minify: boolean;
4
+ name: string;
5
+ version: string;
6
+ };
7
+ export declare const generateCacheKey: (options: CacheQueryOptions) => string;
8
+ export default class Cache {
9
+ private readonly caches;
10
+ constructor();
11
+ get(hash: string): string | undefined;
12
+ set(hash: string, content: string): void;
13
+ }
14
+ export {};
@@ -0,0 +1,9 @@
1
+ export type ServerPolyfill = {
2
+ polyfill: {
3
+ route: string;
4
+ features: Record<string, {
5
+ flags: string[];
6
+ }>;
7
+ minify: boolean;
8
+ };
9
+ };
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@bleedingdev/modern-js-plugin-polyfill",
3
+ "description": "A Progressive React Framework for modern web development.",
4
+ "homepage": "https://github.com/BleedingDev/ultramodern.js#readme",
5
+ "bugs": {
6
+ "url": "https://github.com/BleedingDev/ultramodern.js/issues"
7
+ },
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/BleedingDev/ultramodern.js.git",
11
+ "directory": "packages/server/plugin-polyfill"
12
+ },
13
+ "license": "MIT",
14
+ "keywords": [
15
+ "react",
16
+ "framework",
17
+ "modern",
18
+ "modern.js"
19
+ ],
20
+ "version": "3.2.0-ultramodern.0",
21
+ "types": "./dist/types/cli.d.ts",
22
+ "main": "./dist/cjs/cli.js",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/types/cli.d.ts",
26
+ "node": {
27
+ "import": "./dist/esm-node/cli.mjs",
28
+ "require": "./dist/cjs/cli.js"
29
+ },
30
+ "default": "./dist/cjs/cli.js"
31
+ },
32
+ "./cli": {
33
+ "types": "./dist/types/cli.d.ts",
34
+ "node": {
35
+ "import": "./dist/esm-node/cli.mjs",
36
+ "require": "./dist/cjs/cli.js"
37
+ },
38
+ "default": "./dist/cjs/cli.js"
39
+ },
40
+ "./server": {
41
+ "types": "./dist/types/index.d.ts",
42
+ "node": {
43
+ "import": "./dist/esm-node/index.mjs",
44
+ "require": "./dist/cjs/index.js"
45
+ },
46
+ "default": "./dist/cjs/index.js"
47
+ }
48
+ },
49
+ "typesVersions": {
50
+ "*": {
51
+ ".": [
52
+ "./dist/types/cli.d.ts"
53
+ ],
54
+ "cli": [
55
+ "./dist/types/cli.d.ts"
56
+ ],
57
+ "server": [
58
+ "./dist/types/index.d.ts"
59
+ ]
60
+ }
61
+ },
62
+ "dependencies": {
63
+ "@modern-js/polyfill-lib": "^1.0.2",
64
+ "@swc/helpers": "^0.5.21",
65
+ "lru-cache": "^11.3.6",
66
+ "ua-parser-js": "^2.0.9",
67
+ "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.0"
68
+ },
69
+ "devDependencies": {
70
+ "@rslib/core": "0.21.5",
71
+ "@types/node": "^25.8.0",
72
+ "@types/ua-parser-js": "^0.7.39",
73
+ "@typescript/native-preview": "7.0.0-dev.20260516.1",
74
+ "@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.2.0-ultramodern.0",
75
+ "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.0",
76
+ "@scripts/rstest-config": "2.66.0",
77
+ "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.0"
78
+ },
79
+ "sideEffects": false,
80
+ "publishConfig": {
81
+ "registry": "https://registry.npmjs.org/",
82
+ "access": "public"
83
+ },
84
+ "scripts": {
85
+ "build": "rslib build",
86
+ "dev": "rslib build --watch",
87
+ "test": "rstest --passWithNoTests"
88
+ }
89
+ }
@@ -0,0 +1,4 @@
1
+ import { rslibConfig } from '@modern-js/rslib';
2
+ import { defineConfig } from '@rslib/core';
3
+
4
+ export default defineConfig(rslibConfig);
@@ -0,0 +1,7 @@
1
+ import { withTestPreset } from '@scripts/rstest-config';
2
+
3
+ export default withTestPreset({
4
+ root: __dirname,
5
+ testEnvironment: 'node',
6
+ globals: true,
7
+ });