@modern-js/plugin-polyfill 2.69.4 → 3.0.0-alpha.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/dist/cjs/cli.js CHANGED
@@ -1,53 +1,57 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var cli_exports = {};
20
- __export(cli_exports, {
21
- default: () => cli_default,
22
- polyfillPlugin: () => polyfillPlugin
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
+ polyfillPlugin: ()=>polyfillPlugin,
28
+ default: ()=>cli
23
29
  });
24
- module.exports = __toCommonJS(cli_exports);
25
- var import_const = require("./const");
26
- const polyfillPlugin = () => ({
27
- name: "@modern-js/plugin-polyfill",
28
- setup: (api) => ({
29
- htmlPartials({ entrypoint, partials }) {
30
- const resolvedConfig = api.useResolvedConfigContext();
31
- if (resolvedConfig.output.polyfill === "ua") {
32
- partials.top.push(`<script src="${import_const.defaultPolyfill}" crossorigin></script>`);
33
- }
34
- return {
35
- partials,
36
- entrypoint
37
- };
38
- },
39
- _internalServerPlugins({ plugins }) {
40
- plugins.push({
41
- name: "@modern-js/plugin-polyfill/server"
42
- });
43
- return {
44
- plugins
45
- };
46
- }
47
- })
48
- });
49
- var cli_default = polyfillPlugin;
50
- // Annotate the CommonJS export names for ESM import in node:
51
- 0 && (module.exports = {
52
- polyfillPlugin
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
53
57
  });
package/dist/cjs/const.js CHANGED
@@ -1,37 +1,46 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var const_exports = {};
20
- __export(const_exports, {
21
- defaultPolyfill: () => defaultPolyfill,
22
- getDefaultFeatures: () => getDefaultFeatures
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
23
29
  });
24
- module.exports = __toCommonJS(const_exports);
25
- const defaultPolyfill = "/__polyfill__";
26
- const getDefaultFeatures = () => ({
27
- es6: {
28
- flags: [
29
- "gated"
30
- ]
31
- }
32
- });
33
- // Annotate the CommonJS export names for ESM import in node:
34
- 0 && (module.exports = {
35
- defaultPolyfill,
36
- getDefaultFeatures
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
37
46
  });
package/dist/cjs/index.js CHANGED
@@ -1,83 +1,94 @@
1
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 __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var src_exports = {};
30
- __export(src_exports, {
31
- default: () => src_default
32
- });
33
- module.exports = __toCommonJS(src_exports);
34
- var import_polyfill_lib = require("@modern-js/polyfill-lib");
35
- var import_utils = require("@modern-js/utils");
36
- var import_ua_parser_js = __toESM(require("ua-parser-js"));
37
- var import_const = require("./const");
38
- var import_cache = __toESM(require("./libs/cache"));
39
- var src_default = () => ({
40
- name: "@modern-js/plugin-polyfill",
41
- setup: (api) => ({
42
- prepare() {
43
- const cache = new import_cache.default();
44
- const route = import_const.defaultPolyfill;
45
- const features = (0, import_const.getDefaultFeatures)();
46
- const minify = process.env.NODE_ENV === "production";
47
- const featureDig = Object.keys(features).map((name) => {
48
- const { flags = [
49
- "gated"
50
- ] } = features[name];
51
- const flagStr = flags.join(",");
52
- return `${name}-${flagStr}`;
53
- }).join(",");
54
- const { serverBase } = api.useAppContext();
55
- serverBase === null || serverBase === void 0 ? void 0 : serverBase.get("*", async (context, next) => {
56
- if (context.req.path !== route) {
57
- return next();
58
- }
59
- const parsedUA = (0, import_ua_parser_js.default)(context.req.header("user-agent"));
60
- const { name = "", version = "" } = parsedUA.browser;
61
- const cacheKey = (0, import_cache.generateCacheKey)({
62
- name,
63
- version,
64
- features: featureDig,
65
- minify
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
66
8
  });
67
- const matched = cache.get(cacheKey);
68
- if (matched) {
69
- context.res.headers.set("content-type", import_utils.mime.contentType("js"));
70
- return context.body(matched);
71
- }
72
- const polyfill = await (0, import_polyfill_lib.getPolyfillString)({
73
- uaString: context.req.header("user-agent"),
74
- minify,
75
- features
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'
76
27
  });
77
- cache.set(cacheKey, polyfill);
78
- context.res.headers.set("content-type", import_utils.mime.contentType("js"));
79
- return context.body(polyfill);
80
- });
81
- }
82
- })
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: ()=>src
37
+ });
38
+ const polyfill_lib_namespaceObject = require("@modern-js/polyfill-lib");
39
+ const utils_namespaceObject = require("@modern-js/utils");
40
+ const external_ua_parser_js_namespaceObject = require("ua-parser-js");
41
+ var external_ua_parser_js_default = /*#__PURE__*/ __webpack_require__.n(external_ua_parser_js_namespaceObject);
42
+ const external_const_js_namespaceObject = require("./const.js");
43
+ const cache_js_namespaceObject = require("./libs/cache.js");
44
+ var cache_js_default = /*#__PURE__*/ __webpack_require__.n(cache_js_namespaceObject);
45
+ const src = ()=>({
46
+ name: '@modern-js/plugin-polyfill',
47
+ setup: (api)=>{
48
+ api.onPrepare(()=>{
49
+ const cache = new (cache_js_default())();
50
+ const route = external_const_js_namespaceObject.defaultPolyfill;
51
+ const features = (0, external_const_js_namespaceObject.getDefaultFeatures)();
52
+ const minify = 'production' === process.env.NODE_ENV;
53
+ const featureDig = Object.keys(features).map((name)=>{
54
+ const { flags = [
55
+ 'gated'
56
+ ] } = features[name];
57
+ const flagStr = flags.join(',');
58
+ return `${name}-${flagStr}`;
59
+ }).join(',');
60
+ const { serverBase } = api.getServerContext();
61
+ serverBase?.get('*', async (context, next)=>{
62
+ if (context.req.path !== route) return next();
63
+ const parsedUA = external_ua_parser_js_default()(context.req.header('user-agent'));
64
+ const { name = '', version = '' } = parsedUA.browser;
65
+ const cacheKey = (0, cache_js_namespaceObject.generateCacheKey)({
66
+ name,
67
+ version,
68
+ features: featureDig,
69
+ minify
70
+ });
71
+ const matched = cache.get(cacheKey);
72
+ if (matched) {
73
+ context.res.headers.set('content-type', utils_namespaceObject.mime.contentType('js'));
74
+ return context.body(matched);
75
+ }
76
+ const polyfill = await (0, polyfill_lib_namespaceObject.getPolyfillString)({
77
+ uaString: context.req.header('user-agent'),
78
+ minify,
79
+ features
80
+ });
81
+ cache.set(cacheKey, polyfill);
82
+ context.res.headers.set('content-type', utils_namespaceObject.mime.contentType('js'));
83
+ return context.body(polyfill);
84
+ });
85
+ });
86
+ }
87
+ });
88
+ exports["default"] = __webpack_exports__["default"];
89
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
90
+ "default"
91
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
92
+ Object.defineProperty(exports, '__esModule', {
93
+ value: true
83
94
  });
@@ -1,68 +1,77 @@
1
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 __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var cache_exports = {};
30
- __export(cache_exports, {
31
- default: () => Cache,
32
- generateCacheKey: () => generateCacheKey
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
33
38
  });
34
- module.exports = __toCommonJS(cache_exports);
35
- var import_crypto = __toESM(require("crypto"));
36
- var import_lru_cache = __toESM(require("lru-cache"));
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
+ var external_lru_cache_default = /*#__PURE__*/ __webpack_require__.n(external_lru_cache_namespaceObject);
37
43
  const KB = 1024;
38
44
  const MB = 1024 * KB;
39
- const keyCache = new import_lru_cache.default(1e4);
40
- const generateCacheKey = (options) => {
41
- const { name, version, features, minify } = options;
42
- const str = `${name}-${version}-${Number(minify)}-${features}`;
43
- const matched = keyCache.get(str);
44
- if (matched) {
45
- return matched;
46
- }
47
- const hash = import_crypto.default.createHmac("sha256", "^polyfill$").update(str).digest("hex");
48
- keyCache.set(str, hash);
49
- return hash;
45
+ const keyCache = new (external_lru_cache_default())(10000);
46
+ const generateCacheKey = (options)=>{
47
+ const { name, version, features, minify } = options;
48
+ const str = `${name}-${version}-${Number(minify)}-${features}`;
49
+ const matched = keyCache.get(str);
50
+ if (matched) return matched;
51
+ const hash = external_crypto_default().createHmac('sha256', '^polyfill$').update(str).digest('hex');
52
+ keyCache.set(str, hash);
53
+ return hash;
50
54
  };
51
55
  class Cache {
52
- get(hash) {
53
- return this.caches.get(hash);
54
- }
55
- set(hash, content) {
56
- this.caches.set(hash, content);
57
- }
58
- constructor() {
59
- this.caches = new import_lru_cache.default({
60
- max: 200 * MB,
61
- length: (v) => v.length
62
- });
63
- }
56
+ get(hash) {
57
+ return this.caches.get(hash);
58
+ }
59
+ set(hash, content) {
60
+ this.caches.set(hash, content);
61
+ }
62
+ constructor(){
63
+ this.caches = new (external_lru_cache_default())({
64
+ max: 200 * MB,
65
+ length: (v)=>v.length
66
+ });
67
+ }
64
68
  }
65
- // Annotate the CommonJS export names for ESM import in node:
66
- 0 && (module.exports = {
67
- generateCacheKey
69
+ exports["default"] = __webpack_exports__["default"];
70
+ exports.generateCacheKey = __webpack_exports__.generateCacheKey;
71
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
72
+ "default",
73
+ "generateCacheKey"
74
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
75
+ Object.defineProperty(exports, '__esModule', {
76
+ value: true
68
77
  });
package/dist/cjs/type.js CHANGED
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
- var type_exports = {};
16
- module.exports = __toCommonJS(type_exports);
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,20 @@
1
+ import { defaultPolyfill } from "./const";
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 { cli as default, 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,49 @@
1
+ import { getPolyfillString } from "@modern-js/polyfill-lib";
2
+ import { mime } from "@modern-js/utils";
3
+ import ua_parser_js from "ua-parser-js";
4
+ import { defaultPolyfill, getDefaultFeatures } from "./const";
5
+ import libs_cache, { generateCacheKey } from "./libs/cache";
6
+ const src = ()=>({
7
+ name: '@modern-js/plugin-polyfill',
8
+ setup: (api)=>{
9
+ api.onPrepare(()=>{
10
+ const cache = new libs_cache();
11
+ const route = defaultPolyfill;
12
+ const features = getDefaultFeatures();
13
+ const minify = 'production' === process.env.NODE_ENV;
14
+ const featureDig = Object.keys(features).map((name)=>{
15
+ const { flags = [
16
+ 'gated'
17
+ ] } = features[name];
18
+ const flagStr = flags.join(',');
19
+ return `${name}-${flagStr}`;
20
+ }).join(',');
21
+ const { serverBase } = api.getServerContext();
22
+ serverBase?.get('*', async (context, next)=>{
23
+ if (context.req.path !== route) return next();
24
+ const parsedUA = ua_parser_js(context.req.header('user-agent'));
25
+ const { name = '', version = '' } = parsedUA.browser;
26
+ const cacheKey = generateCacheKey({
27
+ name,
28
+ version,
29
+ features: featureDig,
30
+ minify
31
+ });
32
+ const matched = cache.get(cacheKey);
33
+ if (matched) {
34
+ context.res.headers.set('content-type', mime.contentType('js'));
35
+ return context.body(matched);
36
+ }
37
+ const polyfill = await getPolyfillString({
38
+ uaString: context.req.header('user-agent'),
39
+ minify,
40
+ features
41
+ });
42
+ cache.set(cacheKey, polyfill);
43
+ context.res.headers.set('content-type', mime.contentType('js'));
44
+ return context.body(polyfill);
45
+ });
46
+ });
47
+ }
48
+ });
49
+ export { src as default };
@@ -0,0 +1,29 @@
1
+ import crypto_0 from "crypto";
2
+ import lru_cache from "lru-cache";
3
+ const KB = 1024;
4
+ const MB = 1024 * KB;
5
+ const keyCache = new lru_cache(10000);
6
+ const generateCacheKey = (options)=>{
7
+ const { name, version, features, minify } = options;
8
+ const str = `${name}-${version}-${Number(minify)}-${features}`;
9
+ const matched = keyCache.get(str);
10
+ if (matched) return matched;
11
+ const hash = crypto_0.createHmac('sha256', '^polyfill$').update(str).digest('hex');
12
+ keyCache.set(str, hash);
13
+ return hash;
14
+ };
15
+ class Cache {
16
+ get(hash) {
17
+ return this.caches.get(hash);
18
+ }
19
+ set(hash, content) {
20
+ this.caches.set(hash, content);
21
+ }
22
+ constructor(){
23
+ this.caches = new lru_cache({
24
+ max: 200 * MB,
25
+ length: (v)=>v.length
26
+ });
27
+ }
28
+ }
29
+ export { Cache as default, generateCacheKey };
@@ -0,0 +1,20 @@
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 { cli as default, 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,49 @@
1
+ import { getPolyfillString } from "@modern-js/polyfill-lib";
2
+ import { mime } from "@modern-js/utils";
3
+ import ua_parser_js from "ua-parser-js";
4
+ import { defaultPolyfill, getDefaultFeatures } from "./const.mjs";
5
+ import libs_cache, { generateCacheKey } from "./libs/cache.mjs";
6
+ const src = ()=>({
7
+ name: '@modern-js/plugin-polyfill',
8
+ setup: (api)=>{
9
+ api.onPrepare(()=>{
10
+ const cache = new libs_cache();
11
+ const route = defaultPolyfill;
12
+ const features = getDefaultFeatures();
13
+ const minify = 'production' === process.env.NODE_ENV;
14
+ const featureDig = Object.keys(features).map((name)=>{
15
+ const { flags = [
16
+ 'gated'
17
+ ] } = features[name];
18
+ const flagStr = flags.join(',');
19
+ return `${name}-${flagStr}`;
20
+ }).join(',');
21
+ const { serverBase } = api.getServerContext();
22
+ serverBase?.get('*', async (context, next)=>{
23
+ if (context.req.path !== route) return next();
24
+ const parsedUA = ua_parser_js(context.req.header('user-agent'));
25
+ const { name = '', version = '' } = parsedUA.browser;
26
+ const cacheKey = generateCacheKey({
27
+ name,
28
+ version,
29
+ features: featureDig,
30
+ minify
31
+ });
32
+ const matched = cache.get(cacheKey);
33
+ if (matched) {
34
+ context.res.headers.set('content-type', mime.contentType('js'));
35
+ return context.body(matched);
36
+ }
37
+ const polyfill = await getPolyfillString({
38
+ uaString: context.req.header('user-agent'),
39
+ minify,
40
+ features
41
+ });
42
+ cache.set(cacheKey, polyfill);
43
+ context.res.headers.set('content-type', mime.contentType('js'));
44
+ return context.body(polyfill);
45
+ });
46
+ });
47
+ }
48
+ });
49
+ export { src as default };
@@ -0,0 +1,29 @@
1
+ import crypto_0 from "crypto";
2
+ import lru_cache from "lru-cache";
3
+ const KB = 1024;
4
+ const MB = 1024 * KB;
5
+ const keyCache = new lru_cache(10000);
6
+ const generateCacheKey = (options)=>{
7
+ const { name, version, features, minify } = options;
8
+ const str = `${name}-${version}-${Number(minify)}-${features}`;
9
+ const matched = keyCache.get(str);
10
+ if (matched) return matched;
11
+ const hash = crypto_0.createHmac('sha256', '^polyfill$').update(str).digest('hex');
12
+ keyCache.set(str, hash);
13
+ return hash;
14
+ };
15
+ class Cache {
16
+ get(hash) {
17
+ return this.caches.get(hash);
18
+ }
19
+ set(hash, content) {
20
+ this.caches.set(hash, content);
21
+ }
22
+ constructor(){
23
+ this.caches = new lru_cache({
24
+ max: 200 * MB,
25
+ length: (v)=>v.length
26
+ });
27
+ }
28
+ }
29
+ export { Cache as default, generateCacheKey };
@@ -1,4 +1,3 @@
1
- import type { AppTools } from '@modern-js/app-tools';
2
- import type { CliPlugin } from '@modern-js/core';
1
+ import type { AppTools, CliPlugin } from '@modern-js/app-tools';
3
2
  export declare const polyfillPlugin: () => CliPlugin<AppTools>;
4
3
  export default polyfillPlugin;
@@ -1,3 +1,3 @@
1
- import type { ServerPluginLegacy } from '@modern-js/server-core';
2
- declare const _default: () => ServerPluginLegacy;
1
+ import type { ServerPlugin } from '@modern-js/server-core';
2
+ declare const _default: () => ServerPlugin;
3
3
  export default _default;
package/package.json CHANGED
@@ -15,57 +15,71 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.69.4",
18
+ "version": "3.0.0-alpha.0",
19
19
  "jsnext:source": "./src/cli.ts",
20
20
  "types": "./dist/types/cli.d.ts",
21
21
  "main": "./dist/cjs/cli.js",
22
- "module": "./dist/esm/cli.js",
22
+ "module": "./dist/esm/cli.mjs",
23
23
  "exports": {
24
24
  ".": {
25
+ "types": "./dist/types/cli.d.ts",
25
26
  "jsnext:source": "./src/cli.ts",
26
27
  "node": {
27
- "import": "./dist/cjs/cli.js",
28
+ "import": "./dist/esm-node/cli.mjs",
28
29
  "require": "./dist/cjs/cli.js"
29
30
  },
30
- "default": "./dist/cjs/cli.js"
31
+ "default": "./dist/esm/cli.mjs"
31
32
  },
32
33
  "./cli": {
34
+ "types": "./dist/types/cli.d.ts",
33
35
  "jsnext:source": "./src/cli.ts",
34
36
  "node": {
35
- "import": "./dist/cjs/cli.js",
37
+ "import": "./dist/esm-node/cli.mjs",
36
38
  "require": "./dist/cjs/cli.js"
37
39
  },
38
- "default": "./dist/cjs/cli.js"
40
+ "default": "./dist/esm/cli.mjs"
39
41
  },
40
42
  "./server": {
43
+ "types": "./dist/types/index.d.ts",
44
+ "jsnext:source": "./src/index.ts",
41
45
  "node": {
42
- "jsnext:source": "./src/index.ts",
43
- "import": "./dist/esm-node/index.js",
46
+ "import": "./dist/esm-node/index.mjs",
44
47
  "require": "./dist/cjs/index.js"
45
48
  },
46
- "default": "./dist/esm/index.js"
49
+ "default": "./dist/esm/index.mjs"
50
+ }
51
+ },
52
+ "typesVersions": {
53
+ "*": {
54
+ ".": [
55
+ "./dist/types/cli.d.ts"
56
+ ],
57
+ "cli": [
58
+ "./dist/types/cli.d.ts"
59
+ ],
60
+ "server": [
61
+ "./dist/types/index.d.ts"
62
+ ]
47
63
  }
48
64
  },
49
65
  "dependencies": {
50
66
  "@modern-js/polyfill-lib": "^1.0.2",
51
67
  "@swc/helpers": "^0.5.17",
52
68
  "lru-cache": "^6.0.0",
53
- "ua-parser-js": "^0.7.33",
54
- "@modern-js/utils": "2.69.4"
69
+ "ua-parser-js": "^0.7.41",
70
+ "@modern-js/utils": "3.0.0-alpha.0"
55
71
  },
56
72
  "devDependencies": {
57
- "@types/jest": "^29",
73
+ "@rslib/core": "0.18.5",
58
74
  "@types/lru-cache": "^5.1.1",
59
- "@types/node": "^18",
60
- "@types/ua-parser-js": "^0.7.36",
61
- "jest": "^29",
75
+ "@types/node": "^20",
76
+ "@types/ua-parser-js": "^0.7.39",
62
77
  "typescript": "^5",
63
- "@modern-js/app-tools": "2.69.4",
64
- "@modern-js/core": "2.69.4",
65
- "@modern-js/server-core": "2.69.4",
66
- "@modern-js/types": "2.69.4",
67
- "@scripts/jest-config": "2.66.0",
68
- "@scripts/build": "2.66.0"
78
+ "@modern-js/server-core": "3.0.0-alpha.0",
79
+ "@scripts/rstest-config": "2.66.0",
80
+ "@modern-js/app-tools": "3.0.0-alpha.0",
81
+ "@modern-js/types": "3.0.0-alpha.0",
82
+ "@modern-js/rslib": "2.68.10"
69
83
  },
70
84
  "sideEffects": false,
71
85
  "publishConfig": {
@@ -73,9 +87,8 @@
73
87
  "access": "public"
74
88
  },
75
89
  "scripts": {
76
- "new": "modern-lib new",
77
- "build": "modern-lib build",
78
- "dev": "modern-lib build --watch",
79
- "test": "jest --passWithNoTests"
90
+ "build": "rslib build",
91
+ "dev": "rslib build --watch",
92
+ "test": "rstest --passWithNoTests"
80
93
  }
81
94
  }
@@ -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
+ });
package/dist/esm/cli.js DELETED
@@ -1,35 +0,0 @@
1
- import { defaultPolyfill } from "./const";
2
- var polyfillPlugin = function() {
3
- return {
4
- name: "@modern-js/plugin-polyfill",
5
- setup: function(api) {
6
- return {
7
- htmlPartials: function htmlPartials(param) {
8
- var entrypoint = param.entrypoint, partials = param.partials;
9
- var resolvedConfig = api.useResolvedConfigContext();
10
- if (resolvedConfig.output.polyfill === "ua") {
11
- partials.top.push('<script src="'.concat(defaultPolyfill, '" crossorigin></script>'));
12
- }
13
- return {
14
- partials,
15
- entrypoint
16
- };
17
- },
18
- _internalServerPlugins: function _internalServerPlugins(param) {
19
- var plugins = param.plugins;
20
- plugins.push({
21
- name: "@modern-js/plugin-polyfill/server"
22
- });
23
- return {
24
- plugins
25
- };
26
- }
27
- };
28
- }
29
- };
30
- };
31
- var cli_default = polyfillPlugin;
32
- export {
33
- cli_default as default,
34
- polyfillPlugin
35
- };
package/dist/esm/const.js DELETED
@@ -1,14 +0,0 @@
1
- var defaultPolyfill = "/__polyfill__";
2
- var getDefaultFeatures = function() {
3
- return {
4
- es6: {
5
- flags: [
6
- "gated"
7
- ]
8
- }
9
- };
10
- };
11
- export {
12
- defaultPolyfill,
13
- getDefaultFeatures
14
- };
package/dist/esm/index.js DELETED
@@ -1,85 +0,0 @@
1
- import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
- import { getPolyfillString } from "@modern-js/polyfill-lib";
4
- import { mime } from "@modern-js/utils";
5
- import Parser from "ua-parser-js";
6
- import { defaultPolyfill, getDefaultFeatures } from "./const";
7
- import PolyfillCache, { generateCacheKey } from "./libs/cache";
8
- function src_default() {
9
- return {
10
- name: "@modern-js/plugin-polyfill",
11
- setup: function(api) {
12
- return {
13
- prepare: function prepare() {
14
- var cache = new PolyfillCache();
15
- var route = defaultPolyfill;
16
- var features = getDefaultFeatures();
17
- var minify = process.env.NODE_ENV === "production";
18
- var featureDig = Object.keys(features).map(function(name) {
19
- var _features_name = features[name], _features_name_flags = _features_name.flags, flags = _features_name_flags === void 0 ? [
20
- "gated"
21
- ] : _features_name_flags;
22
- var flagStr = flags.join(",");
23
- return "".concat(name, "-").concat(flagStr);
24
- }).join(",");
25
- var serverBase = api.useAppContext().serverBase;
26
- serverBase === null || serverBase === void 0 ? void 0 : serverBase.get("*", function() {
27
- var _ref = _async_to_generator(function(context, next) {
28
- var parsedUA, _parsedUA_browser, _parsedUA_browser_name, name, _parsedUA_browser_version, version, cacheKey, matched, polyfill;
29
- return _ts_generator(this, function(_state) {
30
- switch (_state.label) {
31
- case 0:
32
- if (context.req.path !== route) {
33
- return [
34
- 2,
35
- next()
36
- ];
37
- }
38
- parsedUA = Parser(context.req.header("user-agent"));
39
- _parsedUA_browser = parsedUA.browser, _parsedUA_browser_name = _parsedUA_browser.name, name = _parsedUA_browser_name === void 0 ? "" : _parsedUA_browser_name, _parsedUA_browser_version = _parsedUA_browser.version, version = _parsedUA_browser_version === void 0 ? "" : _parsedUA_browser_version;
40
- cacheKey = generateCacheKey({
41
- name,
42
- version,
43
- features: featureDig,
44
- minify
45
- });
46
- matched = cache.get(cacheKey);
47
- if (matched) {
48
- context.res.headers.set("content-type", mime.contentType("js"));
49
- return [
50
- 2,
51
- context.body(matched)
52
- ];
53
- }
54
- return [
55
- 4,
56
- getPolyfillString({
57
- uaString: context.req.header("user-agent"),
58
- minify,
59
- features
60
- })
61
- ];
62
- case 1:
63
- polyfill = _state.sent();
64
- cache.set(cacheKey, polyfill);
65
- context.res.headers.set("content-type", mime.contentType("js"));
66
- return [
67
- 2,
68
- context.body(polyfill)
69
- ];
70
- }
71
- });
72
- });
73
- return function(context, next) {
74
- return _ref.apply(this, arguments);
75
- };
76
- }());
77
- }
78
- };
79
- }
80
- };
81
- }
82
- ;
83
- export {
84
- src_default as default
85
- };
@@ -1,41 +0,0 @@
1
- import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
- import crypto from "crypto";
3
- import LRUCache from "lru-cache";
4
- var KB = 1024;
5
- var MB = 1024 * KB;
6
- var keyCache = new LRUCache(1e4);
7
- var generateCacheKey = function(options) {
8
- var name = options.name, version = options.version, features = options.features, minify = options.minify;
9
- var str = "".concat(name, "-").concat(version, "-").concat(Number(minify), "-").concat(features);
10
- var matched = keyCache.get(str);
11
- if (matched) {
12
- return matched;
13
- }
14
- var hash = crypto.createHmac("sha256", "^polyfill$").update(str).digest("hex");
15
- keyCache.set(str, hash);
16
- return hash;
17
- };
18
- var Cache = /* @__PURE__ */ function() {
19
- "use strict";
20
- function Cache2() {
21
- _class_call_check(this, Cache2);
22
- this.caches = new LRUCache({
23
- max: 200 * MB,
24
- length: function(v) {
25
- return v.length;
26
- }
27
- });
28
- }
29
- var _proto = Cache2.prototype;
30
- _proto.get = function get(hash) {
31
- return this.caches.get(hash);
32
- };
33
- _proto.set = function set(hash, content) {
34
- this.caches.set(hash, content);
35
- };
36
- return Cache2;
37
- }();
38
- export {
39
- Cache as default,
40
- generateCacheKey
41
- };
@@ -1,29 +0,0 @@
1
- import { defaultPolyfill } from "./const";
2
- const polyfillPlugin = () => ({
3
- name: "@modern-js/plugin-polyfill",
4
- setup: (api) => ({
5
- htmlPartials({ entrypoint, partials }) {
6
- const resolvedConfig = api.useResolvedConfigContext();
7
- if (resolvedConfig.output.polyfill === "ua") {
8
- partials.top.push(`<script src="${defaultPolyfill}" crossorigin></script>`);
9
- }
10
- return {
11
- partials,
12
- entrypoint
13
- };
14
- },
15
- _internalServerPlugins({ plugins }) {
16
- plugins.push({
17
- name: "@modern-js/plugin-polyfill/server"
18
- });
19
- return {
20
- plugins
21
- };
22
- }
23
- })
24
- });
25
- var cli_default = polyfillPlugin;
26
- export {
27
- cli_default as default,
28
- polyfillPlugin
29
- };
@@ -1,12 +0,0 @@
1
- const defaultPolyfill = "/__polyfill__";
2
- const getDefaultFeatures = () => ({
3
- es6: {
4
- flags: [
5
- "gated"
6
- ]
7
- }
8
- });
9
- export {
10
- defaultPolyfill,
11
- getDefaultFeatures
12
- };
@@ -1,53 +0,0 @@
1
- import { getPolyfillString } from "@modern-js/polyfill-lib";
2
- import { mime } from "@modern-js/utils";
3
- import Parser from "ua-parser-js";
4
- import { defaultPolyfill, getDefaultFeatures } from "./const";
5
- import PolyfillCache, { generateCacheKey } from "./libs/cache";
6
- var src_default = () => ({
7
- name: "@modern-js/plugin-polyfill",
8
- setup: (api) => ({
9
- prepare() {
10
- const cache = new PolyfillCache();
11
- const route = defaultPolyfill;
12
- const features = getDefaultFeatures();
13
- const minify = process.env.NODE_ENV === "production";
14
- const featureDig = Object.keys(features).map((name) => {
15
- const { flags = [
16
- "gated"
17
- ] } = features[name];
18
- const flagStr = flags.join(",");
19
- return `${name}-${flagStr}`;
20
- }).join(",");
21
- const { serverBase } = api.useAppContext();
22
- serverBase === null || serverBase === void 0 ? void 0 : serverBase.get("*", async (context, next) => {
23
- if (context.req.path !== route) {
24
- return next();
25
- }
26
- const parsedUA = Parser(context.req.header("user-agent"));
27
- const { name = "", version = "" } = parsedUA.browser;
28
- const cacheKey = generateCacheKey({
29
- name,
30
- version,
31
- features: featureDig,
32
- minify
33
- });
34
- const matched = cache.get(cacheKey);
35
- if (matched) {
36
- context.res.headers.set("content-type", mime.contentType("js"));
37
- return context.body(matched);
38
- }
39
- const polyfill = await getPolyfillString({
40
- uaString: context.req.header("user-agent"),
41
- minify,
42
- features
43
- });
44
- cache.set(cacheKey, polyfill);
45
- context.res.headers.set("content-type", mime.contentType("js"));
46
- return context.body(polyfill);
47
- });
48
- }
49
- })
50
- });
51
- export {
52
- src_default as default
53
- };
@@ -1,34 +0,0 @@
1
- import crypto from "crypto";
2
- import LRUCache from "lru-cache";
3
- const KB = 1024;
4
- const MB = 1024 * KB;
5
- const keyCache = new LRUCache(1e4);
6
- const generateCacheKey = (options) => {
7
- const { name, version, features, minify } = options;
8
- const str = `${name}-${version}-${Number(minify)}-${features}`;
9
- const matched = keyCache.get(str);
10
- if (matched) {
11
- return matched;
12
- }
13
- const hash = crypto.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
- max: 200 * MB,
27
- length: (v) => v.length
28
- });
29
- }
30
- }
31
- export {
32
- Cache as default,
33
- generateCacheKey
34
- };
File without changes
File without changes