@modern-js/plugin-polyfill 1.1.1 → 1.2.2

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,43 @@
1
1
  # @modern-js/plugin-polyfill
2
2
 
3
+ ## 1.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 272cab15: refactor server plugin manager
8
+ - Updated dependencies [d9cc5ea9]
9
+ - Updated dependencies [bd819a8d]
10
+ - Updated dependencies [d099e5c5]
11
+ - Updated dependencies [bada2879]
12
+ - Updated dependencies [24f616ca]
13
+ - Updated dependencies [272cab15]
14
+ - @modern-js/core@1.4.0
15
+ - @modern-js/server-core@1.2.2
16
+
17
+ ## 1.2.1
18
+
19
+ ### Patch Changes
20
+
21
+ - 83166714: change .npmignore
22
+ - Updated dependencies [83166714]
23
+ - Updated dependencies [c3de9882]
24
+ - Updated dependencies [33ff48af]
25
+ - @modern-js/core@1.3.2
26
+ - @modern-js/server-plugin@1.2.1
27
+
28
+ ## 1.2.0
29
+
30
+ ### Minor Changes
31
+
32
+ - cfe11628: Make Modern.js self bootstraping
33
+
34
+ ### Patch Changes
35
+
36
+ - Updated dependencies [fc71e36f]
37
+ - Updated dependencies [cfe11628]
38
+ - @modern-js/core@1.3.0
39
+ - @modern-js/server-plugin@1.2.0
40
+
3
41
  ## 1.1.1
4
42
 
5
43
  ### Patch Changes
@@ -1,4 +1,4 @@
1
- import { createPlugin } from '@modern-js/server-plugin';
1
+ import { createPlugin } from '@modern-js/server-core';
2
2
  import { getPolyfillString } from '@modern-js/polyfill-lib';
3
3
  import mime from 'mime-types';
4
4
  import Parser from 'ua-parser-js';
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _serverPlugin = require("@modern-js/server-plugin");
8
+ var _serverCore = require("@modern-js/server-core");
9
9
 
10
10
  var _polyfillLib = require("@modern-js/polyfill-lib");
11
11
 
@@ -23,7 +23,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
23
23
 
24
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
25
 
26
- var _default = (0, _serverPlugin.createPlugin)(() => ({
26
+ var _default = (0, _serverCore.createPlugin)(() => ({
27
27
  preServerInit(_) {
28
28
  const cache = new _cache.default();
29
29
  const route = _const.defaultPolyfill;
@@ -1,5 +1,8 @@
1
1
  declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@modern-js/core").Progresses2Threads<{
2
2
  config: import("@modern-js/core").ParallelWorkflow<void, unknown>;
3
+ resolvedConfig: import("@modern-js/core").AsyncWaterfall<{
4
+ resolved: import("@modern-js/core").NormalizedConfig;
5
+ }>;
3
6
  validateSchema: import("@modern-js/core").ParallelWorkflow<void, unknown>;
4
7
  prepare: import("@modern-js/core").AsyncWorkflow<void, void>;
5
8
  commands: import("@modern-js/core").AsyncWorkflow<{
@@ -8,8 +11,10 @@ declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@m
8
11
  watchFiles: import("@modern-js/core").ParallelWorkflow<void, unknown>;
9
12
  fileChange: import("@modern-js/core").AsyncWorkflow<{
10
13
  filename: string;
14
+ eventType: "add" | "unlink" | "change";
11
15
  }, void>;
12
16
  beforeExit: import("@modern-js/core").AsyncWorkflow<void, void>;
17
+ beforeRestart: import("@modern-js/core").AsyncWorkflow<void, void>;
13
18
  } & import("@modern-js/core").ClearDraftProgress<import("@modern-js/core").Hooks>>>>;
14
19
 
15
20
  export default _default;
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
+ ...sharedConfig,
7
+ rootDir: __dirname,
8
+ };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.1",
14
+ "version": "1.2.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -20,12 +20,14 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "node": {
23
+ "jsnext:source": "./src/index.ts",
23
24
  "import": "./dist/js/modern/index.js",
24
25
  "require": "./dist/js/node/index.js"
25
26
  },
26
27
  "default": "./dist/js/treeshaking/index.js"
27
28
  },
28
29
  "./cli": {
30
+ "jsnext:source": "./src/cli.ts",
29
31
  "node": {
30
32
  "import": "./dist/js/node/cli.js",
31
33
  "require": "./dist/js/node/cli.js"
@@ -36,13 +38,13 @@
36
38
  "dependencies": {
37
39
  "@babel/runtime": "^7",
38
40
  "@modern-js/polyfill-lib": "^1.0.0",
39
- "@modern-js/server-plugin": "^1.1.1",
41
+ "@modern-js/server-core": "^1.2.2",
40
42
  "lru-cache": "^6.0.0",
41
43
  "mime-types": "^2.1.32",
42
44
  "ua-parser-js": "^0.7.28"
43
45
  },
44
46
  "devDependencies": {
45
- "@modern-js/types": "^1.1.2",
47
+ "@modern-js/types": "^1.3.0",
46
48
  "@types/jest": "^26",
47
49
  "@types/lru-cache": "^5.1.1",
48
50
  "@types/mime-types": "^2.1.1",
@@ -51,12 +53,13 @@
51
53
  "@types/react-dom": "^17",
52
54
  "@types/ua-parser-js": "^0.7.36",
53
55
  "typescript": "^4",
54
- "@modern-js/core": "^1.1.2",
55
- "@modern-js/plugin-testing": "^1.1.0",
56
- "@modern-js/module-tools": "^1.1.0"
56
+ "@modern-js/core": "^1.4.0",
57
+ "@scripts/build": "0.0.0",
58
+ "jest": "^27",
59
+ "@scripts/jest-config": "0.0.0"
57
60
  },
58
61
  "peerDependencies": {
59
- "@modern-js/core": "^1.1.2"
62
+ "@modern-js/core": "^1.4.0"
60
63
  },
61
64
  "sideEffects": false,
62
65
  "modernConfig": {
@@ -66,12 +69,14 @@
66
69
  },
67
70
  "publishConfig": {
68
71
  "registry": "https://registry.npmjs.org/",
69
- "access": "public"
72
+ "access": "public",
73
+ "types": "./dist/types/index.d.ts"
70
74
  },
71
75
  "scripts": {
72
76
  "new": "modern new",
73
77
  "build": "modern build",
74
78
  "dev": "modern build --watch",
75
- "test": "modern test --passWithNoTests"
76
- }
79
+ "test": "jest --passWithNoTests"
80
+ },
81
+ "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
77
82
  }
package/tsconfig.json CHANGED
@@ -4,9 +4,7 @@
4
4
  "declaration": false,
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
- "paths": {
8
- "@/*": ["./src/*"]
9
- }
7
+ "paths": {}
10
8
  },
11
9
  "include": ["src"]
12
10
  }
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
- };