@faasjs/load 0.0.2-beta.269 → 0.0.2-beta.314

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/README.md CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  资源加载类库
4
4
 
5
- [![License: MIT](https://img.shields.io/npm/l/@faasjs/load.svg)](https://github.com/faasjs/faasjs/blob/master/packages/faasjs/load/LICENSE)
5
+ [![License: MIT](https://img.shields.io/npm/l/@faasjs/load.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/load/LICENSE)
6
6
  [![NPM Stable Version](https://img.shields.io/npm/v/@faasjs/load/stable.svg)](https://www.npmjs.com/package/@faasjs/load)
7
7
  [![NPM Beta Version](https://img.shields.io/npm/v/@faasjs/load/beta.svg)](https://www.npmjs.com/package/@faasjs/load)
@@ -0,0 +1,63 @@
1
+ import { Config as Config$1, Func } from '@faasjs/func';
2
+
3
+ /**
4
+ * 配置类
5
+ */
6
+ declare class Config {
7
+ [key: string]: any;
8
+ readonly root: string;
9
+ readonly filename: string;
10
+ readonly origin: {
11
+ [key: string]: Config$1;
12
+ defaults: Config$1;
13
+ };
14
+ readonly defaults: Config$1;
15
+ /**
16
+ * 创建配置类,并自动读取配置内容
17
+ *
18
+ * @param root {string} 根目录
19
+ * @param filename {filename} 目标文件,用于读取目录层级
20
+ */
21
+ constructor(root: string, filename: string);
22
+ }
23
+ /**
24
+ * 加载配置
25
+ * @param root {string} 根目录
26
+ * @param filename {filename} 目标文件,用于读取目录层级
27
+ */
28
+ declare function loadConfig(root: string, filename: string): Config;
29
+
30
+ /**
31
+ * 加载 ts 文件
32
+ *
33
+ * @param filename {string} 完整源文件路径
34
+ * @param options {object} 配置项
35
+ * @param options.input {object} 读取配置
36
+ * @param options.output {object} 写入配置
37
+ * @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
38
+ * @param options.modules {object} 生成 modules 的配置
39
+ * @param options.modules.excludes {string[]} modules 中需排除的模块
40
+ */
41
+ declare function loadTs(filename: string, options?: {
42
+ input?: {
43
+ [key: string]: any;
44
+ };
45
+ output?: {
46
+ [key: string]: any;
47
+ };
48
+ tmp?: boolean;
49
+ modules?: {
50
+ excludes?: string[];
51
+ additions?: string[];
52
+ };
53
+ }): Promise<{
54
+ module?: Func;
55
+ dependencies: {
56
+ [key: string]: string;
57
+ };
58
+ modules?: {
59
+ [key: string]: string;
60
+ };
61
+ }>;
62
+
63
+ export { loadConfig, loadTs };
package/dist/index.js ADDED
@@ -0,0 +1,237 @@
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 __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __reExport = (target, module2, copyDefault, desc) => {
13
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
14
+ for (let key of __getOwnPropNames(module2))
15
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
16
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
17
+ }
18
+ return target;
19
+ };
20
+ var __toESM = (module2, isNodeMode) => {
21
+ return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", !isNodeMode && module2 && module2.__esModule ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
22
+ };
23
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
24
+ return (module2, temp) => {
25
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
26
+ };
27
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ loadConfig: () => loadConfig,
33
+ loadTs: () => loadTs
34
+ });
35
+
36
+ // src/load_config.ts
37
+ var import_deep_merge = require("@faasjs/deep_merge");
38
+ var import_fs = require("fs");
39
+ var import_path = require("path");
40
+ var import_js_yaml = require("js-yaml");
41
+ var Config = class {
42
+ constructor(root, filename) {
43
+ this.root = root;
44
+ if (!this.root.endsWith(import_path.sep))
45
+ this.root += import_path.sep;
46
+ this.filename = filename;
47
+ const configs = [];
48
+ const paths = [this.root, "."].concat((0, import_path.dirname)(filename.replace(root, "")).split(import_path.sep));
49
+ paths.reduce(function(base, path) {
50
+ const root2 = (0, import_path.join)(base, path);
51
+ if (root2 === base)
52
+ return base;
53
+ const faas = (0, import_path.join)(root2, "faas.yaml");
54
+ if ((0, import_fs.existsSync)(faas))
55
+ configs.push((0, import_js_yaml.load)((0, import_fs.readFileSync)(faas).toString()));
56
+ return root2;
57
+ });
58
+ this.origin = (0, import_deep_merge.deepMerge)(...configs);
59
+ this.defaults = (0, import_deep_merge.deepMerge)(this.origin.defaults || {});
60
+ for (const key in this.origin) {
61
+ if (key !== "defaults")
62
+ this[key] = (0, import_deep_merge.deepMerge)(this.origin.defaults, this.origin[key]);
63
+ const data = this[key];
64
+ if (data.plugins)
65
+ for (const pluginKey in data.plugins) {
66
+ const plugin = data.plugins[pluginKey];
67
+ plugin.name = pluginKey;
68
+ if (plugin.provider)
69
+ if (typeof plugin.provider === "string") {
70
+ if (!data.providers[plugin.provider])
71
+ throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
72
+ plugin.provider = data.providers[plugin.provider];
73
+ } else
74
+ plugin.provider = (0, import_deep_merge.deepMerge)(data.providers[plugin.provider], plugin.provider);
75
+ }
76
+ }
77
+ }
78
+ };
79
+ function loadConfig(root, filename) {
80
+ return new Config(root, filename);
81
+ }
82
+
83
+ // src/load_ts.ts
84
+ var import_deep_merge2 = require("@faasjs/deep_merge");
85
+ var import_fs2 = require("fs");
86
+ var import_rollup = require("rollup");
87
+ var import_path2 = require("path");
88
+ var import_plugin_node_resolve = __toESM(require("@rollup/plugin-node-resolve"));
89
+ var import_core = require("@swc/core");
90
+ var FAAS_PACKAGES = [
91
+ "@faasjs/browser",
92
+ "@faasjs/cli",
93
+ "@faasjs/cloud_function",
94
+ "create-faas-app",
95
+ "@faasjs/deep_merge",
96
+ "@faasjs/deployer",
97
+ "@faasjs/eslint-config-recommended",
98
+ "@faasjs/eslint-config-vue",
99
+ "faasjs",
100
+ "@faasjs/func",
101
+ "@faasjs/graphql-server",
102
+ "@faasjs/http",
103
+ "@faasjs/knex",
104
+ "@faasjs/load",
105
+ "@faasjs/logger",
106
+ "@faasjs/react",
107
+ "@faasjs/redis",
108
+ "@faasjs/request",
109
+ "@faasjs/server",
110
+ "@faasjs/tencentcloud",
111
+ "@faasjs/test",
112
+ "@faasjs/vue-plugin"
113
+ ];
114
+ var NODE_PACKAGES = [
115
+ "async_hooks",
116
+ "child_process",
117
+ "cluster",
118
+ "crypto",
119
+ "dns",
120
+ "events",
121
+ "fs",
122
+ "http",
123
+ "http2",
124
+ "https",
125
+ "inspector",
126
+ "net",
127
+ "os",
128
+ "path",
129
+ "perf_hooks",
130
+ "process",
131
+ "querystring",
132
+ "readline",
133
+ "repl",
134
+ "stream",
135
+ "string_decoder",
136
+ "tls",
137
+ "trace_events",
138
+ "tty",
139
+ "dgram",
140
+ "udp4",
141
+ "url",
142
+ "util",
143
+ "v8",
144
+ "vm",
145
+ "wasi",
146
+ "worker_threads",
147
+ "zlib"
148
+ ];
149
+ function findModule(list, key, basePath, options = { excludes: [] }) {
150
+ if (list[key])
151
+ return;
152
+ if (key.startsWith("@types/") || options.excludes.includes(key))
153
+ return;
154
+ const paths = [(0, import_path2.join)(process.cwd(), "node_modules", key), (0, import_path2.join)(basePath, "node_modules", key)];
155
+ let path;
156
+ for (const p of paths)
157
+ if ((0, import_fs2.existsSync)(p)) {
158
+ path = p;
159
+ break;
160
+ }
161
+ if (!path)
162
+ return;
163
+ list[key] = path;
164
+ if ((0, import_fs2.existsSync)((0, import_path2.join)(path, "package.json"))) {
165
+ const pkg = JSON.parse((0, import_fs2.readFileSync)((0, import_path2.join)(path, "package.json")).toString());
166
+ const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
167
+ deps.map((d) => findModule(list, d, path, options));
168
+ }
169
+ }
170
+ function swc(options) {
171
+ return {
172
+ name: "swc",
173
+ async transform(code, filename) {
174
+ options.filename = filename;
175
+ return (0, import_core.transform)(code, options);
176
+ }
177
+ };
178
+ }
179
+ async function loadTs(filename, options = Object.create(null)) {
180
+ var _a;
181
+ const PackageJSON = require(`${process.cwd()}/package.json`);
182
+ const external = PackageJSON.dependencies ? FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
183
+ if (options.modules && options.modules.excludes == null)
184
+ options.modules.excludes = [];
185
+ const input = (0, import_deep_merge2.deepMerge)({
186
+ input: filename,
187
+ external,
188
+ plugins: [
189
+ (0, import_plugin_node_resolve.default)({
190
+ extensions: [
191
+ ".ts",
192
+ ".tsx",
193
+ ".js",
194
+ ".jsx"
195
+ ]
196
+ }),
197
+ swc({
198
+ jsc: {
199
+ parser: { syntax: "typescript" },
200
+ target: "es2021"
201
+ }
202
+ })
203
+ ],
204
+ onwarn: () => null
205
+ }, options.input || {});
206
+ const bundle = await (0, import_rollup.rollup)(input);
207
+ const dependencies = Object.create(null);
208
+ for (const m of ((_a = bundle.cache) == null ? void 0 : _a.modules) || [])
209
+ for (const d of m.dependencies)
210
+ if (!d.startsWith("/") && !dependencies[d] && !NODE_PACKAGES.includes(d))
211
+ dependencies[d] = "*";
212
+ const output = (0, import_deep_merge2.deepMerge)({
213
+ file: filename + ".tmp.js",
214
+ format: "cjs",
215
+ exports: "auto"
216
+ }, options.output || {});
217
+ await bundle.write(output);
218
+ const result = Object.create(null);
219
+ result.dependencies = dependencies;
220
+ result.module = require(output.file);
221
+ if (options.tmp)
222
+ (0, import_fs2.unlinkSync)(output.file);
223
+ if (options.modules) {
224
+ const modules = Object.create(null);
225
+ Object.keys(dependencies).map((d) => findModule(modules, d, process.cwd(), options.modules));
226
+ if (options.modules.additions)
227
+ options.modules.additions.map((d) => findModule(modules, d, process.cwd(), options.modules));
228
+ result.modules = modules;
229
+ }
230
+ return result;
231
+ }
232
+ module.exports = __toCommonJS(src_exports);
233
+ // Annotate the CommonJS export names for ESM import in node:
234
+ 0 && (module.exports = {
235
+ loadConfig,
236
+ loadTs
237
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,216 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw new Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // src/load_config.ts
10
+ import { deepMerge } from "@faasjs/deep_merge";
11
+ import { existsSync, readFileSync } from "fs";
12
+ import {
13
+ sep,
14
+ dirname,
15
+ join
16
+ } from "path";
17
+ import { load } from "js-yaml";
18
+ var Config = class {
19
+ constructor(root, filename) {
20
+ this.root = root;
21
+ if (!this.root.endsWith(sep))
22
+ this.root += sep;
23
+ this.filename = filename;
24
+ const configs = [];
25
+ const paths = [this.root, "."].concat(dirname(filename.replace(root, "")).split(sep));
26
+ paths.reduce(function(base, path) {
27
+ const root2 = join(base, path);
28
+ if (root2 === base)
29
+ return base;
30
+ const faas = join(root2, "faas.yaml");
31
+ if (existsSync(faas))
32
+ configs.push(load(readFileSync(faas).toString()));
33
+ return root2;
34
+ });
35
+ this.origin = deepMerge(...configs);
36
+ this.defaults = deepMerge(this.origin.defaults || {});
37
+ for (const key in this.origin) {
38
+ if (key !== "defaults")
39
+ this[key] = deepMerge(this.origin.defaults, this.origin[key]);
40
+ const data = this[key];
41
+ if (data.plugins)
42
+ for (const pluginKey in data.plugins) {
43
+ const plugin = data.plugins[pluginKey];
44
+ plugin.name = pluginKey;
45
+ if (plugin.provider)
46
+ if (typeof plugin.provider === "string") {
47
+ if (!data.providers[plugin.provider])
48
+ throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
49
+ plugin.provider = data.providers[plugin.provider];
50
+ } else
51
+ plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
52
+ }
53
+ }
54
+ }
55
+ };
56
+ function loadConfig(root, filename) {
57
+ return new Config(root, filename);
58
+ }
59
+
60
+ // src/load_ts.ts
61
+ import { deepMerge as deepMerge2 } from "@faasjs/deep_merge";
62
+ import {
63
+ existsSync as existsSync2,
64
+ readFileSync as readFileSync2,
65
+ unlinkSync
66
+ } from "fs";
67
+ import { rollup } from "rollup";
68
+ import { join as join2 } from "path";
69
+ import resolve from "@rollup/plugin-node-resolve";
70
+ import { transform } from "@swc/core";
71
+ var FAAS_PACKAGES = [
72
+ "@faasjs/browser",
73
+ "@faasjs/cli",
74
+ "@faasjs/cloud_function",
75
+ "create-faas-app",
76
+ "@faasjs/deep_merge",
77
+ "@faasjs/deployer",
78
+ "@faasjs/eslint-config-recommended",
79
+ "@faasjs/eslint-config-vue",
80
+ "faasjs",
81
+ "@faasjs/func",
82
+ "@faasjs/graphql-server",
83
+ "@faasjs/http",
84
+ "@faasjs/knex",
85
+ "@faasjs/load",
86
+ "@faasjs/logger",
87
+ "@faasjs/react",
88
+ "@faasjs/redis",
89
+ "@faasjs/request",
90
+ "@faasjs/server",
91
+ "@faasjs/tencentcloud",
92
+ "@faasjs/test",
93
+ "@faasjs/vue-plugin"
94
+ ];
95
+ var NODE_PACKAGES = [
96
+ "async_hooks",
97
+ "child_process",
98
+ "cluster",
99
+ "crypto",
100
+ "dns",
101
+ "events",
102
+ "fs",
103
+ "http",
104
+ "http2",
105
+ "https",
106
+ "inspector",
107
+ "net",
108
+ "os",
109
+ "path",
110
+ "perf_hooks",
111
+ "process",
112
+ "querystring",
113
+ "readline",
114
+ "repl",
115
+ "stream",
116
+ "string_decoder",
117
+ "tls",
118
+ "trace_events",
119
+ "tty",
120
+ "dgram",
121
+ "udp4",
122
+ "url",
123
+ "util",
124
+ "v8",
125
+ "vm",
126
+ "wasi",
127
+ "worker_threads",
128
+ "zlib"
129
+ ];
130
+ function findModule(list, key, basePath, options = { excludes: [] }) {
131
+ if (list[key])
132
+ return;
133
+ if (key.startsWith("@types/") || options.excludes.includes(key))
134
+ return;
135
+ const paths = [join2(process.cwd(), "node_modules", key), join2(basePath, "node_modules", key)];
136
+ let path;
137
+ for (const p of paths)
138
+ if (existsSync2(p)) {
139
+ path = p;
140
+ break;
141
+ }
142
+ if (!path)
143
+ return;
144
+ list[key] = path;
145
+ if (existsSync2(join2(path, "package.json"))) {
146
+ const pkg = JSON.parse(readFileSync2(join2(path, "package.json")).toString());
147
+ const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
148
+ deps.map((d) => findModule(list, d, path, options));
149
+ }
150
+ }
151
+ function swc(options) {
152
+ return {
153
+ name: "swc",
154
+ async transform(code, filename) {
155
+ options.filename = filename;
156
+ return transform(code, options);
157
+ }
158
+ };
159
+ }
160
+ async function loadTs(filename, options = Object.create(null)) {
161
+ var _a;
162
+ const PackageJSON = __require(`${process.cwd()}/package.json`);
163
+ const external = PackageJSON.dependencies ? FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
164
+ if (options.modules && options.modules.excludes == null)
165
+ options.modules.excludes = [];
166
+ const input = deepMerge2({
167
+ input: filename,
168
+ external,
169
+ plugins: [
170
+ resolve({
171
+ extensions: [
172
+ ".ts",
173
+ ".tsx",
174
+ ".js",
175
+ ".jsx"
176
+ ]
177
+ }),
178
+ swc({
179
+ jsc: {
180
+ parser: { syntax: "typescript" },
181
+ target: "es2021"
182
+ }
183
+ })
184
+ ],
185
+ onwarn: () => null
186
+ }, options.input || {});
187
+ const bundle = await rollup(input);
188
+ const dependencies = Object.create(null);
189
+ for (const m of ((_a = bundle.cache) == null ? void 0 : _a.modules) || [])
190
+ for (const d of m.dependencies)
191
+ if (!d.startsWith("/") && !dependencies[d] && !NODE_PACKAGES.includes(d))
192
+ dependencies[d] = "*";
193
+ const output = deepMerge2({
194
+ file: filename + ".tmp.js",
195
+ format: "cjs",
196
+ exports: "auto"
197
+ }, options.output || {});
198
+ await bundle.write(output);
199
+ const result = Object.create(null);
200
+ result.dependencies = dependencies;
201
+ result.module = __require(output.file);
202
+ if (options.tmp)
203
+ unlinkSync(output.file);
204
+ if (options.modules) {
205
+ const modules = Object.create(null);
206
+ Object.keys(dependencies).map((d) => findModule(modules, d, process.cwd(), options.modules));
207
+ if (options.modules.additions)
208
+ options.modules.additions.map((d) => findModule(modules, d, process.cwd(), options.modules));
209
+ result.modules = modules;
210
+ }
211
+ return result;
212
+ }
213
+ export {
214
+ loadConfig,
215
+ loadTs
216
+ };
package/package.json CHANGED
@@ -1,37 +1,41 @@
1
1
  {
2
2
  "name": "@faasjs/load",
3
- "version": "0.0.2-beta.269",
3
+ "version": "0.0.2-beta.314",
4
4
  "license": "MIT",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "module": "lib/index.es.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "module": "dist/index.mjs",
8
8
  "homepage": "https://faasjs.com/doc/load.html",
9
9
  "repository": {
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/faasjs/faasjs.git",
12
12
  "directory": "packages/load"
13
13
  },
14
+ "bugs": {
15
+ "url": "https://github.com/faasjs/faasjs/issues"
16
+ },
17
+ "funding": "https://github.com/sponsors/faasjs",
14
18
  "scripts": {
15
- "lint": "eslint --ext .ts src",
16
- "prepack": "rm -rf ./lib && rollup -c && mv lib/*/src/* lib/"
19
+ "build": "rm -rf ./dist && tsup-node src/index.ts --format esm,cjs --dts"
17
20
  },
18
21
  "files": [
19
- "lib"
22
+ "dist"
20
23
  ],
21
24
  "peerDependencies": {
22
25
  "@faasjs/deep_merge": "^0.0.2-beta.244",
23
- "@rollup/plugin-typescript": "*",
26
+ "@faasjs/func": "^0.0.2-beta.302",
27
+ "@rollup/plugin-node-resolve": "*",
28
+ "@swc/core": "*",
24
29
  "js-yaml": "*",
25
- "rollup": "*",
26
- "typescript": "*",
27
- "vm2": "*"
30
+ "rollup": "*"
28
31
  },
29
32
  "devDependencies": {
30
- "@faasjs/func": "^0.0.2-beta.269",
31
- "@types/debug": "*",
32
- "@types/jest": "*",
33
33
  "@types/js-yaml": "*",
34
- "@types/node": "*"
34
+ "tsup": "*",
35
+ "typescript": "*"
36
+ },
37
+ "engines": {
38
+ "npm": ">=8.0.0"
35
39
  },
36
- "gitHead": "4654b7c74cfc41e2b85c10a0545ac999e241cf52"
40
+ "gitHead": "60be0e54eb369177b416d109a3f9a5a12dedbf24"
37
41
  }
package/lib/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import loadConfig from './load_config';
2
- import loadTs from './load_ts';
3
- export { loadConfig, loadTs };
package/lib/index.es.js DELETED
@@ -1,217 +0,0 @@
1
- import { deepMerge } from '@faasjs/deep_merge';
2
- import { existsSync, readFileSync, unlinkSync } from 'fs';
3
- import { sep, dirname, join } from 'path';
4
- import { load } from 'js-yaml';
5
- import * as rollup from 'rollup';
6
- import typescript from '@rollup/plugin-typescript';
7
- import { NodeVM } from 'vm2';
8
-
9
- /**
10
- * 配置类
11
- */
12
- class Config {
13
- /**
14
- * 创建配置类,并自动读取配置内容
15
- *
16
- * @param root {string} 根目录
17
- * @param filename {filename} 目标文件,用于读取目录层级
18
- */
19
- constructor(root, filename) {
20
- this.root = root;
21
- if (!this.root.endsWith(sep))
22
- this.root += sep;
23
- this.filename = filename;
24
- const configs = [];
25
- const paths = [this.root, '.'].concat(dirname(filename.replace(root, '')).split(sep));
26
- paths.reduce(function (base, path) {
27
- const root = join(base, path);
28
- if (root === base)
29
- return base;
30
- const faas = join(root, 'faas.yaml');
31
- if (existsSync(faas))
32
- configs.push(load(readFileSync(faas).toString()));
33
- return root;
34
- });
35
- this.origin = deepMerge(...configs);
36
- if (!this.origin.defaults)
37
- throw Error('[faas.yaml] need defaults env.');
38
- this.defaults = deepMerge(this.origin.defaults);
39
- for (const key in this.origin) {
40
- if (key !== 'defaults')
41
- this[key] = deepMerge(this.origin.defaults, this.origin[key]);
42
- const data = this[key];
43
- if (!data.providers)
44
- throw Error(`[faas.yaml] missing key: ${key}/providers`);
45
- if (!data.plugins)
46
- throw Error(`[faas.yaml] missing key: ${key}/plugins`);
47
- for (const pluginKey in data.plugins) {
48
- const plugin = data.plugins[pluginKey];
49
- plugin.name = pluginKey;
50
- if (plugin.provider)
51
- if (typeof plugin.provider === 'string') {
52
- if (!data.providers[plugin.provider])
53
- throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
54
- plugin.provider = data.providers[plugin.provider];
55
- }
56
- else
57
- plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
58
- }
59
- }
60
- }
61
- }
62
- /**
63
- * 加载配置
64
- * @param root {string} 根目录
65
- * @param filename {filename} 目标文件,用于读取目录层级
66
- */
67
- function loadConfig(root, filename) {
68
- return new Config(root, filename);
69
- }
70
-
71
- const FAAS_PACKAGES = [
72
- '@faasjs/browser',
73
- '@faasjs/cli',
74
- '@faasjs/cloud_function',
75
- 'create-faas-app',
76
- '@faasjs/deep_merge',
77
- '@faasjs/deployer',
78
- '@faasjs/eslint-config-recommended',
79
- '@faasjs/eslint-config-vue',
80
- 'faasjs',
81
- '@faasjs/func',
82
- '@faasjs/graphql-server',
83
- '@faasjs/http',
84
- '@faasjs/knex',
85
- '@faasjs/load',
86
- '@faasjs/logger',
87
- '@faasjs/react',
88
- '@faasjs/redis',
89
- '@faasjs/request',
90
- '@faasjs/server',
91
- '@faasjs/tencentcloud',
92
- '@faasjs/test',
93
- '@faasjs/vue-plugin'
94
- ];
95
- const NODE_PACKAGES = [
96
- 'async_hooks',
97
- 'child_process',
98
- 'cluster',
99
- 'crypto',
100
- 'dns',
101
- 'events',
102
- 'fs',
103
- 'http',
104
- 'http2',
105
- 'https',
106
- 'inspector',
107
- 'net',
108
- 'os',
109
- 'path',
110
- 'perf_hooks',
111
- 'process',
112
- 'querystring',
113
- 'readline',
114
- 'repl',
115
- 'stream',
116
- 'string_decoder',
117
- 'tls',
118
- 'trace_events',
119
- 'tty',
120
- 'dgram',
121
- 'udp4',
122
- 'url',
123
- 'util',
124
- 'v8',
125
- 'vm',
126
- 'wasi',
127
- 'worker_threads',
128
- 'zlib'
129
- ];
130
- function findModule(list, key, basePath, options = { excludes: [] }) {
131
- if (list[key])
132
- return;
133
- if (key.startsWith('@types/') || options.excludes.includes(key))
134
- return;
135
- const paths = [join(process.cwd(), 'node_modules', key), join(basePath, 'node_modules', key)];
136
- let path;
137
- for (const p of paths)
138
- if (existsSync(p)) {
139
- path = p;
140
- break;
141
- }
142
- if (!path)
143
- return;
144
- list[key] = path;
145
- if (existsSync(join(path, 'package.json'))) {
146
- const pkg = JSON.parse(readFileSync(join(path, 'package.json')).toString());
147
- const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
148
- deps.map(d => findModule(list, d, path, options));
149
- }
150
- }
151
- /**
152
- * 加载 ts 文件
153
- *
154
- * @param filename {string} 完整源文件路径
155
- * @param options {object} 配置项
156
- * @param options.input {object} 读取配置
157
- * @param options.output {object} 写入配置
158
- * @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
159
- * @param options.modules {object} 生成 modules 的配置
160
- * @param options.modules.excludes {string[]} modules 中需排除的模块
161
- */
162
- async function loadTs(filename, options = Object.create(null)) {
163
- var _a;
164
- // eslint-disable-next-line @typescript-eslint/no-var-requires
165
- const PackageJSON = require(`${process.cwd()}/package.json`);
166
- const external = PackageJSON.dependencies ?
167
- FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
168
- if ((options.modules) && (options.modules.excludes == null))
169
- options.modules.excludes = [];
170
- const input = deepMerge({
171
- input: filename,
172
- external,
173
- plugins: [typescript({ declaration: false })],
174
- onwarn: () => null
175
- }, (options.input) || {});
176
- const bundle = await rollup.rollup(input);
177
- const dependencies = Object.create(null);
178
- for (const m of ((_a = bundle.cache) === null || _a === void 0 ? void 0 : _a.modules) || [])
179
- for (const d of m.dependencies)
180
- if (!d.startsWith('/') &&
181
- !dependencies[d] &&
182
- !NODE_PACKAGES.includes(d))
183
- dependencies[d] = '*';
184
- const output = deepMerge({
185
- file: filename + '.tmp.js',
186
- format: 'cjs',
187
- exports: 'auto'
188
- }, (options.output) || {});
189
- await bundle.write(output);
190
- const result = Object.create(null);
191
- result.dependencies = dependencies;
192
- if (options.vm) {
193
- const vm = new NodeVM({
194
- require: {
195
- external: true,
196
- context: 'sandbox',
197
- builtin: ['*']
198
- }
199
- });
200
- result.module = vm.require(output.file);
201
- }
202
- else
203
- // eslint-disable-next-line @typescript-eslint/no-var-requires
204
- result.module = require(output.file);
205
- if (options.tmp)
206
- unlinkSync(output.file);
207
- if (options.modules) {
208
- const modules = Object.create(null);
209
- Object.keys(dependencies).map(d => findModule(modules, d, process.cwd(), options.modules));
210
- if (options.modules.additions)
211
- options.modules.additions.map(d => findModule(modules, d, process.cwd(), options.modules));
212
- result.modules = modules;
213
- }
214
- return result;
215
- }
216
-
217
- export { loadConfig, loadTs };
package/lib/index.js DELETED
@@ -1,245 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var deep_merge = require('@faasjs/deep_merge');
6
- var fs = require('fs');
7
- var path = require('path');
8
- var jsYaml = require('js-yaml');
9
- var rollup = require('rollup');
10
- var typescript = require('@rollup/plugin-typescript');
11
- var vm2 = require('vm2');
12
-
13
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
-
15
- function _interopNamespace(e) {
16
- if (e && e.__esModule) return e;
17
- var n = Object.create(null);
18
- if (e) {
19
- Object.keys(e).forEach(function (k) {
20
- if (k !== 'default') {
21
- var d = Object.getOwnPropertyDescriptor(e, k);
22
- Object.defineProperty(n, k, d.get ? d : {
23
- enumerable: true,
24
- get: function () { return e[k]; }
25
- });
26
- }
27
- });
28
- }
29
- n["default"] = e;
30
- return Object.freeze(n);
31
- }
32
-
33
- var rollup__namespace = /*#__PURE__*/_interopNamespace(rollup);
34
- var typescript__default = /*#__PURE__*/_interopDefaultLegacy(typescript);
35
-
36
- /**
37
- * 配置类
38
- */
39
- class Config {
40
- /**
41
- * 创建配置类,并自动读取配置内容
42
- *
43
- * @param root {string} 根目录
44
- * @param filename {filename} 目标文件,用于读取目录层级
45
- */
46
- constructor(root, filename) {
47
- this.root = root;
48
- if (!this.root.endsWith(path.sep))
49
- this.root += path.sep;
50
- this.filename = filename;
51
- const configs = [];
52
- const paths = [this.root, '.'].concat(path.dirname(filename.replace(root, '')).split(path.sep));
53
- paths.reduce(function (base, path$1) {
54
- const root = path.join(base, path$1);
55
- if (root === base)
56
- return base;
57
- const faas = path.join(root, 'faas.yaml');
58
- if (fs.existsSync(faas))
59
- configs.push(jsYaml.load(fs.readFileSync(faas).toString()));
60
- return root;
61
- });
62
- this.origin = deep_merge.deepMerge(...configs);
63
- if (!this.origin.defaults)
64
- throw Error('[faas.yaml] need defaults env.');
65
- this.defaults = deep_merge.deepMerge(this.origin.defaults);
66
- for (const key in this.origin) {
67
- if (key !== 'defaults')
68
- this[key] = deep_merge.deepMerge(this.origin.defaults, this.origin[key]);
69
- const data = this[key];
70
- if (!data.providers)
71
- throw Error(`[faas.yaml] missing key: ${key}/providers`);
72
- if (!data.plugins)
73
- throw Error(`[faas.yaml] missing key: ${key}/plugins`);
74
- for (const pluginKey in data.plugins) {
75
- const plugin = data.plugins[pluginKey];
76
- plugin.name = pluginKey;
77
- if (plugin.provider)
78
- if (typeof plugin.provider === 'string') {
79
- if (!data.providers[plugin.provider])
80
- throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
81
- plugin.provider = data.providers[plugin.provider];
82
- }
83
- else
84
- plugin.provider = deep_merge.deepMerge(data.providers[plugin.provider], plugin.provider);
85
- }
86
- }
87
- }
88
- }
89
- /**
90
- * 加载配置
91
- * @param root {string} 根目录
92
- * @param filename {filename} 目标文件,用于读取目录层级
93
- */
94
- function loadConfig(root, filename) {
95
- return new Config(root, filename);
96
- }
97
-
98
- const FAAS_PACKAGES = [
99
- '@faasjs/browser',
100
- '@faasjs/cli',
101
- '@faasjs/cloud_function',
102
- 'create-faas-app',
103
- '@faasjs/deep_merge',
104
- '@faasjs/deployer',
105
- '@faasjs/eslint-config-recommended',
106
- '@faasjs/eslint-config-vue',
107
- 'faasjs',
108
- '@faasjs/func',
109
- '@faasjs/graphql-server',
110
- '@faasjs/http',
111
- '@faasjs/knex',
112
- '@faasjs/load',
113
- '@faasjs/logger',
114
- '@faasjs/react',
115
- '@faasjs/redis',
116
- '@faasjs/request',
117
- '@faasjs/server',
118
- '@faasjs/tencentcloud',
119
- '@faasjs/test',
120
- '@faasjs/vue-plugin'
121
- ];
122
- const NODE_PACKAGES = [
123
- 'async_hooks',
124
- 'child_process',
125
- 'cluster',
126
- 'crypto',
127
- 'dns',
128
- 'events',
129
- 'fs',
130
- 'http',
131
- 'http2',
132
- 'https',
133
- 'inspector',
134
- 'net',
135
- 'os',
136
- 'path',
137
- 'perf_hooks',
138
- 'process',
139
- 'querystring',
140
- 'readline',
141
- 'repl',
142
- 'stream',
143
- 'string_decoder',
144
- 'tls',
145
- 'trace_events',
146
- 'tty',
147
- 'dgram',
148
- 'udp4',
149
- 'url',
150
- 'util',
151
- 'v8',
152
- 'vm',
153
- 'wasi',
154
- 'worker_threads',
155
- 'zlib'
156
- ];
157
- function findModule(list, key, basePath, options = { excludes: [] }) {
158
- if (list[key])
159
- return;
160
- if (key.startsWith('@types/') || options.excludes.includes(key))
161
- return;
162
- const paths = [path.join(process.cwd(), 'node_modules', key), path.join(basePath, 'node_modules', key)];
163
- let path$1;
164
- for (const p of paths)
165
- if (fs.existsSync(p)) {
166
- path$1 = p;
167
- break;
168
- }
169
- if (!path$1)
170
- return;
171
- list[key] = path$1;
172
- if (fs.existsSync(path.join(path$1, 'package.json'))) {
173
- const pkg = JSON.parse(fs.readFileSync(path.join(path$1, 'package.json')).toString());
174
- const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
175
- deps.map(d => findModule(list, d, path$1, options));
176
- }
177
- }
178
- /**
179
- * 加载 ts 文件
180
- *
181
- * @param filename {string} 完整源文件路径
182
- * @param options {object} 配置项
183
- * @param options.input {object} 读取配置
184
- * @param options.output {object} 写入配置
185
- * @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
186
- * @param options.modules {object} 生成 modules 的配置
187
- * @param options.modules.excludes {string[]} modules 中需排除的模块
188
- */
189
- async function loadTs(filename, options = Object.create(null)) {
190
- var _a;
191
- // eslint-disable-next-line @typescript-eslint/no-var-requires
192
- const PackageJSON = require(`${process.cwd()}/package.json`);
193
- const external = PackageJSON.dependencies ?
194
- FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
195
- if ((options.modules) && (options.modules.excludes == null))
196
- options.modules.excludes = [];
197
- const input = deep_merge.deepMerge({
198
- input: filename,
199
- external,
200
- plugins: [typescript__default["default"]({ declaration: false })],
201
- onwarn: () => null
202
- }, (options.input) || {});
203
- const bundle = await rollup__namespace.rollup(input);
204
- const dependencies = Object.create(null);
205
- for (const m of ((_a = bundle.cache) === null || _a === void 0 ? void 0 : _a.modules) || [])
206
- for (const d of m.dependencies)
207
- if (!d.startsWith('/') &&
208
- !dependencies[d] &&
209
- !NODE_PACKAGES.includes(d))
210
- dependencies[d] = '*';
211
- const output = deep_merge.deepMerge({
212
- file: filename + '.tmp.js',
213
- format: 'cjs',
214
- exports: 'auto'
215
- }, (options.output) || {});
216
- await bundle.write(output);
217
- const result = Object.create(null);
218
- result.dependencies = dependencies;
219
- if (options.vm) {
220
- const vm = new vm2.NodeVM({
221
- require: {
222
- external: true,
223
- context: 'sandbox',
224
- builtin: ['*']
225
- }
226
- });
227
- result.module = vm.require(output.file);
228
- }
229
- else
230
- // eslint-disable-next-line @typescript-eslint/no-var-requires
231
- result.module = require(output.file);
232
- if (options.tmp)
233
- fs.unlinkSync(output.file);
234
- if (options.modules) {
235
- const modules = Object.create(null);
236
- Object.keys(dependencies).map(d => findModule(modules, d, process.cwd(), options.modules));
237
- if (options.modules.additions)
238
- options.modules.additions.map(d => findModule(modules, d, process.cwd(), options.modules));
239
- result.modules = modules;
240
- }
241
- return result;
242
- }
243
-
244
- exports.loadConfig = loadConfig;
245
- exports.loadTs = loadTs;
@@ -1,27 +0,0 @@
1
- import { Config as FuncConfig } from '@faasjs/func';
2
- /**
3
- * 配置类
4
- */
5
- export declare class Config {
6
- [key: string]: any;
7
- readonly root: string;
8
- readonly filename: string;
9
- readonly origin: {
10
- [key: string]: FuncConfig;
11
- defaults: FuncConfig;
12
- };
13
- readonly defaults: FuncConfig;
14
- /**
15
- * 创建配置类,并自动读取配置内容
16
- *
17
- * @param root {string} 根目录
18
- * @param filename {filename} 目标文件,用于读取目录层级
19
- */
20
- constructor(root: string, filename: string);
21
- }
22
- /**
23
- * 加载配置
24
- * @param root {string} 根目录
25
- * @param filename {filename} 目标文件,用于读取目录层级
26
- */
27
- export default function loadConfig(root: string, filename: string): Config;
package/lib/load_ts.d.ts DELETED
@@ -1,34 +0,0 @@
1
- import { Func } from '@faasjs/func';
2
- /**
3
- * 加载 ts 文件
4
- *
5
- * @param filename {string} 完整源文件路径
6
- * @param options {object} 配置项
7
- * @param options.input {object} 读取配置
8
- * @param options.output {object} 写入配置
9
- * @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
10
- * @param options.modules {object} 生成 modules 的配置
11
- * @param options.modules.excludes {string[]} modules 中需排除的模块
12
- */
13
- export default function loadTs(filename: string, options?: {
14
- input?: {
15
- [key: string]: any;
16
- };
17
- output?: {
18
- [key: string]: any;
19
- };
20
- tmp?: boolean;
21
- modules?: {
22
- excludes?: string[];
23
- additions?: string[];
24
- };
25
- vm?: boolean;
26
- }): Promise<{
27
- module?: Func;
28
- dependencies: {
29
- [key: string]: string;
30
- };
31
- modules?: {
32
- [key: string]: string;
33
- };
34
- }>;