@modern-js/plugin-worker 0.0.0-next-20230206081248

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 ADDED
@@ -0,0 +1,10 @@
1
+ # @modern-js/plugin-worker
2
+
3
+ ## 0.0.0-next-20230206081248
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [98a2733ac]
8
+ - Updated dependencies [8c2db5f82]
9
+ - @modern-js/utils@0.0.0-next-20230206081248
10
+ - @modern-js/server-utils@0.0.0-next-20230206081248
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Modern.js
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+
2
+ <p align="center">
3
+ <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>
4
+ </p>
5
+ <p align="center">
6
+ 现代 Web 工程体系
7
+ <br/>
8
+ <a href="https://modernjs.dev" target="blank">
9
+ modernjs.dev
10
+ </a>
11
+ </p>
12
+ <p align="center">
13
+ The meta-framework suite designed from scratch for frontend-focused modern web development
14
+ </p>
15
+
16
+ # Introduction
17
+
18
+ > 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.
19
+
20
+ - [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)
21
+
22
+ ## Getting Started
23
+
24
+ - [Quick Start](https://modernjs.dev/docs/start)
25
+ - [Guides](https://modernjs.dev/docs/guides)
26
+ - [API References](https://modernjs.dev/docs/apis)
27
+
28
+ ## Contributing
29
+
30
+ - [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)
@@ -0,0 +1,22 @@
1
+ const worker = () => `
2
+ // work entry code
3
+ import { createHandler, handleUrl } from "@modern-js/prod-server/worker";
4
+ import { manifest } from "./manifest";
5
+ async function handleRequest(request) {
6
+ const context = {
7
+ request,
8
+ url: handleUrl(request.url),
9
+ body: null,
10
+ };
11
+ const handler = createHandler(manifest);
12
+ await handler(context);
13
+ return new Response(context.body);
14
+ }
15
+
16
+ addEventListener("fetch", (event) => {
17
+ event.respondWith(handleRequest(event.request));
18
+ });
19
+ `;
20
+ export {
21
+ worker
22
+ };
@@ -0,0 +1,18 @@
1
+ const WORKER_SERVER = "worker-server";
2
+ const WORKER_SERVER_ENTRY = "index.js";
3
+ const MANIFEST_FILE = "manifest.js";
4
+ const PKG_FILE = "package.json";
5
+ const LOCK_FILE = {
6
+ yarn: "yarn.lock",
7
+ npm: "package-lock.json",
8
+ pnpm: "pnpm-lock.yaml"
9
+ };
10
+ const WRANGLER_FILE = "wrangler.toml";
11
+ export {
12
+ LOCK_FILE,
13
+ MANIFEST_FILE,
14
+ PKG_FILE,
15
+ WORKER_SERVER,
16
+ WORKER_SERVER_ENTRY,
17
+ WRANGLER_FILE
18
+ };
@@ -0,0 +1,120 @@
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
+ isWorker,
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
+ beforeDeploy() {
43
+ return __async(this, null, function* () {
44
+ const { appDirectory, distDirectory } = ctx.useAppContext();
45
+ const configContext = ctx.useResolvedConfigContext();
46
+ if (!isWorker(configContext)) {
47
+ return;
48
+ }
49
+ const workServerDir = path.join(distDirectory, WORKER_SERVER);
50
+ fs.removeSync(workServerDir);
51
+ fs.mkdirSync(workServerDir);
52
+ fs.writeFileSync(
53
+ path.join(workServerDir, WORKER_SERVER_ENTRY),
54
+ worker()
55
+ );
56
+ const routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
57
+ const { routes } = fs.readJSONSync(routeJSON);
58
+ let importStr = ``;
59
+ let pageStr = ``;
60
+ const routeArr = [];
61
+ routes.forEach(
62
+ (route) => {
63
+ importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.bundle}";
64
+ `;
65
+ pageStr += `"${route.urlPath}": {
66
+ entryName: "${route.entryName}",
67
+ template: "${route.entryPath}",
68
+ serverRender: ${route.entryName}ServerRender,
69
+ },`;
70
+ routeArr.push({
71
+ entryName: route.entryName,
72
+ isSSR: route.isSSR,
73
+ urlPath: route.urlPath
74
+ });
75
+ }
76
+ );
77
+ const manifest = `
78
+ ${importStr}
79
+
80
+ export const manifest = {
81
+ pages: {
82
+ ${pageStr}
83
+ },
84
+ routes: ${JSON.stringify(routeArr, null, " ")}
85
+ }
86
+ `;
87
+ fs.writeFileSync(path.join(workServerDir, MANIFEST_FILE), manifest);
88
+ const pkg = fs.readJSONSync(path.join(appDirectory, PKG_FILE));
89
+ yield fs.writeJSON(
90
+ path.join(distDirectory, PKG_FILE),
91
+ {
92
+ name: pkg.name,
93
+ version: pkg.version,
94
+ dependencies: {
95
+ "@modern-js/prod-server": "0.0.0-next-20230203070739",
96
+ wrangler: "^2.9.0"
97
+ },
98
+ resolutions: pkg.resolutions || {},
99
+ pnpm: pkg.pnpm || {}
100
+ },
101
+ { spaces: 2 }
102
+ );
103
+ const manager = yield getPackageManager(appDirectory);
104
+ const lockfile = LOCK_FILE[manager];
105
+ copyfile(distDirectory, appDirectory, [lockfile]);
106
+ fs.writeFileSync(
107
+ path.join(distDirectory, WRANGLER_FILE),
108
+ `name = "${pkg.name}"
109
+ main = "${path.join(WORKER_SERVER, WORKER_SERVER_ENTRY)}"
110
+ compatibility_date = "${new Date()}"
111
+ `
112
+ );
113
+ });
114
+ }
115
+ };
116
+ }
117
+ });
118
+ export {
119
+ src_default as default
120
+ };
@@ -0,0 +1,30 @@
1
+ import path from "path";
2
+ import { fs } from "@modern-js/utils";
3
+ const copyfile = (target, source, fl) => {
4
+ fl.forEach((ff) => {
5
+ if (!ff) {
6
+ return;
7
+ }
8
+ if (typeof ff === "string") {
9
+ cpfile(target, source, ff);
10
+ return;
11
+ }
12
+ const sourceIncludeFiles = fs.readdirSync(source);
13
+ for (const filename of sourceIncludeFiles) {
14
+ if (ff.test(filename)) {
15
+ cpfile(target, source, filename);
16
+ }
17
+ }
18
+ });
19
+ function cpfile(target2, source2, filename) {
20
+ const filepath = path.join(source2, filename);
21
+ if (!fs.existsSync(filepath)) {
22
+ return;
23
+ }
24
+ const targetPath = path.join(target2, filename);
25
+ fs.copySync(filepath, targetPath);
26
+ }
27
+ };
28
+ export {
29
+ copyfile
30
+ };
@@ -0,0 +1,45 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var code_exports = {};
19
+ __export(code_exports, {
20
+ worker: () => worker
21
+ });
22
+ module.exports = __toCommonJS(code_exports);
23
+ const worker = () => `
24
+ // work entry code
25
+ import { createHandler, handleUrl } from "@modern-js/prod-server/worker";
26
+ import { manifest } from "./manifest";
27
+ async function handleRequest(request) {
28
+ const context = {
29
+ request,
30
+ url: handleUrl(request.url),
31
+ body: null,
32
+ };
33
+ const handler = createHandler(manifest);
34
+ await handler(context);
35
+ return new Response(context.body);
36
+ }
37
+
38
+ addEventListener("fetch", (event) => {
39
+ event.respondWith(handleRequest(event.request));
40
+ });
41
+ `;
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ worker
45
+ });
@@ -0,0 +1,46 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var constants_exports = {};
19
+ __export(constants_exports, {
20
+ LOCK_FILE: () => LOCK_FILE,
21
+ MANIFEST_FILE: () => MANIFEST_FILE,
22
+ PKG_FILE: () => PKG_FILE,
23
+ WORKER_SERVER: () => WORKER_SERVER,
24
+ WORKER_SERVER_ENTRY: () => WORKER_SERVER_ENTRY,
25
+ WRANGLER_FILE: () => WRANGLER_FILE
26
+ });
27
+ module.exports = __toCommonJS(constants_exports);
28
+ const WORKER_SERVER = "worker-server";
29
+ const WORKER_SERVER_ENTRY = "index.js";
30
+ const MANIFEST_FILE = "manifest.js";
31
+ const PKG_FILE = "package.json";
32
+ const LOCK_FILE = {
33
+ yarn: "yarn.lock",
34
+ npm: "package-lock.json",
35
+ pnpm: "pnpm-lock.yaml"
36
+ };
37
+ const WRANGLER_FILE = "wrangler.toml";
38
+ // Annotate the CommonJS export names for ESM import in node:
39
+ 0 && (module.exports = {
40
+ LOCK_FILE,
41
+ MANIFEST_FILE,
42
+ PKG_FILE,
43
+ WORKER_SERVER,
44
+ WORKER_SERVER_ENTRY,
45
+ WRANGLER_FILE
46
+ });
@@ -0,0 +1,135 @@
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
+ beforeDeploy() {
59
+ return __async(this, null, function* () {
60
+ const { appDirectory, distDirectory } = ctx.useAppContext();
61
+ const configContext = ctx.useResolvedConfigContext();
62
+ if (!(0, import_utils.isWorker)(configContext)) {
63
+ return;
64
+ }
65
+ const workServerDir = import_path.default.join(distDirectory, import_constants.WORKER_SERVER);
66
+ import_utils.fs.removeSync(workServerDir);
67
+ import_utils.fs.mkdirSync(workServerDir);
68
+ import_utils.fs.writeFileSync(
69
+ import_path.default.join(workServerDir, import_constants.WORKER_SERVER_ENTRY),
70
+ (0, import_code.worker)()
71
+ );
72
+ const routeJSON = import_path.default.join(distDirectory, import_utils.ROUTE_SPEC_FILE);
73
+ const { routes } = import_utils.fs.readJSONSync(routeJSON);
74
+ let importStr = ``;
75
+ let pageStr = ``;
76
+ const routeArr = [];
77
+ routes.forEach(
78
+ (route) => {
79
+ importStr += `import { serverRender as ${route.entryName}ServerRender } from "../${route.bundle}";
80
+ `;
81
+ pageStr += `"${route.urlPath}": {
82
+ entryName: "${route.entryName}",
83
+ template: "${route.entryPath}",
84
+ serverRender: ${route.entryName}ServerRender,
85
+ },`;
86
+ routeArr.push({
87
+ entryName: route.entryName,
88
+ isSSR: route.isSSR,
89
+ urlPath: route.urlPath
90
+ });
91
+ }
92
+ );
93
+ const manifest = `
94
+ ${importStr}
95
+
96
+ export const manifest = {
97
+ pages: {
98
+ ${pageStr}
99
+ },
100
+ routes: ${JSON.stringify(routeArr, null, " ")}
101
+ }
102
+ `;
103
+ import_utils.fs.writeFileSync(import_path.default.join(workServerDir, import_constants.MANIFEST_FILE), manifest);
104
+ const pkg = import_utils.fs.readJSONSync(import_path.default.join(appDirectory, import_constants.PKG_FILE));
105
+ yield import_utils.fs.writeJSON(
106
+ import_path.default.join(distDirectory, import_constants.PKG_FILE),
107
+ {
108
+ name: pkg.name,
109
+ version: pkg.version,
110
+ dependencies: {
111
+ "@modern-js/prod-server": "0.0.0-next-20230203070739",
112
+ wrangler: "^2.9.0"
113
+ },
114
+ resolutions: pkg.resolutions || {},
115
+ pnpm: pkg.pnpm || {}
116
+ },
117
+ { spaces: 2 }
118
+ );
119
+ const manager = yield (0, import_utils.getPackageManager)(appDirectory);
120
+ const lockfile = import_constants.LOCK_FILE[manager];
121
+ (0, import_utils2.copyfile)(distDirectory, appDirectory, [lockfile]);
122
+ import_utils.fs.writeFileSync(
123
+ import_path.default.join(distDirectory, import_constants.WRANGLER_FILE),
124
+ `name = "${pkg.name}"
125
+ main = "${import_path.default.join(import_constants.WORKER_SERVER, import_constants.WORKER_SERVER_ENTRY)}"
126
+ compatibility_date = "${new Date()}"
127
+ `
128
+ );
129
+ });
130
+ }
131
+ };
132
+ }
133
+ });
134
+ // Annotate the CommonJS export names for ESM import in node:
135
+ 0 && (module.exports = {});
@@ -0,0 +1,59 @@
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 utils_exports = {};
25
+ __export(utils_exports, {
26
+ copyfile: () => copyfile
27
+ });
28
+ module.exports = __toCommonJS(utils_exports);
29
+ var import_path = __toESM(require("path"));
30
+ var import_utils = require("@modern-js/utils");
31
+ const copyfile = (target, source, fl) => {
32
+ fl.forEach((ff) => {
33
+ if (!ff) {
34
+ return;
35
+ }
36
+ if (typeof ff === "string") {
37
+ cpfile(target, source, ff);
38
+ return;
39
+ }
40
+ const sourceIncludeFiles = import_utils.fs.readdirSync(source);
41
+ for (const filename of sourceIncludeFiles) {
42
+ if (ff.test(filename)) {
43
+ cpfile(target, source, filename);
44
+ }
45
+ }
46
+ });
47
+ function cpfile(target2, source2, filename) {
48
+ const filepath = import_path.default.join(source2, filename);
49
+ if (!import_utils.fs.existsSync(filepath)) {
50
+ return;
51
+ }
52
+ const targetPath = import_path.default.join(target2, filename);
53
+ import_utils.fs.copySync(filepath, targetPath);
54
+ }
55
+ };
56
+ // Annotate the CommonJS export names for ESM import in node:
57
+ 0 && (module.exports = {
58
+ copyfile
59
+ });
@@ -0,0 +1,4 @@
1
+ var worker = function() {
2
+ return '\n// work entry code\nimport { createHandler, handleUrl } from "@modern-js/prod-server/worker";\nimport { manifest } from "./manifest";\nasync function handleRequest(request) {\n const context = {\n request,\n url: handleUrl(request.url),\n body: null,\n };\n const handler = createHandler(manifest);\n await handler(context);\n return new Response(context.body);\n}\n\naddEventListener("fetch", (event) => {\n event.respondWith(handleRequest(event.request));\n});\n';
3
+ };
4
+ export { worker };
@@ -0,0 +1,11 @@
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 };
@@ -0,0 +1,208 @@
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, isWorker, 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
+ beforeDeploy: function beforeDeploy() {
136
+ return _asyncToGenerator(function() {
137
+ var _ctx_useAppContext, appDirectory, distDirectory, configContext, workServerDir, routeJSON, routes, importStr, pageStr, routeArr, manifest, pkg, manager, lockfile;
138
+ return __generator(this, function(_state) {
139
+ switch(_state.label){
140
+ case 0:
141
+ _ctx_useAppContext = ctx.useAppContext(), appDirectory = _ctx_useAppContext.appDirectory, distDirectory = _ctx_useAppContext.distDirectory;
142
+ configContext = ctx.useResolvedConfigContext();
143
+ if (!isWorker(configContext)) {
144
+ return [
145
+ 2
146
+ ];
147
+ }
148
+ workServerDir = path.join(distDirectory, WORKER_SERVER);
149
+ fs.removeSync(workServerDir);
150
+ fs.mkdirSync(workServerDir);
151
+ fs.writeFileSync(path.join(workServerDir, WORKER_SERVER_ENTRY), worker());
152
+ routeJSON = path.join(distDirectory, ROUTE_SPEC_FILE);
153
+ routes = fs.readJSONSync(routeJSON).routes;
154
+ importStr = "";
155
+ pageStr = "";
156
+ routeArr = [];
157
+ routes.forEach(function(route) {
158
+ importStr += "import { serverRender as ".concat(route.entryName, 'ServerRender } from "../').concat(route.bundle, '";\n');
159
+ pageStr += '"'.concat(route.urlPath, '": {\n entryName: "').concat(route.entryName, '",\n template: "').concat(route.entryPath, '",\n serverRender: ').concat(route.entryName, "ServerRender,\n },");
160
+ routeArr.push({
161
+ entryName: route.entryName,
162
+ isSSR: route.isSSR,
163
+ urlPath: route.urlPath
164
+ });
165
+ });
166
+ manifest = "\n".concat(importStr, "\n\nexport const manifest = {\n pages: {\n ").concat(pageStr, "\n },\n routes: ").concat(JSON.stringify(routeArr, null, " "), "\n}\n ");
167
+ fs.writeFileSync(path.join(workServerDir, MANIFEST_FILE), manifest);
168
+ pkg = fs.readJSONSync(path.join(appDirectory, PKG_FILE));
169
+ return [
170
+ 4,
171
+ fs.writeJSON(path.join(distDirectory, PKG_FILE), {
172
+ name: pkg.name,
173
+ version: pkg.version,
174
+ dependencies: {
175
+ "@modern-js/prod-server": "0.0.0-next-20230203070739",
176
+ wrangler: "^2.9.0"
177
+ },
178
+ resolutions: pkg.resolutions || {},
179
+ pnpm: pkg.pnpm || {}
180
+ }, {
181
+ spaces: 2
182
+ })
183
+ ];
184
+ case 1:
185
+ _state.sent();
186
+ return [
187
+ 4,
188
+ getPackageManager(appDirectory)
189
+ ];
190
+ case 2:
191
+ manager = _state.sent();
192
+ lockfile = LOCK_FILE[manager];
193
+ copyfile(distDirectory, appDirectory, [
194
+ lockfile
195
+ ]);
196
+ 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(), '"\n '));
197
+ return [
198
+ 2
199
+ ];
200
+ }
201
+ });
202
+ })();
203
+ }
204
+ };
205
+ }
206
+ };
207
+ };
208
+ export { src_default as default };
@@ -0,0 +1,45 @@
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 };
@@ -0,0 +1 @@
1
+ export declare const worker: () => string;
@@ -0,0 +1,6 @@
1
+ export declare const WORKER_SERVER = "worker-server";
2
+ export declare const WORKER_SERVER_ENTRY = "index.js";
3
+ export declare const MANIFEST_FILE = "manifest.js";
4
+ export declare const PKG_FILE = "package.json";
5
+ export declare const LOCK_FILE: Record<string, string>;
6
+ export declare const WRANGLER_FILE = "wrangler.toml";
@@ -0,0 +1,5 @@
1
+ import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2
+
3
+ declare const _default: () => CliPlugin<AppTools>;
4
+
5
+ export default _default;
@@ -0,0 +1 @@
1
+ export declare const copyfile: (target: string, source: string, fl: (string | RegExp)[]) => void;
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@modern-js/plugin-worker",
3
+ "description": "The meta-framework suite designed from scratch for frontend-focused modern web development.",
4
+ "homepage": "https://modernjs.dev",
5
+ "bugs": "https://github.com/modern-js-dev/modern.js/issues",
6
+ "repository": "modern-js-dev/modern.js",
7
+ "license": "MIT",
8
+ "keywords": [
9
+ "react",
10
+ "framework",
11
+ "modern",
12
+ "modern.js"
13
+ ],
14
+ "version": "0.0.0-next-20230206081248",
15
+ "types": "./dist/types/cli.d.ts",
16
+ "jsnext:source": "./src/index.ts",
17
+ "main": "./dist/js/node/index.js",
18
+ "exports": {
19
+ ".": {
20
+ "jsnext:source": "./src/index.ts",
21
+ "default": "./dist/js/node/index.js"
22
+ },
23
+ "./cli": {
24
+ "jsnext:source": "./src/index.ts",
25
+ "default": "./dist/js/node/index.js"
26
+ }
27
+ },
28
+ "typesVersions": {
29
+ "*": {
30
+ ".": [
31
+ "./dist/types/index.d.ts"
32
+ ],
33
+ "cli": [
34
+ "./dist/types/index.d.ts"
35
+ ]
36
+ }
37
+ },
38
+ "dependencies": {
39
+ "@babel/runtime": "^7.18.0",
40
+ "@modern-js/server-utils": "0.0.0-next-20230206081248",
41
+ "@modern-js/utils": "0.0.0-next-20230206081248"
42
+ },
43
+ "devDependencies": {
44
+ "typescript": "^4",
45
+ "@types/jest": "^27",
46
+ "@types/node": "^14",
47
+ "jest": "^27",
48
+ "@modern-js/app-tools": "0.0.0-next-20230206081248",
49
+ "@modern-js/core": "0.0.0-next-20230206081248",
50
+ "@scripts/build": "0.0.0-next-20230206081248",
51
+ "@modern-js/types": "0.0.0-next-20230206081248",
52
+ "@scripts/jest-config": "0.0.0-next-20230206081248"
53
+ },
54
+ "sideEffects": [
55
+ "*.css",
56
+ "*.less",
57
+ "*.sass",
58
+ "*.scss"
59
+ ],
60
+ "publishConfig": {
61
+ "access": "public",
62
+ "registry": "https://registry.npmjs.org/"
63
+ },
64
+ "scripts": {
65
+ "dev": "modern-lib build --watch",
66
+ "build": "modern-lib build",
67
+ "new": "modern-lib new",
68
+ "test": "jest --passWithNoTests"
69
+ }
70
+ }