@modern-js/plugin-worker 2.21.1 → 2.22.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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @modern-js/plugin-worker
2
2
 
3
+ ## 2.22.0
4
+
5
+ ### Minor Changes
6
+
7
+ - cb9e1ec: feat: worker ssr support server hooks
8
+ feat: worker ssr 支持 server hooks
9
+
10
+ ### Patch Changes
11
+
12
+ - Updated dependencies [3d48836]
13
+ - Updated dependencies [5050e8e]
14
+ - Updated dependencies [4991c8a]
15
+ - Updated dependencies [cb9e1ec]
16
+ - Updated dependencies [ea961e7]
17
+ - @modern-js/utils@2.22.0
18
+ - @modern-js/prod-server@2.22.0
19
+ - @modern-js/server-utils@2.22.0
20
+
3
21
  ## 2.21.1
4
22
 
5
23
  ### Patch Changes
package/dist/cjs/code.js CHANGED
@@ -4,7 +4,9 @@ Object.defineProperty(exports, "__esModule", {
4
4
  });
5
5
  Object.defineProperty(exports, "worker", {
6
6
  enumerable: true,
7
- get: () => worker
7
+ get: function() {
8
+ return worker;
9
+ }
8
10
  });
9
11
  const worker = () => `
10
12
  // work entry code
@@ -13,13 +15,14 @@ import loadableStats from "../loadable-stats.json";
13
15
  import routeManifest from "../routes-manifest.json";
14
16
  import { manifest } from "./manifest";
15
17
 
18
+ const handler = createHandler(manifest);
19
+
16
20
  async function handleRequest(request) {
17
21
  const options = {
18
22
  request,
19
23
  loadableStats,
20
24
  routeManifest,
21
25
  }
22
- const handler = createHandler(manifest);
23
26
 
24
27
  const { body, status, headers } = await handler(options);
25
28
 
@@ -10,14 +10,32 @@ function _export(target, all) {
10
10
  });
11
11
  }
12
12
  _export(exports, {
13
- WORKER_SERVER: () => WORKER_SERVER,
14
- WORKER_SERVER_ENTRY: () => WORKER_SERVER_ENTRY,
15
- MANIFEST_FILE: () => MANIFEST_FILE,
16
- PKG_FILE: () => PKG_FILE,
17
- WRANGLER_FILE: () => WRANGLER_FILE
13
+ WORKER_SERVER: function() {
14
+ return WORKER_SERVER;
15
+ },
16
+ WORKER_SERVER_ENTRY: function() {
17
+ return WORKER_SERVER_ENTRY;
18
+ },
19
+ MANIFEST_FILE: function() {
20
+ return MANIFEST_FILE;
21
+ },
22
+ PKG_FILE: function() {
23
+ return PKG_FILE;
24
+ },
25
+ WRANGLER_FILE: function() {
26
+ return WRANGLER_FILE;
27
+ },
28
+ WEB_APP_NAME: function() {
29
+ return WEB_APP_NAME;
30
+ },
31
+ SERVER_HOOKS: function() {
32
+ return SERVER_HOOKS;
33
+ }
18
34
  });
19
35
  const WORKER_SERVER = "worker-server";
20
36
  const WORKER_SERVER_ENTRY = "index.js";
21
37
  const MANIFEST_FILE = "manifest.js";
22
38
  const PKG_FILE = "package.json";
23
39
  const WRANGLER_FILE = "wrangler.toml";
40
+ const WEB_APP_NAME = "index";
41
+ const SERVER_HOOKS = "serverHooks";
package/dist/cjs/index.js CHANGED
@@ -4,48 +4,59 @@ Object.defineProperty(exports, "__esModule", {
4
4
  });
5
5
  Object.defineProperty(exports, "default", {
6
6
  enumerable: true,
7
- get: () => _default
7
+ get: function() {
8
+ return _default;
9
+ }
8
10
  });
9
11
  const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
10
12
  const _path = /* @__PURE__ */ _interop_require_default._(require("path"));
11
13
  const _utils = require("@modern-js/utils");
12
14
  const _constants = require("./constants");
13
15
  const _code = require("./code");
14
- const _default = () => ({
15
- name: "@modern-js/plugin-worker",
16
- setup: (ctx) => {
17
- return {
18
- async config() {
19
- return {
20
- output: {
21
- disableNodePolyfill: false
16
+ const _default = () => {
17
+ return {
18
+ name: "@modern-js/plugin-worker",
19
+ setup: (ctx) => {
20
+ return {
21
+ async config() {
22
+ return {
23
+ output: {
24
+ disableNodePolyfill: false
25
+ }
26
+ };
27
+ },
28
+ validateSchema() {
29
+ return _utils.PLUGIN_SCHEMAS["@modern-js/plugin-worker"];
30
+ },
31
+ async afterDev() {
32
+ var _configContext_deploy_worker;
33
+ const { appDirectory, distDirectory } = ctx.useAppContext();
34
+ const configContext = ctx.useResolvedConfigContext();
35
+ if (!(0, _utils.isServiceWorker)(configContext)) {
36
+ return;
22
37
  }
23
- };
24
- },
25
- validateSchema() {
26
- return _utils.PLUGIN_SCHEMAS["@modern-js/plugin-worker"];
27
- },
28
- async afterDev() {
29
- const { appDirectory, distDirectory } = ctx.useAppContext();
30
- const configContext = ctx.useResolvedConfigContext();
31
- if (!(0, _utils.isServiceWorker)(configContext)) {
32
- return;
33
- }
34
- writeWorkerServerFile(appDirectory, distDirectory);
35
- },
36
- async afterBuild() {
37
- const { appDirectory, distDirectory } = ctx.useAppContext();
38
- const configContext = ctx.useResolvedConfigContext();
39
- if (!(0, _utils.isServiceWorker)(configContext)) {
40
- return;
38
+ const enableWorkerInWeb = configContext.deploy.enableWorker || ((_configContext_deploy_worker = configContext.deploy.worker) === null || _configContext_deploy_worker === void 0 ? void 0 : _configContext_deploy_worker.web);
39
+ writeWorkerServerFile(appDirectory, distDirectory, enableWorkerInWeb);
40
+ },
41
+ async afterBuild() {
42
+ var _configContext_deploy_worker;
43
+ const { appDirectory, distDirectory } = ctx.useAppContext();
44
+ const configContext = ctx.useResolvedConfigContext();
45
+ if (!(0, _utils.isServiceWorker)(configContext)) {
46
+ return;
47
+ }
48
+ const enableWorkerInWeb = configContext.deploy.enableWorker || ((_configContext_deploy_worker = configContext.deploy.worker) === null || _configContext_deploy_worker === void 0 ? void 0 : _configContext_deploy_worker.web);
49
+ writeWorkerServerFile(appDirectory, distDirectory, enableWorkerInWeb);
41
50
  }
42
- writeWorkerServerFile(appDirectory, distDirectory);
43
- }
44
- };
45
- }
46
- });
47
- const writeWorkerServerFile = (appDirectory, distDirectory) => {
51
+ };
52
+ }
53
+ };
54
+ };
55
+ const writeWorkerServerFile = (appDirectory, distDirectory, enableWorkerInWeb) => {
48
56
  const workServerDir = _path.default.join(distDirectory, _constants.WORKER_SERVER);
57
+ const serverHookDir = _path.default.join(appDirectory, _utils.SERVER_DIR, _constants.WEB_APP_NAME);
58
+ const isExistsServerHook = _utils.fs.existsSync(`${serverHookDir}.ts`) || _utils.fs.existsSync(`${serverHookDir}.js`);
59
+ const relativePath = _path.default.relative(workServerDir, serverHookDir);
49
60
  _utils.fs.removeSync(workServerDir);
50
61
  _utils.fs.mkdirSync(workServerDir);
51
62
  _utils.fs.writeFileSync(_path.default.join(workServerDir, _constants.WORKER_SERVER_ENTRY), (0, _code.worker)());
@@ -54,6 +65,10 @@ const writeWorkerServerFile = (appDirectory, distDirectory) => {
54
65
  let importStr = ``;
55
66
  let pageStr = ``;
56
67
  const routeArr = [];
68
+ if (isExistsServerHook && !enableWorkerInWeb) {
69
+ importStr += `import * as ${_constants.SERVER_HOOKS} from '${relativePath}'
70
+ `;
71
+ }
57
72
  routes.forEach((route) => {
58
73
  if (route.isSSR) {
59
74
  importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.worker}";
@@ -63,6 +78,7 @@ const writeWorkerServerFile = (appDirectory, distDirectory) => {
63
78
  importStr += `import ${route.entryName}template from "../${route.entryPath}";
64
79
  `;
65
80
  pageStr += `"${route.urlPath}": {
81
+ ${isExistsServerHook && !enableWorkerInWeb ? `${_constants.SERVER_HOOKS},` : ""}
66
82
  entryName: "${route.entryName}",
67
83
  template: ${route.entryName}template,
68
84
  serverRender: ${route.isSSR ? `${route.entryName}ServerRender` : void 0},
package/dist/esm/code.js CHANGED
@@ -5,13 +5,14 @@ import loadableStats from "../loadable-stats.json";
5
5
  import routeManifest from "../routes-manifest.json";
6
6
  import { manifest } from "./manifest";
7
7
 
8
+ const handler = createHandler(manifest);
9
+
8
10
  async function handleRequest(request) {
9
11
  const options = {
10
12
  request,
11
13
  loadableStats,
12
14
  routeManifest,
13
15
  }
14
- const handler = createHandler(manifest);
15
16
 
16
17
  const { body, status, headers } = await handler(options);
17
18
 
@@ -3,3 +3,5 @@ export const WORKER_SERVER_ENTRY = "index.js";
3
3
  export const MANIFEST_FILE = "manifest.js";
4
4
  export const PKG_FILE = "package.json";
5
5
  export const WRANGLER_FILE = "wrangler.toml";
6
+ export const WEB_APP_NAME = "index";
7
+ export const SERVER_HOOKS = "serverHooks";
package/dist/esm/index.js CHANGED
@@ -1,42 +1,51 @@
1
1
  import path from "path";
2
- import { fs, isServiceWorker, PLUGIN_SCHEMAS, ROUTE_SPEC_FILE } from "@modern-js/utils";
3
- import { MANIFEST_FILE, PKG_FILE, WORKER_SERVER, WORKER_SERVER_ENTRY, WRANGLER_FILE } from "./constants";
2
+ import { fs, isServiceWorker, PLUGIN_SCHEMAS, ROUTE_SPEC_FILE, SERVER_DIR } from "@modern-js/utils";
3
+ import { MANIFEST_FILE, PKG_FILE, SERVER_HOOKS, WEB_APP_NAME, WORKER_SERVER, WORKER_SERVER_ENTRY, WRANGLER_FILE } from "./constants";
4
4
  import { worker } from "./code";
5
- export default () => ({
6
- name: "@modern-js/plugin-worker",
7
- setup: (ctx) => {
8
- return {
9
- async config() {
10
- return {
11
- output: {
12
- disableNodePolyfill: false
5
+ export default () => {
6
+ return {
7
+ name: "@modern-js/plugin-worker",
8
+ setup: (ctx) => {
9
+ return {
10
+ async config() {
11
+ return {
12
+ output: {
13
+ disableNodePolyfill: false
14
+ }
15
+ };
16
+ },
17
+ validateSchema() {
18
+ return PLUGIN_SCHEMAS["@modern-js/plugin-worker"];
19
+ },
20
+ async afterDev() {
21
+ var _configContext_deploy_worker;
22
+ const { appDirectory, distDirectory } = ctx.useAppContext();
23
+ const configContext = ctx.useResolvedConfigContext();
24
+ if (!isServiceWorker(configContext)) {
25
+ return;
13
26
  }
14
- };
15
- },
16
- validateSchema() {
17
- return PLUGIN_SCHEMAS["@modern-js/plugin-worker"];
18
- },
19
- async afterDev() {
20
- const { appDirectory, distDirectory } = ctx.useAppContext();
21
- const configContext = ctx.useResolvedConfigContext();
22
- if (!isServiceWorker(configContext)) {
23
- return;
24
- }
25
- writeWorkerServerFile(appDirectory, distDirectory);
26
- },
27
- async afterBuild() {
28
- const { appDirectory, distDirectory } = ctx.useAppContext();
29
- const configContext = ctx.useResolvedConfigContext();
30
- if (!isServiceWorker(configContext)) {
31
- return;
27
+ const enableWorkerInWeb = configContext.deploy.enableWorker || ((_configContext_deploy_worker = configContext.deploy.worker) === null || _configContext_deploy_worker === void 0 ? void 0 : _configContext_deploy_worker.web);
28
+ writeWorkerServerFile(appDirectory, distDirectory, enableWorkerInWeb);
29
+ },
30
+ async afterBuild() {
31
+ var _configContext_deploy_worker;
32
+ const { appDirectory, distDirectory } = ctx.useAppContext();
33
+ const configContext = ctx.useResolvedConfigContext();
34
+ if (!isServiceWorker(configContext)) {
35
+ return;
36
+ }
37
+ const enableWorkerInWeb = configContext.deploy.enableWorker || ((_configContext_deploy_worker = configContext.deploy.worker) === null || _configContext_deploy_worker === void 0 ? void 0 : _configContext_deploy_worker.web);
38
+ writeWorkerServerFile(appDirectory, distDirectory, enableWorkerInWeb);
32
39
  }
33
- writeWorkerServerFile(appDirectory, distDirectory);
34
- }
35
- };
36
- }
37
- });
38
- const writeWorkerServerFile = (appDirectory, distDirectory) => {
40
+ };
41
+ }
42
+ };
43
+ };
44
+ const writeWorkerServerFile = (appDirectory, distDirectory, enableWorkerInWeb) => {
39
45
  const workServerDir = path.join(distDirectory, WORKER_SERVER);
46
+ const serverHookDir = path.join(appDirectory, SERVER_DIR, WEB_APP_NAME);
47
+ const isExistsServerHook = fs.existsSync(`${serverHookDir}.ts`) || fs.existsSync(`${serverHookDir}.js`);
48
+ const relativePath = path.relative(workServerDir, serverHookDir);
40
49
  fs.removeSync(workServerDir);
41
50
  fs.mkdirSync(workServerDir);
42
51
  fs.writeFileSync(path.join(workServerDir, WORKER_SERVER_ENTRY), worker());
@@ -45,6 +54,10 @@ const writeWorkerServerFile = (appDirectory, distDirectory) => {
45
54
  let importStr = ``;
46
55
  let pageStr = ``;
47
56
  const routeArr = [];
57
+ if (isExistsServerHook && !enableWorkerInWeb) {
58
+ importStr += `import * as ${SERVER_HOOKS} from '${relativePath}'
59
+ `;
60
+ }
48
61
  routes.forEach((route) => {
49
62
  if (route.isSSR) {
50
63
  importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.worker}";
@@ -54,6 +67,7 @@ const writeWorkerServerFile = (appDirectory, distDirectory) => {
54
67
  importStr += `import ${route.entryName}template from "../${route.entryPath}";
55
68
  `;
56
69
  pageStr += `"${route.urlPath}": {
70
+ ${isExistsServerHook && !enableWorkerInWeb ? `${SERVER_HOOKS},` : ""}
57
71
  entryName: "${route.entryName}",
58
72
  template: ${route.entryName}template,
59
73
  serverRender: ${route.isSSR ? `${route.entryName}ServerRender` : void 0},
@@ -2,4 +2,6 @@ export declare const WORKER_SERVER = "worker-server";
2
2
  export declare const WORKER_SERVER_ENTRY = "index.js";
3
3
  export declare const MANIFEST_FILE = "manifest.js";
4
4
  export declare const PKG_FILE = "package.json";
5
- export declare const WRANGLER_FILE = "wrangler.toml";
5
+ export declare const WRANGLER_FILE = "wrangler.toml";
6
+ export declare const WEB_APP_NAME = "index";
7
+ export declare const SERVER_HOOKS = "serverHooks";
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.21.1",
18
+ "version": "2.22.0",
19
19
  "types": "./dist/types/index.d.ts",
20
20
  "jsnext:source": "./src/index.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -43,20 +43,20 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@swc/helpers": "0.5.1",
46
- "@modern-js/server-utils": "2.21.1",
47
- "@modern-js/utils": "2.21.1",
48
- "@modern-js/prod-server": "2.21.1"
46
+ "@modern-js/server-utils": "2.22.0",
47
+ "@modern-js/utils": "2.22.0",
48
+ "@modern-js/prod-server": "2.22.0"
49
49
  },
50
50
  "devDependencies": {
51
51
  "typescript": "^5",
52
52
  "@types/jest": "^29",
53
53
  "@types/node": "^14",
54
54
  "jest": "^29",
55
- "@modern-js/app-tools": "2.21.1",
56
- "@modern-js/core": "2.21.1",
57
- "@scripts/build": "2.21.1",
58
- "@modern-js/types": "2.21.1",
59
- "@scripts/jest-config": "2.21.1"
55
+ "@modern-js/app-tools": "2.22.0",
56
+ "@modern-js/core": "2.22.0",
57
+ "@modern-js/types": "2.22.0",
58
+ "@scripts/jest-config": "2.22.0",
59
+ "@scripts/build": "2.22.0"
60
60
  },
61
61
  "sideEffects": [
62
62
  "*.css",