@modern-js/plugin-worker 2.3.1-beta.0 → 2.5.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.
File without changes
File without changes
@@ -0,0 +1,124 @@
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 src_exports = {};
25
+ __export(src_exports, {
26
+ default: () => src_default
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+ var import_path = __toESM(require("path"));
30
+ var import_utils = require("@modern-js/utils");
31
+ var import_constants = require("./constants");
32
+ var import_code = require("./code");
33
+ var import_utils2 = require("./utils");
34
+ var src_default = () => ({
35
+ name: "@modern-js/plugin-worker",
36
+ setup: (ctx) => {
37
+ return {
38
+ async config() {
39
+ return {
40
+ output: {
41
+ disableNodePolyfill: false
42
+ }
43
+ };
44
+ },
45
+ async beforeDeploy() {
46
+ const { appDirectory, distDirectory } = ctx.useAppContext();
47
+ const configContext = ctx.useResolvedConfigContext();
48
+ if (!(0, import_utils.isServiceWorker)(configContext)) {
49
+ return;
50
+ }
51
+ const workServerDir = import_path.default.join(distDirectory, import_constants.WORKER_SERVER);
52
+ import_utils.fs.removeSync(workServerDir);
53
+ import_utils.fs.mkdirSync(workServerDir);
54
+ import_utils.fs.writeFileSync(
55
+ import_path.default.join(workServerDir, import_constants.WORKER_SERVER_ENTRY),
56
+ (0, import_code.worker)()
57
+ );
58
+ const routeJSON = import_path.default.join(distDirectory, import_utils.ROUTE_SPEC_FILE);
59
+ const { routes } = import_utils.fs.readJSONSync(routeJSON);
60
+ let importStr = ``;
61
+ let pageStr = ``;
62
+ const routeArr = [];
63
+ routes.forEach(
64
+ (route) => {
65
+ if (route.isSSR) {
66
+ importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.worker}";
67
+ `;
68
+ }
69
+ importStr += `import ${route.entryName}template from "../${route.entryPath}";
70
+ `;
71
+ pageStr += `"${route.urlPath}": {
72
+ entryName: "${route.entryName}",
73
+ template: ${route.entryName}template,
74
+ serverRender: ${route.isSSR ? `${route.entryName}ServerRender` : void 0},
75
+ },`;
76
+ routeArr.push({
77
+ entryName: route.entryName,
78
+ isSSR: route.isSSR,
79
+ urlPath: route.urlPath
80
+ });
81
+ }
82
+ );
83
+ const manifest = `
84
+ ${importStr}
85
+
86
+ export const manifest = {
87
+ pages: {
88
+ ${pageStr}
89
+ },
90
+ routes: ${JSON.stringify(routeArr, null, " ")}
91
+ }
92
+ `;
93
+ import_utils.fs.writeFileSync(import_path.default.join(workServerDir, import_constants.MANIFEST_FILE), manifest);
94
+ const pkg = import_utils.fs.readJSONSync(import_path.default.join(appDirectory, import_constants.PKG_FILE));
95
+ await import_utils.fs.writeJSON(
96
+ import_path.default.join(distDirectory, import_constants.PKG_FILE),
97
+ {
98
+ name: pkg.name,
99
+ version: pkg.version,
100
+ dependencies: {
101
+ "@modern-js/prod-server": "0.0.0-next-20230209112307",
102
+ wrangler: "^2.9.0"
103
+ },
104
+ resolutions: pkg.resolutions || {},
105
+ pnpm: pkg.pnpm || {}
106
+ },
107
+ { spaces: 2 }
108
+ );
109
+ const manager = await (0, import_utils.getPackageManager)(appDirectory);
110
+ const lockfile = import_constants.LOCK_FILE[manager];
111
+ (0, import_utils2.copyfile)(distDirectory, appDirectory, [lockfile]);
112
+ import_utils.fs.writeFileSync(
113
+ import_path.default.join(distDirectory, import_constants.WRANGLER_FILE),
114
+ `name = "${pkg.name}"
115
+ main = "${import_path.default.join(import_constants.WORKER_SERVER, import_constants.WORKER_SERVER_ENTRY)}"
116
+ compatibility_date = "${new Date().toISOString().substring(0, 10)}"
117
+ `
118
+ );
119
+ }
120
+ };
121
+ }
122
+ });
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {});
File without changes
File without changes
File without changes
@@ -0,0 +1,109 @@
1
+ import path from "path";
2
+ import {
3
+ fs,
4
+ getPackageManager,
5
+ isServiceWorker,
6
+ ROUTE_SPEC_FILE
7
+ } from "@modern-js/utils";
8
+ import {
9
+ LOCK_FILE,
10
+ MANIFEST_FILE,
11
+ PKG_FILE,
12
+ WORKER_SERVER,
13
+ WORKER_SERVER_ENTRY,
14
+ WRANGLER_FILE
15
+ } from "./constants";
16
+ import { worker } from "./code";
17
+ import { copyfile } from "./utils";
18
+ var src_default = () => ({
19
+ name: "@modern-js/plugin-worker",
20
+ setup: (ctx) => {
21
+ return {
22
+ async config() {
23
+ return {
24
+ output: {
25
+ disableNodePolyfill: false
26
+ }
27
+ };
28
+ },
29
+ async beforeDeploy() {
30
+ const { appDirectory, distDirectory } = ctx.useAppContext();
31
+ const configContext = ctx.useResolvedConfigContext();
32
+ if (!isServiceWorker(configContext)) {
33
+ return;
34
+ }
35
+ const workServerDir = path.join(distDirectory, WORKER_SERVER);
36
+ fs.removeSync(workServerDir);
37
+ fs.mkdirSync(workServerDir);
38
+ fs.writeFileSync(
39
+ path.join(workServerDir, WORKER_SERVER_ENTRY),
40
+ worker()
41
+ );
42
+ const routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
43
+ const { routes } = fs.readJSONSync(routeJSON);
44
+ let importStr = ``;
45
+ let pageStr = ``;
46
+ const routeArr = [];
47
+ routes.forEach(
48
+ (route) => {
49
+ if (route.isSSR) {
50
+ importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.worker}";
51
+ `;
52
+ }
53
+ importStr += `import ${route.entryName}template from "../${route.entryPath}";
54
+ `;
55
+ pageStr += `"${route.urlPath}": {
56
+ entryName: "${route.entryName}",
57
+ template: ${route.entryName}template,
58
+ serverRender: ${route.isSSR ? `${route.entryName}ServerRender` : void 0},
59
+ },`;
60
+ routeArr.push({
61
+ entryName: route.entryName,
62
+ isSSR: route.isSSR,
63
+ urlPath: route.urlPath
64
+ });
65
+ }
66
+ );
67
+ const manifest = `
68
+ ${importStr}
69
+
70
+ export const manifest = {
71
+ pages: {
72
+ ${pageStr}
73
+ },
74
+ routes: ${JSON.stringify(routeArr, null, " ")}
75
+ }
76
+ `;
77
+ fs.writeFileSync(path.join(workServerDir, MANIFEST_FILE), manifest);
78
+ const pkg = fs.readJSONSync(path.join(appDirectory, PKG_FILE));
79
+ await fs.writeJSON(
80
+ path.join(distDirectory, PKG_FILE),
81
+ {
82
+ name: pkg.name,
83
+ version: pkg.version,
84
+ dependencies: {
85
+ "@modern-js/prod-server": "0.0.0-next-20230209112307",
86
+ wrangler: "^2.9.0"
87
+ },
88
+ resolutions: pkg.resolutions || {},
89
+ pnpm: pkg.pnpm || {}
90
+ },
91
+ { spaces: 2 }
92
+ );
93
+ const manager = await getPackageManager(appDirectory);
94
+ const lockfile = LOCK_FILE[manager];
95
+ copyfile(distDirectory, appDirectory, [lockfile]);
96
+ fs.writeFileSync(
97
+ path.join(distDirectory, WRANGLER_FILE),
98
+ `name = "${pkg.name}"
99
+ main = "${path.join(WORKER_SERVER, WORKER_SERVER_ENTRY)}"
100
+ compatibility_date = "${new Date().toISOString().substring(0, 10)}"
101
+ `
102
+ );
103
+ }
104
+ };
105
+ }
106
+ });
107
+ export {
108
+ src_default as default
109
+ };
File without changes
package/package.json CHANGED
@@ -11,18 +11,18 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.3.1-beta.0",
15
- "types": "./dist/types/cli.d.ts",
14
+ "version": "2.5.0",
15
+ "types": "./dist/types/index.d.ts",
16
16
  "jsnext:source": "./src/index.ts",
17
- "main": "./dist/js/node/index.js",
17
+ "main": "./dist/cjs/index.js",
18
18
  "exports": {
19
19
  ".": {
20
20
  "jsnext:source": "./src/index.ts",
21
- "default": "./dist/js/node/index.js"
21
+ "default": "./dist/cjs/index.js"
22
22
  },
23
23
  "./cli": {
24
24
  "jsnext:source": "./src/index.ts",
25
- "default": "./dist/js/node/index.js"
25
+ "default": "./dist/cjs/index.js"
26
26
  }
27
27
  },
28
28
  "typesVersions": {
@@ -37,19 +37,19 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@babel/runtime": "^7.18.0",
40
- "@modern-js/server-utils": "2.4.1-beta.0",
41
- "@modern-js/utils": "2.4.1-beta.0"
40
+ "@modern-js/utils": "2.5.0",
41
+ "@modern-js/server-utils": "2.5.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "^4",
45
45
  "@types/jest": "^27",
46
46
  "@types/node": "^14",
47
47
  "jest": "^27",
48
- "@modern-js/app-tools": "3.0.0-beta.0",
49
- "@modern-js/core": "2.4.1-beta.0",
50
- "@modern-js/types": "2.4.0",
51
- "@scripts/build": "2.4.0",
52
- "@scripts/jest-config": "2.4.0"
48
+ "@modern-js/app-tools": "2.5.0",
49
+ "@modern-js/core": "2.5.0",
50
+ "@modern-js/types": "2.5.0",
51
+ "@scripts/build": "2.5.0",
52
+ "@scripts/jest-config": "2.5.0"
53
53
  },
54
54
  "sideEffects": [
55
55
  "*.css",
@@ -1,133 +0,0 @@
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 path from "path";
22
- import {
23
- fs,
24
- getPackageManager,
25
- isServiceWorker,
26
- ROUTE_SPEC_FILE
27
- } from "@modern-js/utils";
28
- import {
29
- LOCK_FILE,
30
- MANIFEST_FILE,
31
- PKG_FILE,
32
- WORKER_SERVER,
33
- WORKER_SERVER_ENTRY,
34
- WRANGLER_FILE
35
- } from "./constants";
36
- import { worker } from "./code";
37
- import { copyfile } from "./utils";
38
- var src_default = () => ({
39
- name: "@modern-js/plugin-worker",
40
- setup: (ctx) => {
41
- return {
42
- config() {
43
- return __async(this, null, function* () {
44
- return {
45
- output: {
46
- disableNodePolyfill: false
47
- }
48
- };
49
- });
50
- },
51
- beforeDeploy() {
52
- return __async(this, null, function* () {
53
- const { appDirectory, distDirectory } = ctx.useAppContext();
54
- const configContext = ctx.useResolvedConfigContext();
55
- if (!isServiceWorker(configContext)) {
56
- return;
57
- }
58
- const workServerDir = path.join(distDirectory, WORKER_SERVER);
59
- fs.removeSync(workServerDir);
60
- fs.mkdirSync(workServerDir);
61
- fs.writeFileSync(
62
- path.join(workServerDir, WORKER_SERVER_ENTRY),
63
- worker()
64
- );
65
- const routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
66
- const { routes } = fs.readJSONSync(routeJSON);
67
- let importStr = ``;
68
- let pageStr = ``;
69
- const routeArr = [];
70
- routes.forEach(
71
- (route) => {
72
- if (route.isSSR) {
73
- importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.worker}";
74
- `;
75
- }
76
- importStr += `import ${route.entryName}template from "../${route.entryPath}";
77
- `;
78
- pageStr += `"${route.urlPath}": {
79
- entryName: "${route.entryName}",
80
- template: ${route.entryName}template,
81
- serverRender: ${route.isSSR ? `${route.entryName}ServerRender` : void 0},
82
- },`;
83
- routeArr.push({
84
- entryName: route.entryName,
85
- isSSR: route.isSSR,
86
- urlPath: route.urlPath
87
- });
88
- }
89
- );
90
- const manifest = `
91
- ${importStr}
92
-
93
- export const manifest = {
94
- pages: {
95
- ${pageStr}
96
- },
97
- routes: ${JSON.stringify(routeArr, null, " ")}
98
- }
99
- `;
100
- fs.writeFileSync(path.join(workServerDir, MANIFEST_FILE), manifest);
101
- const pkg = fs.readJSONSync(path.join(appDirectory, PKG_FILE));
102
- yield fs.writeJSON(
103
- path.join(distDirectory, PKG_FILE),
104
- {
105
- name: pkg.name,
106
- version: pkg.version,
107
- dependencies: {
108
- "@modern-js/prod-server": "0.0.0-next-20230209112307",
109
- wrangler: "^2.9.0"
110
- },
111
- resolutions: pkg.resolutions || {},
112
- pnpm: pkg.pnpm || {}
113
- },
114
- { spaces: 2 }
115
- );
116
- const manager = yield getPackageManager(appDirectory);
117
- const lockfile = LOCK_FILE[manager];
118
- copyfile(distDirectory, appDirectory, [lockfile]);
119
- fs.writeFileSync(
120
- path.join(distDirectory, WRANGLER_FILE),
121
- `name = "${pkg.name}"
122
- main = "${path.join(WORKER_SERVER, WORKER_SERVER_ENTRY)}"
123
- compatibility_date = "${new Date().toISOString().substring(0, 10)}"
124
- `
125
- );
126
- });
127
- }
128
- };
129
- }
130
- });
131
- export {
132
- src_default as default
133
- };
@@ -1,148 +0,0 @@
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 __async = (__this, __arguments, generator) => {
25
- return new Promise((resolve, reject) => {
26
- var fulfilled = (value) => {
27
- try {
28
- step(generator.next(value));
29
- } catch (e) {
30
- reject(e);
31
- }
32
- };
33
- var rejected = (value) => {
34
- try {
35
- step(generator.throw(value));
36
- } catch (e) {
37
- reject(e);
38
- }
39
- };
40
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
41
- step((generator = generator.apply(__this, __arguments)).next());
42
- });
43
- };
44
- var src_exports = {};
45
- __export(src_exports, {
46
- default: () => src_default
47
- });
48
- module.exports = __toCommonJS(src_exports);
49
- var import_path = __toESM(require("path"));
50
- var import_utils = require("@modern-js/utils");
51
- var import_constants = require("./constants");
52
- var import_code = require("./code");
53
- var import_utils2 = require("./utils");
54
- var src_default = () => ({
55
- name: "@modern-js/plugin-worker",
56
- setup: (ctx) => {
57
- return {
58
- config() {
59
- return __async(this, null, function* () {
60
- return {
61
- output: {
62
- disableNodePolyfill: false
63
- }
64
- };
65
- });
66
- },
67
- beforeDeploy() {
68
- return __async(this, null, function* () {
69
- const { appDirectory, distDirectory } = ctx.useAppContext();
70
- const configContext = ctx.useResolvedConfigContext();
71
- if (!(0, import_utils.isServiceWorker)(configContext)) {
72
- return;
73
- }
74
- const workServerDir = import_path.default.join(distDirectory, import_constants.WORKER_SERVER);
75
- import_utils.fs.removeSync(workServerDir);
76
- import_utils.fs.mkdirSync(workServerDir);
77
- import_utils.fs.writeFileSync(
78
- import_path.default.join(workServerDir, import_constants.WORKER_SERVER_ENTRY),
79
- (0, import_code.worker)()
80
- );
81
- const routeJSON = import_path.default.join(distDirectory, import_utils.ROUTE_SPEC_FILE);
82
- const { routes } = import_utils.fs.readJSONSync(routeJSON);
83
- let importStr = ``;
84
- let pageStr = ``;
85
- const routeArr = [];
86
- routes.forEach(
87
- (route) => {
88
- if (route.isSSR) {
89
- importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.worker}";
90
- `;
91
- }
92
- importStr += `import ${route.entryName}template from "../${route.entryPath}";
93
- `;
94
- pageStr += `"${route.urlPath}": {
95
- entryName: "${route.entryName}",
96
- template: ${route.entryName}template,
97
- serverRender: ${route.isSSR ? `${route.entryName}ServerRender` : void 0},
98
- },`;
99
- routeArr.push({
100
- entryName: route.entryName,
101
- isSSR: route.isSSR,
102
- urlPath: route.urlPath
103
- });
104
- }
105
- );
106
- const manifest = `
107
- ${importStr}
108
-
109
- export const manifest = {
110
- pages: {
111
- ${pageStr}
112
- },
113
- routes: ${JSON.stringify(routeArr, null, " ")}
114
- }
115
- `;
116
- import_utils.fs.writeFileSync(import_path.default.join(workServerDir, import_constants.MANIFEST_FILE), manifest);
117
- const pkg = import_utils.fs.readJSONSync(import_path.default.join(appDirectory, import_constants.PKG_FILE));
118
- yield import_utils.fs.writeJSON(
119
- import_path.default.join(distDirectory, import_constants.PKG_FILE),
120
- {
121
- name: pkg.name,
122
- version: pkg.version,
123
- dependencies: {
124
- "@modern-js/prod-server": "0.0.0-next-20230209112307",
125
- wrangler: "^2.9.0"
126
- },
127
- resolutions: pkg.resolutions || {},
128
- pnpm: pkg.pnpm || {}
129
- },
130
- { spaces: 2 }
131
- );
132
- const manager = yield (0, import_utils.getPackageManager)(appDirectory);
133
- const lockfile = import_constants.LOCK_FILE[manager];
134
- (0, import_utils2.copyfile)(distDirectory, appDirectory, [lockfile]);
135
- import_utils.fs.writeFileSync(
136
- import_path.default.join(distDirectory, import_constants.WRANGLER_FILE),
137
- `name = "${pkg.name}"
138
- main = "${import_path.default.join(import_constants.WORKER_SERVER, import_constants.WORKER_SERVER_ENTRY)}"
139
- compatibility_date = "${new Date().toISOString().substring(0, 10)}"
140
- `
141
- );
142
- });
143
- }
144
- };
145
- }
146
- });
147
- // Annotate the CommonJS export names for ESM import in node:
148
- 0 && (module.exports = {});
@@ -1,4 +0,0 @@
1
- var worker = function() {
2
- return '\n// work entry code\nimport { createHandler, handleUrl } from "@modern-js/prod-server/worker";\nimport loadableStats from "../loadable-stats.json";\nimport routeManifest from "../routes-manifest.json";\nimport { manifest } from "./manifest";\nasync function handleRequest(request) {\n const context = {\n request: {\n ...request,\n url: request.url,\n },\n url: handleUrl(request.url),\n body: null,\n loadableStats,\n routeManifest,\n };\n const handler = createHandler(manifest);\n await handler(context);\n return new Response(context.body, {\n headers: {\n \'content-type\': \'text/html;charset=UTF-8\',\n },\n });\n}\n\naddEventListener("fetch", (event) => {\n event.respondWith(handleRequest(event.request));\n});\n';
3
- };
4
- export { worker };
@@ -1,11 +0,0 @@
1
- var WORKER_SERVER = "worker-server";
2
- var WORKER_SERVER_ENTRY = "index.js";
3
- var MANIFEST_FILE = "manifest.js";
4
- var PKG_FILE = "package.json";
5
- var LOCK_FILE = {
6
- yarn: "yarn.lock",
7
- npm: "package-lock.json",
8
- pnpm: "pnpm-lock.yaml"
9
- };
10
- var WRANGLER_FILE = "wrangler.toml";
11
- export { LOCK_FILE, MANIFEST_FILE, PKG_FILE, WORKER_SERVER, WORKER_SERVER_ENTRY, WRANGLER_FILE };
@@ -1,225 +0,0 @@
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 path from "path";
126
- import { fs, getPackageManager, isServiceWorker, ROUTE_SPEC_FILE } from "@modern-js/utils";
127
- import { LOCK_FILE, MANIFEST_FILE, PKG_FILE, WORKER_SERVER, WORKER_SERVER_ENTRY, WRANGLER_FILE } from "./constants";
128
- import { worker } from "./code";
129
- import { copyfile } from "./utils";
130
- var src_default = function() {
131
- return {
132
- name: "@modern-js/plugin-worker",
133
- setup: function(ctx) {
134
- return {
135
- config: function config() {
136
- return _asyncToGenerator(function() {
137
- return __generator(this, function(_state) {
138
- return [
139
- 2,
140
- {
141
- output: {
142
- disableNodePolyfill: false
143
- }
144
- }
145
- ];
146
- });
147
- })();
148
- },
149
- beforeDeploy: function beforeDeploy() {
150
- return _asyncToGenerator(function() {
151
- var _ctx_useAppContext, appDirectory, distDirectory, configContext, workServerDir, routeJSON, routes, importStr, pageStr, routeArr, manifest, pkg, manager, lockfile;
152
- return __generator(this, function(_state) {
153
- switch(_state.label){
154
- case 0:
155
- _ctx_useAppContext = ctx.useAppContext(), appDirectory = _ctx_useAppContext.appDirectory, distDirectory = _ctx_useAppContext.distDirectory;
156
- configContext = ctx.useResolvedConfigContext();
157
- if (!isServiceWorker(configContext)) {
158
- return [
159
- 2
160
- ];
161
- }
162
- workServerDir = path.join(distDirectory, WORKER_SERVER);
163
- fs.removeSync(workServerDir);
164
- fs.mkdirSync(workServerDir);
165
- fs.writeFileSync(path.join(workServerDir, WORKER_SERVER_ENTRY), worker());
166
- routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
167
- routes = fs.readJSONSync(routeJSON).routes;
168
- importStr = "";
169
- pageStr = "";
170
- routeArr = [];
171
- routes.forEach(function(route) {
172
- if (route.isSSR) {
173
- importStr += "import { serverRender as ".concat(route.entryName, 'ServerRender } from "../').concat(route.worker, '";\n');
174
- }
175
- importStr += "import ".concat(route.entryName, 'template from "../').concat(route.entryPath, '";\n');
176
- pageStr += '"'.concat(route.urlPath, '": {\n entryName: "').concat(route.entryName, '",\n template: ').concat(route.entryName, "template,\n serverRender: ").concat(route.isSSR ? "".concat(route.entryName, "ServerRender") : void 0, ",\n },");
177
- routeArr.push({
178
- entryName: route.entryName,
179
- isSSR: route.isSSR,
180
- urlPath: route.urlPath
181
- });
182
- });
183
- manifest = "\n".concat(importStr, "\n\nexport const manifest = {\n pages: {\n ").concat(pageStr, "\n },\n routes: ").concat(JSON.stringify(routeArr, null, " "), "\n}\n ");
184
- fs.writeFileSync(path.join(workServerDir, MANIFEST_FILE), manifest);
185
- pkg = fs.readJSONSync(path.join(appDirectory, PKG_FILE));
186
- return [
187
- 4,
188
- fs.writeJSON(path.join(distDirectory, PKG_FILE), {
189
- name: pkg.name,
190
- version: pkg.version,
191
- dependencies: {
192
- "@modern-js/prod-server": "0.0.0-next-20230209112307",
193
- wrangler: "^2.9.0"
194
- },
195
- resolutions: pkg.resolutions || {},
196
- pnpm: pkg.pnpm || {}
197
- }, {
198
- spaces: 2
199
- })
200
- ];
201
- case 1:
202
- _state.sent();
203
- return [
204
- 4,
205
- getPackageManager(appDirectory)
206
- ];
207
- case 2:
208
- manager = _state.sent();
209
- lockfile = LOCK_FILE[manager];
210
- copyfile(distDirectory, appDirectory, [
211
- lockfile
212
- ]);
213
- fs.writeFileSync(path.join(distDirectory, WRANGLER_FILE), 'name = "'.concat(pkg.name, '"\nmain = "').concat(path.join(WORKER_SERVER, WORKER_SERVER_ENTRY), '"\ncompatibility_date = "').concat(new Date().toISOString().substring(0, 10), '"\n '));
214
- return [
215
- 2
216
- ];
217
- }
218
- });
219
- })();
220
- }
221
- };
222
- }
223
- };
224
- };
225
- export { src_default as default };
@@ -1,45 +0,0 @@
1
- import path from "path";
2
- import { fs } from "@modern-js/utils";
3
- var copyfile = function(target, source, fl) {
4
- var cpfile = function cpfile(target2, source2, filename) {
5
- var filepath = path.join(source2, filename);
6
- if (!fs.existsSync(filepath)) {
7
- return;
8
- }
9
- var targetPath = path.join(target2, filename);
10
- fs.copySync(filepath, targetPath);
11
- };
12
- fl.forEach(function(ff) {
13
- if (!ff) {
14
- return;
15
- }
16
- if (typeof ff === "string") {
17
- cpfile(target, source, ff);
18
- return;
19
- }
20
- var sourceIncludeFiles = fs.readdirSync(source);
21
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
22
- try {
23
- for(var _iterator = sourceIncludeFiles[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
24
- var filename = _step.value;
25
- if (ff.test(filename)) {
26
- cpfile(target, source, filename);
27
- }
28
- }
29
- } catch (err) {
30
- _didIteratorError = true;
31
- _iteratorError = err;
32
- } finally{
33
- try {
34
- if (!_iteratorNormalCompletion && _iterator.return != null) {
35
- _iterator.return();
36
- }
37
- } finally{
38
- if (_didIteratorError) {
39
- throw _iteratorError;
40
- }
41
- }
42
- }
43
- });
44
- };
45
- export { copyfile };