@modern-js/plugin-polyfill 1.2.0 → 1.2.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,16 @@
1
1
  # @modern-js/plugin-polyfill
2
2
 
3
+ ## 1.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+ - Updated dependencies [83166714]
9
+ - Updated dependencies [c3de9882]
10
+ - Updated dependencies [33ff48af]
11
+ - @modern-js/core@1.3.2
12
+ - @modern-js/server-plugin@1.2.1
13
+
3
14
  ## 1.2.0
4
15
 
5
16
  ### Minor Changes
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.0",
14
+ "version": "1.2.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -38,13 +38,13 @@
38
38
  "dependencies": {
39
39
  "@babel/runtime": "^7",
40
40
  "@modern-js/polyfill-lib": "^1.0.0",
41
- "@modern-js/server-plugin": "^1.2.0",
41
+ "@modern-js/server-plugin": "^1.2.1",
42
42
  "lru-cache": "^6.0.0",
43
43
  "mime-types": "^2.1.32",
44
44
  "ua-parser-js": "^0.7.28"
45
45
  },
46
46
  "devDependencies": {
47
- "@modern-js/types": "^1.2.0",
47
+ "@modern-js/types": "^1.2.1",
48
48
  "@types/jest": "^26",
49
49
  "@types/lru-cache": "^5.1.1",
50
50
  "@types/mime-types": "^2.1.1",
@@ -53,13 +53,13 @@
53
53
  "@types/react-dom": "^17",
54
54
  "@types/ua-parser-js": "^0.7.36",
55
55
  "typescript": "^4",
56
- "@modern-js/core": "^1.3.0",
56
+ "@modern-js/core": "^1.3.2",
57
57
  "@scripts/build": "0.0.0",
58
58
  "jest": "^27",
59
59
  "@scripts/jest-config": "0.0.0"
60
60
  },
61
61
  "peerDependencies": {
62
- "@modern-js/core": "^1.3.0"
62
+ "@modern-js/core": "^1.3.2"
63
63
  },
64
64
  "sideEffects": false,
65
65
  "modernConfig": {
package/src/cli.ts DELETED
@@ -1,20 +0,0 @@
1
- import { createPlugin, useResolvedConfigContext } from '@modern-js/core';
2
- import { defaultPolyfill } from './const';
3
-
4
- export default createPlugin(
5
- () =>
6
- ({
7
- htmlPartials({ entrypoint, partials }: any) {
8
- // eslint-disable-next-line react-hooks/rules-of-hooks
9
- const resolvedConfig = useResolvedConfigContext();
10
- if (resolvedConfig.output.polyfill === 'ua') {
11
- partials.top.push(
12
- `<script src="${defaultPolyfill}" crossorigin></script>`,
13
- );
14
- }
15
-
16
- return { partials, entrypoint };
17
- },
18
- } as any),
19
- { name: '@modern-js/plugin-polyfill' },
20
- );
package/src/const.ts DELETED
@@ -1,5 +0,0 @@
1
- export const defaultPolyfill = '/__polyfill__';
2
-
3
- export const defaultFeatures: Record<string, { flags: string[] }> = {
4
- es6: { flags: ['gated'] },
5
- };
package/src/global.d.ts DELETED
@@ -1,7 +0,0 @@
1
- declare module '@modern-js/polyfill-lib' {
2
- export const getPolyfillString: (options: {
3
- uaString: string;
4
- minify: boolean;
5
- features: Record<string, { flags: string[] }>;
6
- }) => Promise<string>;
7
- }
package/src/index.ts DELETED
@@ -1,66 +0,0 @@
1
- import { createPlugin } from '@modern-js/server-plugin';
2
- import { NextFunction, ModernServerContext } from '@modern-js/types/server';
3
- import type { NormalizedConfig } from '@modern-js/core';
4
- import { getPolyfillString } from '@modern-js/polyfill-lib';
5
- import mime from 'mime-types';
6
- import Parser from 'ua-parser-js';
7
- import { defaultFeatures, defaultPolyfill } from './const';
8
- import PolyfillCache, { generateCacheKey } from './libs/cache';
9
-
10
- export default createPlugin(
11
- () => ({
12
- preServerInit(_: NormalizedConfig) {
13
- const cache = new PolyfillCache();
14
- const route = defaultPolyfill;
15
- const features = defaultFeatures;
16
- const minify = process.env.NODE_ENV === 'production';
17
-
18
- const featureDig = Object.keys(features)
19
- .map(name => {
20
- const { flags = ['gated'] } = features[name];
21
- const flagStr = flags.join(',');
22
-
23
- return `${name}-${flagStr}`;
24
- })
25
- .join(',');
26
-
27
- return async (context: ModernServerContext, next: NextFunction) => {
28
- if (context.url !== route) {
29
- return next();
30
- }
31
-
32
- const parsedUA = Parser(context.headers['user-agent']);
33
- const { name = '', version = '' } = parsedUA.browser;
34
-
35
- const cacheKey = generateCacheKey({
36
- name,
37
- version,
38
- features: featureDig,
39
- minify,
40
- });
41
- const matched = cache.get(cacheKey);
42
- if (matched) {
43
- context.res.setHeader(
44
- 'content-type',
45
- mime.contentType('js') as string,
46
- );
47
- return context.res.end(matched);
48
- }
49
-
50
- const polyfill = await getPolyfillString({
51
- uaString: context.headers['user-agent'] as string,
52
- minify,
53
- features,
54
- });
55
-
56
- cache.set(cacheKey, polyfill);
57
-
58
- context.res.setHeader('content-type', mime.contentType('js') as string);
59
- return context.res.end(polyfill);
60
- };
61
- },
62
- }),
63
- {
64
- name: '@modern-js/plugin-polyfill',
65
- },
66
- ) as any;
package/src/libs/cache.ts DELETED
@@ -1,49 +0,0 @@
1
- import crypto from 'crypto';
2
- import LRUCache from 'lru-cache';
3
-
4
- type CacheQueryOptions = {
5
- features: string;
6
- minify: boolean;
7
- name: string;
8
- version: string;
9
- };
10
-
11
- const KB = 1024;
12
- const MB = 1024 * KB;
13
-
14
- const keyCache = new LRUCache<string, string>(10000);
15
- export const generateCacheKey = (options: CacheQueryOptions) => {
16
- const { name, version, features, minify } = options;
17
-
18
- const str = `${name}-${version}-${Number(minify)}-${features}`;
19
- const matched = keyCache.get(str);
20
- if (matched) {
21
- return matched;
22
- }
23
-
24
- const hash = crypto
25
- .createHmac('sha256', '^polyfill$')
26
- .update(str)
27
- .digest('hex');
28
- keyCache.set(str, hash);
29
- return hash;
30
- };
31
-
32
- export default class Cache {
33
- private readonly caches: LRUCache<string, string>;
34
-
35
- constructor() {
36
- this.caches = new LRUCache({
37
- max: 200 * MB,
38
- length: v => v.length,
39
- });
40
- }
41
-
42
- public get(hash: string) {
43
- return this.caches.get(hash);
44
- }
45
-
46
- public set(hash: string, content: string) {
47
- this.caches.set(hash, content);
48
- }
49
- }
package/src/type.ts DELETED
@@ -1,7 +0,0 @@
1
- export type ServerPolyfill = {
2
- polyfill: {
3
- route: string;
4
- features: Record<string, { flags: string[] }>;
5
- minify: boolean;
6
- };
7
- };