@modern-js/plugin-polyfill 2.0.0-beta.2 → 2.0.0-beta.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,71 @@
1
1
  # @modern-js/plugin-polyfill
2
2
 
3
+ ## 2.0.0-beta.4
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - ebbeed1ece: fix(plugin-polyfill): failed to join flags when restart CLI
12
+
13
+ fix(plugin-polyfill): 修复重启 CLI 时报错 join flags 失败的问题
14
+
15
+ - Updated dependencies [7879e8f]
16
+ - Updated dependencies [6aca875]
17
+ - Updated dependencies [2e6031955e]
18
+ - Updated dependencies [7b7d12c]
19
+ - Updated dependencies [92f0eade39]
20
+ - Updated dependencies [edd1cfb1af]
21
+ - Updated dependencies [cc971eabfc]
22
+ - Updated dependencies [5b9049f2e9]
23
+ - Updated dependencies [92004d1906]
24
+ - Updated dependencies [b8bbe036c7]
25
+ - Updated dependencies [d5a31df781]
26
+ - Updated dependencies [dda38c9c3e]
27
+ - Updated dependencies [3bbea92b2a]
28
+ - Updated dependencies [b710adb843]
29
+ - Updated dependencies [ea7cf06]
30
+ - Updated dependencies [bbe4c4a]
31
+ - Updated dependencies [e4558a0]
32
+ - Updated dependencies [abf3421a75]
33
+ - Updated dependencies [543be9558e]
34
+ - Updated dependencies [14b712da84]
35
+ - @modern-js/utils@2.0.0-beta.4
36
+
37
+ ## 2.0.0-beta.3
38
+
39
+ ### Major Changes
40
+
41
+ - dda38c9c3e: chore: v2
42
+
43
+ ### Patch Changes
44
+
45
+ - ebbeed1ece: fix(plugin-polyfill): failed to join flags when restart CLI
46
+
47
+ fix(plugin-polyfill): 修复重启 CLI 时报错 join flags 失败的问题
48
+
49
+ - Updated dependencies [6aca875]
50
+ - Updated dependencies [2e60319]
51
+ - Updated dependencies [92f0eade39]
52
+ - Updated dependencies [edd1cfb1af]
53
+ - Updated dependencies [cc971eabfc]
54
+ - Updated dependencies [5b9049f2e9]
55
+ - Updated dependencies [92004d1906]
56
+ - Updated dependencies [b8bbe036c7]
57
+ - Updated dependencies [d5a31df781]
58
+ - Updated dependencies [dda38c9c3e]
59
+ - Updated dependencies [3bbea92b2a]
60
+ - Updated dependencies [b710adb]
61
+ - Updated dependencies [ea7cf06]
62
+ - Updated dependencies [bbe4c4a]
63
+ - Updated dependencies [e4558a0]
64
+ - Updated dependencies [abf3421a75]
65
+ - Updated dependencies [543be9558e]
66
+ - Updated dependencies [14b712da84]
67
+ - @modern-js/utils@2.0.0-beta.3
68
+
3
69
  ## 2.0.0-beta.2
4
70
 
5
71
  ### Major Changes
@@ -1,19 +1,18 @@
1
1
  import { defaultPolyfill } from "./const";
2
- export default (() => ({
3
- name: '@modern-js/plugin-polyfill',
4
- setup: api => ({
5
- htmlPartials({
6
- entrypoint,
7
- partials
8
- }) {
2
+ var cli_default = () => ({
3
+ name: "@modern-js/plugin-polyfill",
4
+ setup: (api) => ({
5
+ htmlPartials({ entrypoint, partials }) {
9
6
  const resolvedConfig = api.useResolvedConfigContext();
10
- if (resolvedConfig.output.polyfill === 'ua') {
11
- partials.top.push(`<script src="${defaultPolyfill}" crossorigin></script>`);
7
+ if (resolvedConfig.output.polyfill === "ua") {
8
+ partials.top.push(
9
+ `<script src="${defaultPolyfill}" crossorigin><\/script>`
10
+ );
12
11
  }
13
- return {
14
- partials,
15
- entrypoint
16
- };
12
+ return { partials, entrypoint };
17
13
  }
18
14
  })
19
- }));
15
+ });
16
+ export {
17
+ cli_default as default
18
+ };
@@ -1,6 +1,8 @@
1
- export const defaultPolyfill = '/__polyfill__';
2
- export const getDefaultFeatures = () => ({
3
- es6: {
4
- flags: ['gated']
5
- }
6
- });
1
+ const defaultPolyfill = "/__polyfill__";
2
+ const getDefaultFeatures = () => ({
3
+ es6: { flags: ["gated"] }
4
+ });
5
+ export {
6
+ defaultPolyfill,
7
+ getDefaultFeatures
8
+ };
File without changes
@@ -1,32 +1,47 @@
1
- import { getPolyfillString } from '@modern-js/polyfill-lib';
2
- import { mime } from '@modern-js/utils';
3
- import Parser from 'ua-parser-js';
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import { getPolyfillString } from "@modern-js/polyfill-lib";
22
+ import { mime } from "@modern-js/utils";
23
+ import Parser from "ua-parser-js";
4
24
  import { getDefaultFeatures, defaultPolyfill } from "./const";
5
25
  import PolyfillCache, { generateCacheKey } from "./libs/cache";
6
- export default (() => ({
7
- name: '@modern-js/plugin-polyfill',
26
+ var src_default = () => ({
27
+ name: "@modern-js/plugin-polyfill",
8
28
  setup: () => ({
9
29
  beforeProdServer() {
10
30
  const cache = new PolyfillCache();
11
31
  const route = defaultPolyfill;
12
32
  const features = getDefaultFeatures();
13
- const minify = process.env.NODE_ENV === 'production';
14
- const featureDig = Object.keys(features).map(name => {
15
- const {
16
- flags = ['gated']
17
- } = features[name];
18
- const flagStr = flags.join(',');
33
+ const minify = process.env.NODE_ENV === "production";
34
+ const featureDig = Object.keys(features).map((name) => {
35
+ const { flags = ["gated"] } = features[name];
36
+ const flagStr = flags.join(",");
19
37
  return `${name}-${flagStr}`;
20
- }).join(',');
21
- return async (context, next) => {
38
+ }).join(",");
39
+ return (context, next) => __async(this, null, function* () {
22
40
  if (context.url !== route) {
23
41
  return next();
24
42
  }
25
- const parsedUA = Parser(context.headers['user-agent']);
26
- const {
27
- name = '',
28
- version = ''
29
- } = parsedUA.browser;
43
+ const parsedUA = Parser(context.headers["user-agent"]);
44
+ const { name = "", version = "" } = parsedUA.browser;
30
45
  const cacheKey = generateCacheKey({
31
46
  name,
32
47
  version,
@@ -35,18 +50,24 @@ export default (() => ({
35
50
  });
36
51
  const matched = cache.get(cacheKey);
37
52
  if (matched) {
38
- context.res.setHeader('content-type', mime.contentType('js'));
53
+ context.res.setHeader(
54
+ "content-type",
55
+ mime.contentType("js")
56
+ );
39
57
  return context.res.end(matched);
40
58
  }
41
- const polyfill = await getPolyfillString({
42
- uaString: context.headers['user-agent'],
59
+ const polyfill = yield getPolyfillString({
60
+ uaString: context.headers["user-agent"],
43
61
  minify,
44
62
  features
45
63
  });
46
64
  cache.set(cacheKey, polyfill);
47
- context.res.setHeader('content-type', mime.contentType('js'));
65
+ context.res.setHeader("content-type", mime.contentType("js"));
48
66
  return context.res.end(polyfill);
49
- };
67
+ });
50
68
  }
51
69
  })
52
- }));
70
+ });
71
+ export {
72
+ src_default as default
73
+ };
@@ -1,31 +1,24 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
- import crypto from 'crypto';
3
- import LRUCache from 'lru-cache';
1
+ import crypto from "crypto";
2
+ import LRUCache from "lru-cache";
4
3
  const KB = 1024;
5
4
  const MB = 1024 * KB;
6
- const keyCache = new LRUCache(10000);
7
- export const generateCacheKey = options => {
8
- const {
9
- name,
10
- version,
11
- features,
12
- minify
13
- } = options;
5
+ const keyCache = new LRUCache(1e4);
6
+ const generateCacheKey = (options) => {
7
+ const { name, version, features, minify } = options;
14
8
  const str = `${name}-${version}-${Number(minify)}-${features}`;
15
9
  const matched = keyCache.get(str);
16
10
  if (matched) {
17
11
  return matched;
18
12
  }
19
- const hash = crypto.createHmac('sha256', '^polyfill$').update(str).digest('hex');
13
+ const hash = crypto.createHmac("sha256", "^polyfill$").update(str).digest("hex");
20
14
  keyCache.set(str, hash);
21
15
  return hash;
22
16
  };
23
- export default class Cache {
17
+ class Cache {
24
18
  constructor() {
25
- _defineProperty(this, "caches", void 0);
26
19
  this.caches = new LRUCache({
27
20
  max: 200 * MB,
28
- length: v => v.length
21
+ length: (v) => v.length
29
22
  });
30
23
  }
31
24
  get(hash) {
@@ -34,4 +27,8 @@ export default class Cache {
34
27
  set(hash, content) {
35
28
  this.caches.set(hash, content);
36
29
  }
37
- }
30
+ }
31
+ export {
32
+ Cache as default,
33
+ generateCacheKey
34
+ };
@@ -1,26 +1,37 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var stdin_exports = {};
19
+ __export(stdin_exports, {
20
+ default: () => cli_default
5
21
  });
6
- exports.default = void 0;
7
- var _const = require("./const");
8
- var _default = () => ({
9
- name: '@modern-js/plugin-polyfill',
10
- setup: api => ({
11
- htmlPartials({
12
- entrypoint,
13
- partials
14
- }) {
22
+ module.exports = __toCommonJS(stdin_exports);
23
+ var import_const = require("./const");
24
+ var cli_default = () => ({
25
+ name: "@modern-js/plugin-polyfill",
26
+ setup: (api) => ({
27
+ htmlPartials({ entrypoint, partials }) {
15
28
  const resolvedConfig = api.useResolvedConfigContext();
16
- if (resolvedConfig.output.polyfill === 'ua') {
17
- partials.top.push(`<script src="${_const.defaultPolyfill}" crossorigin></script>`);
29
+ if (resolvedConfig.output.polyfill === "ua") {
30
+ partials.top.push(
31
+ `<script src="${import_const.defaultPolyfill}" crossorigin><\/script>`
32
+ );
18
33
  }
19
- return {
20
- partials,
21
- entrypoint
22
- };
34
+ return { partials, entrypoint };
23
35
  }
24
36
  })
25
37
  });
26
- exports.default = _default;
@@ -1,14 +1,27 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var stdin_exports = {};
19
+ __export(stdin_exports, {
20
+ defaultPolyfill: () => defaultPolyfill,
21
+ getDefaultFeatures: () => getDefaultFeatures
5
22
  });
6
- exports.getDefaultFeatures = exports.defaultPolyfill = void 0;
7
- const defaultPolyfill = '/__polyfill__';
8
- exports.defaultPolyfill = defaultPolyfill;
23
+ module.exports = __toCommonJS(stdin_exports);
24
+ const defaultPolyfill = "/__polyfill__";
9
25
  const getDefaultFeatures = () => ({
10
- es6: {
11
- flags: ['gated']
12
- }
26
+ es6: { flags: ["gated"] }
13
27
  });
14
- exports.getDefaultFeatures = getDefaultFeatures;
File without changes
@@ -1,42 +1,76 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var stdin_exports = {};
25
+ __export(stdin_exports, {
26
+ default: () => src_default
5
27
  });
6
- exports.default = void 0;
7
- var _polyfillLib = require("@modern-js/polyfill-lib");
8
- var _utils = require("@modern-js/utils");
9
- var _uaParserJs = _interopRequireDefault(require("ua-parser-js"));
10
- var _const = require("./const");
11
- var _cache = _interopRequireWildcard(require("./libs/cache"));
12
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
- var _default = () => ({
16
- name: '@modern-js/plugin-polyfill',
28
+ module.exports = __toCommonJS(stdin_exports);
29
+ var import_polyfill_lib = require("@modern-js/polyfill-lib");
30
+ var import_utils = require("@modern-js/utils");
31
+ var import_ua_parser_js = __toESM(require("ua-parser-js"));
32
+ var import_const = require("./const");
33
+ var import_cache = __toESM(require("./libs/cache"));
34
+ var __async = (__this, __arguments, generator) => {
35
+ return new Promise((resolve, reject) => {
36
+ var fulfilled = (value) => {
37
+ try {
38
+ step(generator.next(value));
39
+ } catch (e) {
40
+ reject(e);
41
+ }
42
+ };
43
+ var rejected = (value) => {
44
+ try {
45
+ step(generator.throw(value));
46
+ } catch (e) {
47
+ reject(e);
48
+ }
49
+ };
50
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
51
+ step((generator = generator.apply(__this, __arguments)).next());
52
+ });
53
+ };
54
+ var src_default = () => ({
55
+ name: "@modern-js/plugin-polyfill",
17
56
  setup: () => ({
18
57
  beforeProdServer() {
19
- const cache = new _cache.default();
20
- const route = _const.defaultPolyfill;
21
- const features = (0, _const.getDefaultFeatures)();
22
- const minify = process.env.NODE_ENV === 'production';
23
- const featureDig = Object.keys(features).map(name => {
24
- const {
25
- flags = ['gated']
26
- } = features[name];
27
- const flagStr = flags.join(',');
58
+ const cache = new import_cache.default();
59
+ const route = import_const.defaultPolyfill;
60
+ const features = (0, import_const.getDefaultFeatures)();
61
+ const minify = process.env.NODE_ENV === "production";
62
+ const featureDig = Object.keys(features).map((name) => {
63
+ const { flags = ["gated"] } = features[name];
64
+ const flagStr = flags.join(",");
28
65
  return `${name}-${flagStr}`;
29
- }).join(',');
30
- return async (context, next) => {
66
+ }).join(",");
67
+ return (context, next) => __async(this, null, function* () {
31
68
  if (context.url !== route) {
32
69
  return next();
33
70
  }
34
- const parsedUA = (0, _uaParserJs.default)(context.headers['user-agent']);
35
- const {
36
- name = '',
37
- version = ''
38
- } = parsedUA.browser;
39
- const cacheKey = (0, _cache.generateCacheKey)({
71
+ const parsedUA = (0, import_ua_parser_js.default)(context.headers["user-agent"]);
72
+ const { name = "", version = "" } = parsedUA.browser;
73
+ const cacheKey = (0, import_cache.generateCacheKey)({
40
74
  name,
41
75
  version,
42
76
  features: featureDig,
@@ -44,19 +78,21 @@ var _default = () => ({
44
78
  });
45
79
  const matched = cache.get(cacheKey);
46
80
  if (matched) {
47
- context.res.setHeader('content-type', _utils.mime.contentType('js'));
81
+ context.res.setHeader(
82
+ "content-type",
83
+ import_utils.mime.contentType("js")
84
+ );
48
85
  return context.res.end(matched);
49
86
  }
50
- const polyfill = await (0, _polyfillLib.getPolyfillString)({
51
- uaString: context.headers['user-agent'],
87
+ const polyfill = yield (0, import_polyfill_lib.getPolyfillString)({
88
+ uaString: context.headers["user-agent"],
52
89
  minify,
53
90
  features
54
91
  });
55
92
  cache.set(cacheKey, polyfill);
56
- context.res.setHeader('content-type', _utils.mime.contentType('js'));
93
+ context.res.setHeader("content-type", import_utils.mime.contentType("js"));
57
94
  return context.res.end(polyfill);
58
- };
95
+ });
59
96
  }
60
97
  })
61
98
  });
62
- exports.default = _default;
@@ -1,39 +1,53 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+ var stdin_exports = {};
25
+ __export(stdin_exports, {
26
+ default: () => Cache,
27
+ generateCacheKey: () => generateCacheKey
5
28
  });
6
- exports.generateCacheKey = exports.default = void 0;
7
- var _crypto = _interopRequireDefault(require("crypto"));
8
- var _lruCache = _interopRequireDefault(require("lru-cache"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
29
+ module.exports = __toCommonJS(stdin_exports);
30
+ var import_crypto = __toESM(require("crypto"));
31
+ var import_lru_cache = __toESM(require("lru-cache"));
11
32
  const KB = 1024;
12
33
  const MB = 1024 * KB;
13
- const keyCache = new _lruCache.default(10000);
14
- const generateCacheKey = options => {
15
- const {
16
- name,
17
- version,
18
- features,
19
- minify
20
- } = options;
34
+ const keyCache = new import_lru_cache.default(1e4);
35
+ const generateCacheKey = (options) => {
36
+ const { name, version, features, minify } = options;
21
37
  const str = `${name}-${version}-${Number(minify)}-${features}`;
22
38
  const matched = keyCache.get(str);
23
39
  if (matched) {
24
40
  return matched;
25
41
  }
26
- const hash = _crypto.default.createHmac('sha256', '^polyfill$').update(str).digest('hex');
42
+ const hash = import_crypto.default.createHmac("sha256", "^polyfill$").update(str).digest("hex");
27
43
  keyCache.set(str, hash);
28
44
  return hash;
29
45
  };
30
- exports.generateCacheKey = generateCacheKey;
31
46
  class Cache {
32
47
  constructor() {
33
- _defineProperty(this, "caches", void 0);
34
- this.caches = new _lruCache.default({
48
+ this.caches = new import_lru_cache.default({
35
49
  max: 200 * MB,
36
- length: v => v.length
50
+ length: (v) => v.length
37
51
  });
38
52
  }
39
53
  get(hash) {
@@ -43,4 +57,3 @@ class Cache {
43
57
  this.caches.set(hash, content);
44
58
  }
45
59
  }
46
- exports.default = Cache;
@@ -0,0 +1,22 @@
1
+ import { defaultPolyfill } from "./const";
2
+ var cli_default = 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: partials,
15
+ entrypoint: entrypoint
16
+ };
17
+ }
18
+ };
19
+ }
20
+ };
21
+ };
22
+ export { cli_default as default };
@@ -0,0 +1,11 @@
1
+ var defaultPolyfill = "/__polyfill__";
2
+ var getDefaultFeatures = function() {
3
+ return {
4
+ es6: {
5
+ flags: [
6
+ "gated"
7
+ ]
8
+ }
9
+ };
10
+ };
11
+ export { defaultPolyfill, getDefaultFeatures };
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,203 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _asyncToGenerator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ var __generator = this && this.__generator || function(thisArg, body) {
31
+ var f, y, t, g, _ = {
32
+ label: 0,
33
+ sent: function() {
34
+ if (t[0] & 1) throw t[1];
35
+ return t[1];
36
+ },
37
+ trys: [],
38
+ ops: []
39
+ };
40
+ return(g = {
41
+ next: verb(0),
42
+ "throw": verb(1),
43
+ "return": verb(2)
44
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
45
+ return this;
46
+ }), g);
47
+ function verb(n) {
48
+ return function(v) {
49
+ return step([
50
+ n,
51
+ v
52
+ ]);
53
+ };
54
+ }
55
+ function step(op) {
56
+ if (f) throw new TypeError("Generator is already executing.");
57
+ while(_)try {
58
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
59
+ if (y = 0, t) op = [
60
+ op[0] & 2,
61
+ t.value
62
+ ];
63
+ switch(op[0]){
64
+ case 0:
65
+ case 1:
66
+ t = op;
67
+ break;
68
+ case 4:
69
+ _.label++;
70
+ return {
71
+ value: op[1],
72
+ done: false
73
+ };
74
+ case 5:
75
+ _.label++;
76
+ y = op[1];
77
+ op = [
78
+ 0
79
+ ];
80
+ continue;
81
+ case 7:
82
+ op = _.ops.pop();
83
+ _.trys.pop();
84
+ continue;
85
+ default:
86
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
87
+ _ = 0;
88
+ continue;
89
+ }
90
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
91
+ _.label = op[1];
92
+ break;
93
+ }
94
+ if (op[0] === 6 && _.label < t[1]) {
95
+ _.label = t[1];
96
+ t = op;
97
+ break;
98
+ }
99
+ if (t && _.label < t[2]) {
100
+ _.label = t[2];
101
+ _.ops.push(op);
102
+ break;
103
+ }
104
+ if (t[2]) _.ops.pop();
105
+ _.trys.pop();
106
+ continue;
107
+ }
108
+ op = body.call(thisArg, _);
109
+ } catch (e) {
110
+ op = [
111
+ 6,
112
+ e
113
+ ];
114
+ y = 0;
115
+ } finally{
116
+ f = t = 0;
117
+ }
118
+ if (op[0] & 5) throw op[1];
119
+ return {
120
+ value: op[0] ? op[1] : void 0,
121
+ done: true
122
+ };
123
+ }
124
+ };
125
+ import { getPolyfillString } from "@modern-js/polyfill-lib";
126
+ import { mime } from "@modern-js/utils";
127
+ import Parser from "ua-parser-js";
128
+ import { getDefaultFeatures, defaultPolyfill } from "./const";
129
+ import PolyfillCache, { generateCacheKey } from "./libs/cache";
130
+ var src_default = function() {
131
+ return {
132
+ name: "@modern-js/plugin-polyfill",
133
+ setup: function() {
134
+ return {
135
+ beforeProdServer: function beforeProdServer() {
136
+ var cache = new PolyfillCache();
137
+ var route = defaultPolyfill;
138
+ var features = getDefaultFeatures();
139
+ var minify = process.env.NODE_ENV === "production";
140
+ var featureDig = Object.keys(features).map(function(name) {
141
+ var _name = features[name], _flags = _name.flags, flags = _flags === void 0 ? [
142
+ "gated"
143
+ ] : _flags;
144
+ var flagStr = flags.join(",");
145
+ return "".concat(name, "-").concat(flagStr);
146
+ }).join(",");
147
+ return function() {
148
+ var _ref = _asyncToGenerator(function(context, next) {
149
+ var parsedUA, _browser, _name, name, _version, version, cacheKey, matched, polyfill;
150
+ return __generator(this, function(_state) {
151
+ switch(_state.label){
152
+ case 0:
153
+ if (context.url !== route) {
154
+ return [
155
+ 2,
156
+ next()
157
+ ];
158
+ }
159
+ parsedUA = Parser(context.headers["user-agent"]);
160
+ _browser = parsedUA.browser, _name = _browser.name, name = _name === void 0 ? "" : _name, _version = _browser.version, version = _version === void 0 ? "" : _version;
161
+ cacheKey = generateCacheKey({
162
+ name: name,
163
+ version: version,
164
+ features: featureDig,
165
+ minify: minify
166
+ });
167
+ matched = cache.get(cacheKey);
168
+ if (matched) {
169
+ context.res.setHeader("content-type", mime.contentType("js"));
170
+ return [
171
+ 2,
172
+ context.res.end(matched)
173
+ ];
174
+ }
175
+ return [
176
+ 4,
177
+ getPolyfillString({
178
+ uaString: context.headers["user-agent"],
179
+ minify: minify,
180
+ features: features
181
+ })
182
+ ];
183
+ case 1:
184
+ polyfill = _state.sent();
185
+ cache.set(cacheKey, polyfill);
186
+ context.res.setHeader("content-type", mime.contentType("js"));
187
+ return [
188
+ 2,
189
+ context.res.end(polyfill)
190
+ ];
191
+ }
192
+ });
193
+ });
194
+ return function(context, next) {
195
+ return _ref.apply(this, arguments);
196
+ };
197
+ }();
198
+ }
199
+ };
200
+ }
201
+ };
202
+ };
203
+ export { src_default as default };
@@ -0,0 +1,77 @@
1
+ function _classCallCheck(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
+ }
6
+ function _defineProperties(target, props) {
7
+ for(var i = 0; i < props.length; i++){
8
+ var descriptor = props[i];
9
+ descriptor.enumerable = descriptor.enumerable || false;
10
+ descriptor.configurable = true;
11
+ if ("value" in descriptor) descriptor.writable = true;
12
+ Object.defineProperty(target, descriptor.key, descriptor);
13
+ }
14
+ }
15
+ function _createClass(Constructor, protoProps, staticProps) {
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
+ }
20
+ function _defineProperty(obj, key, value) {
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value: value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
27
+ });
28
+ } else {
29
+ obj[key] = value;
30
+ }
31
+ return obj;
32
+ }
33
+ import crypto from "crypto";
34
+ import LRUCache from "lru-cache";
35
+ var KB = 1024;
36
+ var MB = 1024 * KB;
37
+ var keyCache = new LRUCache(1e4);
38
+ var generateCacheKey = function(options) {
39
+ var name = options.name, version = options.version, features = options.features, minify = options.minify;
40
+ var str = "".concat(name, "-").concat(version, "-").concat(Number(minify), "-").concat(features);
41
+ var matched = keyCache.get(str);
42
+ if (matched) {
43
+ return matched;
44
+ }
45
+ var hash = crypto.createHmac("sha256", "^polyfill$").update(str).digest("hex");
46
+ keyCache.set(str, hash);
47
+ return hash;
48
+ };
49
+ var Cache = /*#__PURE__*/ function() {
50
+ "use strict";
51
+ function Cache() {
52
+ _classCallCheck(this, Cache);
53
+ _defineProperty(this, "caches", void 0);
54
+ this.caches = new LRUCache({
55
+ max: 200 * MB,
56
+ length: function(v) {
57
+ return v.length;
58
+ }
59
+ });
60
+ }
61
+ _createClass(Cache, [
62
+ {
63
+ key: "get",
64
+ value: function get(hash) {
65
+ return this.caches.get(hash);
66
+ }
67
+ },
68
+ {
69
+ key: "set",
70
+ value: function set(hash, content) {
71
+ this.caches.set(hash, content);
72
+ }
73
+ }
74
+ ]);
75
+ return Cache;
76
+ }();
77
+ export { Cache as default, generateCacheKey };
@@ -0,0 +1 @@
1
+ "use strict";
@@ -1,7 +1,9 @@
1
1
  import type { CliPlugin } from '@modern-js/core';
2
2
  import type { UserConfig, ServerOptions } from '@modern-js/server-core';
3
+
3
4
  declare const _default: () => CliPlugin<{
4
5
  userConfig: UserConfig;
5
6
  normalizedConfig: ServerOptions;
6
7
  }>;
8
+
7
9
  export default _default;
@@ -1,3 +1,5 @@
1
1
  import type { ServerPlugin } from '@modern-js/server-core';
2
+
2
3
  declare const _default: () => ServerPlugin;
4
+
3
5
  export default _default;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.0.0-beta.2",
14
+ "version": "2.0.0-beta.4",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -40,7 +40,7 @@
40
40
  "@modern-js/polyfill-lib": "^1.0.0",
41
41
  "lru-cache": "^6.0.0",
42
42
  "ua-parser-js": "^0.7.28",
43
- "@modern-js/utils": "2.0.0-beta.2"
43
+ "@modern-js/utils": "2.0.0-beta.4"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/jest": "^27",
@@ -49,18 +49,13 @@
49
49
  "@types/ua-parser-js": "^0.7.36",
50
50
  "typescript": "^4",
51
51
  "jest": "^27",
52
- "@modern-js/server-core": "2.0.0-beta.2",
53
- "@modern-js/types": "2.0.0-beta.2",
54
- "@modern-js/core": "2.0.0-beta.2",
55
- "@scripts/build": "2.0.0-beta.2",
56
- "@scripts/jest-config": "2.0.0-beta.2"
52
+ "@modern-js/server-core": "2.0.0-beta.4",
53
+ "@modern-js/types": "2.0.0-beta.4",
54
+ "@modern-js/core": "2.0.0-beta.4",
55
+ "@scripts/build": "2.0.0-beta.4",
56
+ "@scripts/jest-config": "2.0.0-beta.4"
57
57
  },
58
58
  "sideEffects": false,
59
- "modernConfig": {
60
- "output": {
61
- "packageMode": "node-js"
62
- }
63
- },
64
59
  "publishConfig": {
65
60
  "registry": "https://registry.npmjs.org/",
66
61
  "access": "public"