@modern-js/plugin-polyfill 2.0.0-beta.0 → 2.0.0-beta.1

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,31 @@
1
1
  # @modern-js/plugin-polyfill
2
2
 
3
+ ## 2.0.0-beta.1
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - ebbeed1: fix(plugin-polyfill): failed to join flags when restart CLI
12
+
13
+ fix(plugin-polyfill): 修复重启 CLI 时报错 join flags 失败的问题
14
+
15
+ - Updated dependencies [92f0ead]
16
+ - Updated dependencies [edd1cfb1af]
17
+ - Updated dependencies [cc971eabfc]
18
+ - Updated dependencies [5b9049f]
19
+ - Updated dependencies [92004d1]
20
+ - Updated dependencies [b8bbe036c7]
21
+ - Updated dependencies [d5a31df781]
22
+ - Updated dependencies [dda38c9]
23
+ - Updated dependencies [3bbea92b2a]
24
+ - Updated dependencies [abf3421]
25
+ - Updated dependencies [543be9558e]
26
+ - Updated dependencies [14b712d]
27
+ - @modern-js/utils@2.0.0-beta.1
28
+
3
29
  ## 2.0.0-beta.0
4
30
 
5
31
  ### Major Changes
@@ -7,16 +7,13 @@ export default (() => ({
7
7
  partials
8
8
  }) {
9
9
  const resolvedConfig = api.useResolvedConfigContext();
10
-
11
10
  if (resolvedConfig.output.polyfill === 'ua') {
12
11
  partials.top.push(`<script src="${defaultPolyfill}" crossorigin></script>`);
13
12
  }
14
-
15
13
  return {
16
14
  partials,
17
15
  entrypoint
18
16
  };
19
17
  }
20
-
21
18
  })
22
19
  }));
@@ -1,6 +1,6 @@
1
1
  export const defaultPolyfill = '/__polyfill__';
2
- export const defaultFeatures = {
2
+ export const getDefaultFeatures = () => ({
3
3
  es6: {
4
4
  flags: ['gated']
5
5
  }
6
- };
6
+ });
@@ -1,7 +1,7 @@
1
1
  import { getPolyfillString } from '@modern-js/polyfill-lib';
2
2
  import { mime } from '@modern-js/utils';
3
3
  import Parser from 'ua-parser-js';
4
- import { defaultFeatures, defaultPolyfill } from "./const";
4
+ import { getDefaultFeatures, defaultPolyfill } from "./const";
5
5
  import PolyfillCache, { generateCacheKey } from "./libs/cache";
6
6
  export default (() => ({
7
7
  name: '@modern-js/plugin-polyfill',
@@ -9,7 +9,7 @@ export default (() => ({
9
9
  beforeProdServer() {
10
10
  const cache = new PolyfillCache();
11
11
  const route = defaultPolyfill;
12
- const features = defaultFeatures;
12
+ const features = getDefaultFeatures();
13
13
  const minify = process.env.NODE_ENV === 'production';
14
14
  const featureDig = Object.keys(features).map(name => {
15
15
  const {
@@ -22,7 +22,6 @@ export default (() => ({
22
22
  if (context.url !== route) {
23
23
  return next();
24
24
  }
25
-
26
25
  const parsedUA = Parser(context.headers['user-agent']);
27
26
  const {
28
27
  name = '',
@@ -35,12 +34,10 @@ export default (() => ({
35
34
  minify
36
35
  });
37
36
  const matched = cache.get(cacheKey);
38
-
39
37
  if (matched) {
40
38
  context.res.setHeader('content-type', mime.contentType('js'));
41
39
  return context.res.end(matched);
42
40
  }
43
-
44
41
  const polyfill = await getPolyfillString({
45
42
  uaString: context.headers['user-agent'],
46
43
  minify,
@@ -51,6 +48,5 @@ export default (() => ({
51
48
  return context.res.end(polyfill);
52
49
  };
53
50
  }
54
-
55
51
  })
56
52
  }));
@@ -1,5 +1,4 @@
1
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
-
3
2
  import crypto from 'crypto';
4
3
  import LRUCache from 'lru-cache';
5
4
  const KB = 1024;
@@ -14,11 +13,9 @@ export const generateCacheKey = options => {
14
13
  } = options;
15
14
  const str = `${name}-${version}-${Number(minify)}-${features}`;
16
15
  const matched = keyCache.get(str);
17
-
18
16
  if (matched) {
19
17
  return matched;
20
18
  }
21
-
22
19
  const hash = crypto.createHmac('sha256', '^polyfill$').update(str).digest('hex');
23
20
  keyCache.set(str, hash);
24
21
  return hash;
@@ -26,19 +23,15 @@ export const generateCacheKey = options => {
26
23
  export default class Cache {
27
24
  constructor() {
28
25
  _defineProperty(this, "caches", void 0);
29
-
30
26
  this.caches = new LRUCache({
31
27
  max: 200 * MB,
32
28
  length: v => v.length
33
29
  });
34
30
  }
35
-
36
31
  get(hash) {
37
32
  return this.caches.get(hash);
38
33
  }
39
-
40
34
  set(hash, content) {
41
35
  this.caches.set(hash, content);
42
36
  }
43
-
44
37
  }
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _const = require("./const");
9
-
10
8
  var _default = () => ({
11
9
  name: '@modern-js/plugin-polyfill',
12
10
  setup: api => ({
@@ -15,18 +13,14 @@ var _default = () => ({
15
13
  partials
16
14
  }) {
17
15
  const resolvedConfig = api.useResolvedConfigContext();
18
-
19
16
  if (resolvedConfig.output.polyfill === 'ua') {
20
17
  partials.top.push(`<script src="${_const.defaultPolyfill}" crossorigin></script>`);
21
18
  }
22
-
23
19
  return {
24
20
  partials,
25
21
  entrypoint
26
22
  };
27
23
  }
28
-
29
24
  })
30
25
  });
31
-
32
26
  exports.default = _default;
@@ -3,12 +3,12 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.defaultPolyfill = exports.defaultFeatures = void 0;
6
+ exports.getDefaultFeatures = exports.defaultPolyfill = void 0;
7
7
  const defaultPolyfill = '/__polyfill__';
8
8
  exports.defaultPolyfill = defaultPolyfill;
9
- const defaultFeatures = {
9
+ const getDefaultFeatures = () => ({
10
10
  es6: {
11
11
  flags: ['gated']
12
12
  }
13
- };
14
- exports.defaultFeatures = defaultFeatures;
13
+ });
14
+ exports.getDefaultFeatures = getDefaultFeatures;
@@ -4,30 +4,21 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _polyfillLib = require("@modern-js/polyfill-lib");
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  var _uaParserJs = _interopRequireDefault(require("ua-parser-js"));
13
-
14
10
  var _const = require("./const");
15
-
16
11
  var _cache = _interopRequireWildcard(require("./libs/cache"));
17
-
18
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); }
19
-
20
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; }
21
-
22
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
15
  var _default = () => ({
25
16
  name: '@modern-js/plugin-polyfill',
26
17
  setup: () => ({
27
18
  beforeProdServer() {
28
19
  const cache = new _cache.default();
29
20
  const route = _const.defaultPolyfill;
30
- const features = _const.defaultFeatures;
21
+ const features = (0, _const.getDefaultFeatures)();
31
22
  const minify = process.env.NODE_ENV === 'production';
32
23
  const featureDig = Object.keys(features).map(name => {
33
24
  const {
@@ -40,7 +31,6 @@ var _default = () => ({
40
31
  if (context.url !== route) {
41
32
  return next();
42
33
  }
43
-
44
34
  const parsedUA = (0, _uaParserJs.default)(context.headers['user-agent']);
45
35
  const {
46
36
  name = '',
@@ -53,12 +43,10 @@ var _default = () => ({
53
43
  minify
54
44
  });
55
45
  const matched = cache.get(cacheKey);
56
-
57
46
  if (matched) {
58
47
  context.res.setHeader('content-type', _utils.mime.contentType('js'));
59
48
  return context.res.end(matched);
60
49
  }
61
-
62
50
  const polyfill = await (0, _polyfillLib.getPolyfillString)({
63
51
  uaString: context.headers['user-agent'],
64
52
  minify,
@@ -69,8 +57,6 @@ var _default = () => ({
69
57
  return context.res.end(polyfill);
70
58
  };
71
59
  }
72
-
73
60
  })
74
61
  });
75
-
76
62
  exports.default = _default;
@@ -4,19 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.generateCacheKey = exports.default = void 0;
7
-
8
7
  var _crypto = _interopRequireDefault(require("crypto"));
9
-
10
8
  var _lruCache = _interopRequireDefault(require("lru-cache"));
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
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; }
15
-
16
11
  const KB = 1024;
17
12
  const MB = 1024 * KB;
18
13
  const keyCache = new _lruCache.default(10000);
19
-
20
14
  const generateCacheKey = options => {
21
15
  const {
22
16
  name,
@@ -26,37 +20,27 @@ const generateCacheKey = options => {
26
20
  } = options;
27
21
  const str = `${name}-${version}-${Number(minify)}-${features}`;
28
22
  const matched = keyCache.get(str);
29
-
30
23
  if (matched) {
31
24
  return matched;
32
25
  }
33
-
34
26
  const hash = _crypto.default.createHmac('sha256', '^polyfill$').update(str).digest('hex');
35
-
36
27
  keyCache.set(str, hash);
37
28
  return hash;
38
29
  };
39
-
40
30
  exports.generateCacheKey = generateCacheKey;
41
-
42
31
  class Cache {
43
32
  constructor() {
44
33
  _defineProperty(this, "caches", void 0);
45
-
46
34
  this.caches = new _lruCache.default({
47
35
  max: 200 * MB,
48
36
  length: v => v.length
49
37
  });
50
38
  }
51
-
52
39
  get(hash) {
53
40
  return this.caches.get(hash);
54
41
  }
55
-
56
42
  set(hash, content) {
57
43
  this.caches.set(hash, content);
58
44
  }
59
-
60
45
  }
61
-
62
46
  exports.default = Cache;
@@ -1,5 +1,3 @@
1
1
  import type { CliPlugin } from '@modern-js/core';
2
-
3
2
  declare const _default: () => CliPlugin;
4
-
5
3
  export default _default;
@@ -1,4 +1,4 @@
1
1
  export declare const defaultPolyfill = "/__polyfill__";
2
- export declare const defaultFeatures: Record<string, {
2
+ export declare const getDefaultFeatures: () => Record<string, {
3
3
  flags: string[];
4
4
  }>;
@@ -1,5 +1,3 @@
1
1
  import type { ServerPlugin } from '@modern-js/server-core';
2
-
3
2
  declare const _default: () => ServerPlugin;
4
-
5
3
  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.0",
14
+ "version": "2.0.0-beta.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -37,23 +37,23 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@babel/runtime": "^7.18.0",
40
- "@modern-js/utils": "2.0.0-beta.0",
41
40
  "@modern-js/polyfill-lib": "^1.0.0",
42
41
  "lru-cache": "^6.0.0",
43
- "ua-parser-js": "^0.7.28"
42
+ "ua-parser-js": "^0.7.28",
43
+ "@modern-js/utils": "2.0.0-beta.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@modern-js/server-core": "2.0.0-beta.0",
47
- "@modern-js/types": "2.0.0-beta.0",
48
46
  "@types/jest": "^27",
49
47
  "@types/lru-cache": "^5.1.1",
50
48
  "@types/node": "^14",
51
49
  "@types/ua-parser-js": "^0.7.36",
52
50
  "typescript": "^4",
53
- "@modern-js/core": "2.0.0-beta.0",
54
- "@scripts/build": "2.0.0-beta.0",
55
51
  "jest": "^27",
56
- "@scripts/jest-config": "2.0.0-beta.0"
52
+ "@modern-js/server-core": "2.0.0-beta.1",
53
+ "@modern-js/types": "2.0.0-beta.1",
54
+ "@modern-js/core": "2.0.0-beta.1",
55
+ "@scripts/build": "2.0.0-beta.1",
56
+ "@scripts/jest-config": "2.0.0-beta.1"
57
57
  },
58
58
  "sideEffects": false,
59
59
  "modernConfig": {