@faasjs/load 0.0.2-beta.3 → 0.0.2-beta.318

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,232 @@
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/ant-design",
92
+ "@faasjs/aws",
93
+ "@faasjs/browser",
94
+ "@faasjs/cli",
95
+ "@faasjs/cloud_function",
96
+ "create-faas-app",
97
+ "@faasjs/deep_merge",
98
+ "@faasjs/deployer",
99
+ "@faasjs/eslint-config-recommended",
100
+ "@faasjs/eslint-config-vue",
101
+ "faasjs",
102
+ "@faasjs/func",
103
+ "@faasjs/graphql-server",
104
+ "@faasjs/http",
105
+ "@faasjs/knex",
106
+ "@faasjs/load",
107
+ "@faasjs/logger",
108
+ "@faasjs/react",
109
+ "@faasjs/redis",
110
+ "@faasjs/request",
111
+ "@faasjs/server",
112
+ "@faasjs/tencentcloud",
113
+ "@faasjs/test",
114
+ "@faasjs/vue-plugin"
115
+ ];
116
+ var NODE_PACKAGES = [
117
+ "async_hooks",
118
+ "child_process",
119
+ "cluster",
120
+ "crypto",
121
+ "dns",
122
+ "events",
123
+ "fs",
124
+ "http",
125
+ "http2",
126
+ "https",
127
+ "inspector",
128
+ "net",
129
+ "os",
130
+ "path",
131
+ "perf_hooks",
132
+ "process",
133
+ "querystring",
134
+ "readline",
135
+ "repl",
136
+ "stream",
137
+ "string_decoder",
138
+ "tls",
139
+ "trace_events",
140
+ "tty",
141
+ "dgram",
142
+ "udp4",
143
+ "url",
144
+ "util",
145
+ "v8",
146
+ "vm",
147
+ "wasi",
148
+ "worker_threads",
149
+ "zlib"
150
+ ];
151
+ function findModule(list, key, options = { excludes: [] }) {
152
+ if (list[key])
153
+ return;
154
+ if (key.startsWith("@types/") || options.excludes.includes(key))
155
+ return;
156
+ try {
157
+ list[key] = (0, import_path2.dirname)(require.resolve((0, import_path2.join)(key, "package.json")));
158
+ const pkg = JSON.parse((0, import_fs2.readFileSync)((0, import_path2.join)(list[key], "package.json")).toString());
159
+ const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
160
+ deps.map((d) => findModule(list, d, options));
161
+ } catch (error) {
162
+ console.warn(`[FaasJS] Cannot find module path: ${key}`, error);
163
+ }
164
+ }
165
+ function swc(options) {
166
+ return {
167
+ name: "swc",
168
+ async transform(code, filename) {
169
+ options.filename = filename;
170
+ return (0, import_core.transform)(code, options);
171
+ }
172
+ };
173
+ }
174
+ async function loadTs(filename, options = Object.create(null)) {
175
+ var _a;
176
+ const PackageJSON = require(`${process.cwd()}/package.json`);
177
+ const external = PackageJSON.dependencies ? FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
178
+ if (options.modules && options.modules.excludes == null)
179
+ options.modules.excludes = [];
180
+ const input = (0, import_deep_merge2.deepMerge)({
181
+ input: filename,
182
+ external,
183
+ plugins: [
184
+ (0, import_plugin_node_resolve.default)({
185
+ extensions: [
186
+ ".ts",
187
+ ".tsx",
188
+ ".js",
189
+ ".jsx"
190
+ ]
191
+ }),
192
+ swc({
193
+ jsc: {
194
+ parser: { syntax: "typescript" },
195
+ target: "es2021"
196
+ }
197
+ })
198
+ ],
199
+ onwarn: () => null
200
+ }, options.input || {});
201
+ const bundle = await (0, import_rollup.rollup)(input);
202
+ const dependencies = Object.create(null);
203
+ for (const m of ((_a = bundle.cache) == null ? void 0 : _a.modules) || [])
204
+ for (const d of m.dependencies)
205
+ if (!d.startsWith("/") && !dependencies[d] && !NODE_PACKAGES.includes(d))
206
+ dependencies[d] = "*";
207
+ const output = (0, import_deep_merge2.deepMerge)({
208
+ file: filename + ".tmp.js",
209
+ format: "cjs",
210
+ exports: "auto"
211
+ }, options.output || {});
212
+ await bundle.write(output);
213
+ const result = Object.create(null);
214
+ result.dependencies = dependencies;
215
+ result.module = require(output.file);
216
+ if (options.tmp)
217
+ (0, import_fs2.unlinkSync)(output.file);
218
+ if (options.modules) {
219
+ const modules = Object.create(null);
220
+ Object.keys(dependencies).map((d) => findModule(modules, d, options.modules));
221
+ if (options.modules.additions)
222
+ options.modules.additions.map((d) => findModule(modules, d, options.modules));
223
+ result.modules = modules;
224
+ }
225
+ return result;
226
+ }
227
+ module.exports = __toCommonJS(src_exports);
228
+ // Annotate the CommonJS export names for ESM import in node:
229
+ 0 && (module.exports = {
230
+ loadConfig,
231
+ loadTs
232
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,207 @@
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 { readFileSync as readFileSync2, unlinkSync } from "fs";
63
+ import { rollup } from "rollup";
64
+ import { dirname as dirname2, join as join2 } from "path";
65
+ import resolve from "@rollup/plugin-node-resolve";
66
+ import { transform } from "@swc/core";
67
+ var FAAS_PACKAGES = [
68
+ "@faasjs/ant-design",
69
+ "@faasjs/aws",
70
+ "@faasjs/browser",
71
+ "@faasjs/cli",
72
+ "@faasjs/cloud_function",
73
+ "create-faas-app",
74
+ "@faasjs/deep_merge",
75
+ "@faasjs/deployer",
76
+ "@faasjs/eslint-config-recommended",
77
+ "@faasjs/eslint-config-vue",
78
+ "faasjs",
79
+ "@faasjs/func",
80
+ "@faasjs/graphql-server",
81
+ "@faasjs/http",
82
+ "@faasjs/knex",
83
+ "@faasjs/load",
84
+ "@faasjs/logger",
85
+ "@faasjs/react",
86
+ "@faasjs/redis",
87
+ "@faasjs/request",
88
+ "@faasjs/server",
89
+ "@faasjs/tencentcloud",
90
+ "@faasjs/test",
91
+ "@faasjs/vue-plugin"
92
+ ];
93
+ var NODE_PACKAGES = [
94
+ "async_hooks",
95
+ "child_process",
96
+ "cluster",
97
+ "crypto",
98
+ "dns",
99
+ "events",
100
+ "fs",
101
+ "http",
102
+ "http2",
103
+ "https",
104
+ "inspector",
105
+ "net",
106
+ "os",
107
+ "path",
108
+ "perf_hooks",
109
+ "process",
110
+ "querystring",
111
+ "readline",
112
+ "repl",
113
+ "stream",
114
+ "string_decoder",
115
+ "tls",
116
+ "trace_events",
117
+ "tty",
118
+ "dgram",
119
+ "udp4",
120
+ "url",
121
+ "util",
122
+ "v8",
123
+ "vm",
124
+ "wasi",
125
+ "worker_threads",
126
+ "zlib"
127
+ ];
128
+ function findModule(list, key, options = { excludes: [] }) {
129
+ if (list[key])
130
+ return;
131
+ if (key.startsWith("@types/") || options.excludes.includes(key))
132
+ return;
133
+ try {
134
+ list[key] = dirname2(__require.resolve(join2(key, "package.json")));
135
+ const pkg = JSON.parse(readFileSync2(join2(list[key], "package.json")).toString());
136
+ const deps = Object.keys(pkg.dependencies || {}).concat(Object.keys(pkg.peerDependencies || {}));
137
+ deps.map((d) => findModule(list, d, options));
138
+ } catch (error) {
139
+ console.warn(`[FaasJS] Cannot find module path: ${key}`, error);
140
+ }
141
+ }
142
+ function swc(options) {
143
+ return {
144
+ name: "swc",
145
+ async transform(code, filename) {
146
+ options.filename = filename;
147
+ return transform(code, options);
148
+ }
149
+ };
150
+ }
151
+ async function loadTs(filename, options = Object.create(null)) {
152
+ var _a;
153
+ const PackageJSON = __require(`${process.cwd()}/package.json`);
154
+ const external = PackageJSON.dependencies ? FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES;
155
+ if (options.modules && options.modules.excludes == null)
156
+ options.modules.excludes = [];
157
+ const input = deepMerge2({
158
+ input: filename,
159
+ external,
160
+ plugins: [
161
+ resolve({
162
+ extensions: [
163
+ ".ts",
164
+ ".tsx",
165
+ ".js",
166
+ ".jsx"
167
+ ]
168
+ }),
169
+ swc({
170
+ jsc: {
171
+ parser: { syntax: "typescript" },
172
+ target: "es2021"
173
+ }
174
+ })
175
+ ],
176
+ onwarn: () => null
177
+ }, options.input || {});
178
+ const bundle = await rollup(input);
179
+ const dependencies = Object.create(null);
180
+ for (const m of ((_a = bundle.cache) == null ? void 0 : _a.modules) || [])
181
+ for (const d of m.dependencies)
182
+ if (!d.startsWith("/") && !dependencies[d] && !NODE_PACKAGES.includes(d))
183
+ dependencies[d] = "*";
184
+ const output = deepMerge2({
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
+ result.module = __require(output.file);
193
+ if (options.tmp)
194
+ unlinkSync(output.file);
195
+ if (options.modules) {
196
+ const modules = Object.create(null);
197
+ Object.keys(dependencies).map((d) => findModule(modules, d, options.modules));
198
+ if (options.modules.additions)
199
+ options.modules.additions.map((d) => findModule(modules, d, options.modules));
200
+ result.modules = modules;
201
+ }
202
+ return result;
203
+ }
204
+ export {
205
+ loadConfig,
206
+ loadTs
207
+ };
package/package.json CHANGED
@@ -1,31 +1,41 @@
1
1
  {
2
2
  "name": "@faasjs/load",
3
- "version": "0.0.2-beta.3",
3
+ "version": "0.0.2-beta.318",
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
+ "homepage": "https://faasjs.com/doc/load.html",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/faasjs/faasjs.git",
12
+ "directory": "packages/load"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/faasjs/faasjs/issues"
16
+ },
17
+ "funding": "https://github.com/sponsors/faasjs",
8
18
  "scripts": {
9
- "lint": "eslint --ext .ts src",
10
- "prepack": "rm -rf ./lib && rollup -c && mv lib/*/src/* lib/",
11
- "ci": "yarn lint && jest --silent"
19
+ "build": "rm -rf ./dist && tsup-node src/index.ts --format esm,cjs --dts"
12
20
  },
13
21
  "files": [
14
- "lib"
22
+ "dist"
15
23
  ],
16
- "dependencies": {
17
- "@faasjs/deep_merge": "^0.0.2-beta.1",
24
+ "peerDependencies": {
25
+ "@faasjs/deep_merge": "^0.0.2-beta.244",
26
+ "@faasjs/func": "^0.0.2-beta.302",
27
+ "@rollup/plugin-node-resolve": "*",
28
+ "@swc/core": "*",
18
29
  "js-yaml": "*",
19
- "rollup": "*",
20
- "rollup-plugin-typescript2": "*",
21
- "typescript": "*"
30
+ "rollup": "*"
22
31
  },
23
32
  "devDependencies": {
24
- "@faasjs/func": "^0.0.2-beta.3",
25
- "@types/debug": "*",
26
- "@types/jest": "*",
27
33
  "@types/js-yaml": "*",
28
- "@types/node": "*"
34
+ "tsup": "*",
35
+ "typescript": "*"
36
+ },
37
+ "engines": {
38
+ "npm": ">=8.0.0"
29
39
  },
30
- "gitHead": "b8a0f4fa85f5de73e017cebfcfe23068cc2aba38"
40
+ "gitHead": "4a9f699171ad7e20d922e68b74418d1ec5b7d016"
31
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,154 +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 { safeLoad } from 'js-yaml';
5
- import { rollup } from 'rollup';
6
- import typescript from 'rollup-plugin-typescript2';
7
-
8
- /**
9
- * 配置类
10
- */
11
- class Config {
12
- /**
13
- * 创建配置类,并自动读取配置内容
14
- *
15
- * @param root {string} 根目录
16
- * @param filename {filename} 目标文件,用于读取目录层级
17
- */
18
- constructor(root, filename) {
19
- this.root = root;
20
- if (!this.root.endsWith(sep)) {
21
- this.root += sep;
22
- }
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
- const faas = join(root, 'faas.yaml');
29
- if (existsSync(faas)) {
30
- configs.push(safeLoad(readFileSync(faas).toString()));
31
- }
32
- return root;
33
- });
34
- this.origin = deepMerge(...configs);
35
- if (!this.origin.defaults) {
36
- throw Error('[faas.yaml] need defaults env.');
37
- }
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
- }
43
- const data = this[key];
44
- if (!data.providers) {
45
- throw Error(`[faas.yaml] missing key: ${key}/providers`);
46
- }
47
- if (!data.plugins) {
48
- throw Error(`[faas.yaml] missing key: ${key}/plugins`);
49
- }
50
- for (const pluginKey in data.plugins) {
51
- const plugin = data.plugins[pluginKey];
52
- plugin.name = pluginKey;
53
- if (plugin.provider) {
54
- if (typeof plugin.provider === 'string') {
55
- if (!data.providers[plugin.provider]) {
56
- throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
57
- }
58
- plugin.provider = data.providers[plugin.provider];
59
- }
60
- else {
61
- plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
62
- }
63
- }
64
- }
65
- }
66
- }
67
- }
68
- /**
69
- * 加载配置
70
- * @param root {string} 根目录
71
- * @param filename {filename} 目标文件,用于读取目录层级
72
- */
73
- function loadConfig(root, filename) {
74
- return new Config(root, filename);
75
- }
76
-
77
- const FAAS_PACKAGES = [
78
- '@faasjs/browser',
79
- '@faasjs/cli',
80
- '@faasjs/cloud_function',
81
- 'create-faas-app',
82
- '@faasjs/deep_merge',
83
- '@faasjs/deployer',
84
- '@faasjs/eslint-config-recommended',
85
- '@faasjs/eslint-config-vue',
86
- 'faasjs',
87
- '@faasjs/func',
88
- '@faasjs/http',
89
- '@faasjs/load',
90
- '@faasjs/logger',
91
- '@faasjs/nuxt',
92
- '@faasjs/redis',
93
- '@faasjs/request',
94
- '@faasjs/server',
95
- '@faasjs/sql',
96
- '@faasjs/tencentcloud',
97
- '@faasjs/test',
98
- '@faasjs/vue-plugin'
99
- ];
100
- /**
101
- * 加载 ts 文件
102
- *
103
- * @param filename {string} 完整源文件路径
104
- * @param options {object} 配置项
105
- * @param options.input {object} 读取配置
106
- * @param options.output {object} 写入配置
107
- * @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
108
- */
109
- async function loadTs(filename, options = Object.create(null)) {
110
- // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
111
- const PackageJSON = require(`${process.cwd()}/package.json`);
112
- const input = deepMerge({
113
- input: filename,
114
- external: PackageJSON['dependencies'] ? FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES,
115
- plugins: [
116
- typescript({
117
- tsconfigOverride: {
118
- compilerOptions: {
119
- declaration: false
120
- }
121
- }
122
- })
123
- ]
124
- }, options.input || {});
125
- const bundle = await rollup(input);
126
- const dependencies = Object.create(null);
127
- for (const m of bundle.cache.modules || []) {
128
- for (const d of m.dependencies) {
129
- if (!d.startsWith('/') && !dependencies[d]) {
130
- dependencies[d] = '*';
131
- }
132
- }
133
- }
134
- // 特殊处理,避免引入 tslib
135
- if (dependencies['\u0000tslib.js']) {
136
- delete dependencies['\u0000tslib.js'];
137
- }
138
- const output = deepMerge({
139
- file: filename + '.tmp.js',
140
- format: 'cjs'
141
- }, options.output || {});
142
- await bundle.write(output);
143
- // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
144
- const module = require(output.file);
145
- if (options.tmp) {
146
- unlinkSync(output.file);
147
- }
148
- return {
149
- module,
150
- dependencies
151
- };
152
- }
153
-
154
- export { loadConfig, loadTs };
package/lib/index.js DELETED
@@ -1,161 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var deepMerge = _interopDefault(require('@faasjs/deep_merge'));
8
- var fs = require('fs');
9
- var path = require('path');
10
- var jsYaml = require('js-yaml');
11
- var rollup = require('rollup');
12
- var typescript = _interopDefault(require('rollup-plugin-typescript2'));
13
-
14
- /**
15
- * 配置类
16
- */
17
- class Config {
18
- /**
19
- * 创建配置类,并自动读取配置内容
20
- *
21
- * @param root {string} 根目录
22
- * @param filename {filename} 目标文件,用于读取目录层级
23
- */
24
- constructor(root, filename) {
25
- this.root = root;
26
- if (!this.root.endsWith(path.sep)) {
27
- this.root += path.sep;
28
- }
29
- this.filename = filename;
30
- const configs = [];
31
- const paths = [this.root, '.'].concat(path.dirname(filename.replace(root, '')).split(path.sep));
32
- paths.reduce(function (base, path$1) {
33
- const root = path.join(base, path$1);
34
- const faas = path.join(root, 'faas.yaml');
35
- if (fs.existsSync(faas)) {
36
- configs.push(jsYaml.safeLoad(fs.readFileSync(faas).toString()));
37
- }
38
- return root;
39
- });
40
- this.origin = deepMerge(...configs);
41
- if (!this.origin.defaults) {
42
- throw Error('[faas.yaml] need defaults env.');
43
- }
44
- this.defaults = deepMerge(this.origin.defaults);
45
- for (const key in this.origin) {
46
- if (key !== 'defaults') {
47
- this[key] = deepMerge(this.origin.defaults, this.origin[key]);
48
- }
49
- const data = this[key];
50
- if (!data.providers) {
51
- throw Error(`[faas.yaml] missing key: ${key}/providers`);
52
- }
53
- if (!data.plugins) {
54
- throw Error(`[faas.yaml] missing key: ${key}/plugins`);
55
- }
56
- for (const pluginKey in data.plugins) {
57
- const plugin = data.plugins[pluginKey];
58
- plugin.name = pluginKey;
59
- if (plugin.provider) {
60
- if (typeof plugin.provider === 'string') {
61
- if (!data.providers[plugin.provider]) {
62
- throw Error(`[faas.yaml] missing provider: ${plugin.provider} <${key}/plugins/${pluginKey}>`);
63
- }
64
- plugin.provider = data.providers[plugin.provider];
65
- }
66
- else {
67
- plugin.provider = deepMerge(data.providers[plugin.provider], plugin.provider);
68
- }
69
- }
70
- }
71
- }
72
- }
73
- }
74
- /**
75
- * 加载配置
76
- * @param root {string} 根目录
77
- * @param filename {filename} 目标文件,用于读取目录层级
78
- */
79
- function loadConfig(root, filename) {
80
- return new Config(root, filename);
81
- }
82
-
83
- const FAAS_PACKAGES = [
84
- '@faasjs/browser',
85
- '@faasjs/cli',
86
- '@faasjs/cloud_function',
87
- 'create-faas-app',
88
- '@faasjs/deep_merge',
89
- '@faasjs/deployer',
90
- '@faasjs/eslint-config-recommended',
91
- '@faasjs/eslint-config-vue',
92
- 'faasjs',
93
- '@faasjs/func',
94
- '@faasjs/http',
95
- '@faasjs/load',
96
- '@faasjs/logger',
97
- '@faasjs/nuxt',
98
- '@faasjs/redis',
99
- '@faasjs/request',
100
- '@faasjs/server',
101
- '@faasjs/sql',
102
- '@faasjs/tencentcloud',
103
- '@faasjs/test',
104
- '@faasjs/vue-plugin'
105
- ];
106
- /**
107
- * 加载 ts 文件
108
- *
109
- * @param filename {string} 完整源文件路径
110
- * @param options {object} 配置项
111
- * @param options.input {object} 读取配置
112
- * @param options.output {object} 写入配置
113
- * @param options.tmp {boolean} 是否为临时文件,true 则生成的文件会被删除,默认为 false
114
- */
115
- async function loadTs(filename, options = Object.create(null)) {
116
- // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
117
- const PackageJSON = require(`${process.cwd()}/package.json`);
118
- const input = deepMerge({
119
- input: filename,
120
- external: PackageJSON['dependencies'] ? FAAS_PACKAGES.concat(Object.keys(PackageJSON.dependencies)) : FAAS_PACKAGES,
121
- plugins: [
122
- typescript({
123
- tsconfigOverride: {
124
- compilerOptions: {
125
- declaration: false
126
- }
127
- }
128
- })
129
- ]
130
- }, options.input || {});
131
- const bundle = await rollup.rollup(input);
132
- const dependencies = Object.create(null);
133
- for (const m of bundle.cache.modules || []) {
134
- for (const d of m.dependencies) {
135
- if (!d.startsWith('/') && !dependencies[d]) {
136
- dependencies[d] = '*';
137
- }
138
- }
139
- }
140
- // 特殊处理,避免引入 tslib
141
- if (dependencies['\u0000tslib.js']) {
142
- delete dependencies['\u0000tslib.js'];
143
- }
144
- const output = deepMerge({
145
- file: filename + '.tmp.js',
146
- format: 'cjs'
147
- }, options.output || {});
148
- await bundle.write(output);
149
- // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
150
- const module = require(output.file);
151
- if (options.tmp) {
152
- fs.unlinkSync(output.file);
153
- }
154
- return {
155
- module,
156
- dependencies
157
- };
158
- }
159
-
160
- exports.loadConfig = loadConfig;
161
- 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,24 +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
- */
11
- export default function loadTs(filename: string, options?: {
12
- input?: {
13
- [key: string]: any;
14
- };
15
- output?: {
16
- [key: string]: any;
17
- };
18
- tmp?: boolean;
19
- }): Promise<{
20
- module: Func;
21
- dependencies: {
22
- [key: string]: string;
23
- };
24
- }>;